PublicApiController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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::getFull($s->id, $user->profile_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. ->limit($limit)
  317. ->get()
  318. ->map(function($s) use ($user) {
  319. $status = StatusService::getFull($s->id, $user->profile_id);
  320. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  321. return $status;
  322. });
  323. $res = $timeline->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. $types = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'];
  369. $textOnlyReplies = false;
  370. if(config('exp.top')) {
  371. $textOnlyPosts = (bool) Redis::zscore('pf:tl:top', $pid);
  372. $textOnlyReplies = (bool) Redis::zscore('pf:tl:replies', $pid);
  373. if($textOnlyPosts) {
  374. array_push($types, 'text');
  375. }
  376. }
  377. if(config('exp.polls') == true) {
  378. array_push($types, 'poll');
  379. }
  380. if($min || $max) {
  381. $dir = $min ? '>' : '<';
  382. $id = $min ?? $max;
  383. $timeline = Status::select(
  384. 'id',
  385. 'uri',
  386. 'caption',
  387. 'rendered',
  388. 'profile_id',
  389. 'type',
  390. 'in_reply_to_id',
  391. 'reblog_of_id',
  392. 'is_nsfw',
  393. 'scope',
  394. 'local',
  395. 'reply_count',
  396. 'comments_disabled',
  397. 'place_id',
  398. 'likes_count',
  399. 'reblogs_count',
  400. 'created_at',
  401. 'updated_at'
  402. )
  403. ->whereIn('type', $types)
  404. ->when($textOnlyReplies != true, function($q, $textOnlyReplies) {
  405. return $q->whereNull('in_reply_to_id');
  406. })
  407. ->with('profile', 'hashtags', 'mentions')
  408. ->where('id', $dir, $id)
  409. ->whereIn('profile_id', $following)
  410. ->whereNotIn('profile_id', $filtered)
  411. ->whereIn('visibility',['public', 'unlisted', 'private'])
  412. ->orderBy('created_at', 'desc')
  413. ->limit($limit)
  414. ->get();
  415. } else {
  416. $timeline = Status::select(
  417. 'id',
  418. 'uri',
  419. 'caption',
  420. 'rendered',
  421. 'profile_id',
  422. 'type',
  423. 'in_reply_to_id',
  424. 'reblog_of_id',
  425. 'is_nsfw',
  426. 'scope',
  427. 'local',
  428. 'reply_count',
  429. 'comments_disabled',
  430. 'place_id',
  431. 'likes_count',
  432. 'reblogs_count',
  433. 'created_at',
  434. 'updated_at'
  435. )
  436. ->whereIn('type', $types)
  437. ->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
  438. return $q->whereNull('in_reply_to_id');
  439. })
  440. ->with('profile', 'hashtags', 'mentions')
  441. ->whereIn('profile_id', $following)
  442. ->whereNotIn('profile_id', $filtered)
  443. ->whereIn('visibility',['public', 'unlisted', 'private'])
  444. ->orderBy('created_at', 'desc')
  445. ->simplePaginate($limit);
  446. }
  447. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  448. $res = $this->fractal->createData($fractal)->toArray();
  449. return response()->json($res);
  450. }
  451. public function networkTimelineApi(Request $request)
  452. {
  453. abort_if(!Auth::check(), 403);
  454. abort_if(config('federation.network_timeline') == false, 404);
  455. $this->validate($request,[
  456. 'page' => 'nullable|integer|max:40',
  457. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  458. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  459. 'limit' => 'nullable|integer|max:30'
  460. ]);
  461. $page = $request->input('page');
  462. $min = $request->input('min_id');
  463. $max = $request->input('max_id');
  464. $limit = $request->input('limit') ?? 3;
  465. $user = $request->user();
  466. $amin = SnowflakeService::byDate(now()->subDays(90));
  467. $key = 'user:last_active_at:id:'.$user->id;
  468. $ttl = now()->addMinutes(5);
  469. Cache::remember($key, $ttl, function() use($user) {
  470. $user->last_active_at = now();
  471. $user->save();
  472. return;
  473. });
  474. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  475. if($min || $max) {
  476. $dir = $min ? '>' : '<';
  477. $id = $min ?? $max;
  478. $timeline = Status::select(
  479. 'id',
  480. 'uri',
  481. 'type',
  482. 'scope',
  483. 'created_at',
  484. )
  485. ->where('id', $dir, $id)
  486. ->whereNotIn('profile_id', $filtered)
  487. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  488. ->whereNotNull('uri')
  489. ->whereScope('public')
  490. ->where('id', '>', $amin)
  491. ->orderBy('created_at', 'desc')
  492. ->limit($limit)
  493. ->get()
  494. ->map(function($s) use ($user) {
  495. $status = StatusService::get($s->id);
  496. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  497. return $status;
  498. });
  499. $res = $timeline->toArray();
  500. } else {
  501. $timeline = Status::select(
  502. 'id',
  503. 'uri',
  504. 'type',
  505. 'scope',
  506. 'created_at',
  507. )
  508. ->whereNotIn('profile_id', $filtered)
  509. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  510. ->whereNotNull('uri')
  511. ->whereScope('public')
  512. ->where('id', '>', $amin)
  513. ->orderBy('created_at', 'desc')
  514. ->limit($limit)
  515. ->get()
  516. ->map(function($s) use ($user) {
  517. $status = StatusService::get($s->id);
  518. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  519. return $status;
  520. });
  521. $res = $timeline->toArray();
  522. }
  523. return response()->json($res);
  524. }
  525. public function relationships(Request $request)
  526. {
  527. if(!Auth::check()) {
  528. return response()->json([]);
  529. }
  530. $this->validate($request, [
  531. 'id' => 'required|array|min:1|max:20',
  532. 'id.*' => 'required|integer'
  533. ]);
  534. $ids = collect($request->input('id'));
  535. $filtered = $ids->filter(function($v) {
  536. return $v != Auth::user()->profile->id;
  537. });
  538. $relations = Profile::whereNull('status')->findOrFail($filtered->all());
  539. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  540. $res = $this->fractal->createData($fractal)->toArray();
  541. return response()->json($res);
  542. }
  543. public function account(Request $request, $id)
  544. {
  545. $res = AccountService::get($id);
  546. return response()->json($res);
  547. }
  548. public function accountFollowers(Request $request, $id)
  549. {
  550. abort_unless(Auth::check(), 403);
  551. $profile = Profile::with('user')->whereNull('status')->findOrFail($id);
  552. $owner = Auth::id() == $profile->user_id;
  553. if(Auth::id() != $profile->user_id && $profile->is_private) {
  554. return response()->json([]);
  555. }
  556. if(!$profile->domain && !$profile->user->settings->show_profile_followers) {
  557. return response()->json([]);
  558. }
  559. if(!$owner && $request->page > 5) {
  560. return [];
  561. }
  562. $res = Follower::select('id', 'profile_id', 'following_id')
  563. ->whereFollowingId($profile->id)
  564. ->orderByDesc('id')
  565. ->simplePaginate(10)
  566. ->map(function($follower) {
  567. return ProfileService::get($follower['profile_id']);
  568. })
  569. ->toArray();
  570. return response()->json($res);
  571. }
  572. public function accountFollowing(Request $request, $id)
  573. {
  574. abort_unless(Auth::check(), 403);
  575. $profile = Profile::with('user')
  576. ->whereNull('status')
  577. ->findOrFail($id);
  578. // filter by username
  579. $search = $request->input('fbu');
  580. $owner = Auth::id() == $profile->user_id;
  581. $filter = ($owner == true) && ($search != null);
  582. abort_if($owner == false && $profile->is_private == true && !$profile->followedBy(Auth::user()->profile), 404);
  583. if(!$profile->domain) {
  584. abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
  585. }
  586. if(!$owner && $request->page > 5) {
  587. return [];
  588. }
  589. if($search) {
  590. abort_if(!$owner, 404);
  591. $following = $profile->following()
  592. ->where('profiles.username', 'like', '%'.$search.'%')
  593. ->orderByDesc('followers.created_at')
  594. ->paginate(10);
  595. } else {
  596. $following = $profile->following()
  597. ->orderByDesc('followers.created_at')
  598. ->paginate(10);
  599. }
  600. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  601. $res = $this->fractal->createData($resource)->toArray();
  602. return response()->json($res);
  603. }
  604. public function accountStatuses(Request $request, $id)
  605. {
  606. $this->validate($request, [
  607. 'only_media' => 'nullable',
  608. 'pinned' => 'nullable',
  609. 'exclude_replies' => 'nullable',
  610. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  611. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  612. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  613. 'limit' => 'nullable|integer|min:1|max:24'
  614. ]);
  615. $user = $request->user();
  616. $profile = AccountService::get($id);
  617. abort_if(!$profile, 404);
  618. $limit = $request->limit ?? 9;
  619. $max_id = $request->max_id;
  620. $min_id = $request->min_id;
  621. $scope = ['photo', 'photo:album', 'video', 'video:album'];
  622. if($profile['locked']) {
  623. if(!$user) {
  624. return response()->json([]);
  625. }
  626. $pid = $user->profile_id;
  627. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  628. $following = Follower::whereProfileId($pid)->pluck('following_id');
  629. return $following->push($pid)->toArray();
  630. });
  631. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  632. } else {
  633. if($user) {
  634. $pid = $user->profile_id;
  635. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  636. $following = Follower::whereProfileId($pid)->pluck('following_id');
  637. return $following->push($pid)->toArray();
  638. });
  639. $visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  640. } else {
  641. $visibility = ['public', 'unlisted'];
  642. }
  643. }
  644. $dir = $min_id ? '>' : '<';
  645. $id = $min_id ?? $max_id;
  646. $res = Status::whereProfileId($profile['id'])
  647. ->whereNull('in_reply_to_id')
  648. ->whereNull('reblog_of_id')
  649. ->whereIn('type', $scope)
  650. ->where('id', $dir, $id)
  651. ->whereIn('scope', $visibility)
  652. ->limit($limit)
  653. ->orderByDesc('id')
  654. ->get()
  655. ->map(function($s) use($user) {
  656. try {
  657. $status = StatusService::get($s->id, false);
  658. } catch (\Exception $e) {
  659. $status = false;
  660. }
  661. if($user && $status) {
  662. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  663. }
  664. return $status;
  665. })
  666. ->filter(function($s) {
  667. return $s;
  668. })
  669. ->values();
  670. return response()->json($res);
  671. }
  672. }