PublicApiController.php 28 KB

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