PublicApiController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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', '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', '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', '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. $page = $request->input('page');
  194. $min = $request->input('min_id');
  195. $max = $request->input('max_id');
  196. $limit = $request->input('limit') ?? 3;
  197. $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  198. return Profile::whereIsPrivate(true)
  199. ->orWhere('unlisted', true)
  200. ->orWhere('status', '!=', null)
  201. ->pluck('id');
  202. });
  203. if(Auth::check()) {
  204. $pid = Auth::user()->profile->id;
  205. $filters = UserFilter::whereUserId($pid)
  206. ->whereFilterableType('App\Profile')
  207. ->whereIn('filter_type', ['mute', 'block'])
  208. ->pluck('filterable_id')->toArray();
  209. $filtered = array_merge($private->toArray(), $filters);
  210. } else {
  211. $filtered = $private->toArray();
  212. }
  213. if($min || $max) {
  214. $dir = $min ? '>' : '<';
  215. $id = $min ?? $max;
  216. $timeline = Status::select(
  217. 'id',
  218. 'uri',
  219. 'caption',
  220. 'rendered',
  221. 'profile_id',
  222. 'type',
  223. 'in_reply_to_id',
  224. 'reblog_of_id',
  225. 'is_nsfw',
  226. 'scope',
  227. 'local',
  228. 'reply_count',
  229. 'comments_disabled',
  230. 'created_at',
  231. 'updated_at'
  232. )->where('id', $dir, $id)
  233. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  234. ->whereLocal(true)
  235. ->whereNull('uri')
  236. ->whereNotIn('profile_id', $filtered)
  237. ->whereNull('in_reply_to_id')
  238. ->whereNull('reblog_of_id')
  239. ->whereVisibility('public')
  240. ->orderBy('created_at', 'desc')
  241. ->limit($limit)
  242. ->get();
  243. } else {
  244. $timeline = Status::select(
  245. 'id',
  246. 'uri',
  247. 'caption',
  248. 'rendered',
  249. 'profile_id',
  250. 'type',
  251. 'in_reply_to_id',
  252. 'reblog_of_id',
  253. 'is_nsfw',
  254. 'scope',
  255. 'local',
  256. 'reply_count',
  257. 'comments_disabled',
  258. 'created_at',
  259. 'updated_at'
  260. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  261. ->whereLocal(true)
  262. ->whereNull('uri')
  263. ->whereNotIn('profile_id', $filtered)
  264. ->whereNull('in_reply_to_id')
  265. ->whereNull('reblog_of_id')
  266. ->whereVisibility('public')
  267. ->orderBy('created_at', 'desc')
  268. ->simplePaginate($limit);
  269. }
  270. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  271. $res = $this->fractal->createData($fractal)->toArray();
  272. return response()->json($res);
  273. }
  274. public function homeTimelineApi(Request $request)
  275. {
  276. if(!Auth::check()) {
  277. return abort(403);
  278. }
  279. $this->validate($request,[
  280. 'page' => 'nullable|integer|max:40',
  281. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  282. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  283. 'limit' => 'nullable|integer|max:20'
  284. ]);
  285. $page = $request->input('page');
  286. $min = $request->input('min_id');
  287. $max = $request->input('max_id');
  288. $limit = $request->input('limit') ?? 3;
  289. // TODO: Use redis for timelines
  290. // $timeline = Timeline::build()->local();
  291. $pid = Auth::user()->profile->id;
  292. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  293. $following = Follower::whereProfileId($pid)->pluck('following_id');
  294. return $following->push($pid)->toArray();
  295. });
  296. $private = Cache::remember('profiles:private', 1440, function() {
  297. return Profile::whereIsPrivate(true)
  298. ->orWhere('unlisted', true)
  299. ->orWhere('status', '!=', null)
  300. ->pluck('id');
  301. });
  302. $filters = UserFilter::whereUserId($pid)
  303. ->whereFilterableType('App\Profile')
  304. ->whereIn('filter_type', ['mute', 'block'])
  305. ->pluck('filterable_id')->toArray();
  306. $filtered = array_merge($private->toArray(), $filters);
  307. if($min || $max) {
  308. $dir = $min ? '>' : '<';
  309. $id = $min ?? $max;
  310. $timeline = Status::select(
  311. 'id',
  312. 'uri',
  313. 'caption',
  314. 'rendered',
  315. 'profile_id',
  316. 'type',
  317. 'in_reply_to_id',
  318. 'reblog_of_id',
  319. 'is_nsfw',
  320. 'scope',
  321. 'local',
  322. 'reply_count',
  323. 'comments_disabled',
  324. 'created_at',
  325. 'updated_at'
  326. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  327. ->where('id', $dir, $id)
  328. ->whereIn('profile_id', $following)
  329. ->whereNotIn('profile_id', $filtered)
  330. ->whereNull('in_reply_to_id')
  331. ->whereNull('reblog_of_id')
  332. ->whereIn('visibility',['public', 'unlisted', 'private'])
  333. ->orderBy('created_at', 'desc')
  334. ->limit($limit)
  335. ->get();
  336. } else {
  337. $timeline = Status::select(
  338. 'id',
  339. 'uri',
  340. 'caption',
  341. 'rendered',
  342. 'profile_id',
  343. 'type',
  344. 'in_reply_to_id',
  345. 'reblog_of_id',
  346. 'is_nsfw',
  347. 'scope',
  348. 'local',
  349. 'reply_count',
  350. 'comments_disabled',
  351. 'created_at',
  352. 'updated_at'
  353. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  354. ->whereIn('profile_id', $following)
  355. ->whereNotIn('profile_id', $filtered)
  356. ->whereNull('in_reply_to_id')
  357. ->whereNull('reblog_of_id')
  358. ->whereIn('visibility',['public', 'unlisted', 'private'])
  359. ->orderBy('created_at', 'desc')
  360. ->simplePaginate($limit);
  361. }
  362. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  363. $res = $this->fractal->createData($fractal)->toArray();
  364. return response()->json($res);
  365. }
  366. public function networkTimelineApi(Request $request)
  367. {
  368. if(!Auth::check()) {
  369. return abort(403);
  370. }
  371. $this->validate($request,[
  372. 'page' => 'nullable|integer|max:40',
  373. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  374. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  375. 'limit' => 'nullable|integer|max:20'
  376. ]);
  377. $page = $request->input('page');
  378. $min = $request->input('min_id');
  379. $max = $request->input('max_id');
  380. $limit = $request->input('limit') ?? 3;
  381. // TODO: Use redis for timelines
  382. // $timeline = Timeline::build()->local();
  383. $pid = Auth::user()->profile->id;
  384. $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  385. return Profile::whereIsPrivate(true)
  386. ->orWhere('unlisted', true)
  387. ->orWhere('status', '!=', null)
  388. ->pluck('id');
  389. });
  390. $filters = UserFilter::whereUserId($pid)
  391. ->whereFilterableType('App\Profile')
  392. ->whereIn('filter_type', ['mute', 'block'])
  393. ->pluck('filterable_id')->toArray();
  394. $filtered = array_merge($private->toArray(), $filters);
  395. if($min || $max) {
  396. $dir = $min ? '>' : '<';
  397. $id = $min ?? $max;
  398. $timeline = Status::select(
  399. 'id',
  400. 'uri',
  401. 'caption',
  402. 'rendered',
  403. 'profile_id',
  404. 'type',
  405. 'in_reply_to_id',
  406. 'reblog_of_id',
  407. 'is_nsfw',
  408. 'scope',
  409. 'local',
  410. 'reply_count',
  411. 'comments_disabled',
  412. 'created_at',
  413. 'updated_at'
  414. )->where('id', $dir, $id)
  415. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  416. ->whereNotIn('profile_id', $filtered)
  417. ->whereNotNull('uri')
  418. ->whereNull('in_reply_to_id')
  419. ->whereNull('reblog_of_id')
  420. ->whereVisibility('public')
  421. ->latest()
  422. ->limit($limit)
  423. ->get();
  424. } else {
  425. $timeline = Status::select(
  426. 'id',
  427. 'uri',
  428. 'caption',
  429. 'rendered',
  430. 'profile_id',
  431. 'type',
  432. 'in_reply_to_id',
  433. 'reblog_of_id',
  434. 'is_nsfw',
  435. 'scope',
  436. 'local',
  437. 'reply_count',
  438. 'comments_disabled',
  439. 'created_at',
  440. 'updated_at'
  441. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  442. ->whereNotIn('profile_id', $filtered)
  443. ->whereNull('in_reply_to_id')
  444. ->whereNull('reblog_of_id')
  445. ->whereNotNull('uri')
  446. ->whereVisibility('public')
  447. ->latest()
  448. ->simplePaginate($limit);
  449. }
  450. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  451. $res = $this->fractal->createData($fractal)->toArray();
  452. return response()->json($res);
  453. }
  454. public function relationships(Request $request)
  455. {
  456. abort_if(!Auth::check(), 403);
  457. $this->validate($request, [
  458. 'id' => 'required|array|min:1|max:20',
  459. 'id.*' => 'required|integer'
  460. ]);
  461. $ids = collect($request->input('id'));
  462. $filtered = $ids->filter(function($v) {
  463. return $v != Auth::user()->profile->id;
  464. });
  465. $relations = Profile::findOrFail($filtered->all());
  466. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  467. $res = $this->fractal->createData($fractal)->toArray();
  468. return response()->json($res);
  469. }
  470. public function account(Request $request, $id)
  471. {
  472. $profile = Profile::whereNull('status')->findOrFail($id);
  473. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  474. $res = $this->fractal->createData($resource)->toArray();
  475. return response()->json($res);
  476. }
  477. public function accountFollowers(Request $request, $id)
  478. {
  479. abort_unless(Auth::check(), 403);
  480. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  481. if($profile->is_private || !$profile->user->settings->show_profile_followers) {
  482. return [];
  483. }
  484. $followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
  485. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  486. $res = $this->fractal->createData($resource)->toArray();
  487. return response()->json($res);
  488. }
  489. public function accountFollowing(Request $request, $id)
  490. {
  491. abort_unless(Auth::check(), 403);
  492. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  493. if($profile->is_private || !$profile->user->settings->show_profile_following) {
  494. return [];
  495. }
  496. $following = $profile->following()->orderByDesc('followers.created_at')->paginate(10);
  497. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  498. $res = $this->fractal->createData($resource)->toArray();
  499. return response()->json($res);
  500. }
  501. public function accountStatuses(Request $request, $id)
  502. {
  503. $this->validate($request, [
  504. 'only_media' => 'nullable',
  505. 'pinned' => 'nullable',
  506. 'exclude_replies' => 'nullable',
  507. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  508. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  509. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  510. 'limit' => 'nullable|integer|min:1|max:24'
  511. ]);
  512. $profile = Profile::whereNull('status')->findOrFail($id);
  513. $limit = $request->limit ?? 9;
  514. $max_id = $request->max_id;
  515. $min_id = $request->min_id;
  516. $scope = $request->only_media == true ?
  517. ['photo', 'photo:album', 'video', 'video:album'] :
  518. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  519. if($profile->is_private) {
  520. if(!Auth::check()) {
  521. return response()->json([]);
  522. }
  523. $pid = Auth::user()->profile->id;
  524. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  525. $following = Follower::whereProfileId($pid)->pluck('following_id');
  526. return $following->push($pid)->toArray();
  527. });
  528. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  529. } else {
  530. if(Auth::check()) {
  531. $pid = Auth::user()->profile->id;
  532. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  533. $following = Follower::whereProfileId($pid)->pluck('following_id');
  534. return $following->push($pid)->toArray();
  535. });
  536. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  537. } else {
  538. $visibility = ['public', 'unlisted'];
  539. }
  540. }
  541. $dir = $min_id ? '>' : '<';
  542. $id = $min_id ?? $max_id;
  543. $timeline = Status::select(
  544. 'id',
  545. 'uri',
  546. 'caption',
  547. 'rendered',
  548. 'profile_id',
  549. 'type',
  550. 'in_reply_to_id',
  551. 'reblog_of_id',
  552. 'is_nsfw',
  553. 'scope',
  554. 'local',
  555. 'created_at',
  556. 'updated_at'
  557. )->whereProfileId($profile->id)
  558. ->whereIn('type', $scope)
  559. ->whereLocal(true)
  560. ->whereNull('uri')
  561. ->where('id', $dir, $id)
  562. ->whereIn('visibility', $visibility)
  563. ->latest()
  564. ->limit($limit)
  565. ->get();
  566. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  567. $res = $this->fractal->createData($resource)->toArray();
  568. return response()->json($res);
  569. }
  570. }