PublicApiController.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. ->whereScope('public')
  299. ->orderBy('id', 'desc')
  300. ->limit($limit)
  301. ->get()
  302. ->map(function($s) use ($user) {
  303. $status = StatusService::getFull($s->id, $user->profile_id);
  304. if(!$status) {
  305. return false;
  306. }
  307. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  308. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  309. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  310. return $status;
  311. })
  312. ->filter(function($s) use($filtered) {
  313. return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
  314. })
  315. ->values();
  316. $res = $timeline->toArray();
  317. } else {
  318. $timeline = Status::select(
  319. 'id',
  320. 'uri',
  321. 'caption',
  322. 'rendered',
  323. 'profile_id',
  324. 'type',
  325. 'in_reply_to_id',
  326. 'reblog_of_id',
  327. 'is_nsfw',
  328. 'scope',
  329. 'local',
  330. 'reply_count',
  331. 'comments_disabled',
  332. 'created_at',
  333. 'place_id',
  334. 'likes_count',
  335. 'reblogs_count',
  336. 'updated_at'
  337. )
  338. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  339. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  340. ->with('profile', 'hashtags', 'mentions')
  341. ->whereLocal(true)
  342. ->whereScope('public')
  343. ->orderBy('id', 'desc')
  344. ->limit($limit)
  345. ->get()
  346. ->map(function($s) use ($user) {
  347. $status = StatusService::getFull($s->id, $user->profile_id);
  348. if(!$status) {
  349. return false;
  350. }
  351. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  352. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  353. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  354. return $status;
  355. })
  356. ->filter(function($s) use($filtered) {
  357. return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
  358. })
  359. ->values();
  360. $res = $timeline->toArray();
  361. }
  362. } else {
  363. Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
  364. if(PublicTimelineService::count() == 0) {
  365. PublicTimelineService::warmCache(true, 400);
  366. }
  367. });
  368. if ($max) {
  369. $feed = PublicTimelineService::getRankedMaxId($max, $limit);
  370. } else if ($min) {
  371. $feed = PublicTimelineService::getRankedMinId($min, $limit);
  372. } else {
  373. $feed = PublicTimelineService::get(0, $limit);
  374. }
  375. $res = collect($feed)
  376. ->take($limit)
  377. ->map(function($k) use($user) {
  378. $status = StatusService::get($k);
  379. if($status && isset($status['account']) && $user) {
  380. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
  381. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
  382. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
  383. $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
  384. }
  385. return $status;
  386. })
  387. ->filter(function($s) use($filtered) {
  388. return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
  389. })
  390. ->values()
  391. ->toArray();
  392. }
  393. return response()->json($res);
  394. }
  395. public function homeTimelineApi(Request $request)
  396. {
  397. if(!$request->user()) {
  398. return response('', 403);
  399. }
  400. $this->validate($request,[
  401. 'page' => 'nullable|integer|max:40',
  402. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  403. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  404. 'limit' => 'nullable|integer|max:40',
  405. 'recent_feed' => 'nullable',
  406. 'recent_min' => 'nullable|integer'
  407. ]);
  408. $recentFeed = $request->input('recent_feed') == 'true';
  409. $recentFeedMin = $request->input('recent_min');
  410. $page = $request->input('page');
  411. $min = $request->input('min_id');
  412. $max = $request->input('max_id');
  413. $limit = $request->input('limit') ?? 3;
  414. $user = $request->user();
  415. $key = 'user:last_active_at:id:'.$user->id;
  416. $ttl = now()->addMinutes(20);
  417. Cache::remember($key, $ttl, function() use($user) {
  418. $user->last_active_at = now();
  419. $user->save();
  420. return;
  421. });
  422. $pid = $user->profile_id;
  423. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  424. $following = Follower::whereProfileId($pid)->pluck('following_id');
  425. return $following->push($pid)->toArray();
  426. });
  427. if($recentFeed == true) {
  428. $key = 'profile:home-timeline-cursor:'.$user->id;
  429. $ttl = now()->addMinutes(30);
  430. $min = Cache::remember($key, $ttl, function() use($pid) {
  431. $res = StatusView::whereProfileId($pid)->orderByDesc('status_id')->first();
  432. return $res ? $res->status_id : null;
  433. });
  434. }
  435. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  436. $types = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'];
  437. // $types = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album', 'text'];
  438. $textOnlyReplies = false;
  439. if(config('exp.top')) {
  440. $textOnlyPosts = (bool) Redis::zscore('pf:tl:top', $pid);
  441. $textOnlyReplies = (bool) Redis::zscore('pf:tl:replies', $pid);
  442. if($textOnlyPosts) {
  443. array_push($types, 'text');
  444. }
  445. }
  446. if(config('exp.polls') == true) {
  447. array_push($types, 'poll');
  448. }
  449. if($min || $max) {
  450. $dir = $min ? '>' : '<';
  451. $id = $min ?? $max;
  452. return Status::select(
  453. 'id',
  454. 'uri',
  455. 'caption',
  456. 'rendered',
  457. 'profile_id',
  458. 'type',
  459. 'in_reply_to_id',
  460. 'reblog_of_id',
  461. 'is_nsfw',
  462. 'scope',
  463. 'local',
  464. 'reply_count',
  465. 'comments_disabled',
  466. 'place_id',
  467. 'likes_count',
  468. 'reblogs_count',
  469. 'created_at',
  470. 'updated_at'
  471. )
  472. ->whereIn('type', $types)
  473. ->when($textOnlyReplies != true, function($q, $textOnlyReplies) {
  474. return $q->whereNull('in_reply_to_id');
  475. })
  476. ->with('profile', 'hashtags', 'mentions')
  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. ->with('profile', 'hashtags', 'mentions')
  524. ->whereIn('profile_id', $following)
  525. ->whereIn('visibility',['public', 'unlisted', 'private'])
  526. ->orderBy('created_at', 'desc')
  527. ->limit($limit)
  528. ->get()
  529. ->map(function($s) use ($user) {
  530. $status = StatusService::get($s->id, false);
  531. if(!$status) {
  532. return false;
  533. }
  534. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  535. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  536. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  537. return $status;
  538. })
  539. ->filter(function($s) use($filtered) {
  540. return $s && in_array($s['account']['id'], $filtered) == false;
  541. })
  542. ->values()
  543. ->toArray();
  544. }
  545. }
  546. public function networkTimelineApi(Request $request)
  547. {
  548. if(!$request->user()) {
  549. return response('', 403);
  550. }
  551. abort_if(config('federation.network_timeline') == false, 404);
  552. $this->validate($request,[
  553. 'page' => 'nullable|integer|max:40',
  554. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  555. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  556. 'limit' => 'nullable|integer|max:30'
  557. ]);
  558. $page = $request->input('page');
  559. $min = $request->input('min_id');
  560. $max = $request->input('max_id');
  561. $limit = $request->input('limit') ?? 3;
  562. $user = $request->user();
  563. $amin = SnowflakeService::byDate(now()->subDays(config('federation.network_timeline_days_falloff')));
  564. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  565. if(config('instance.timeline.network.cached') == false) {
  566. if($min || $max) {
  567. $dir = $min ? '>' : '<';
  568. $id = $min ?? $max;
  569. $timeline = Status::select(
  570. 'id',
  571. 'uri',
  572. 'type',
  573. 'scope',
  574. 'created_at',
  575. )
  576. ->where('id', $dir, $id)
  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. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  605. ->whereNotNull('uri')
  606. ->whereScope('public')
  607. ->where('id', '>', $amin)
  608. ->orderBy('created_at', 'desc')
  609. ->limit($limit)
  610. ->get()
  611. ->map(function($s) use ($user) {
  612. $status = StatusService::get($s->id);
  613. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  614. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  615. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  616. return $status;
  617. });
  618. $res = $timeline->toArray();
  619. }
  620. } else {
  621. Cache::remember('api:v1:timelines:network:cache_check', 10368000, function() {
  622. if(NetworkTimelineService::count() == 0) {
  623. NetworkTimelineService::warmCache(true, 400);
  624. }
  625. });
  626. if ($max) {
  627. $feed = NetworkTimelineService::getRankedMaxId($max, $limit);
  628. } else if ($min) {
  629. $feed = NetworkTimelineService::getRankedMinId($min, $limit);
  630. } else {
  631. $feed = NetworkTimelineService::get(0, $limit);
  632. }
  633. $res = collect($feed)
  634. ->take($limit)
  635. ->map(function($k) use($user) {
  636. $status = StatusService::get($k);
  637. if($status && isset($status['account']) && $user) {
  638. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
  639. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
  640. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
  641. $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
  642. }
  643. return $status;
  644. })
  645. ->filter(function($s) use($filtered) {
  646. return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
  647. })
  648. ->values()
  649. ->toArray();
  650. }
  651. return response()->json($res);
  652. }
  653. public function relationships(Request $request)
  654. {
  655. if(!Auth::check()) {
  656. return response()->json([]);
  657. }
  658. $pid = $request->user()->profile_id;
  659. $this->validate($request, [
  660. 'id' => 'required|array|min:1|max:20',
  661. 'id.*' => 'required|integer'
  662. ]);
  663. $ids = collect($request->input('id'));
  664. $res = $ids->filter(function($v) use($pid) {
  665. return $v != $pid;
  666. })
  667. ->map(function($id) use($pid) {
  668. return RelationshipService::get($pid, $id);
  669. });
  670. return response()->json($res);
  671. }
  672. public function account(Request $request, $id)
  673. {
  674. $res = AccountService::get($id);
  675. return response()->json($res);
  676. }
  677. public function accountFollowers(Request $request, $id)
  678. {
  679. abort_if(!$request->user(), 403);
  680. $account = AccountService::get($id);
  681. abort_if(!$account, 404);
  682. $pid = $request->user()->profile_id;
  683. if($pid != $account['id']) {
  684. if($account['locked']) {
  685. if(!FollowerService::follows($pid, $account['id'])) {
  686. return [];
  687. }
  688. }
  689. if(AccountService::hiddenFollowers($id)) {
  690. return [];
  691. }
  692. if($request->has('page') && $request->page >= 5) {
  693. return [];
  694. }
  695. }
  696. $res = DB::table('followers')
  697. ->select('id', 'profile_id', 'following_id')
  698. ->whereFollowingId($account['id'])
  699. ->orderByDesc('id')
  700. ->simplePaginate(10)
  701. ->map(function($follower) {
  702. return AccountService::get($follower->profile_id);
  703. })
  704. ->filter(function($account) {
  705. return $account && isset($account['id']);
  706. })
  707. ->values()
  708. ->toArray();
  709. return response()->json($res);
  710. }
  711. public function accountFollowing(Request $request, $id)
  712. {
  713. abort_if(!$request->user(), 403);
  714. $account = AccountService::get($id);
  715. abort_if(!$account, 404);
  716. $pid = $request->user()->profile_id;
  717. if($pid != $account['id']) {
  718. if($account['locked']) {
  719. if(!FollowerService::follows($pid, $account['id'])) {
  720. return [];
  721. }
  722. }
  723. if(AccountService::hiddenFollowing($id)) {
  724. return [];
  725. }
  726. if($request->has('page') && $request->page >= 5) {
  727. return [];
  728. }
  729. }
  730. $res = DB::table('followers')
  731. ->select('id', 'profile_id', 'following_id')
  732. ->whereProfileId($account['id'])
  733. ->orderByDesc('id')
  734. ->simplePaginate(10)
  735. ->map(function($follower) {
  736. return AccountService::get($follower->following_id);
  737. })
  738. ->filter(function($account) {
  739. return $account && isset($account['id']);
  740. })
  741. ->values()
  742. ->toArray();
  743. return response()->json($res);
  744. }
  745. public function accountStatuses(Request $request, $id)
  746. {
  747. $this->validate($request, [
  748. 'only_media' => 'nullable',
  749. 'pinned' => 'nullable',
  750. 'exclude_replies' => 'nullable',
  751. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  752. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  753. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  754. 'limit' => 'nullable|integer|min:1|max:24'
  755. ]);
  756. $user = $request->user();
  757. $profile = AccountService::get($id);
  758. abort_if(!$profile, 404);
  759. $limit = $request->limit ?? 9;
  760. $max_id = $request->max_id;
  761. $min_id = $request->min_id;
  762. $scope = ['photo', 'photo:album', 'video', 'video:album'];
  763. $onlyMedia = $request->input('only_media', true);
  764. if(!$min_id && !$max_id) {
  765. $min_id = 1;
  766. }
  767. if($profile['locked']) {
  768. if(!$user) {
  769. return response()->json([]);
  770. }
  771. $pid = $user->profile_id;
  772. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  773. $following = Follower::whereProfileId($pid)->pluck('following_id');
  774. return $following->push($pid)->toArray();
  775. });
  776. $visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : [];
  777. } else {
  778. if($user) {
  779. $pid = $user->profile_id;
  780. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  781. $following = Follower::whereProfileId($pid)->pluck('following_id');
  782. return $following->push($pid)->toArray();
  783. });
  784. $visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  785. } else {
  786. $visibility = ['public', 'unlisted'];
  787. }
  788. }
  789. $dir = $min_id ? '>' : '<';
  790. $id = $min_id ?? $max_id;
  791. $res = Status::whereProfileId($profile['id'])
  792. ->whereNull('in_reply_to_id')
  793. ->whereNull('reblog_of_id')
  794. ->whereIn('type', $scope)
  795. ->where('id', $dir, $id)
  796. ->whereIn('scope', $visibility)
  797. ->limit($limit)
  798. ->orderByDesc('id')
  799. ->get()
  800. ->map(function($s) use($user) {
  801. try {
  802. $status = StatusService::get($s->id, false);
  803. } catch (\Exception $e) {
  804. $status = false;
  805. }
  806. if($user && $status) {
  807. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  808. }
  809. return $status;
  810. })
  811. ->filter(function($s) use($onlyMedia) {
  812. if($onlyMedia) {
  813. if(
  814. !isset($s['media_attachments']) ||
  815. !is_array($s['media_attachments']) ||
  816. empty($s['media_attachments'])
  817. ) {
  818. return false;
  819. }
  820. }
  821. return $s;
  822. })
  823. ->values();
  824. return response()->json($res);
  825. }
  826. }