PublicApiController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. UserFilter
  14. };
  15. use Auth,Cache;
  16. use Carbon\Carbon;
  17. use League\Fractal;
  18. use App\Transformer\Api\{
  19. AccountTransformer,
  20. RelationshipTransformer,
  21. StatusTransformer,
  22. };
  23. use App\Jobs\StatusPipeline\NewStatusPipeline;
  24. use League\Fractal\Serializer\ArraySerializer;
  25. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  26. class PublicApiController extends Controller
  27. {
  28. protected $fractal;
  29. public function __construct()
  30. {
  31. $this->fractal = new Fractal\Manager();
  32. $this->fractal->setSerializer(new ArraySerializer());
  33. }
  34. protected function getUserData()
  35. {
  36. if(false == Auth::check()) {
  37. return [];
  38. } else {
  39. $profile = Auth::user()->profile;
  40. if($profile->status) {
  41. return [];
  42. }
  43. $user = new Fractal\Resource\Item($profile, new AccountTransformer());
  44. return $this->fractal->createData($user)->toArray();
  45. }
  46. }
  47. protected function getLikes($status)
  48. {
  49. if(false == Auth::check()) {
  50. return [];
  51. } else {
  52. $profile = Auth::user()->profile;
  53. if($profile->status) {
  54. return [];
  55. }
  56. $likes = $status->likedBy()->orderBy('created_at','desc')->paginate(10);
  57. $collection = new Fractal\Resource\Collection($likes, new AccountTransformer());
  58. return $this->fractal->createData($collection)->toArray();
  59. }
  60. }
  61. protected function getShares($status)
  62. {
  63. if(false == Auth::check()) {
  64. return [];
  65. } else {
  66. $profile = Auth::user()->profile;
  67. if($profile->status) {
  68. return [];
  69. }
  70. $shares = $status->sharedBy()->orderBy('created_at','desc')->paginate(10);
  71. $collection = new Fractal\Resource\Collection($shares, new AccountTransformer());
  72. return $this->fractal->createData($collection)->toArray();
  73. }
  74. }
  75. public function status(Request $request, $username, int $postid)
  76. {
  77. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  78. $status = Status::whereProfileId($profile->id)->findOrFail($postid);
  79. $this->scopeCheck($profile, $status);
  80. $item = new Fractal\Resource\Item($status, new StatusTransformer());
  81. $res = [
  82. 'status' => $this->fractal->createData($item)->toArray(),
  83. 'user' => $this->getUserData(),
  84. 'likes' => $this->getLikes($status),
  85. 'shares' => $this->getShares($status),
  86. 'reactions' => [
  87. 'liked' => $status->liked(),
  88. 'shared' => $status->shared(),
  89. 'bookmarked' => $status->bookmarked(),
  90. ],
  91. ];
  92. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  93. }
  94. public function statusComments(Request $request, $username, int $postId)
  95. {
  96. $this->validate($request, [
  97. 'min_id' => 'nullable|integer|min:1',
  98. 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  99. 'limit' => 'nullable|integer|min:5|max:50'
  100. ]);
  101. $limit = $request->limit ?? 10;
  102. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  103. $status = Status::whereProfileId($profile->id)->whereCommentsDisabled(false)->findOrFail($postId);
  104. $this->scopeCheck($profile, $status);
  105. if($request->filled('min_id') || $request->filled('max_id')) {
  106. if($request->filled('min_id')) {
  107. $replies = $status->comments()
  108. ->whereNull('reblog_of_id')
  109. ->select('id', 'caption', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  110. ->where('id', '>=', $request->min_id)
  111. ->orderBy('id', 'desc')
  112. ->paginate($limit);
  113. }
  114. if($request->filled('max_id')) {
  115. $replies = $status->comments()
  116. ->whereNull('reblog_of_id')
  117. ->select('id', 'caption', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  118. ->where('id', '<=', $request->max_id)
  119. ->orderBy('id', 'desc')
  120. ->paginate($limit);
  121. }
  122. } else {
  123. $replies = $status->comments()
  124. ->whereNull('reblog_of_id')
  125. ->select('id', 'caption', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  126. ->orderBy('id', 'desc')
  127. ->paginate($limit);
  128. }
  129. $resource = new Fractal\Resource\Collection($replies, new StatusTransformer(), 'data');
  130. $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
  131. $res = $this->fractal->createData($resource)->toArray();
  132. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  133. }
  134. public function statusLikes(Request $request, $username, $id)
  135. {
  136. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  137. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  138. $this->scopeCheck($profile, $status);
  139. $likes = $this->getLikes($status);
  140. return response()->json([
  141. 'data' => $likes
  142. ]);
  143. }
  144. public function statusShares(Request $request, $username, $id)
  145. {
  146. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  147. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  148. $this->scopeCheck($profile, $status);
  149. $shares = $this->getShares($status);
  150. return response()->json([
  151. 'data' => $shares
  152. ]);
  153. }
  154. protected function scopeCheck(Profile $profile, Status $status)
  155. {
  156. if($profile->is_private == true && Auth::check() == false) {
  157. abort(404);
  158. }
  159. switch ($status->scope) {
  160. case 'public':
  161. case 'unlisted':
  162. break;
  163. case 'private':
  164. $user = Auth::check() ? Auth::user() : false;
  165. if(!$user) {
  166. abort(403);
  167. } else {
  168. $follows = $profile->followedBy($user->profile);
  169. if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) {
  170. abort(404);
  171. }
  172. }
  173. break;
  174. case 'direct':
  175. abort(404);
  176. break;
  177. case 'draft':
  178. abort(404);
  179. break;
  180. default:
  181. abort(404);
  182. break;
  183. }
  184. }
  185. public function publicTimelineApi(Request $request)
  186. {
  187. $this->validate($request,[
  188. 'page' => 'nullable|integer|max:40',
  189. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  190. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  191. 'limit' => 'nullable|integer|max:20'
  192. ]);
  193. if(config('instance.timeline.local.is_public') == false && !Auth::check()) {
  194. abort(403, 'Authentication required.');
  195. }
  196. $page = $request->input('page');
  197. $min = $request->input('min_id');
  198. $max = $request->input('max_id');
  199. $limit = $request->input('limit') ?? 3;
  200. $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  201. return Profile::whereIsPrivate(true)
  202. ->orWhere('unlisted', true)
  203. ->orWhere('status', '!=', null)
  204. ->pluck('id');
  205. });
  206. if(Auth::check()) {
  207. $pid = Auth::user()->profile->id;
  208. $filters = UserFilter::whereUserId($pid)
  209. ->whereFilterableType('App\Profile')
  210. ->whereIn('filter_type', ['mute', 'block'])
  211. ->pluck('filterable_id')->toArray();
  212. $filtered = array_merge($private->toArray(), $filters);
  213. } else {
  214. $filtered = $private->toArray();
  215. }
  216. if($min || $max) {
  217. $dir = $min ? '>' : '<';
  218. $id = $min ?? $max;
  219. $timeline = Status::select(
  220. 'id',
  221. 'uri',
  222. 'caption',
  223. 'rendered',
  224. 'profile_id',
  225. 'type',
  226. 'in_reply_to_id',
  227. 'reblog_of_id',
  228. 'is_nsfw',
  229. 'scope',
  230. 'local',
  231. 'reply_count',
  232. 'comments_disabled',
  233. 'created_at',
  234. 'updated_at'
  235. )->where('id', $dir, $id)
  236. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  237. ->whereLocal(true)
  238. ->whereNull('uri')
  239. ->whereNotIn('profile_id', $filtered)
  240. ->whereNull('in_reply_to_id')
  241. ->whereNull('reblog_of_id')
  242. ->whereVisibility('public')
  243. ->orderBy('created_at', 'desc')
  244. ->limit($limit)
  245. ->get();
  246. } else {
  247. $timeline = Status::select(
  248. 'id',
  249. 'uri',
  250. 'caption',
  251. 'rendered',
  252. 'profile_id',
  253. 'type',
  254. 'in_reply_to_id',
  255. 'reblog_of_id',
  256. 'is_nsfw',
  257. 'scope',
  258. 'local',
  259. 'reply_count',
  260. 'comments_disabled',
  261. 'created_at',
  262. 'updated_at'
  263. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  264. ->whereLocal(true)
  265. ->whereNull('uri')
  266. ->whereNotIn('profile_id', $filtered)
  267. ->whereNull('in_reply_to_id')
  268. ->whereNull('reblog_of_id')
  269. ->whereVisibility('public')
  270. ->orderBy('created_at', 'desc')
  271. ->simplePaginate($limit);
  272. }
  273. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  274. $res = $this->fractal->createData($fractal)->toArray();
  275. return response()->json($res);
  276. }
  277. public function homeTimelineApi(Request $request)
  278. {
  279. if(!Auth::check()) {
  280. return abort(403);
  281. }
  282. $this->validate($request,[
  283. 'page' => 'nullable|integer|max:40',
  284. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  285. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  286. 'limit' => 'nullable|integer|max:20'
  287. ]);
  288. $page = $request->input('page');
  289. $min = $request->input('min_id');
  290. $max = $request->input('max_id');
  291. $limit = $request->input('limit') ?? 3;
  292. // TODO: Use redis for timelines
  293. // $timeline = Timeline::build()->local();
  294. $pid = Auth::user()->profile->id;
  295. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  296. $following = Follower::whereProfileId($pid)->pluck('following_id');
  297. return $following->push($pid)->toArray();
  298. });
  299. $private = Cache::remember('profiles:private', 1440, function() {
  300. return Profile::whereIsPrivate(true)
  301. ->orWhere('unlisted', true)
  302. ->orWhere('status', '!=', null)
  303. ->pluck('id');
  304. });
  305. $private = $private->diff($following)->flatten();
  306. $filters = UserFilter::whereUserId($pid)
  307. ->whereFilterableType('App\Profile')
  308. ->whereIn('filter_type', ['mute', 'block'])
  309. ->pluck('filterable_id')->toArray();
  310. $filtered = array_merge($private->toArray(), $filters);
  311. if($min || $max) {
  312. $dir = $min ? '>' : '<';
  313. $id = $min ?? $max;
  314. $timeline = Status::select(
  315. 'id',
  316. 'uri',
  317. 'caption',
  318. 'rendered',
  319. 'profile_id',
  320. 'type',
  321. 'in_reply_to_id',
  322. 'reblog_of_id',
  323. 'is_nsfw',
  324. 'scope',
  325. 'local',
  326. 'reply_count',
  327. 'comments_disabled',
  328. 'created_at',
  329. 'updated_at'
  330. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  331. ->where('id', $dir, $id)
  332. ->whereIn('profile_id', $following)
  333. ->whereNotIn('profile_id', $filtered)
  334. ->whereNull('in_reply_to_id')
  335. ->whereNull('reblog_of_id')
  336. ->whereIn('visibility',['public', 'unlisted', 'private'])
  337. ->orderBy('created_at', 'desc')
  338. ->limit($limit)
  339. ->get();
  340. } else {
  341. $timeline = Status::select(
  342. 'id',
  343. 'uri',
  344. 'caption',
  345. 'rendered',
  346. 'profile_id',
  347. 'type',
  348. 'in_reply_to_id',
  349. 'reblog_of_id',
  350. 'is_nsfw',
  351. 'scope',
  352. 'local',
  353. 'reply_count',
  354. 'comments_disabled',
  355. 'created_at',
  356. 'updated_at'
  357. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  358. ->whereIn('profile_id', $following)
  359. ->whereNotIn('profile_id', $filtered)
  360. ->whereNull('in_reply_to_id')
  361. ->whereNull('reblog_of_id')
  362. ->whereIn('visibility',['public', 'unlisted', 'private'])
  363. ->orderBy('created_at', 'desc')
  364. ->simplePaginate($limit);
  365. }
  366. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  367. $res = $this->fractal->createData($fractal)->toArray();
  368. return response()->json($res);
  369. }
  370. public function networkTimelineApi(Request $request)
  371. {
  372. if(!Auth::check()) {
  373. return abort(403);
  374. }
  375. $this->validate($request,[
  376. 'page' => 'nullable|integer|max:40',
  377. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  378. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  379. 'limit' => 'nullable|integer|max:20'
  380. ]);
  381. $page = $request->input('page');
  382. $min = $request->input('min_id');
  383. $max = $request->input('max_id');
  384. $limit = $request->input('limit') ?? 3;
  385. // TODO: Use redis for timelines
  386. // $timeline = Timeline::build()->local();
  387. $pid = Auth::user()->profile->id;
  388. $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  389. return Profile::whereIsPrivate(true)
  390. ->orWhere('unlisted', true)
  391. ->orWhere('status', '!=', null)
  392. ->pluck('id');
  393. });
  394. $filters = UserFilter::whereUserId($pid)
  395. ->whereFilterableType('App\Profile')
  396. ->whereIn('filter_type', ['mute', 'block'])
  397. ->pluck('filterable_id')->toArray();
  398. $filtered = array_merge($private->toArray(), $filters);
  399. if($min || $max) {
  400. $dir = $min ? '>' : '<';
  401. $id = $min ?? $max;
  402. $timeline = Status::select(
  403. 'id',
  404. 'uri',
  405. 'caption',
  406. 'rendered',
  407. 'profile_id',
  408. 'type',
  409. 'in_reply_to_id',
  410. 'reblog_of_id',
  411. 'is_nsfw',
  412. 'scope',
  413. 'local',
  414. 'reply_count',
  415. 'comments_disabled',
  416. 'created_at',
  417. 'updated_at'
  418. )->where('id', $dir, $id)
  419. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  420. ->whereNotIn('profile_id', $filtered)
  421. ->whereNotNull('uri')
  422. ->whereNull('in_reply_to_id')
  423. ->whereNull('reblog_of_id')
  424. ->whereVisibility('public')
  425. ->latest()
  426. ->limit($limit)
  427. ->get();
  428. } else {
  429. $timeline = Status::select(
  430. 'id',
  431. 'uri',
  432. 'caption',
  433. 'rendered',
  434. 'profile_id',
  435. 'type',
  436. 'in_reply_to_id',
  437. 'reblog_of_id',
  438. 'is_nsfw',
  439. 'scope',
  440. 'local',
  441. 'reply_count',
  442. 'comments_disabled',
  443. 'created_at',
  444. 'updated_at'
  445. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  446. ->whereNotIn('profile_id', $filtered)
  447. ->whereNull('in_reply_to_id')
  448. ->whereNull('reblog_of_id')
  449. ->whereNotNull('uri')
  450. ->whereVisibility('public')
  451. ->latest()
  452. ->simplePaginate($limit);
  453. }
  454. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  455. $res = $this->fractal->createData($fractal)->toArray();
  456. return response()->json($res);
  457. }
  458. public function relationships(Request $request)
  459. {
  460. abort_if(!Auth::check(), 403);
  461. $this->validate($request, [
  462. 'id' => 'required|array|min:1|max:20',
  463. 'id.*' => 'required|integer'
  464. ]);
  465. $ids = collect($request->input('id'));
  466. $filtered = $ids->filter(function($v) {
  467. return $v != Auth::user()->profile->id;
  468. });
  469. $relations = Profile::findOrFail($filtered->all());
  470. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  471. $res = $this->fractal->createData($fractal)->toArray();
  472. return response()->json($res);
  473. }
  474. public function account(Request $request, $id)
  475. {
  476. $profile = Profile::whereNull('status')->findOrFail($id);
  477. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  478. $res = $this->fractal->createData($resource)->toArray();
  479. return response()->json($res);
  480. }
  481. public function accountFollowers(Request $request, $id)
  482. {
  483. abort_unless(Auth::check(), 403);
  484. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  485. if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_followers) {
  486. return response()->json([]);
  487. }
  488. $followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
  489. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  490. $res = $this->fractal->createData($resource)->toArray();
  491. return response()->json($res);
  492. }
  493. public function accountFollowing(Request $request, $id)
  494. {
  495. abort_unless(Auth::check(), 403);
  496. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  497. if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_following) {
  498. return response()->json([]);
  499. }
  500. $following = $profile->following()->orderByDesc('followers.created_at')->paginate(10);
  501. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  502. $res = $this->fractal->createData($resource)->toArray();
  503. return response()->json($res);
  504. }
  505. public function accountStatuses(Request $request, $id)
  506. {
  507. $this->validate($request, [
  508. 'only_media' => 'nullable',
  509. 'pinned' => 'nullable',
  510. 'exclude_replies' => 'nullable',
  511. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  512. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  513. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  514. 'limit' => 'nullable|integer|min:1|max:24'
  515. ]);
  516. $profile = Profile::whereNull('status')->findOrFail($id);
  517. $limit = $request->limit ?? 9;
  518. $max_id = $request->max_id;
  519. $min_id = $request->min_id;
  520. $scope = $request->only_media == true ?
  521. ['photo', 'photo:album', 'video', 'video:album'] :
  522. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  523. if($profile->is_private) {
  524. if(!Auth::check()) {
  525. return response()->json([]);
  526. }
  527. $pid = Auth::user()->profile->id;
  528. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  529. $following = Follower::whereProfileId($pid)->pluck('following_id');
  530. return $following->push($pid)->toArray();
  531. });
  532. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  533. } else {
  534. if(Auth::check()) {
  535. $pid = Auth::user()->profile->id;
  536. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  537. $following = Follower::whereProfileId($pid)->pluck('following_id');
  538. return $following->push($pid)->toArray();
  539. });
  540. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  541. } else {
  542. $visibility = ['public', 'unlisted'];
  543. }
  544. }
  545. $dir = $min_id ? '>' : '<';
  546. $id = $min_id ?? $max_id;
  547. $timeline = Status::select(
  548. 'id',
  549. 'uri',
  550. 'caption',
  551. 'rendered',
  552. 'profile_id',
  553. 'type',
  554. 'in_reply_to_id',
  555. 'reblog_of_id',
  556. 'is_nsfw',
  557. 'scope',
  558. 'local',
  559. 'created_at',
  560. 'updated_at'
  561. )->whereProfileId($profile->id)
  562. ->whereIn('type', $scope)
  563. ->whereLocal(true)
  564. ->whereNull('uri')
  565. ->where('id', $dir, $id)
  566. ->whereIn('visibility', $visibility)
  567. ->latest()
  568. ->limit($limit)
  569. ->get();
  570. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  571. $res = $this->fractal->createData($resource)->toArray();
  572. return response()->json($res);
  573. }
  574. }