PublicApiController.php 29 KB

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