PublicApiController.php 29 KB

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