PublicApiController.php 29 KB

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