PublicApiController.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. if(Cache::get($key) == null) {
  423. $user->last_active_at = now();
  424. $user->save();
  425. Cache::put($key, true, 43200);
  426. }
  427. $pid = $user->profile_id;
  428. $following = Cache::remember('profile:following:'.$pid, 1209600, function() use($pid) {
  429. $following = Follower::whereProfileId($pid)->pluck('following_id');
  430. return $following->push($pid)->toArray();
  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(config('instance.timeline.home.cached') && $limit == 6 && (!$min && !$max)) {
  447. $ttl = config('instance.timeline.home.cache_ttl');
  448. $res = Cache::remember(
  449. 'pf:timelines:home:' . $pid,
  450. $ttl,
  451. function() use(
  452. $types,
  453. $textOnlyReplies,
  454. $following,
  455. $limit,
  456. $filtered,
  457. $user
  458. ) {
  459. return Status::select(
  460. 'id',
  461. 'uri',
  462. 'caption',
  463. 'rendered',
  464. 'profile_id',
  465. 'type',
  466. 'in_reply_to_id',
  467. 'reblog_of_id',
  468. 'is_nsfw',
  469. 'scope',
  470. 'local',
  471. 'reply_count',
  472. 'comments_disabled',
  473. 'place_id',
  474. 'likes_count',
  475. 'reblogs_count',
  476. 'created_at',
  477. 'updated_at'
  478. )
  479. ->whereIn('type', $types)
  480. ->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
  481. return $q->whereNull('in_reply_to_id');
  482. })
  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. return $status;
  494. })
  495. ->filter(function($s) use($filtered) {
  496. return $s && in_array($s['account']['id'], $filtered) == false;
  497. })
  498. ->values()
  499. ->toArray();
  500. });
  501. $res = collect($res)
  502. ->map(function($s) use ($user) {
  503. $status = StatusService::get($s['id'], false);
  504. if(!$status) {
  505. return false;
  506. }
  507. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s['id']);
  508. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s['id']);
  509. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s['id']);
  510. return $status;
  511. })
  512. ->filter(function($s) use($filtered) {
  513. return $s && in_array($s['account']['id'], $filtered) == false;
  514. })
  515. ->values()
  516. ->take($limit)
  517. ->toArray();
  518. return $res;
  519. }
  520. if($min || $max) {
  521. $dir = $min ? '>' : '<';
  522. $id = $min ?? $max;
  523. return Status::select(
  524. 'id',
  525. 'uri',
  526. 'caption',
  527. 'rendered',
  528. 'profile_id',
  529. 'type',
  530. 'in_reply_to_id',
  531. 'reblog_of_id',
  532. 'is_nsfw',
  533. 'scope',
  534. 'local',
  535. 'reply_count',
  536. 'comments_disabled',
  537. 'place_id',
  538. 'likes_count',
  539. 'reblogs_count',
  540. 'created_at',
  541. 'updated_at'
  542. )
  543. ->whereIn('type', $types)
  544. ->when($textOnlyReplies != true, function($q, $textOnlyReplies) {
  545. return $q->whereNull('in_reply_to_id');
  546. })
  547. ->where('id', $dir, $id)
  548. ->whereIn('profile_id', $following)
  549. ->whereIn('visibility',['public', 'unlisted', 'private'])
  550. ->orderBy('created_at', 'desc')
  551. ->limit($limit)
  552. ->get()
  553. ->map(function($s) use ($user) {
  554. $status = StatusService::get($s->id, false);
  555. if(!$status) {
  556. return false;
  557. }
  558. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  559. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  560. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  561. return $status;
  562. })
  563. ->filter(function($s) use($filtered) {
  564. return $s && in_array($s['account']['id'], $filtered) == false;
  565. })
  566. ->values()
  567. ->toArray();
  568. } else {
  569. return Status::select(
  570. 'id',
  571. 'uri',
  572. 'caption',
  573. 'rendered',
  574. 'profile_id',
  575. 'type',
  576. 'in_reply_to_id',
  577. 'reblog_of_id',
  578. 'is_nsfw',
  579. 'scope',
  580. 'local',
  581. 'reply_count',
  582. 'comments_disabled',
  583. 'place_id',
  584. 'likes_count',
  585. 'reblogs_count',
  586. 'created_at',
  587. 'updated_at'
  588. )
  589. ->whereIn('type', $types)
  590. ->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
  591. return $q->whereNull('in_reply_to_id');
  592. })
  593. ->whereIn('profile_id', $following)
  594. ->whereIn('visibility',['public', 'unlisted', 'private'])
  595. ->orderBy('created_at', 'desc')
  596. ->limit($limit)
  597. ->get()
  598. ->map(function($s) use ($user) {
  599. $status = StatusService::get($s->id, false);
  600. if(!$status) {
  601. return false;
  602. }
  603. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  604. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  605. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  606. return $status;
  607. })
  608. ->filter(function($s) use($filtered) {
  609. return $s && in_array($s['account']['id'], $filtered) == false;
  610. })
  611. ->values()
  612. ->toArray();
  613. }
  614. }
  615. public function networkTimelineApi(Request $request)
  616. {
  617. if(!$request->user()) {
  618. return response('', 403);
  619. }
  620. abort_if(config('federation.network_timeline') == false, 404);
  621. $this->validate($request,[
  622. 'page' => 'nullable|integer|max:40',
  623. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  624. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  625. 'limit' => 'nullable|integer|max:30'
  626. ]);
  627. $page = $request->input('page');
  628. $min = $request->input('min_id');
  629. $max = $request->input('max_id');
  630. $limit = $request->input('limit') ?? 3;
  631. $user = $request->user();
  632. $amin = SnowflakeService::byDate(now()->subDays(config('federation.network_timeline_days_falloff')));
  633. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  634. $hideNsfw = config('instance.hide_nsfw_on_public_feeds');
  635. if(config('instance.timeline.network.cached') == false) {
  636. if($min || $max) {
  637. $dir = $min ? '>' : '<';
  638. $id = $min ?? $max;
  639. $timeline = Status::select(
  640. 'id',
  641. 'uri',
  642. 'type',
  643. 'scope',
  644. 'created_at',
  645. )
  646. ->where('id', $dir, $id)
  647. ->when($hideNsfw, function($q, $hideNsfw) {
  648. return $q->where('is_nsfw', false);
  649. })
  650. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  651. ->whereNotIn('profile_id', $filtered)
  652. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  653. ->whereNotNull('uri')
  654. ->whereScope('public')
  655. ->where('id', '>', $amin)
  656. ->orderBy('created_at', 'desc')
  657. ->limit($limit)
  658. ->get()
  659. ->map(function($s) use ($user) {
  660. $status = StatusService::get($s->id);
  661. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  662. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  663. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  664. return $status;
  665. });
  666. $res = $timeline->toArray();
  667. } else {
  668. $timeline = Status::select(
  669. 'id',
  670. 'uri',
  671. 'type',
  672. 'scope',
  673. 'created_at',
  674. )
  675. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  676. ->whereNotIn('profile_id', $filtered)
  677. ->when($hideNsfw, function($q, $hideNsfw) {
  678. return $q->where('is_nsfw', false);
  679. })
  680. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  681. ->whereNotNull('uri')
  682. ->whereScope('public')
  683. ->where('id', '>', $amin)
  684. ->orderBy('created_at', 'desc')
  685. ->limit($limit)
  686. ->get()
  687. ->map(function($s) use ($user) {
  688. $status = StatusService::get($s->id);
  689. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  690. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
  691. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
  692. return $status;
  693. });
  694. $res = $timeline->toArray();
  695. }
  696. } else {
  697. Cache::remember('api:v1:timelines:network:cache_check', 10368000, function() {
  698. if(NetworkTimelineService::count() == 0) {
  699. NetworkTimelineService::warmCache(true, 400);
  700. }
  701. });
  702. if ($max) {
  703. $feed = NetworkTimelineService::getRankedMaxId($max, $limit);
  704. } else if ($min) {
  705. $feed = NetworkTimelineService::getRankedMinId($min, $limit);
  706. } else {
  707. $feed = NetworkTimelineService::get(0, $limit);
  708. }
  709. $res = collect($feed)
  710. ->take($limit)
  711. ->map(function($k) use($user) {
  712. $status = StatusService::get($k);
  713. if($status && isset($status['account']) && $user) {
  714. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
  715. $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
  716. $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
  717. $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
  718. }
  719. return $status;
  720. })
  721. ->filter(function($s) use($filtered) {
  722. return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
  723. })
  724. ->values()
  725. ->toArray();
  726. }
  727. return response()->json($res);
  728. }
  729. public function relationships(Request $request)
  730. {
  731. if(!Auth::check()) {
  732. return response()->json([]);
  733. }
  734. $pid = $request->user()->profile_id;
  735. $this->validate($request, [
  736. 'id' => 'required|array|min:1|max:20',
  737. 'id.*' => 'required|integer'
  738. ]);
  739. $ids = collect($request->input('id'));
  740. $res = $ids->filter(function($v) use($pid) {
  741. return $v != $pid;
  742. })
  743. ->map(function($id) use($pid) {
  744. return RelationshipService::get($pid, $id);
  745. });
  746. return response()->json($res);
  747. }
  748. public function account(Request $request, $id)
  749. {
  750. $res = AccountService::get($id);
  751. return response()->json($res);
  752. }
  753. public function accountFollowers(Request $request, $id)
  754. {
  755. abort_if(!$request->user(), 403);
  756. $account = AccountService::get($id, true);
  757. abort_if(!$account, 404);
  758. $pid = $request->user()->profile_id;
  759. if($pid != $account['id']) {
  760. if($account['locked']) {
  761. if(!FollowerService::follows($pid, $account['id'])) {
  762. return [];
  763. }
  764. }
  765. if(AccountService::hiddenFollowers($id)) {
  766. return [];
  767. }
  768. if($request->has('page') && $request->page >= 10) {
  769. return [];
  770. }
  771. }
  772. $res = collect(FollowerService::followersPaginate($account['id'], $request->input('page', 1)))
  773. ->map(fn($id) => AccountService::get($id, true))
  774. ->filter()
  775. ->values();
  776. return response()->json($res);
  777. }
  778. public function accountFollowing(Request $request, $id)
  779. {
  780. abort_if(!$request->user(), 403);
  781. $account = AccountService::get($id, true);
  782. abort_if(!$account, 404);
  783. $pid = $request->user()->profile_id;
  784. if($pid != $account['id']) {
  785. if($account['locked']) {
  786. if(!FollowerService::follows($pid, $account['id'])) {
  787. return [];
  788. }
  789. }
  790. if(AccountService::hiddenFollowing($id)) {
  791. return [];
  792. }
  793. if($request->has('page') && $request->page >= 10) {
  794. return [];
  795. }
  796. }
  797. $res = collect(FollowerService::followingPaginate($account['id'], $request->input('page', 1)))
  798. ->map(fn($id) => AccountService::get($id, true))
  799. ->filter()
  800. ->values();
  801. return response()->json($res);
  802. }
  803. public function accountStatuses(Request $request, $id)
  804. {
  805. $this->validate($request, [
  806. 'only_media' => 'nullable',
  807. 'pinned' => 'nullable',
  808. 'exclude_replies' => 'nullable',
  809. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  810. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  811. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  812. 'limit' => 'nullable|integer|min:1|max:24'
  813. ]);
  814. $user = $request->user();
  815. $profile = AccountService::get($id);
  816. abort_if(!$profile, 404);
  817. $limit = $request->limit ?? 9;
  818. $max_id = $request->max_id;
  819. $min_id = $request->min_id;
  820. $scope = ['photo', 'photo:album', 'video', 'video:album'];
  821. $onlyMedia = $request->input('only_media', true);
  822. if(!$min_id && !$max_id) {
  823. $min_id = 1;
  824. }
  825. if($profile['locked']) {
  826. if(!$user) {
  827. return response()->json([]);
  828. }
  829. $pid = $user->profile_id;
  830. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  831. $following = Follower::whereProfileId($pid)->pluck('following_id');
  832. return $following->push($pid)->toArray();
  833. });
  834. $visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : [];
  835. } else {
  836. if($user) {
  837. $pid = $user->profile_id;
  838. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  839. $following = Follower::whereProfileId($pid)->pluck('following_id');
  840. return $following->push($pid)->toArray();
  841. });
  842. $visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  843. } else {
  844. $visibility = ['public', 'unlisted'];
  845. }
  846. }
  847. $dir = $min_id ? '>' : '<';
  848. $id = $min_id ?? $max_id;
  849. $res = Status::whereProfileId($profile['id'])
  850. ->whereNull('in_reply_to_id')
  851. ->whereNull('reblog_of_id')
  852. ->whereIn('type', $scope)
  853. ->where('id', $dir, $id)
  854. ->whereIn('scope', $visibility)
  855. ->limit($limit)
  856. ->orderByDesc('id')
  857. ->get()
  858. ->map(function($s) use($user) {
  859. try {
  860. $status = StatusService::get($s->id, false);
  861. } catch (\Exception $e) {
  862. $status = false;
  863. }
  864. if($user && $status) {
  865. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
  866. }
  867. return $status;
  868. })
  869. ->filter(function($s) use($onlyMedia) {
  870. if($onlyMedia) {
  871. if(
  872. !isset($s['media_attachments']) ||
  873. !is_array($s['media_attachments']) ||
  874. empty($s['media_attachments'])
  875. ) {
  876. return false;
  877. }
  878. }
  879. return $s;
  880. })
  881. ->values();
  882. return response()->json($res);
  883. }
  884. }