PublicApiController.php 33 KB

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