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