PublicApiController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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;
  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. LikeService,
  29. PublicTimelineService,
  30. ProfileService,
  31. RelationshipService,
  32. StatusService,
  33. SnowflakeService,
  34. UserFilterService
  35. };
  36. use App\Jobs\StatusPipeline\NewStatusPipeline;
  37. use League\Fractal\Serializer\ArraySerializer;
  38. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  39. class PublicApiController extends Controller
  40. {
  41. protected $fractal;
  42. public function __construct()
  43. {
  44. $this->fractal = new Fractal\Manager();
  45. $this->fractal->setSerializer(new ArraySerializer());
  46. }
  47. protected function getUserData($user)
  48. {
  49. if(!$user) {
  50. return [];
  51. } else {
  52. return AccountService::get($user->profile_id);
  53. }
  54. }
  55. protected function getLikes($status)
  56. {
  57. if(false == Auth::check()) {
  58. return [];
  59. } else {
  60. $profile = Auth::user()->profile;
  61. if($profile->status) {
  62. return [];
  63. }
  64. $likes = $status->likedBy()->orderBy('created_at','desc')->paginate(10);
  65. $collection = new Fractal\Resource\Collection($likes, new AccountTransformer());
  66. return $this->fractal->createData($collection)->toArray();
  67. }
  68. }
  69. protected function getShares($status)
  70. {
  71. if(false == Auth::check()) {
  72. return [];
  73. } else {
  74. $profile = Auth::user()->profile;
  75. if($profile->status) {
  76. return [];
  77. }
  78. $shares = $status->sharedBy()->orderBy('created_at','desc')->paginate(10);
  79. $collection = new Fractal\Resource\Collection($shares, new AccountTransformer());
  80. return $this->fractal->createData($collection)->toArray();
  81. }
  82. }
  83. public function status(Request $request, $username, int $postid)
  84. {
  85. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  86. $status = Status::whereProfileId($profile->id)->findOrFail($postid);
  87. $this->scopeCheck($profile, $status);
  88. if(!$request->user()) {
  89. $res = ['status' => StatusService::get($status->id)];
  90. } else {
  91. $item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
  92. $res = [
  93. 'status' => $this->fractal->createData($item)->toArray(),
  94. ];
  95. }
  96. return response()->json($res);
  97. }
  98. public function statusState(Request $request, $username, int $postid)
  99. {
  100. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  101. $status = Status::whereProfileId($profile->id)->findOrFail($postid);
  102. $this->scopeCheck($profile, $status);
  103. if(!Auth::check()) {
  104. $res = [
  105. 'user' => [],
  106. 'likes' => [],
  107. 'shares' => [],
  108. 'reactions' => [
  109. 'liked' => false,
  110. 'shared' => false,
  111. 'bookmarked' => false,
  112. ],
  113. ];
  114. return response()->json($res);
  115. }
  116. $res = [
  117. 'user' => $this->getUserData($request->user()),
  118. 'likes' => [],
  119. 'shares' => [],
  120. 'reactions' => [
  121. 'liked' => (bool) $status->liked(),
  122. 'shared' => (bool) $status->shared(),
  123. 'bookmarked' => (bool) $status->bookmarked(),
  124. ],
  125. ];
  126. return response()->json($res);
  127. }
  128. public function statusComments(Request $request, $username, int $postId)
  129. {
  130. $this->validate($request, [
  131. 'min_id' => 'nullable|integer|min:1',
  132. 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  133. 'limit' => 'nullable|integer|min:5|max:50'
  134. ]);
  135. $limit = $request->limit ?? 10;
  136. $profile = Profile::whereNull('status')->findOrFail($username);
  137. $status = Status::whereProfileId($profile->id)->whereCommentsDisabled(false)->findOrFail($postId);
  138. $this->scopeCheck($profile, $status);
  139. if(Auth::check()) {
  140. $p = Auth::user()->profile;
  141. $filtered = UserFilter::whereUserId($p->id)
  142. ->whereFilterableType('App\Profile')
  143. ->whereIn('filter_type', ['mute', 'block'])
  144. ->pluck('filterable_id')->toArray();
  145. $scope = $p->id == $status->profile_id ? ['public', 'private', 'unlisted'] : ['public','unlisted'];
  146. } else {
  147. $filtered = [];
  148. $scope = ['public', 'unlisted'];
  149. }
  150. if($request->filled('min_id') || $request->filled('max_id')) {
  151. if($request->filled('min_id')) {
  152. $replies = $status->comments()
  153. ->whereNull('reblog_of_id')
  154. ->whereIn('scope', $scope)
  155. ->whereNotIn('profile_id', $filtered)
  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->min_id)
  158. ->orderBy('id', 'desc')
  159. ->paginate($limit);
  160. }
  161. if($request->filled('max_id')) {
  162. $replies = $status->comments()
  163. ->whereNull('reblog_of_id')
  164. ->whereIn('scope', $scope)
  165. ->whereNotIn('profile_id', $filtered)
  166. ->select('id', 'caption', 'local', 'visibility', 'scope', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  167. ->where('id', '<=', $request->max_id)
  168. ->orderBy('id', 'desc')
  169. ->paginate($limit);
  170. }
  171. } else {
  172. $replies = $status->comments()
  173. ->whereNull('reblog_of_id')
  174. ->whereIn('scope', $scope)
  175. ->whereNotIn('profile_id', $filtered)
  176. ->select('id', 'caption', 'local', 'visibility', 'scope', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  177. ->orderBy('id', 'desc')
  178. ->paginate($limit);
  179. }
  180. $resource = new Fractal\Resource\Collection($replies, new StatusTransformer(), 'data');
  181. $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
  182. $res = $this->fractal->createData($resource)->toArray();
  183. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  184. }
  185. public function statusLikes(Request $request, $username, $id)
  186. {
  187. abort_if(!$request->user(), 404);
  188. $status = Status::findOrFail($id);
  189. $this->scopeCheck($status->profile, $status);
  190. $page = $request->input('page');
  191. if($page && $page >= 3 && $request->user()->profile_id != $status->profile_id) {
  192. return response()->json([
  193. 'data' => []
  194. ]);
  195. }
  196. $likes = $this->getLikes($status);
  197. return response()->json([
  198. 'data' => $likes
  199. ]);
  200. }
  201. public function statusShares(Request $request, $username, $id)
  202. {
  203. abort_if(!$request->user(), 404);
  204. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  205. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  206. $this->scopeCheck($profile, $status);
  207. $page = $request->input('page');
  208. if($page && $page >= 3 && $request->user()->profile_id != $status->profile_id) {
  209. return response()->json([
  210. 'data' => []
  211. ]);
  212. }
  213. $shares = $this->getShares($status);
  214. return response()->json([
  215. 'data' => $shares
  216. ]);
  217. }
  218. protected function scopeCheck(Profile $profile, Status $status)
  219. {
  220. if($profile->is_private == true && Auth::check() == false) {
  221. abort(404);
  222. }
  223. switch ($status->scope) {
  224. case 'public':
  225. case 'unlisted':
  226. break;
  227. case 'private':
  228. $user = Auth::check() ? Auth::user() : false;
  229. if(!$user) {
  230. abort(403);
  231. } else {
  232. $follows = $profile->followedBy($user->profile);
  233. if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) {
  234. abort(404);
  235. }
  236. }
  237. break;
  238. case 'direct':
  239. abort(404);
  240. break;
  241. case 'draft':
  242. abort(404);
  243. break;
  244. default:
  245. abort(404);
  246. break;
  247. }
  248. }
  249. public function publicTimelineApi(Request $request)
  250. {
  251. $this->validate($request,[
  252. 'page' => 'nullable|integer|max:40',
  253. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  254. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  255. 'limit' => 'nullable|integer|max:30'
  256. ]);
  257. if(config('instance.timeline.local.is_public') == false && !Auth::check()) {
  258. abort(403, 'Authentication required.');
  259. }
  260. $page = $request->input('page');
  261. $min = $request->input('min_id');
  262. $max = $request->input('max_id');
  263. $limit = $request->input('limit') ?? 3;
  264. $user = $request->user();
  265. Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
  266. if(PublicTimelineService::count() == 0) {
  267. PublicTimelineService::warmCache(true, 400);
  268. }
  269. });
  270. if ($max) {
  271. $feed = PublicTimelineService::getRankedMaxId($max, $limit);
  272. } else if ($min) {
  273. $feed = PublicTimelineService::getRankedMinId($min, $limit);
  274. } else {
  275. $feed = PublicTimelineService::get(0, $limit);
  276. }
  277. $res = collect($feed)
  278. ->map(function($k) use($user) {
  279. $status = StatusService::get($k);
  280. if($user) {
  281. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
  282. $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
  283. }
  284. return $status;
  285. })
  286. ->toArray();
  287. return response()->json($res);
  288. }
  289. public function homeTimelineApi(Request $request)
  290. {
  291. if(!Auth::check()) {
  292. return abort(403);
  293. }
  294. $this->validate($request,[
  295. 'page' => 'nullable|integer|max:40',
  296. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  297. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  298. 'limit' => 'nullable|integer|max:40',
  299. 'recent_feed' => 'nullable',
  300. 'recent_min' => 'nullable|integer'
  301. ]);
  302. $recentFeed = $request->input('recent_feed') == 'true';
  303. $recentFeedMin = $request->input('recent_min');
  304. $page = $request->input('page');
  305. $min = $request->input('min_id');
  306. $max = $request->input('max_id');
  307. $limit = $request->input('limit') ?? 3;
  308. $user = $request->user();
  309. $key = 'user:last_active_at:id:'.$user->id;
  310. $ttl = now()->addMinutes(20);
  311. Cache::remember($key, $ttl, function() use($user) {
  312. $user->last_active_at = now();
  313. $user->save();
  314. return;
  315. });
  316. $pid = $user->profile_id;
  317. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  318. $following = Follower::whereProfileId($pid)->pluck('following_id');
  319. return $following->push($pid)->toArray();
  320. });
  321. if($recentFeed == true) {
  322. $key = 'profile:home-timeline-cursor:'.$user->id;
  323. $ttl = now()->addMinutes(30);
  324. $min = Cache::remember($key, $ttl, function() use($pid) {
  325. $res = StatusView::whereProfileId($pid)->orderByDesc('status_id')->first();
  326. return $res ? $res->status_id : null;
  327. });
  328. }
  329. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  330. $types = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'];
  331. $textOnlyReplies = false;
  332. if(config('exp.top')) {
  333. $textOnlyPosts = (bool) Redis::zscore('pf:tl:top', $pid);
  334. $textOnlyReplies = (bool) Redis::zscore('pf:tl:replies', $pid);
  335. if($textOnlyPosts) {
  336. array_push($types, 'text');
  337. }
  338. }
  339. if(config('exp.polls') == true) {
  340. array_push($types, 'poll');
  341. }
  342. if($min || $max) {
  343. $dir = $min ? '>' : '<';
  344. $id = $min ?? $max;
  345. $timeline = Status::select(
  346. 'id',
  347. 'uri',
  348. 'caption',
  349. 'rendered',
  350. 'profile_id',
  351. 'type',
  352. 'in_reply_to_id',
  353. 'reblog_of_id',
  354. 'is_nsfw',
  355. 'scope',
  356. 'local',
  357. 'reply_count',
  358. 'comments_disabled',
  359. 'place_id',
  360. 'likes_count',
  361. 'reblogs_count',
  362. 'created_at',
  363. 'updated_at'
  364. )
  365. ->whereIn('type', $types)
  366. ->when($textOnlyReplies != true, function($q, $textOnlyReplies) {
  367. return $q->whereNull('in_reply_to_id');
  368. })
  369. ->with('profile', 'hashtags', 'mentions')
  370. ->where('id', $dir, $id)
  371. ->whereIn('profile_id', $following)
  372. ->whereNotIn('profile_id', $filtered)
  373. ->whereIn('visibility',['public', 'unlisted', 'private'])
  374. ->orderBy('created_at', 'desc')
  375. ->limit($limit)
  376. ->get();
  377. } else {
  378. $timeline = Status::select(
  379. 'id',
  380. 'uri',
  381. 'caption',
  382. 'rendered',
  383. 'profile_id',
  384. 'type',
  385. 'in_reply_to_id',
  386. 'reblog_of_id',
  387. 'is_nsfw',
  388. 'scope',
  389. 'local',
  390. 'reply_count',
  391. 'comments_disabled',
  392. 'place_id',
  393. 'likes_count',
  394. 'reblogs_count',
  395. 'created_at',
  396. 'updated_at'
  397. )
  398. ->whereIn('type', $types)
  399. ->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
  400. return $q->whereNull('in_reply_to_id');
  401. })
  402. ->with('profile', 'hashtags', 'mentions')
  403. ->whereIn('profile_id', $following)
  404. ->whereNotIn('profile_id', $filtered)
  405. ->whereIn('visibility',['public', 'unlisted', 'private'])
  406. ->orderBy('created_at', 'desc')
  407. ->simplePaginate($limit);
  408. }
  409. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  410. $res = $this->fractal->createData($fractal)->toArray();
  411. return response()->json($res);
  412. }
  413. public function networkTimelineApi(Request $request)
  414. {
  415. abort_if(!Auth::check(), 403);
  416. abort_if(config('federation.network_timeline') == false, 404);
  417. $this->validate($request,[
  418. 'page' => 'nullable|integer|max:40',
  419. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  420. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  421. 'limit' => 'nullable|integer|max:30'
  422. ]);
  423. $page = $request->input('page');
  424. $min = $request->input('min_id');
  425. $max = $request->input('max_id');
  426. $limit = $request->input('limit') ?? 3;
  427. $user = $request->user();
  428. $amin = SnowflakeService::byDate(now()->subDays(90));
  429. $key = 'user:last_active_at:id:'.$user->id;
  430. $ttl = now()->addMinutes(5);
  431. Cache::remember($key, $ttl, function() use($user) {
  432. $user->last_active_at = now();
  433. $user->save();
  434. return;
  435. });
  436. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  437. if($min || $max) {
  438. $dir = $min ? '>' : '<';
  439. $id = $min ?? $max;
  440. $timeline = Status::select(
  441. 'id',
  442. 'uri',
  443. 'type',
  444. 'scope',
  445. 'created_at',
  446. )
  447. ->where('id', $dir, $id)
  448. ->whereNotIn('profile_id', $filtered)
  449. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  450. ->whereNotNull('uri')
  451. ->whereScope('public')
  452. ->where('id', '>', $amin)
  453. ->orderBy('created_at', 'desc')
  454. ->limit($limit)
  455. ->get()
  456. ->map(function($s) use ($user) {
  457. $status = StatusService::get($s->id);
  458. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  459. return $status;
  460. });
  461. $res = $timeline->toArray();
  462. } else {
  463. $timeline = Status::select(
  464. 'id',
  465. 'uri',
  466. 'type',
  467. 'scope',
  468. 'created_at',
  469. )
  470. ->whereNotIn('profile_id', $filtered)
  471. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  472. ->whereNotNull('uri')
  473. ->whereScope('public')
  474. ->where('id', '>', $amin)
  475. ->orderBy('created_at', 'desc')
  476. ->limit($limit)
  477. ->get()
  478. ->map(function($s) use ($user) {
  479. $status = StatusService::get($s->id);
  480. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  481. return $status;
  482. });
  483. $res = $timeline->toArray();
  484. }
  485. return response()->json($res);
  486. }
  487. public function relationships(Request $request)
  488. {
  489. if(!Auth::check()) {
  490. return response()->json([]);
  491. }
  492. $pid = $request->user()->profile_id;
  493. $this->validate($request, [
  494. 'id' => 'required|array|min:1|max:20',
  495. 'id.*' => 'required|integer'
  496. ]);
  497. $ids = collect($request->input('id'));
  498. $res = $ids->filter(function($v) use($pid) {
  499. return $v != $pid;
  500. })
  501. ->map(function($id) use($pid) {
  502. return RelationshipService::get($pid, $id);
  503. });
  504. return response()->json($res);
  505. }
  506. public function account(Request $request, $id)
  507. {
  508. $res = AccountService::get($id);
  509. return response()->json($res);
  510. }
  511. public function accountFollowers(Request $request, $id)
  512. {
  513. abort_unless(Auth::check(), 403);
  514. $profile = Profile::with('user')->whereNull('status')->findOrFail($id);
  515. $owner = Auth::id() == $profile->user_id;
  516. if(Auth::id() != $profile->user_id && $profile->is_private) {
  517. return response()->json([]);
  518. }
  519. if(!$profile->domain && !$profile->user->settings->show_profile_followers) {
  520. return response()->json([]);
  521. }
  522. if(!$owner && $request->page > 5) {
  523. return [];
  524. }
  525. $res = Follower::select('id', 'profile_id', 'following_id')
  526. ->whereFollowingId($profile->id)
  527. ->orderByDesc('id')
  528. ->simplePaginate(10)
  529. ->map(function($follower) {
  530. return ProfileService::get($follower['profile_id']);
  531. })
  532. ->toArray();
  533. return response()->json($res);
  534. }
  535. public function accountFollowing(Request $request, $id)
  536. {
  537. abort_unless(Auth::check(), 403);
  538. $profile = Profile::with('user')
  539. ->whereNull('status')
  540. ->findOrFail($id);
  541. // filter by username
  542. $search = $request->input('fbu');
  543. $owner = Auth::id() == $profile->user_id;
  544. $filter = ($owner == true) && ($search != null);
  545. abort_if($owner == false && $profile->is_private == true && !$profile->followedBy(Auth::user()->profile), 404);
  546. if(!$profile->domain) {
  547. abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
  548. }
  549. if(!$owner && $request->page > 5) {
  550. return [];
  551. }
  552. if($search) {
  553. abort_if(!$owner, 404);
  554. $following = $profile->following()
  555. ->where('profiles.username', 'like', '%'.$search.'%')
  556. ->orderByDesc('followers.created_at')
  557. ->paginate(10);
  558. } else {
  559. $following = $profile->following()
  560. ->orderByDesc('followers.created_at')
  561. ->paginate(10);
  562. }
  563. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  564. $res = $this->fractal->createData($resource)->toArray();
  565. return response()->json($res);
  566. }
  567. public function accountStatuses(Request $request, $id)
  568. {
  569. $this->validate($request, [
  570. 'only_media' => 'nullable',
  571. 'pinned' => 'nullable',
  572. 'exclude_replies' => 'nullable',
  573. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  574. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  575. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  576. 'limit' => 'nullable|integer|min:1|max:24'
  577. ]);
  578. $user = $request->user();
  579. $profile = AccountService::get($id);
  580. abort_if(!$profile, 404);
  581. $limit = $request->limit ?? 9;
  582. $max_id = $request->max_id;
  583. $min_id = $request->min_id;
  584. $scope = ['photo', 'photo:album', 'video', 'video:album'];
  585. if($profile['locked']) {
  586. if(!$user) {
  587. return response()->json([]);
  588. }
  589. $pid = $user->profile_id;
  590. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  591. $following = Follower::whereProfileId($pid)->pluck('following_id');
  592. return $following->push($pid)->toArray();
  593. });
  594. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  595. } else {
  596. if($user) {
  597. $pid = $user->profile_id;
  598. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  599. $following = Follower::whereProfileId($pid)->pluck('following_id');
  600. return $following->push($pid)->toArray();
  601. });
  602. $visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  603. } else {
  604. $visibility = ['public', 'unlisted'];
  605. }
  606. }
  607. $dir = $min_id ? '>' : '<';
  608. $id = $min_id ?? $max_id;
  609. $res = Status::whereProfileId($profile['id'])
  610. ->whereNull('in_reply_to_id')
  611. ->whereNull('reblog_of_id')
  612. ->whereIn('type', $scope)
  613. ->where('id', $dir, $id)
  614. ->whereIn('scope', $visibility)
  615. ->limit($limit)
  616. ->orderByDesc('id')
  617. ->get()
  618. ->map(function($s) use($user) {
  619. try {
  620. $status = StatusService::get($s->id, false);
  621. } catch (\Exception $e) {
  622. $status = false;
  623. }
  624. if($user && $status) {
  625. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  626. }
  627. return $status;
  628. })
  629. ->filter(function($s) {
  630. return $s;
  631. })
  632. ->values();
  633. return response()->json($res);
  634. }
  635. }