PublicApiController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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)->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', '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', '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', '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(Auth::user()->profile);
  169. if($follows == false && $profile->id !== $user->profile->id) {
  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. if(!Auth::check()) {
  188. return abort(403);
  189. }
  190. $this->validate($request,[
  191. 'page' => 'nullable|integer|max:40',
  192. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  193. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  194. 'limit' => 'nullable|integer|max:20'
  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. // TODO: Use redis for timelines
  201. // $timeline = Timeline::build()->local();
  202. $pid = Auth::user()->profile->id;
  203. $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  204. return Profile::whereIsPrivate(true)
  205. ->orWhere('unlisted', true)
  206. ->orWhere('status', '!=', null)
  207. ->pluck('id');
  208. });
  209. $filters = UserFilter::whereUserId($pid)
  210. ->whereFilterableType('App\Profile')
  211. ->whereIn('filter_type', ['mute', 'block'])
  212. ->pluck('filterable_id')->toArray();
  213. $filtered = array_merge($private->toArray(), $filters);
  214. if($min || $max) {
  215. $dir = $min ? '>' : '<';
  216. $id = $min ?? $max;
  217. $timeline = Status::select(
  218. 'id',
  219. 'uri',
  220. 'caption',
  221. 'rendered',
  222. 'profile_id',
  223. 'type',
  224. 'in_reply_to_id',
  225. 'reblog_of_id',
  226. 'is_nsfw',
  227. 'scope',
  228. 'local',
  229. 'created_at',
  230. 'updated_at'
  231. )->where('id', $dir, $id)
  232. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  233. ->whereLocal(true)
  234. ->whereNull('uri')
  235. ->whereNotIn('profile_id', $filtered)
  236. ->whereNull('in_reply_to_id')
  237. ->whereNull('reblog_of_id')
  238. ->whereVisibility('public')
  239. ->orderBy('created_at', 'desc')
  240. ->limit($limit)
  241. ->get();
  242. } else {
  243. $timeline = Status::select(
  244. 'id',
  245. 'uri',
  246. 'caption',
  247. 'rendered',
  248. 'profile_id',
  249. 'type',
  250. 'in_reply_to_id',
  251. 'reblog_of_id',
  252. 'is_nsfw',
  253. 'scope',
  254. 'local',
  255. 'created_at',
  256. 'updated_at'
  257. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  258. ->whereLocal(true)
  259. ->whereNull('uri')
  260. ->whereNotIn('profile_id', $filtered)
  261. ->whereNull('in_reply_to_id')
  262. ->whereNull('reblog_of_id')
  263. ->whereVisibility('public')
  264. ->orderBy('created_at', 'desc')
  265. ->simplePaginate($limit);
  266. }
  267. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  268. $res = $this->fractal->createData($fractal)->toArray();
  269. return response()->json($res);
  270. }
  271. public function homeTimelineApi(Request $request)
  272. {
  273. if(!Auth::check()) {
  274. return abort(403);
  275. }
  276. $this->validate($request,[
  277. 'page' => 'nullable|integer|max:40',
  278. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  279. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  280. 'limit' => 'nullable|integer|max:20'
  281. ]);
  282. $page = $request->input('page');
  283. $min = $request->input('min_id');
  284. $max = $request->input('max_id');
  285. $limit = $request->input('limit') ?? 3;
  286. // TODO: Use redis for timelines
  287. // $timeline = Timeline::build()->local();
  288. $pid = Auth::user()->profile->id;
  289. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  290. $following = Follower::whereProfileId($pid)->pluck('following_id');
  291. return $following->push($pid)->toArray();
  292. });
  293. $private = Cache::remember('profiles:private', 1440, function() {
  294. return Profile::whereIsPrivate(true)
  295. ->orWhere('unlisted', true)
  296. ->orWhere('status', '!=', null)
  297. ->pluck('id');
  298. });
  299. $filters = UserFilter::whereUserId($pid)
  300. ->whereFilterableType('App\Profile')
  301. ->whereIn('filter_type', ['mute', 'block'])
  302. ->pluck('filterable_id')->toArray();
  303. $filtered = array_merge($private->toArray(), $filters);
  304. if($min || $max) {
  305. $dir = $min ? '>' : '<';
  306. $id = $min ?? $max;
  307. $timeline = Status::select(
  308. 'id',
  309. 'uri',
  310. 'caption',
  311. 'rendered',
  312. 'profile_id',
  313. 'type',
  314. 'in_reply_to_id',
  315. 'reblog_of_id',
  316. 'is_nsfw',
  317. 'scope',
  318. 'local',
  319. 'created_at',
  320. 'updated_at'
  321. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  322. ->whereLocal(true)
  323. ->whereNull('uri')
  324. ->where('id', $dir, $id)
  325. ->whereIn('profile_id', $following)
  326. ->whereNotIn('profile_id', $filtered)
  327. ->whereNull('in_reply_to_id')
  328. ->whereNull('reblog_of_id')
  329. ->whereIn('visibility',['public', 'unlisted', 'private'])
  330. ->orderBy('created_at', 'desc')
  331. ->limit($limit)
  332. ->get();
  333. } else {
  334. $timeline = Status::select(
  335. 'id',
  336. 'uri',
  337. 'caption',
  338. 'rendered',
  339. 'profile_id',
  340. 'type',
  341. 'in_reply_to_id',
  342. 'reblog_of_id',
  343. 'is_nsfw',
  344. 'scope',
  345. 'local',
  346. 'created_at',
  347. 'updated_at'
  348. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  349. ->whereLocal(true)
  350. ->whereNull('uri')
  351. ->whereIn('profile_id', $following)
  352. ->whereNotIn('profile_id', $filtered)
  353. ->whereNull('in_reply_to_id')
  354. ->whereNull('reblog_of_id')
  355. ->whereIn('visibility',['public', 'unlisted', 'private'])
  356. ->orderBy('created_at', 'desc')
  357. ->simplePaginate($limit);
  358. }
  359. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  360. $res = $this->fractal->createData($fractal)->toArray();
  361. return response()->json($res);
  362. }
  363. public function relationships(Request $request)
  364. {
  365. abort_if(!Auth::check(), 403);
  366. $this->validate($request, [
  367. 'id' => 'required|array|min:1|max:20',
  368. 'id.*' => 'required|integer'
  369. ]);
  370. $ids = collect($request->input('id'));
  371. $filtered = $ids->filter(function($v) {
  372. return $v != Auth::user()->profile->id;
  373. });
  374. $relations = Profile::findOrFail($filtered->all());
  375. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  376. $res = $this->fractal->createData($fractal)->toArray();
  377. return response()->json($res);
  378. }
  379. public function account(Request $request, $id)
  380. {
  381. $profile = Profile::whereNull('status')->findOrFail($id);
  382. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  383. $res = $this->fractal->createData($resource)->toArray();
  384. return response()->json($res);
  385. }
  386. public function accountFollowers(Request $request, $id)
  387. {
  388. abort_unless(Auth::check(), 403);
  389. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  390. if($profile->is_private || !$profile->user->settings->show_profile_followers) {
  391. return [];
  392. }
  393. $followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
  394. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  395. $res = $this->fractal->createData($resource)->toArray();
  396. return response()->json($res);
  397. }
  398. public function accountFollowing(Request $request, $id)
  399. {
  400. abort_unless(Auth::check(), 403);
  401. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  402. if($profile->is_private || !$profile->user->settings->show_profile_following) {
  403. return [];
  404. }
  405. $following = $profile->following()->orderByDesc('followers.created_at')->paginate(10);
  406. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  407. $res = $this->fractal->createData($resource)->toArray();
  408. return response()->json($res);
  409. }
  410. public function accountStatuses(Request $request, $id)
  411. {
  412. $this->validate($request, [
  413. 'only_media' => 'nullable',
  414. 'pinned' => 'nullable',
  415. 'exclude_replies' => 'nullable',
  416. 'max_id' => 'nullable|integer|min:1',
  417. 'since_id' => 'nullable|integer|min:1',
  418. 'min_id' => 'nullable|integer|min:1',
  419. 'limit' => 'nullable|integer|min:1|max:24'
  420. ]);
  421. $limit = $request->limit ?? 20;
  422. $max_id = $request->max_id ?? false;
  423. $min_id = $request->min_id ?? false;
  424. $since_id = $request->since_id ?? false;
  425. $only_media = $request->only_media ?? false;
  426. $user = Auth::user();
  427. $account = Profile::whereNull('status')->findOrFail($id);
  428. $statuses = Status::whereProfileId($id)
  429. ->whereNull('uri');
  430. if(!$user || $user->profile->id != $account->id && !$user->profile->follows($account)) {
  431. $statuses = $statuses->whereVisibility('public');
  432. } else {
  433. $statuses = $statuses->whereIn('visibility', ['public', 'unlisted', 'private']);
  434. }
  435. if($only_media == true) {
  436. $statuses = $statuses
  437. ->whereHas('media')
  438. ->whereNull('in_reply_to_id')
  439. ->whereNull('reblog_of_id');
  440. }
  441. if($id == $account->id && !$max_id && !$min_id && !$since_id) {
  442. $statuses = $statuses->orderBy('id', 'desc')
  443. ->simplePaginate($limit);
  444. } else if($since_id) {
  445. $statuses = $statuses->where('id', '>', $since_id)
  446. ->orderBy('id', 'DESC')
  447. ->simplePaginate($limit);
  448. } else if($min_id) {
  449. $statuses = $statuses->where('id', '>', $min_id)
  450. ->orderBy('id', 'ASC')
  451. ->simplePaginate($limit);
  452. } else if($max_id) {
  453. $statuses = $statuses->where('id', '<', $max_id)
  454. ->orderBy('id', 'DESC')
  455. ->simplePaginate($limit);
  456. } else {
  457. $statuses = $statuses->orderBy('id', 'desc')->simplePaginate($limit);
  458. }
  459. $resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
  460. $res = $this->fractal->createData($resource)->toArray();
  461. return response()->json($res);
  462. }
  463. }