PublicApiController.php 33 KB

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