PublicApiController.php 33 KB

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