PublicApiController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. UserFilter
  14. };
  15. use Auth,Cache;
  16. use Carbon\Carbon;
  17. use League\Fractal;
  18. use App\Transformer\Api\{
  19. AccountTransformer,
  20. RelationshipTransformer,
  21. StatusTransformer,
  22. StatusStatelessTransformer
  23. };
  24. use App\Services\{
  25. AccountService,
  26. PublicTimelineService,
  27. UserFilterService
  28. };
  29. use App\Jobs\StatusPipeline\NewStatusPipeline;
  30. use League\Fractal\Serializer\ArraySerializer;
  31. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  32. class PublicApiController extends Controller
  33. {
  34. protected $fractal;
  35. public function __construct()
  36. {
  37. $this->fractal = new Fractal\Manager();
  38. $this->fractal->setSerializer(new ArraySerializer());
  39. }
  40. protected function getUserData($user)
  41. {
  42. if(!$user) {
  43. return [];
  44. } else {
  45. return AccountService::get($user->profile_id);
  46. }
  47. }
  48. protected function getLikes($status)
  49. {
  50. if(false == Auth::check()) {
  51. return [];
  52. } else {
  53. $profile = Auth::user()->profile;
  54. if($profile->status) {
  55. return [];
  56. }
  57. $likes = $status->likedBy()->orderBy('created_at','desc')->paginate(10);
  58. $collection = new Fractal\Resource\Collection($likes, new AccountTransformer());
  59. return $this->fractal->createData($collection)->toArray();
  60. }
  61. }
  62. protected function getShares($status)
  63. {
  64. if(false == Auth::check()) {
  65. return [];
  66. } else {
  67. $profile = Auth::user()->profile;
  68. if($profile->status) {
  69. return [];
  70. }
  71. $shares = $status->sharedBy()->orderBy('created_at','desc')->paginate(10);
  72. $collection = new Fractal\Resource\Collection($shares, new AccountTransformer());
  73. return $this->fractal->createData($collection)->toArray();
  74. }
  75. }
  76. public function status(Request $request, $username, int $postid)
  77. {
  78. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  79. $status = Status::whereProfileId($profile->id)->findOrFail($postid);
  80. $this->scopeCheck($profile, $status);
  81. if(!Auth::check()) {
  82. $res = Cache::remember('wapi:v1:status:stateless_byid:' . $status->id, now()->addMinutes(30), function() use($status) {
  83. $item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
  84. $res = [
  85. 'status' => $this->fractal->createData($item)->toArray(),
  86. ];
  87. return $res;
  88. });
  89. return response()->json($res);
  90. }
  91. $item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
  92. $res = [
  93. 'status' => $this->fractal->createData($item)->toArray(),
  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. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  187. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  188. $this->scopeCheck($profile, $status);
  189. $likes = $this->getLikes($status);
  190. return response()->json([
  191. 'data' => $likes
  192. ]);
  193. }
  194. public function statusShares(Request $request, $username, $id)
  195. {
  196. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  197. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  198. $this->scopeCheck($profile, $status);
  199. $shares = $this->getShares($status);
  200. return response()->json([
  201. 'data' => $shares
  202. ]);
  203. }
  204. protected function scopeCheck(Profile $profile, Status $status)
  205. {
  206. if($profile->is_private == true && Auth::check() == false) {
  207. abort(404);
  208. }
  209. switch ($status->scope) {
  210. case 'public':
  211. case 'unlisted':
  212. break;
  213. case 'private':
  214. $user = Auth::check() ? Auth::user() : false;
  215. if(!$user) {
  216. abort(403);
  217. } else {
  218. $follows = $profile->followedBy($user->profile);
  219. if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) {
  220. abort(404);
  221. }
  222. }
  223. break;
  224. case 'direct':
  225. abort(404);
  226. break;
  227. case 'draft':
  228. abort(404);
  229. break;
  230. default:
  231. abort(404);
  232. break;
  233. }
  234. }
  235. public function publicTimelineApi(Request $request)
  236. {
  237. $this->validate($request,[
  238. 'page' => 'nullable|integer|max:40',
  239. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  240. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  241. 'limit' => 'nullable|integer|max:30'
  242. ]);
  243. if(config('instance.timeline.local.is_public') == false && !Auth::check()) {
  244. abort(403, 'Authentication required.');
  245. }
  246. $page = $request->input('page');
  247. $min = $request->input('min_id');
  248. $max = $request->input('max_id');
  249. $limit = $request->input('limit') ?? 3;
  250. $user = $request->user();
  251. $key = 'user:last_active_at:id:'.$user->id;
  252. $ttl = now()->addMinutes(5);
  253. Cache::remember($key, $ttl, function() use($user) {
  254. $user->last_active_at = now();
  255. $user->save();
  256. return;
  257. });
  258. $filtered = UserFilter::whereUserId($user->profile_id)
  259. ->whereFilterableType('App\Profile')
  260. ->whereIn('filter_type', ['mute', 'block'])
  261. ->pluck('filterable_id')->toArray();
  262. if($min || $max) {
  263. $dir = $min ? '>' : '<';
  264. $id = $min ?? $max;
  265. $timeline = Status::select(
  266. 'id',
  267. 'uri',
  268. 'caption',
  269. 'rendered',
  270. 'profile_id',
  271. 'type',
  272. 'in_reply_to_id',
  273. 'reblog_of_id',
  274. 'is_nsfw',
  275. 'scope',
  276. 'local',
  277. 'reply_count',
  278. 'comments_disabled',
  279. 'place_id',
  280. 'likes_count',
  281. 'reblogs_count',
  282. 'created_at',
  283. 'updated_at'
  284. )->where('id', $dir, $id)
  285. ->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  286. ->whereNotIn('profile_id', $filtered)
  287. ->whereLocal(true)
  288. ->whereScope('public')
  289. ->where('created_at', '>', now()->subMonths(3))
  290. ->orderBy('created_at', 'desc')
  291. ->limit($limit)
  292. ->get();
  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. )->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  314. ->whereNotIn('profile_id', $filtered)
  315. ->with('profile', 'hashtags', 'mentions')
  316. ->whereLocal(true)
  317. ->whereScope('public')
  318. ->where('created_at', '>', now()->subMonths(3))
  319. ->orderBy('created_at', 'desc')
  320. ->simplePaginate($limit);
  321. }
  322. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  323. $res = $this->fractal->createData($fractal)->toArray();
  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. ]);
  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(5);
  344. Cache::remember($key, $ttl, function() use($user) {
  345. $user->last_active_at = now();
  346. $user->save();
  347. return;
  348. });
  349. // TODO: Use redis for timelines
  350. // $timeline = Timeline::build()->local();
  351. $pid = Auth::user()->profile->id;
  352. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  353. $following = Follower::whereProfileId($pid)->pluck('following_id');
  354. return $following->push($pid)->toArray();
  355. });
  356. // $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  357. // return Profile::whereIsPrivate(true)
  358. // ->orWhere('unlisted', true)
  359. // ->orWhere('status', '!=', null)
  360. // ->pluck('id');
  361. // });
  362. // $private = $private->diff($following)->flatten();
  363. // $filters = UserFilter::whereUserId($pid)
  364. // ->whereFilterableType('App\Profile')
  365. // ->whereIn('filter_type', ['mute', 'block'])
  366. // ->pluck('filterable_id')->toArray();
  367. // $filtered = array_merge($private->toArray(), $filters);
  368. $filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : [];
  369. if($min || $max) {
  370. $dir = $min ? '>' : '<';
  371. $id = $min ?? $max;
  372. $timeline = Status::select(
  373. 'id',
  374. 'uri',
  375. 'caption',
  376. 'rendered',
  377. 'profile_id',
  378. 'type',
  379. 'in_reply_to_id',
  380. 'reblog_of_id',
  381. 'is_nsfw',
  382. 'scope',
  383. 'local',
  384. 'reply_count',
  385. 'comments_disabled',
  386. 'place_id',
  387. 'likes_count',
  388. 'reblogs_count',
  389. 'created_at',
  390. 'updated_at'
  391. )->whereIn('type', ['text','photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  392. ->with('profile', 'hashtags', 'mentions')
  393. ->where('id', $dir, $id)
  394. ->whereIn('profile_id', $following)
  395. ->whereNotIn('profile_id', $filtered)
  396. ->whereIn('visibility',['public', 'unlisted', 'private'])
  397. ->orderBy('created_at', 'desc')
  398. ->limit($limit)
  399. ->get();
  400. } else {
  401. $timeline = Status::select(
  402. 'id',
  403. 'uri',
  404. 'caption',
  405. 'rendered',
  406. 'profile_id',
  407. 'type',
  408. 'in_reply_to_id',
  409. 'reblog_of_id',
  410. 'is_nsfw',
  411. 'scope',
  412. 'local',
  413. 'reply_count',
  414. 'comments_disabled',
  415. 'place_id',
  416. 'likes_count',
  417. 'reblogs_count',
  418. 'created_at',
  419. 'updated_at'
  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. $key = 'user:last_active_at:id:'.$user->id;
  448. $ttl = now()->addMinutes(5);
  449. Cache::remember($key, $ttl, function() use($user) {
  450. $user->last_active_at = now();
  451. $user->save();
  452. return;
  453. });
  454. if($min || $max) {
  455. $dir = $min ? '>' : '<';
  456. $id = $min ?? $max;
  457. $timeline = Status::select(
  458. 'id',
  459. 'uri',
  460. 'caption',
  461. 'rendered',
  462. 'profile_id',
  463. 'type',
  464. 'in_reply_to_id',
  465. 'reblog_of_id',
  466. 'is_nsfw',
  467. 'scope',
  468. 'local',
  469. 'reply_count',
  470. 'comments_disabled',
  471. 'place_id',
  472. 'likes_count',
  473. 'reblogs_count',
  474. 'created_at',
  475. 'updated_at'
  476. )->where('id', $dir, $id)
  477. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  478. ->whereNotNull('uri')
  479. ->whereScope('public')
  480. ->where('created_at', '>', now()->subMonths(3))
  481. ->orderBy('created_at', 'desc')
  482. ->limit($limit)
  483. ->get();
  484. } else {
  485. $timeline = Status::select(
  486. 'id',
  487. 'uri',
  488. 'caption',
  489. 'rendered',
  490. 'profile_id',
  491. 'type',
  492. 'in_reply_to_id',
  493. 'reblog_of_id',
  494. 'is_nsfw',
  495. 'scope',
  496. 'local',
  497. 'reply_count',
  498. 'comments_disabled',
  499. 'created_at',
  500. 'place_id',
  501. 'likes_count',
  502. 'reblogs_count',
  503. 'updated_at'
  504. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  505. ->with('profile', 'hashtags', 'mentions')
  506. ->whereNotNull('uri')
  507. ->whereScope('public')
  508. ->where('created_at', '>', now()->subMonths(3))
  509. ->orderBy('created_at', 'desc')
  510. ->simplePaginate($limit);
  511. }
  512. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  513. $res = $this->fractal->createData($fractal)->toArray();
  514. return response()->json($res);
  515. }
  516. public function relationships(Request $request)
  517. {
  518. if(!Auth::check()) {
  519. return response()->json([]);
  520. }
  521. $this->validate($request, [
  522. 'id' => 'required|array|min:1|max:20',
  523. 'id.*' => 'required|integer'
  524. ]);
  525. $ids = collect($request->input('id'));
  526. $filtered = $ids->filter(function($v) {
  527. return $v != Auth::user()->profile->id;
  528. });
  529. $relations = Profile::whereNull('status')->findOrFail($filtered->all());
  530. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  531. $res = $this->fractal->createData($fractal)->toArray();
  532. return response()->json($res);
  533. }
  534. public function account(Request $request, $id)
  535. {
  536. $res = AccountService::get($id);
  537. return response()->json($res);
  538. }
  539. public function accountFollowers(Request $request, $id)
  540. {
  541. abort_unless(Auth::check(), 403);
  542. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  543. if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_followers) {
  544. return response()->json([]);
  545. }
  546. $followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
  547. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  548. $res = $this->fractal->createData($resource)->toArray();
  549. return response()->json($res);
  550. }
  551. public function accountFollowing(Request $request, $id)
  552. {
  553. abort_unless(Auth::check(), 403);
  554. $profile = Profile::with('user')
  555. ->whereNull('status')
  556. ->whereNull('domain')
  557. ->findOrFail($id);
  558. // filter by username
  559. $search = $request->input('fbu');
  560. $owner = Auth::id() == $profile->user_id;
  561. $filter = ($owner == true) && ($search != null);
  562. abort_if($owner == false && $profile->is_private == true && !$profile->followedBy(Auth::user()->profile), 404);
  563. abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
  564. if($search) {
  565. abort_if(!$owner, 404);
  566. $following = $profile->following()
  567. ->where('profiles.username', 'like', '%'.$search.'%')
  568. ->orderByDesc('followers.created_at')
  569. ->paginate(10);
  570. } else {
  571. $following = $profile->following()
  572. ->orderByDesc('followers.created_at')
  573. ->paginate(10);
  574. }
  575. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  576. $res = $this->fractal->createData($resource)->toArray();
  577. return response()->json($res);
  578. }
  579. public function accountStatuses(Request $request, $id)
  580. {
  581. $this->validate($request, [
  582. 'only_media' => 'nullable',
  583. 'pinned' => 'nullable',
  584. 'exclude_replies' => 'nullable',
  585. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  586. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  587. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  588. 'limit' => 'nullable|integer|min:1|max:24'
  589. ]);
  590. $profile = Profile::whereNull('status')->findOrFail($id);
  591. $limit = $request->limit ?? 9;
  592. $max_id = $request->max_id;
  593. $min_id = $request->min_id;
  594. $scope = $request->only_media == true ?
  595. ['photo', 'photo:album', 'video', 'video:album'] :
  596. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  597. if($profile->is_private) {
  598. if(!Auth::check()) {
  599. return response()->json([]);
  600. }
  601. $pid = Auth::user()->profile->id;
  602. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  603. $following = Follower::whereProfileId($pid)->pluck('following_id');
  604. return $following->push($pid)->toArray();
  605. });
  606. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  607. } else {
  608. if(Auth::check()) {
  609. $pid = Auth::user()->profile->id;
  610. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  611. $following = Follower::whereProfileId($pid)->pluck('following_id');
  612. return $following->push($pid)->toArray();
  613. });
  614. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  615. } else {
  616. $visibility = ['public', 'unlisted'];
  617. }
  618. }
  619. $tag = in_array('private', $visibility) ? 'private' : 'public';
  620. if($min_id == 1 && $limit == 9 && $tag == 'public') {
  621. $limit = 9;
  622. $scope = ['photo', 'photo:album', 'video', 'video:album'];
  623. $key = '_api:statuses:recent_9:'.$profile->id;
  624. $res = Cache::remember($key, now()->addHours(24), function() use($profile, $scope, $visibility, $limit) {
  625. $dir = '>';
  626. $id = 1;
  627. $timeline = Status::select(
  628. 'id',
  629. 'uri',
  630. 'caption',
  631. 'rendered',
  632. 'profile_id',
  633. 'type',
  634. 'in_reply_to_id',
  635. 'reblog_of_id',
  636. 'is_nsfw',
  637. 'likes_count',
  638. 'reblogs_count',
  639. 'scope',
  640. 'visibility',
  641. 'local',
  642. 'place_id',
  643. 'comments_disabled',
  644. 'cw_summary',
  645. 'created_at',
  646. 'updated_at'
  647. )->whereProfileId($profile->id)
  648. ->whereIn('type', $scope)
  649. ->where('id', $dir, $id)
  650. ->whereIn('visibility', $visibility)
  651. ->limit($limit)
  652. ->orderByDesc('id')
  653. ->get();
  654. $resource = new Fractal\Resource\Collection($timeline, new StatusStatelessTransformer());
  655. $res = $this->fractal->createData($resource)->toArray();
  656. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  657. });
  658. return $res;
  659. }
  660. $dir = $min_id ? '>' : '<';
  661. $id = $min_id ?? $max_id;
  662. $timeline = Status::select(
  663. 'id',
  664. 'uri',
  665. 'caption',
  666. 'rendered',
  667. 'profile_id',
  668. 'type',
  669. 'in_reply_to_id',
  670. 'reblog_of_id',
  671. 'is_nsfw',
  672. 'likes_count',
  673. 'reblogs_count',
  674. 'scope',
  675. 'visibility',
  676. 'local',
  677. 'place_id',
  678. 'comments_disabled',
  679. 'cw_summary',
  680. 'created_at',
  681. 'updated_at'
  682. )->whereProfileId($profile->id)
  683. ->whereIn('type', $scope)
  684. ->where('id', $dir, $id)
  685. ->whereIn('visibility', $visibility)
  686. ->limit($limit)
  687. ->orderByDesc('id')
  688. ->get();
  689. $resource = new Fractal\Resource\Collection($timeline, new StatusStatelessTransformer());
  690. $res = $this->fractal->createData($resource)->toArray();
  691. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  692. }
  693. }