PublicApiController.php 29 KB

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