PublicApiController.php 27 KB

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