PublicApiController.php 28 KB

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