PublicApiController.php 30 KB

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