PublicApiController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. $status = Status::findOrFail($id);
  187. $this->scopeCheck($status->profile, $status);
  188. $likes = $this->getLikes($status);
  189. return response()->json([
  190. 'data' => $likes
  191. ]);
  192. }
  193. public function statusShares(Request $request, $username, $id)
  194. {
  195. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  196. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  197. $this->scopeCheck($profile, $status);
  198. $shares = $this->getShares($status);
  199. return response()->json([
  200. 'data' => $shares
  201. ]);
  202. }
  203. protected function scopeCheck(Profile $profile, Status $status)
  204. {
  205. if($profile->is_private == true && Auth::check() == false) {
  206. abort(404);
  207. }
  208. switch ($status->scope) {
  209. case 'public':
  210. case 'unlisted':
  211. break;
  212. case 'private':
  213. $user = Auth::check() ? Auth::user() : false;
  214. if(!$user) {
  215. abort(403);
  216. } else {
  217. $follows = $profile->followedBy($user->profile);
  218. if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) {
  219. abort(404);
  220. }
  221. }
  222. break;
  223. case 'direct':
  224. abort(404);
  225. break;
  226. case 'draft':
  227. abort(404);
  228. break;
  229. default:
  230. abort(404);
  231. break;
  232. }
  233. }
  234. public function publicTimelineApi(Request $request)
  235. {
  236. $this->validate($request,[
  237. 'page' => 'nullable|integer|max:40',
  238. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  239. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  240. 'limit' => 'nullable|integer|max:30'
  241. ]);
  242. if(config('instance.timeline.local.is_public') == false && !Auth::check()) {
  243. abort(403, 'Authentication required.');
  244. }
  245. $page = $request->input('page');
  246. $min = $request->input('min_id');
  247. $max = $request->input('max_id');
  248. $limit = $request->input('limit') ?? 3;
  249. $user = $request->user();
  250. $key = 'user:last_active_at:id:'.$user->id;
  251. $ttl = now()->addMinutes(5);
  252. Cache::remember($key, $ttl, function() use($user) {
  253. $user->last_active_at = now();
  254. $user->save();
  255. return;
  256. });
  257. $filtered = UserFilter::whereUserId($user->profile_id)
  258. ->whereFilterableType('App\Profile')
  259. ->whereIn('filter_type', ['mute', 'block'])
  260. ->pluck('filterable_id')->toArray();
  261. if($min || $max) {
  262. $dir = $min ? '>' : '<';
  263. $id = $min ?? $max;
  264. $timeline = Status::select(
  265. 'id',
  266. 'uri',
  267. 'caption',
  268. 'rendered',
  269. 'profile_id',
  270. 'type',
  271. 'in_reply_to_id',
  272. 'reblog_of_id',
  273. 'is_nsfw',
  274. 'scope',
  275. 'local',
  276. 'reply_count',
  277. 'comments_disabled',
  278. 'place_id',
  279. 'likes_count',
  280. 'reblogs_count',
  281. 'created_at',
  282. 'updated_at'
  283. )->where('id', $dir, $id)
  284. ->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  285. ->whereNotIn('profile_id', $filtered)
  286. ->whereLocal(true)
  287. ->whereScope('public')
  288. ->where('created_at', '>', now()->subMonths(6))
  289. ->orderBy('created_at', 'desc')
  290. ->limit($limit)
  291. ->get();
  292. } else {
  293. $timeline = Status::select(
  294. 'id',
  295. 'uri',
  296. 'caption',
  297. 'rendered',
  298. 'profile_id',
  299. 'type',
  300. 'in_reply_to_id',
  301. 'reblog_of_id',
  302. 'is_nsfw',
  303. 'scope',
  304. 'local',
  305. 'reply_count',
  306. 'comments_disabled',
  307. 'created_at',
  308. 'place_id',
  309. 'likes_count',
  310. 'reblogs_count',
  311. 'updated_at'
  312. )->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  313. ->whereNotIn('profile_id', $filtered)
  314. ->with('profile', 'hashtags', 'mentions')
  315. ->whereLocal(true)
  316. ->whereScope('public')
  317. ->where('created_at', '>', now()->subMonths(6))
  318. ->orderBy('created_at', 'desc')
  319. ->simplePaginate($limit);
  320. }
  321. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  322. $res = $this->fractal->createData($fractal)->toArray();
  323. return response()->json($res);
  324. }
  325. public function homeTimelineApi(Request $request)
  326. {
  327. if(!Auth::check()) {
  328. return abort(403);
  329. }
  330. $this->validate($request,[
  331. 'page' => 'nullable|integer|max:40',
  332. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  333. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  334. 'limit' => 'nullable|integer|max:40'
  335. ]);
  336. $page = $request->input('page');
  337. $min = $request->input('min_id');
  338. $max = $request->input('max_id');
  339. $limit = $request->input('limit') ?? 3;
  340. $user = $request->user();
  341. $key = 'user:last_active_at:id:'.$user->id;
  342. $ttl = now()->addMinutes(5);
  343. Cache::remember($key, $ttl, function() use($user) {
  344. $user->last_active_at = now();
  345. $user->save();
  346. return;
  347. });
  348. // TODO: Use redis for timelines
  349. // $timeline = Timeline::build()->local();
  350. $pid = Auth::user()->profile->id;
  351. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  352. $following = Follower::whereProfileId($pid)->pluck('following_id');
  353. return $following->push($pid)->toArray();
  354. });
  355. // $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  356. // return Profile::whereIsPrivate(true)
  357. // ->orWhere('unlisted', true)
  358. // ->orWhere('status', '!=', null)
  359. // ->pluck('id');
  360. // });
  361. // $private = $private->diff($following)->flatten();
  362. // $filters = UserFilter::whereUserId($pid)
  363. // ->whereFilterableType('App\Profile')
  364. // ->whereIn('filter_type', ['mute', 'block'])
  365. // ->pluck('filterable_id')->toArray();
  366. // $filtered = array_merge($private->toArray(), $filters);
  367. $filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : [];
  368. if($min || $max) {
  369. $dir = $min ? '>' : '<';
  370. $id = $min ?? $max;
  371. $timeline = Status::select(
  372. 'id',
  373. 'uri',
  374. 'caption',
  375. 'rendered',
  376. 'profile_id',
  377. 'type',
  378. 'in_reply_to_id',
  379. 'reblog_of_id',
  380. 'is_nsfw',
  381. 'scope',
  382. 'local',
  383. 'reply_count',
  384. 'comments_disabled',
  385. 'place_id',
  386. 'likes_count',
  387. 'reblogs_count',
  388. 'created_at',
  389. 'updated_at'
  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. )->whereIn('type', ['text','photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  420. ->with('profile', 'hashtags', 'mentions')
  421. ->whereIn('profile_id', $following)
  422. ->whereNotIn('profile_id', $filtered)
  423. ->whereIn('visibility',['public', 'unlisted', 'private'])
  424. ->orderBy('created_at', 'desc')
  425. ->simplePaginate($limit);
  426. }
  427. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  428. $res = $this->fractal->createData($fractal)->toArray();
  429. return response()->json($res);
  430. }
  431. public function networkTimelineApi(Request $request)
  432. {
  433. abort_if(!Auth::check(), 403);
  434. abort_if(config('federation.network_timeline') == false, 404);
  435. $this->validate($request,[
  436. 'page' => 'nullable|integer|max:40',
  437. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  438. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  439. 'limit' => 'nullable|integer|max:30'
  440. ]);
  441. $page = $request->input('page');
  442. $min = $request->input('min_id');
  443. $max = $request->input('max_id');
  444. $limit = $request->input('limit') ?? 3;
  445. $user = $request->user();
  446. $key = 'user:last_active_at:id:'.$user->id;
  447. $ttl = now()->addMinutes(5);
  448. Cache::remember($key, $ttl, function() use($user) {
  449. $user->last_active_at = now();
  450. $user->save();
  451. return;
  452. });
  453. if($min || $max) {
  454. $dir = $min ? '>' : '<';
  455. $id = $min ?? $max;
  456. $timeline = Status::select(
  457. 'id',
  458. 'uri',
  459. 'caption',
  460. 'rendered',
  461. 'profile_id',
  462. 'type',
  463. 'in_reply_to_id',
  464. 'reblog_of_id',
  465. 'is_nsfw',
  466. 'scope',
  467. 'local',
  468. 'reply_count',
  469. 'comments_disabled',
  470. 'place_id',
  471. 'likes_count',
  472. 'reblogs_count',
  473. 'created_at',
  474. 'updated_at'
  475. )->where('id', $dir, $id)
  476. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  477. ->whereNotNull('uri')
  478. ->whereScope('public')
  479. ->where('created_at', '>', now()->subMonths(3))
  480. ->orderBy('created_at', 'desc')
  481. ->limit($limit)
  482. ->get();
  483. } else {
  484. $timeline = Status::select(
  485. 'id',
  486. 'uri',
  487. 'caption',
  488. 'rendered',
  489. 'profile_id',
  490. 'type',
  491. 'in_reply_to_id',
  492. 'reblog_of_id',
  493. 'is_nsfw',
  494. 'scope',
  495. 'local',
  496. 'reply_count',
  497. 'comments_disabled',
  498. 'created_at',
  499. 'place_id',
  500. 'likes_count',
  501. 'reblogs_count',
  502. 'updated_at'
  503. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  504. ->with('profile', 'hashtags', 'mentions')
  505. ->whereNotNull('uri')
  506. ->whereScope('public')
  507. ->where('created_at', '>', now()->subMonths(3))
  508. ->orderBy('created_at', 'desc')
  509. ->simplePaginate($limit);
  510. }
  511. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  512. $res = $this->fractal->createData($fractal)->toArray();
  513. return response()->json($res);
  514. }
  515. public function relationships(Request $request)
  516. {
  517. if(!Auth::check()) {
  518. return response()->json([]);
  519. }
  520. $this->validate($request, [
  521. 'id' => 'required|array|min:1|max:20',
  522. 'id.*' => 'required|integer'
  523. ]);
  524. $ids = collect($request->input('id'));
  525. $filtered = $ids->filter(function($v) {
  526. return $v != Auth::user()->profile->id;
  527. });
  528. $relations = Profile::whereNull('status')->findOrFail($filtered->all());
  529. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  530. $res = $this->fractal->createData($fractal)->toArray();
  531. return response()->json($res);
  532. }
  533. public function account(Request $request, $id)
  534. {
  535. $res = AccountService::get($id);
  536. return response()->json($res);
  537. }
  538. public function accountFollowers(Request $request, $id)
  539. {
  540. abort_unless(Auth::check(), 403);
  541. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  542. if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_followers) {
  543. return response()->json([]);
  544. }
  545. $followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
  546. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  547. $res = $this->fractal->createData($resource)->toArray();
  548. return response()->json($res);
  549. }
  550. public function accountFollowing(Request $request, $id)
  551. {
  552. abort_unless(Auth::check(), 403);
  553. $profile = Profile::with('user')
  554. ->whereNull('status')
  555. ->whereNull('domain')
  556. ->findOrFail($id);
  557. // filter by username
  558. $search = $request->input('fbu');
  559. $owner = Auth::id() == $profile->user_id;
  560. $filter = ($owner == true) && ($search != null);
  561. abort_if($owner == false && $profile->is_private == true && !$profile->followedBy(Auth::user()->profile), 404);
  562. abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
  563. if($search) {
  564. abort_if(!$owner, 404);
  565. $following = $profile->following()
  566. ->where('profiles.username', 'like', '%'.$search.'%')
  567. ->orderByDesc('followers.created_at')
  568. ->paginate(10);
  569. } else {
  570. $following = $profile->following()
  571. ->orderByDesc('followers.created_at')
  572. ->paginate(10);
  573. }
  574. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  575. $res = $this->fractal->createData($resource)->toArray();
  576. return response()->json($res);
  577. }
  578. public function accountStatuses(Request $request, $id)
  579. {
  580. $this->validate($request, [
  581. 'only_media' => 'nullable',
  582. 'pinned' => 'nullable',
  583. 'exclude_replies' => 'nullable',
  584. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  585. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  586. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  587. 'limit' => 'nullable|integer|min:1|max:24'
  588. ]);
  589. $profile = Profile::whereNull('status')->findOrFail($id);
  590. $limit = $request->limit ?? 9;
  591. $max_id = $request->max_id;
  592. $min_id = $request->min_id;
  593. $scope = $request->only_media == true ?
  594. ['photo', 'photo:album', 'video', 'video:album'] :
  595. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  596. if($profile->is_private) {
  597. if(!Auth::check()) {
  598. return response()->json([]);
  599. }
  600. $pid = Auth::user()->profile->id;
  601. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  602. $following = Follower::whereProfileId($pid)->pluck('following_id');
  603. return $following->push($pid)->toArray();
  604. });
  605. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  606. } else {
  607. if(Auth::check()) {
  608. $pid = Auth::user()->profile->id;
  609. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  610. $following = Follower::whereProfileId($pid)->pluck('following_id');
  611. return $following->push($pid)->toArray();
  612. });
  613. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  614. } else {
  615. $visibility = ['public', 'unlisted'];
  616. }
  617. }
  618. $tag = in_array('private', $visibility) ? 'private' : 'public';
  619. if($min_id == 1 && $limit == 9 && $tag == 'public') {
  620. $limit = 9;
  621. $scope = ['photo', 'photo:album', 'video', 'video:album'];
  622. $key = '_api:statuses:recent_9:'.$profile->id;
  623. $res = Cache::remember($key, now()->addHours(24), function() use($profile, $scope, $visibility, $limit) {
  624. $dir = '>';
  625. $id = 1;
  626. $timeline = Status::select(
  627. 'id',
  628. 'uri',
  629. 'caption',
  630. 'rendered',
  631. 'profile_id',
  632. 'type',
  633. 'in_reply_to_id',
  634. 'reblog_of_id',
  635. 'is_nsfw',
  636. 'likes_count',
  637. 'reblogs_count',
  638. 'scope',
  639. 'visibility',
  640. 'local',
  641. 'place_id',
  642. 'comments_disabled',
  643. 'cw_summary',
  644. 'created_at',
  645. 'updated_at'
  646. )->whereProfileId($profile->id)
  647. ->whereIn('type', $scope)
  648. ->where('id', $dir, $id)
  649. ->whereIn('visibility', $visibility)
  650. ->limit($limit)
  651. ->orderByDesc('id')
  652. ->get();
  653. $resource = new Fractal\Resource\Collection($timeline, new StatusStatelessTransformer());
  654. $res = $this->fractal->createData($resource)->toArray();
  655. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  656. });
  657. return $res;
  658. }
  659. $dir = $min_id ? '>' : '<';
  660. $id = $min_id ?? $max_id;
  661. $timeline = Status::select(
  662. 'id',
  663. 'uri',
  664. 'caption',
  665. 'rendered',
  666. 'profile_id',
  667. 'type',
  668. 'in_reply_to_id',
  669. 'reblog_of_id',
  670. 'is_nsfw',
  671. 'likes_count',
  672. 'reblogs_count',
  673. 'scope',
  674. 'visibility',
  675. 'local',
  676. 'place_id',
  677. 'comments_disabled',
  678. 'cw_summary',
  679. 'created_at',
  680. 'updated_at'
  681. )->whereProfileId($profile->id)
  682. ->whereIn('type', $scope)
  683. ->where('id', $dir, $id)
  684. ->whereIn('visibility', $visibility)
  685. ->limit($limit)
  686. ->orderByDesc('id')
  687. ->get();
  688. $resource = new Fractal\Resource\Collection($timeline, new StatusStatelessTransformer());
  689. $res = $this->fractal->createData($resource)->toArray();
  690. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  691. }
  692. }