1
0

PublicApiController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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, $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. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  272. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  273. ->whereLocal(true)
  274. ->whereScope('public')
  275. ->orderBy('id', 'desc')
  276. ->limit($limit)
  277. ->get()
  278. ->map(function($s) use ($user) {
  279. $status = StatusService::getFull($s->id, $user->profile_id);
  280. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  281. return $status;
  282. })
  283. ->filter(function($s) use($filtered) {
  284. return $s && in_array($s['account']['id'], $filtered) == false;
  285. })
  286. ->values();
  287. $res = $timeline->toArray();
  288. } else {
  289. $timeline = Status::select(
  290. 'id',
  291. 'uri',
  292. 'caption',
  293. 'rendered',
  294. 'profile_id',
  295. 'type',
  296. 'in_reply_to_id',
  297. 'reblog_of_id',
  298. 'is_nsfw',
  299. 'scope',
  300. 'local',
  301. 'reply_count',
  302. 'comments_disabled',
  303. 'created_at',
  304. 'place_id',
  305. 'likes_count',
  306. 'reblogs_count',
  307. 'updated_at'
  308. )
  309. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  310. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  311. ->with('profile', 'hashtags', 'mentions')
  312. ->whereLocal(true)
  313. ->whereScope('public')
  314. ->orderBy('id', 'desc')
  315. ->limit($limit)
  316. ->get()
  317. ->map(function($s) use ($user) {
  318. $status = StatusService::getFull($s->id, $user->profile_id);
  319. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  320. return $status;
  321. })
  322. ->filter(function($s) use($filtered) {
  323. return $s && in_array($s['account']['id'], $filtered) == false;
  324. })
  325. ->values();
  326. $res = $timeline->toArray();
  327. }
  328. } else {
  329. Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
  330. if(PublicTimelineService::count() == 0) {
  331. PublicTimelineService::warmCache(true, 400);
  332. }
  333. });
  334. if ($max) {
  335. $feed = PublicTimelineService::getRankedMaxId($max, $limit);
  336. } else if ($min) {
  337. $feed = PublicTimelineService::getRankedMinId($min, $limit);
  338. } else {
  339. $feed = PublicTimelineService::get(0, $limit);
  340. }
  341. $res = collect($feed)
  342. ->map(function($k) use($user) {
  343. $status = StatusService::get($k);
  344. if($user) {
  345. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
  346. $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
  347. }
  348. return $status;
  349. })
  350. ->filter(function($s) use($filtered) {
  351. return in_array($s['account']['id'], $filtered) == false;
  352. })
  353. ->values()
  354. ->toArray();
  355. }
  356. return response()->json($res);
  357. }
  358. public function homeTimelineApi(Request $request)
  359. {
  360. if(!Auth::check()) {
  361. return abort(403);
  362. }
  363. $this->validate($request,[
  364. 'page' => 'nullable|integer|max:40',
  365. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  366. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  367. 'limit' => 'nullable|integer|max:40',
  368. 'recent_feed' => 'nullable',
  369. 'recent_min' => 'nullable|integer'
  370. ]);
  371. $recentFeed = $request->input('recent_feed') == 'true';
  372. $recentFeedMin = $request->input('recent_min');
  373. $page = $request->input('page');
  374. $min = $request->input('min_id');
  375. $max = $request->input('max_id');
  376. $limit = $request->input('limit') ?? 3;
  377. $user = $request->user();
  378. $key = 'user:last_active_at:id:'.$user->id;
  379. $ttl = now()->addMinutes(20);
  380. Cache::remember($key, $ttl, function() use($user) {
  381. $user->last_active_at = now();
  382. $user->save();
  383. return;
  384. });
  385. $pid = $user->profile_id;
  386. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  387. $following = Follower::whereProfileId($pid)->pluck('following_id');
  388. return $following->push($pid)->toArray();
  389. });
  390. if($recentFeed == true) {
  391. $key = 'profile:home-timeline-cursor:'.$user->id;
  392. $ttl = now()->addMinutes(30);
  393. $min = Cache::remember($key, $ttl, function() use($pid) {
  394. $res = StatusView::whereProfileId($pid)->orderByDesc('status_id')->first();
  395. return $res ? $res->status_id : null;
  396. });
  397. }
  398. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  399. $types = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'];
  400. // $types = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album', 'text'];
  401. $textOnlyReplies = false;
  402. if(config('exp.top')) {
  403. $textOnlyPosts = (bool) Redis::zscore('pf:tl:top', $pid);
  404. $textOnlyReplies = (bool) Redis::zscore('pf:tl:replies', $pid);
  405. if($textOnlyPosts) {
  406. array_push($types, 'text');
  407. }
  408. }
  409. if(config('exp.polls') == true) {
  410. array_push($types, 'poll');
  411. }
  412. if($min || $max) {
  413. $dir = $min ? '>' : '<';
  414. $id = $min ?? $max;
  415. $timeline = Status::select(
  416. 'id',
  417. 'uri',
  418. 'caption',
  419. 'rendered',
  420. 'profile_id',
  421. 'type',
  422. 'in_reply_to_id',
  423. 'reblog_of_id',
  424. 'is_nsfw',
  425. 'scope',
  426. 'local',
  427. 'reply_count',
  428. 'comments_disabled',
  429. 'place_id',
  430. 'likes_count',
  431. 'reblogs_count',
  432. 'created_at',
  433. 'updated_at'
  434. )
  435. ->whereIn('type', $types)
  436. ->when($textOnlyReplies != true, function($q, $textOnlyReplies) {
  437. return $q->whereNull('in_reply_to_id');
  438. })
  439. ->with('profile', 'hashtags', 'mentions')
  440. ->where('id', $dir, $id)
  441. ->whereIn('profile_id', $following)
  442. ->whereNotIn('profile_id', $filtered)
  443. ->whereIn('visibility',['public', 'unlisted', 'private'])
  444. ->orderBy('created_at', 'desc')
  445. ->limit($limit)
  446. ->get();
  447. } else {
  448. $timeline = Status::select(
  449. 'id',
  450. 'uri',
  451. 'caption',
  452. 'rendered',
  453. 'profile_id',
  454. 'type',
  455. 'in_reply_to_id',
  456. 'reblog_of_id',
  457. 'is_nsfw',
  458. 'scope',
  459. 'local',
  460. 'reply_count',
  461. 'comments_disabled',
  462. 'place_id',
  463. 'likes_count',
  464. 'reblogs_count',
  465. 'created_at',
  466. 'updated_at'
  467. )
  468. ->whereIn('type', $types)
  469. ->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
  470. return $q->whereNull('in_reply_to_id');
  471. })
  472. ->with('profile', 'hashtags', 'mentions')
  473. ->whereIn('profile_id', $following)
  474. ->whereNotIn('profile_id', $filtered)
  475. ->whereIn('visibility',['public', 'unlisted', 'private'])
  476. ->orderBy('created_at', 'desc')
  477. ->simplePaginate($limit);
  478. }
  479. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  480. $res = $this->fractal->createData($fractal)->toArray();
  481. return response()->json($res);
  482. }
  483. public function networkTimelineApi(Request $request)
  484. {
  485. abort_if(!Auth::check(), 403);
  486. abort_if(config('federation.network_timeline') == false, 404);
  487. $this->validate($request,[
  488. 'page' => 'nullable|integer|max:40',
  489. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  490. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  491. 'limit' => 'nullable|integer|max:30'
  492. ]);
  493. $page = $request->input('page');
  494. $min = $request->input('min_id');
  495. $max = $request->input('max_id');
  496. $limit = $request->input('limit') ?? 3;
  497. $user = $request->user();
  498. $amin = SnowflakeService::byDate(now()->subDays(490));
  499. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  500. if($min || $max) {
  501. $dir = $min ? '>' : '<';
  502. $id = $min ?? $max;
  503. $timeline = Status::select(
  504. 'id',
  505. 'uri',
  506. 'type',
  507. 'scope',
  508. 'created_at',
  509. )
  510. ->where('id', $dir, $id)
  511. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  512. ->whereNotIn('profile_id', $filtered)
  513. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  514. ->whereNotNull('uri')
  515. ->whereScope('public')
  516. ->where('id', '>', $amin)
  517. ->orderBy('created_at', 'desc')
  518. ->limit($limit)
  519. ->get()
  520. ->map(function($s) use ($user) {
  521. $status = StatusService::get($s->id);
  522. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  523. return $status;
  524. });
  525. $res = $timeline->toArray();
  526. } else {
  527. $timeline = Status::select(
  528. 'id',
  529. 'uri',
  530. 'type',
  531. 'scope',
  532. 'created_at',
  533. )
  534. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  535. ->whereNotIn('profile_id', $filtered)
  536. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  537. ->whereNotNull('uri')
  538. ->whereScope('public')
  539. ->where('id', '>', $amin)
  540. ->orderBy('created_at', 'desc')
  541. ->limit($limit)
  542. ->get()
  543. ->map(function($s) use ($user) {
  544. $status = StatusService::get($s->id);
  545. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  546. return $status;
  547. });
  548. $res = $timeline->toArray();
  549. }
  550. return response()->json($res);
  551. }
  552. public function relationships(Request $request)
  553. {
  554. if(!Auth::check()) {
  555. return response()->json([]);
  556. }
  557. $pid = $request->user()->profile_id;
  558. $this->validate($request, [
  559. 'id' => 'required|array|min:1|max:20',
  560. 'id.*' => 'required|integer'
  561. ]);
  562. $ids = collect($request->input('id'));
  563. $res = $ids->filter(function($v) use($pid) {
  564. return $v != $pid;
  565. })
  566. ->map(function($id) use($pid) {
  567. return RelationshipService::get($pid, $id);
  568. });
  569. return response()->json($res);
  570. }
  571. public function account(Request $request, $id)
  572. {
  573. $res = AccountService::get($id);
  574. return response()->json($res);
  575. }
  576. public function accountFollowers(Request $request, $id)
  577. {
  578. abort_unless(Auth::check(), 403);
  579. $profile = Profile::with('user')->whereNull('status')->findOrFail($id);
  580. $owner = Auth::id() == $profile->user_id;
  581. if(Auth::id() != $profile->user_id && $profile->is_private) {
  582. return response()->json([]);
  583. }
  584. if(!$profile->domain && !$profile->user->settings->show_profile_followers) {
  585. return response()->json([]);
  586. }
  587. if(!$owner && $request->page > 5) {
  588. return [];
  589. }
  590. $res = Follower::select('id', 'profile_id', 'following_id')
  591. ->whereFollowingId($profile->id)
  592. ->orderByDesc('id')
  593. ->simplePaginate(10)
  594. ->map(function($follower) {
  595. return ProfileService::get($follower['profile_id']);
  596. })
  597. ->toArray();
  598. return response()->json($res);
  599. }
  600. public function accountFollowing(Request $request, $id)
  601. {
  602. abort_unless(Auth::check(), 403);
  603. $profile = Profile::with('user')
  604. ->whereNull('status')
  605. ->findOrFail($id);
  606. // filter by username
  607. $search = $request->input('fbu');
  608. $owner = Auth::id() == $profile->user_id;
  609. $filter = ($owner == true) && ($search != null);
  610. abort_if($owner == false && $profile->is_private == true && !$profile->followedBy(Auth::user()->profile), 404);
  611. if(!$profile->domain) {
  612. abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
  613. }
  614. if(!$owner && $request->page > 5) {
  615. return [];
  616. }
  617. if($search) {
  618. abort_if(!$owner, 404);
  619. $following = $profile->following()
  620. ->where('profiles.username', 'like', '%'.$search.'%')
  621. ->orderByDesc('followers.created_at')
  622. ->paginate(10);
  623. } else {
  624. $following = $profile->following()
  625. ->orderByDesc('followers.created_at')
  626. ->paginate(10);
  627. }
  628. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  629. $res = $this->fractal->createData($resource)->toArray();
  630. return response()->json($res);
  631. }
  632. public function accountStatuses(Request $request, $id)
  633. {
  634. $this->validate($request, [
  635. 'only_media' => 'nullable',
  636. 'pinned' => 'nullable',
  637. 'exclude_replies' => 'nullable',
  638. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  639. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  640. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  641. 'limit' => 'nullable|integer|min:1|max:24'
  642. ]);
  643. $user = $request->user();
  644. $profile = AccountService::get($id);
  645. abort_if(!$profile, 404);
  646. $limit = $request->limit ?? 9;
  647. $max_id = $request->max_id;
  648. $min_id = $request->min_id;
  649. $scope = ['photo', 'photo:album', 'video', 'video:album'];
  650. if($profile['locked']) {
  651. if(!$user) {
  652. return response()->json([]);
  653. }
  654. $pid = $user->profile_id;
  655. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  656. $following = Follower::whereProfileId($pid)->pluck('following_id');
  657. return $following->push($pid)->toArray();
  658. });
  659. $visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : [];
  660. } else {
  661. if($user) {
  662. $pid = $user->profile_id;
  663. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  664. $following = Follower::whereProfileId($pid)->pluck('following_id');
  665. return $following->push($pid)->toArray();
  666. });
  667. $visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  668. } else {
  669. $visibility = ['public', 'unlisted'];
  670. }
  671. }
  672. $dir = $min_id ? '>' : '<';
  673. $id = $min_id ?? $max_id;
  674. $res = Status::whereProfileId($profile['id'])
  675. ->whereNull('in_reply_to_id')
  676. ->whereNull('reblog_of_id')
  677. ->whereIn('type', $scope)
  678. ->where('id', $dir, $id)
  679. ->whereIn('scope', $visibility)
  680. ->limit($limit)
  681. ->orderByDesc('id')
  682. ->get()
  683. ->map(function($s) use($user) {
  684. try {
  685. $status = StatusService::get($s->id, false);
  686. } catch (\Exception $e) {
  687. $status = false;
  688. }
  689. if($user && $status) {
  690. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  691. }
  692. return $status;
  693. })
  694. ->filter(function($s) {
  695. return $s;
  696. })
  697. ->values();
  698. return response()->json($res);
  699. }
  700. }