PublicApiController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. $key = 'user:last_active_at:id:'.$user->id;
  269. $ttl = now()->addMinutes(5);
  270. Cache::remember($key, $ttl, function() use($user) {
  271. $user->last_active_at = now();
  272. $user->save();
  273. return;
  274. });
  275. $filtered = UserFilter::whereUserId($user->profile_id)
  276. ->whereFilterableType('App\Profile')
  277. ->whereIn('filter_type', ['mute', 'block'])
  278. ->pluck('filterable_id')->toArray();
  279. if($min || $max) {
  280. $dir = $min ? '>' : '<';
  281. $id = $min ?? $max;
  282. $timeline = Status::select(
  283. 'id',
  284. 'uri',
  285. 'caption',
  286. 'rendered',
  287. 'profile_id',
  288. 'type',
  289. 'in_reply_to_id',
  290. 'reblog_of_id',
  291. 'is_nsfw',
  292. 'scope',
  293. 'local',
  294. 'reply_count',
  295. 'comments_disabled',
  296. 'place_id',
  297. 'likes_count',
  298. 'reblogs_count',
  299. 'created_at',
  300. 'updated_at'
  301. )->where('id', $dir, $id)
  302. ->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  303. ->whereNotIn('profile_id', $filtered)
  304. ->whereLocal(true)
  305. ->whereScope('public')
  306. ->where('created_at', '>', now()->subMonths(6))
  307. ->orderBy('created_at', 'desc')
  308. ->limit($limit)
  309. ->get();
  310. } else {
  311. $timeline = Status::select(
  312. 'id',
  313. 'uri',
  314. 'caption',
  315. 'rendered',
  316. 'profile_id',
  317. 'type',
  318. 'in_reply_to_id',
  319. 'reblog_of_id',
  320. 'is_nsfw',
  321. 'scope',
  322. 'local',
  323. 'reply_count',
  324. 'comments_disabled',
  325. 'created_at',
  326. 'place_id',
  327. 'likes_count',
  328. 'reblogs_count',
  329. 'updated_at'
  330. )->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  331. ->whereNotIn('profile_id', $filtered)
  332. ->with('profile', 'hashtags', 'mentions')
  333. ->whereLocal(true)
  334. ->whereScope('public')
  335. ->where('created_at', '>', now()->subMonths(6))
  336. ->orderBy('created_at', 'desc')
  337. ->simplePaginate($limit);
  338. }
  339. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  340. $res = $this->fractal->createData($fractal)->toArray();
  341. return response()->json($res);
  342. }
  343. public function homeTimelineApi(Request $request)
  344. {
  345. if(!Auth::check()) {
  346. return abort(403);
  347. }
  348. $this->validate($request,[
  349. 'page' => 'nullable|integer|max:40',
  350. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  351. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  352. 'limit' => 'nullable|integer|max:40',
  353. 'recent_feed' => 'nullable',
  354. 'recent_min' => 'nullable|integer'
  355. ]);
  356. $recentFeed = $request->input('recent_feed') == 'true';
  357. $recentFeedMin = $request->input('recent_min');
  358. $page = $request->input('page');
  359. $min = $request->input('min_id');
  360. $max = $request->input('max_id');
  361. $limit = $request->input('limit') ?? 3;
  362. $user = $request->user();
  363. $key = 'user:last_active_at:id:'.$user->id;
  364. $ttl = now()->addMinutes(5);
  365. Cache::remember($key, $ttl, function() use($user) {
  366. $user->last_active_at = now();
  367. $user->save();
  368. return;
  369. });
  370. $pid = Auth::user()->profile_id;
  371. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  372. $following = Follower::whereProfileId($pid)->pluck('following_id');
  373. return $following->push($pid)->toArray();
  374. });
  375. if($recentFeed == true) {
  376. $key = 'profile:home-timeline-cursor:'.$user->id;
  377. $ttl = now()->addMinutes(30);
  378. $min = Cache::remember($key, $ttl, function() use($pid) {
  379. $res = StatusView::whereProfileId($pid)->orderByDesc('status_id')->first();
  380. return $res ? $res->status_id : null;
  381. });
  382. }
  383. $filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : [];
  384. if($min || $max) {
  385. $dir = $min ? '>' : '<';
  386. $id = $min ?? $max;
  387. $timeline = Status::select(
  388. 'id',
  389. 'uri',
  390. 'caption',
  391. 'rendered',
  392. 'profile_id',
  393. 'type',
  394. 'in_reply_to_id',
  395. 'reblog_of_id',
  396. 'is_nsfw',
  397. 'scope',
  398. 'local',
  399. 'reply_count',
  400. 'comments_disabled',
  401. 'place_id',
  402. 'likes_count',
  403. 'reblogs_count',
  404. 'created_at',
  405. 'updated_at'
  406. )->whereIn('type', ['text','photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  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. )->whereIn('type', ['text','photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  436. ->with('profile', 'hashtags', 'mentions')
  437. ->whereIn('profile_id', $following)
  438. ->whereNotIn('profile_id', $filtered)
  439. ->whereIn('visibility',['public', 'unlisted', 'private'])
  440. ->orderBy('created_at', 'desc')
  441. ->simplePaginate($limit);
  442. }
  443. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  444. $res = $this->fractal->createData($fractal)->toArray();
  445. return response()->json($res);
  446. }
  447. public function networkTimelineApi(Request $request)
  448. {
  449. abort_if(!Auth::check(), 403);
  450. abort_if(config('federation.network_timeline') == false, 404);
  451. $this->validate($request,[
  452. 'page' => 'nullable|integer|max:40',
  453. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  454. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  455. 'limit' => 'nullable|integer|max:30'
  456. ]);
  457. $page = $request->input('page');
  458. $min = $request->input('min_id');
  459. $max = $request->input('max_id');
  460. $limit = $request->input('limit') ?? 3;
  461. $user = $request->user();
  462. $amin = SnowflakeService::byDate(now()->subDays(90));
  463. $key = 'user:last_active_at:id:'.$user->id;
  464. $ttl = now()->addMinutes(5);
  465. Cache::remember($key, $ttl, function() use($user) {
  466. $user->last_active_at = now();
  467. $user->save();
  468. return;
  469. });
  470. if($min || $max) {
  471. $dir = $min ? '>' : '<';
  472. $id = $min ?? $max;
  473. $timeline = Status::select(
  474. 'id',
  475. 'uri',
  476. 'type',
  477. 'scope',
  478. 'created_at',
  479. )->where('id', $dir, $id)
  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. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  501. ->whereNotNull('uri')
  502. ->whereScope('public')
  503. ->where('id', '>', $amin)
  504. ->orderBy('created_at', 'desc')
  505. ->limit($limit)
  506. ->get()
  507. ->map(function($s) use ($user) {
  508. $status = StatusService::get($s->id);
  509. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  510. return $status;
  511. });
  512. $res = $timeline->toArray();
  513. }
  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. }