PublicApiController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\{
  5. Hashtag,
  6. Follower,
  7. Like,
  8. Media,
  9. Notification,
  10. Profile,
  11. StatusHashtag,
  12. Status,
  13. StatusView,
  14. UserFilter
  15. };
  16. use Auth, Cache, DB;
  17. use Illuminate\Support\Facades\Redis;
  18. use Carbon\Carbon;
  19. use League\Fractal;
  20. use App\Transformer\Api\{
  21. AccountTransformer,
  22. RelationshipTransformer,
  23. StatusTransformer,
  24. StatusStatelessTransformer
  25. };
  26. use App\Services\{
  27. AccountService,
  28. BookmarkService,
  29. FollowerService,
  30. LikeService,
  31. PublicTimelineService,
  32. ProfileService,
  33. NetworkTimelineService,
  34. ReblogService,
  35. RelationshipService,
  36. StatusService,
  37. SnowflakeService,
  38. UserFilterService
  39. };
  40. use App\Jobs\StatusPipeline\NewStatusPipeline;
  41. use League\Fractal\Serializer\ArraySerializer;
  42. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  43. class PublicApiController extends Controller
  44. {
  45. protected $fractal;
  46. public function __construct()
  47. {
  48. $this->fractal = new Fractal\Manager();
  49. $this->fractal->setSerializer(new ArraySerializer());
  50. }
  51. protected function getUserData($user)
  52. {
  53. if(!$user) {
  54. return [];
  55. } else {
  56. return AccountService::get($user->profile_id);
  57. }
  58. }
  59. public function getStatus(Request $request, $id)
  60. {
  61. abort_if(!$request->user(), 403);
  62. $status = StatusService::get($id, false);
  63. abort_if(!$status, 404);
  64. if(in_array($status['visibility'], ['public', 'unlisted'])) {
  65. return $status;
  66. }
  67. $pid = $request->user()->profile_id;
  68. if($status['account']['id'] == $pid) {
  69. return $status;
  70. }
  71. if($status['visibility'] == 'private') {
  72. if(FollowerService::follows($pid, $status['account']['id'])) {
  73. return $status;
  74. }
  75. }
  76. abort(404);
  77. }
  78. public function status(Request $request, $username, int $postid)
  79. {
  80. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  81. $status = Status::whereProfileId($profile->id)->findOrFail($postid);
  82. $this->scopeCheck($profile, $status);
  83. if(!$request->user()) {
  84. $cached = StatusService::get($status->id, false);
  85. abort_if(!in_array($cached['visibility'], ['public', 'unlisted']), 403);
  86. $res = ['status' => $cached];
  87. } else {
  88. $item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
  89. $res = [
  90. 'status' => $this->fractal->createData($item)->toArray(),
  91. ];
  92. }
  93. return response()->json($res);
  94. }
  95. public function statusState(Request $request, $username, int $postid)
  96. {
  97. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  98. $status = Status::whereProfileId($profile->id)->findOrFail($postid);
  99. $this->scopeCheck($profile, $status);
  100. if(!Auth::check()) {
  101. $res = [
  102. 'user' => [],
  103. 'likes' => [],
  104. 'shares' => [],
  105. 'reactions' => [
  106. 'liked' => false,
  107. 'shared' => false,
  108. 'bookmarked' => false,
  109. ],
  110. ];
  111. return response()->json($res);
  112. }
  113. $res = [
  114. 'user' => $this->getUserData($request->user()),
  115. 'likes' => [],
  116. 'shares' => [],
  117. 'reactions' => [
  118. 'liked' => (bool) $status->liked(),
  119. 'shared' => (bool) $status->shared(),
  120. 'bookmarked' => (bool) $status->bookmarked(),
  121. ],
  122. ];
  123. return response()->json($res);
  124. }
  125. public function statusComments(Request $request, $username, int $postId)
  126. {
  127. $this->validate($request, [
  128. 'min_id' => 'nullable|integer|min:1',
  129. 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  130. 'limit' => 'nullable|integer|min:5|max:50'
  131. ]);
  132. $limit = $request->limit ?? 10;
  133. $profile = Profile::whereNull('status')->findOrFail($username);
  134. $status = Status::whereProfileId($profile->id)->whereCommentsDisabled(false)->findOrFail($postId);
  135. $this->scopeCheck($profile, $status);
  136. if(Auth::check()) {
  137. $p = Auth::user()->profile;
  138. $scope = $p->id == $status->profile_id || FollowerService::follows($p->id, $profile->id) ? ['public', 'private', 'unlisted'] : ['public','unlisted'];
  139. } else {
  140. $scope = ['public', 'unlisted'];
  141. }
  142. if($request->filled('min_id') || $request->filled('max_id')) {
  143. if($request->filled('min_id')) {
  144. $replies = $status->comments()
  145. ->whereNull('reblog_of_id')
  146. ->whereIn('scope', $scope)
  147. ->select('id', 'caption', 'local', 'visibility', 'scope', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  148. ->where('id', '>=', $request->min_id)
  149. ->orderBy('id', 'desc')
  150. ->paginate($limit);
  151. }
  152. if($request->filled('max_id')) {
  153. $replies = $status->comments()
  154. ->whereNull('reblog_of_id')
  155. ->whereIn('scope', $scope)
  156. ->select('id', 'caption', 'local', 'visibility', 'scope', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  157. ->where('id', '<=', $request->max_id)
  158. ->orderBy('id', 'desc')
  159. ->paginate($limit);
  160. }
  161. } else {
  162. $replies = Status::whereInReplyToId($status->id)
  163. ->whereNull('reblog_of_id')
  164. ->whereIn('scope', $scope)
  165. ->select('id', 'caption', 'local', 'visibility', 'scope', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  166. ->orderBy('id', 'desc')
  167. ->paginate($limit);
  168. }
  169. $resource = new Fractal\Resource\Collection($replies, new StatusStatelessTransformer(), 'data');
  170. $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
  171. $res = $this->fractal->createData($resource)->toArray();
  172. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  173. }
  174. protected function scopeCheck(Profile $profile, Status $status)
  175. {
  176. if($profile->is_private == true && Auth::check() == false) {
  177. abort(404);
  178. }
  179. switch ($status->scope) {
  180. case 'public':
  181. case 'unlisted':
  182. break;
  183. case 'private':
  184. $user = Auth::check() ? Auth::user() : false;
  185. if(!$user) {
  186. abort(403);
  187. } else {
  188. $follows = $profile->followedBy($user->profile);
  189. if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) {
  190. abort(404);
  191. }
  192. }
  193. break;
  194. case 'direct':
  195. abort(404);
  196. break;
  197. case 'draft':
  198. abort(404);
  199. break;
  200. default:
  201. abort(404);
  202. break;
  203. }
  204. }
  205. public function publicTimelineApi(Request $request)
  206. {
  207. $this->validate($request,[
  208. 'page' => 'nullable|integer|max:40',
  209. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  210. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  211. 'limit' => 'nullable|integer|max:30'
  212. ]);
  213. if(!$request->user()) {
  214. return response('', 403);
  215. }
  216. $page = $request->input('page');
  217. $min = $request->input('min_id');
  218. $max = $request->input('max_id');
  219. $limit = $request->input('limit') ?? 3;
  220. $user = $request->user();
  221. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  222. $hideNsfw = config('instance.hide_nsfw_on_public_feeds');
  223. if(config('exp.cached_public_timeline') == false) {
  224. if($min || $max) {
  225. $dir = $min ? '>' : '<';
  226. $id = $min ?? $max;
  227. $timeline = Status::select(
  228. 'id',
  229. 'profile_id',
  230. 'type',
  231. 'scope',
  232. 'local'
  233. )
  234. ->where('id', $dir, $id)
  235. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  236. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  237. ->whereLocal(true)
  238. ->when($hideNsfw, function($q, $hideNsfw) {
  239. return $q->where('is_nsfw', false);
  240. })
  241. ->whereScope('public')
  242. ->orderBy('id', 'desc')
  243. ->limit($limit)
  244. ->get()
  245. ->map(function($s) use ($user) {
  246. $status = StatusService::getFull($s->id, $user->profile_id);
  247. if(!$status) {
  248. return false;
  249. }
  250. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  251. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  252. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  253. return $status;
  254. })
  255. ->filter(function($s) use($filtered) {
  256. return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
  257. })
  258. ->values();
  259. $res = $timeline->toArray();
  260. } else {
  261. $timeline = Status::select(
  262. 'id',
  263. 'uri',
  264. 'caption',
  265. 'rendered',
  266. 'profile_id',
  267. 'type',
  268. 'in_reply_to_id',
  269. 'reblog_of_id',
  270. 'is_nsfw',
  271. 'scope',
  272. 'local',
  273. 'reply_count',
  274. 'comments_disabled',
  275. 'created_at',
  276. 'place_id',
  277. 'likes_count',
  278. 'reblogs_count',
  279. 'updated_at'
  280. )
  281. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  282. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  283. ->whereLocal(true)
  284. ->when($hideNsfw, function($q, $hideNsfw) {
  285. return $q->where('is_nsfw', false);
  286. })
  287. ->whereScope('public')
  288. ->orderBy('id', 'desc')
  289. ->limit($limit)
  290. ->get()
  291. ->map(function($s) use ($user) {
  292. $status = StatusService::getFull($s->id, $user->profile_id);
  293. if(!$status) {
  294. return false;
  295. }
  296. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  297. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  298. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  299. return $status;
  300. })
  301. ->filter(function($s) use($filtered) {
  302. return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
  303. })
  304. ->values();
  305. $res = $timeline->toArray();
  306. }
  307. } else {
  308. Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
  309. if(PublicTimelineService::count() == 0) {
  310. PublicTimelineService::warmCache(true, 400);
  311. }
  312. });
  313. if ($max) {
  314. $feed = PublicTimelineService::getRankedMaxId($max, $limit);
  315. } else if ($min) {
  316. $feed = PublicTimelineService::getRankedMinId($min, $limit);
  317. } else {
  318. $feed = PublicTimelineService::get(0, $limit);
  319. }
  320. $res = collect($feed)
  321. ->take($limit)
  322. ->map(function($k) use($user) {
  323. $status = StatusService::get($k);
  324. if($status && isset($status['account']) && $user) {
  325. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
  326. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
  327. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
  328. $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
  329. }
  330. return $status;
  331. })
  332. ->filter(function($s) use($filtered) {
  333. return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
  334. })
  335. ->values()
  336. ->toArray();
  337. }
  338. return response()->json($res);
  339. }
  340. public function homeTimelineApi(Request $request)
  341. {
  342. if(!$request->user()) {
  343. return response('', 403);
  344. }
  345. $this->validate($request,[
  346. 'page' => 'nullable|integer|max:40',
  347. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  348. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  349. 'limit' => 'nullable|integer|max:40',
  350. 'recent_feed' => 'nullable',
  351. 'recent_min' => 'nullable|integer'
  352. ]);
  353. $recentFeed = $request->input('recent_feed') == 'true';
  354. $recentFeedMin = $request->input('recent_min');
  355. $page = $request->input('page');
  356. $min = $request->input('min_id');
  357. $max = $request->input('max_id');
  358. $limit = $request->input('limit') ?? 3;
  359. $user = $request->user();
  360. $key = 'user:last_active_at:id:'.$user->id;
  361. if(Cache::get($key) == null) {
  362. $user->last_active_at = now();
  363. $user->save();
  364. Cache::put($key, true, 43200);
  365. }
  366. $pid = $user->profile_id;
  367. $following = Cache::remember('profile:following:'.$pid, 1209600, function() use($pid) {
  368. $following = Follower::whereProfileId($pid)->pluck('following_id');
  369. return $following->push($pid)->toArray();
  370. });
  371. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  372. $types = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'];
  373. // $types = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album', 'text'];
  374. $textOnlyReplies = false;
  375. if(config('exp.top')) {
  376. $textOnlyReplies = (bool) Redis::zscore('pf:tl:replies', $pid);
  377. $textOnlyPosts = (bool) Redis::zscore('pf:tl:top', $pid);
  378. if($textOnlyPosts) {
  379. array_push($types, 'text');
  380. }
  381. }
  382. if(config('exp.polls') == true) {
  383. array_push($types, 'poll');
  384. }
  385. if(config('instance.timeline.home.cached') && $limit == 6 && (!$min && !$max)) {
  386. $ttl = config('instance.timeline.home.cache_ttl');
  387. $res = Cache::remember(
  388. 'pf:timelines:home:' . $pid,
  389. $ttl,
  390. function() use(
  391. $types,
  392. $textOnlyReplies,
  393. $following,
  394. $limit,
  395. $filtered,
  396. $user
  397. ) {
  398. return Status::select(
  399. 'id',
  400. 'uri',
  401. 'caption',
  402. 'rendered',
  403. 'profile_id',
  404. 'type',
  405. 'in_reply_to_id',
  406. 'reblog_of_id',
  407. 'is_nsfw',
  408. 'scope',
  409. 'local',
  410. 'reply_count',
  411. 'comments_disabled',
  412. 'place_id',
  413. 'likes_count',
  414. 'reblogs_count',
  415. 'created_at',
  416. 'updated_at'
  417. )
  418. ->whereIn('type', $types)
  419. ->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
  420. return $q->whereNull('in_reply_to_id');
  421. })
  422. ->whereIn('profile_id', $following)
  423. ->whereIn('visibility',['public', 'unlisted', 'private'])
  424. ->orderBy('created_at', 'desc')
  425. ->limit($limit)
  426. ->get()
  427. ->map(function($s) use ($user) {
  428. $status = StatusService::get($s->id, false);
  429. if(!$status) {
  430. return false;
  431. }
  432. return $status;
  433. })
  434. ->filter(function($s) use($filtered) {
  435. return $s && in_array($s['account']['id'], $filtered) == false;
  436. })
  437. ->values()
  438. ->toArray();
  439. });
  440. $res = collect($res)
  441. ->map(function($s) use ($user) {
  442. $status = StatusService::get($s['id'], false);
  443. if(!$status) {
  444. return false;
  445. }
  446. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s['id']);
  447. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s['id']);
  448. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s['id']);
  449. return $status;
  450. })
  451. ->filter(function($s) use($filtered) {
  452. return $s && in_array($s['account']['id'], $filtered) == false;
  453. })
  454. ->values()
  455. ->take($limit)
  456. ->toArray();
  457. return $res;
  458. }
  459. if($min || $max) {
  460. $dir = $min ? '>' : '<';
  461. $id = $min ?? $max;
  462. return Status::select(
  463. 'id',
  464. 'uri',
  465. 'caption',
  466. 'rendered',
  467. 'profile_id',
  468. 'type',
  469. 'in_reply_to_id',
  470. 'reblog_of_id',
  471. 'is_nsfw',
  472. 'scope',
  473. 'local',
  474. 'reply_count',
  475. 'comments_disabled',
  476. 'place_id',
  477. 'likes_count',
  478. 'reblogs_count',
  479. 'created_at',
  480. 'updated_at'
  481. )
  482. ->whereIn('type', $types)
  483. ->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
  484. return $q->whereNull('in_reply_to_id');
  485. })
  486. ->where('id', $dir, $id)
  487. ->whereIn('profile_id', $following)
  488. ->whereIn('visibility',['public', 'unlisted', 'private'])
  489. ->orderBy('created_at', 'desc')
  490. ->limit($limit)
  491. ->get()
  492. ->map(function($s) use ($user) {
  493. $status = StatusService::get($s->id, false);
  494. if(!$status) {
  495. return false;
  496. }
  497. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  498. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  499. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  500. return $status;
  501. })
  502. ->filter(function($s) use($filtered) {
  503. return $s && in_array($s['account']['id'], $filtered) == false;
  504. })
  505. ->values()
  506. ->toArray();
  507. } else {
  508. return Status::select(
  509. 'id',
  510. 'uri',
  511. 'caption',
  512. 'rendered',
  513. 'profile_id',
  514. 'type',
  515. 'in_reply_to_id',
  516. 'reblog_of_id',
  517. 'is_nsfw',
  518. 'scope',
  519. 'local',
  520. 'reply_count',
  521. 'comments_disabled',
  522. 'place_id',
  523. 'likes_count',
  524. 'reblogs_count',
  525. 'created_at',
  526. 'updated_at'
  527. )
  528. ->whereIn('type', $types)
  529. ->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
  530. return $q->whereNull('in_reply_to_id');
  531. })
  532. ->whereIn('profile_id', $following)
  533. ->whereIn('visibility',['public', 'unlisted', 'private'])
  534. ->orderBy('created_at', 'desc')
  535. ->limit($limit)
  536. ->get()
  537. ->map(function($s) use ($user) {
  538. $status = StatusService::get($s->id, false);
  539. if(!$status) {
  540. return false;
  541. }
  542. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  543. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  544. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  545. return $status;
  546. })
  547. ->filter(function($s) use($filtered) {
  548. return $s && in_array($s['account']['id'], $filtered) == false;
  549. })
  550. ->values()
  551. ->toArray();
  552. }
  553. }
  554. public function networkTimelineApi(Request $request)
  555. {
  556. if(!$request->user()) {
  557. return response('', 403);
  558. }
  559. abort_if(config('federation.network_timeline') == false, 404);
  560. $this->validate($request,[
  561. 'page' => 'nullable|integer|max:40',
  562. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  563. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  564. 'limit' => 'nullable|integer|max:30'
  565. ]);
  566. $page = $request->input('page');
  567. $min = $request->input('min_id');
  568. $max = $request->input('max_id');
  569. $limit = $request->input('limit') ?? 3;
  570. $user = $request->user();
  571. $amin = SnowflakeService::byDate(now()->subDays(config('federation.network_timeline_days_falloff')));
  572. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  573. $hideNsfw = config('instance.hide_nsfw_on_public_feeds');
  574. if(config('instance.timeline.network.cached') == false) {
  575. if($min || $max) {
  576. $dir = $min ? '>' : '<';
  577. $id = $min ?? $max;
  578. $timeline = Status::select(
  579. 'id',
  580. 'uri',
  581. 'type',
  582. 'scope',
  583. 'created_at',
  584. )
  585. ->where('id', $dir, $id)
  586. ->when($hideNsfw, function($q, $hideNsfw) {
  587. return $q->where('is_nsfw', false);
  588. })
  589. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  590. ->whereNotIn('profile_id', $filtered)
  591. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  592. ->whereNotNull('uri')
  593. ->whereScope('public')
  594. ->where('id', '>', $amin)
  595. ->orderBy('created_at', 'desc')
  596. ->limit($limit)
  597. ->get()
  598. ->map(function($s) use ($user) {
  599. $status = StatusService::get($s->id);
  600. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  601. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  602. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  603. return $status;
  604. });
  605. $res = $timeline->toArray();
  606. } else {
  607. $timeline = Status::select(
  608. 'id',
  609. 'uri',
  610. 'type',
  611. 'scope',
  612. 'created_at',
  613. )
  614. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  615. ->whereNotIn('profile_id', $filtered)
  616. ->when($hideNsfw, function($q, $hideNsfw) {
  617. return $q->where('is_nsfw', false);
  618. })
  619. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  620. ->whereNotNull('uri')
  621. ->whereScope('public')
  622. ->where('id', '>', $amin)
  623. ->orderBy('created_at', 'desc')
  624. ->limit($limit)
  625. ->get()
  626. ->map(function($s) use ($user) {
  627. $status = StatusService::get($s->id);
  628. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  629. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  630. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  631. return $status;
  632. });
  633. $res = $timeline->toArray();
  634. }
  635. } else {
  636. Cache::remember('api:v1:timelines:network:cache_check', 10368000, function() {
  637. if(NetworkTimelineService::count() == 0) {
  638. NetworkTimelineService::warmCache(true, 400);
  639. }
  640. });
  641. if ($max) {
  642. $feed = NetworkTimelineService::getRankedMaxId($max, $limit);
  643. } else if ($min) {
  644. $feed = NetworkTimelineService::getRankedMinId($min, $limit);
  645. } else {
  646. $feed = NetworkTimelineService::get(0, $limit);
  647. }
  648. $res = collect($feed)
  649. ->take($limit)
  650. ->map(function($k) use($user) {
  651. $status = StatusService::get($k);
  652. if($status && isset($status['account']) && $user) {
  653. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
  654. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
  655. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
  656. $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
  657. }
  658. return $status;
  659. })
  660. ->filter(function($s) use($filtered) {
  661. return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
  662. })
  663. ->values()
  664. ->toArray();
  665. }
  666. return response()->json($res);
  667. }
  668. public function relationships(Request $request)
  669. {
  670. if(!Auth::check()) {
  671. return response()->json([]);
  672. }
  673. $pid = $request->user()->profile_id;
  674. $this->validate($request, [
  675. 'id' => 'required|array|min:1|max:20',
  676. 'id.*' => 'required|integer'
  677. ]);
  678. $ids = collect($request->input('id'));
  679. $res = $ids->filter(function($v) use($pid) {
  680. return $v != $pid;
  681. })
  682. ->map(function($id) use($pid) {
  683. return RelationshipService::get($pid, $id);
  684. });
  685. return response()->json($res);
  686. }
  687. public function account(Request $request, $id)
  688. {
  689. $res = AccountService::get($id);
  690. return response()->json($res);
  691. }
  692. public function accountStatuses(Request $request, $id)
  693. {
  694. $this->validate($request, [
  695. 'only_media' => 'nullable',
  696. 'pinned' => 'nullable',
  697. 'exclude_replies' => 'nullable',
  698. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  699. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  700. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  701. 'limit' => 'nullable|integer|min:1|max:24'
  702. ]);
  703. $user = $request->user();
  704. $profile = AccountService::get($id);
  705. abort_if(!$profile, 404);
  706. $limit = $request->limit ?? 9;
  707. $max_id = $request->max_id;
  708. $min_id = $request->min_id;
  709. $scope = ['photo', 'photo:album', 'video', 'video:album'];
  710. $onlyMedia = $request->input('only_media', true);
  711. if(!$min_id && !$max_id) {
  712. $min_id = 1;
  713. }
  714. if($profile['locked']) {
  715. if(!$user) {
  716. return response()->json([]);
  717. }
  718. $pid = $user->profile_id;
  719. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  720. $following = Follower::whereProfileId($pid)->pluck('following_id');
  721. return $following->push($pid)->toArray();
  722. });
  723. $visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : [];
  724. } else {
  725. if($user) {
  726. $pid = $user->profile_id;
  727. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  728. $following = Follower::whereProfileId($pid)->pluck('following_id');
  729. return $following->push($pid)->toArray();
  730. });
  731. $visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  732. } else {
  733. $visibility = ['public', 'unlisted'];
  734. }
  735. }
  736. $dir = $min_id ? '>' : '<';
  737. $id = $min_id ?? $max_id;
  738. $res = Status::whereProfileId($profile['id'])
  739. ->whereNull('in_reply_to_id')
  740. ->whereNull('reblog_of_id')
  741. ->whereIn('type', $scope)
  742. ->where('id', $dir, $id)
  743. ->whereIn('scope', $visibility)
  744. ->limit($limit)
  745. ->orderByDesc('id')
  746. ->get()
  747. ->map(function($s) use($user) {
  748. try {
  749. $status = StatusService::get($s->id, false);
  750. } catch (\Exception $e) {
  751. $status = false;
  752. }
  753. if($user && $status) {
  754. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  755. }
  756. return $status;
  757. })
  758. ->filter(function($s) use($onlyMedia) {
  759. if($onlyMedia) {
  760. if(
  761. !isset($s['media_attachments']) ||
  762. !is_array($s['media_attachments']) ||
  763. empty($s['media_attachments'])
  764. ) {
  765. return false;
  766. }
  767. }
  768. return $s;
  769. })
  770. ->values();
  771. return response()->json($res);
  772. }
  773. }