1
0

PublicApiController.php 34 KB

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