PublicApiController.php 26 KB

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