PublicApiController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. StatusTransformer,
  21. };
  22. use App\Jobs\StatusPipeline\NewStatusPipeline;
  23. use League\Fractal\Serializer\ArraySerializer;
  24. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  25. class PublicApiController extends Controller
  26. {
  27. protected $fractal;
  28. public function __construct()
  29. {
  30. $this->middleware('throttle:3000, 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. ->select('id', 'caption', 'rendered', 'profile_id', 'in_reply_to_id', 'created_at')
  125. ->orderBy('id', 'desc')
  126. ->paginate($limit);
  127. }
  128. $resource = new Fractal\Resource\Collection($replies, new StatusTransformer(), 'data');
  129. $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
  130. $res = $this->fractal->createData($resource)->toArray();
  131. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  132. }
  133. public function statusLikes(Request $request, $username, $id)
  134. {
  135. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  136. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  137. $this->scopeCheck($profile, $status);
  138. $likes = $this->getLikes($status);
  139. return response()->json([
  140. 'data' => $likes
  141. ]);
  142. }
  143. public function statusShares(Request $request, $username, $id)
  144. {
  145. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  146. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  147. $this->scopeCheck($profile, $status);
  148. $shares = $this->getShares($status);
  149. return response()->json([
  150. 'data' => $shares
  151. ]);
  152. }
  153. protected function scopeCheck(Profile $profile, Status $status)
  154. {
  155. if($profile->is_private == true && Auth::check() == false) {
  156. abort(404);
  157. }
  158. switch ($status->scope) {
  159. case 'public':
  160. case 'unlisted':
  161. case 'private':
  162. $user = Auth::check() ? Auth::user() : false;
  163. if($user && $profile->is_private) {
  164. $follows = Follower::whereProfileId($user->profile->id)
  165. ->whereFollowingId($profile->id)
  166. ->exists();
  167. if($follows == false && $profile->id !== $user->profile->id) {
  168. abort(404);
  169. }
  170. }
  171. break;
  172. case 'direct':
  173. abort(404);
  174. break;
  175. case 'draft':
  176. abort(404);
  177. break;
  178. default:
  179. abort(404);
  180. break;
  181. }
  182. }
  183. public function publicTimelineApi(Request $request)
  184. {
  185. if(!Auth::check()) {
  186. return abort(403);
  187. }
  188. $this->validate($request,[
  189. 'page' => 'nullable|integer|max:40',
  190. 'min_id' => 'nullable|integer',
  191. 'max_id' => 'nullable|integer',
  192. 'limit' => 'nullable|integer|max:20'
  193. ]);
  194. $page = $request->input('page');
  195. $min = $request->input('min_id');
  196. $max = $request->input('max_id');
  197. $limit = $request->input('limit') ?? 10;
  198. // TODO: Use redis for timelines
  199. // $timeline = Timeline::build()->local();
  200. $pid = Auth::user()->profile->id;
  201. $private = Profile::whereIsPrivate(true)->orWhereNotNull('status')->where('id', '!=', $pid)->pluck('id');
  202. $filters = UserFilter::whereUserId($pid)
  203. ->whereFilterableType('App\Profile')
  204. ->whereIn('filter_type', ['mute', 'block'])
  205. ->pluck('filterable_id')->toArray();
  206. $filtered = array_merge($private->toArray(), $filters);
  207. if($min || $max) {
  208. $dir = $min ? '>' : '<';
  209. $id = $min ?? $max;
  210. $timeline = Status::whereHas('media')
  211. ->where('id', $dir, $id)
  212. ->whereNotIn('profile_id', $filtered)
  213. ->whereNull('in_reply_to_id')
  214. ->whereNull('reblog_of_id')
  215. ->whereVisibility('public')
  216. ->withCount(['comments', 'likes'])
  217. ->orderBy('created_at', 'desc')
  218. ->limit($limit)
  219. ->get();
  220. } else {
  221. $timeline = Status::whereHas('media')
  222. ->whereNotIn('profile_id', $filtered)
  223. ->whereNull('in_reply_to_id')
  224. ->whereNull('reblog_of_id')
  225. ->whereVisibility('public')
  226. ->withCount(['comments', 'likes'])
  227. ->orderBy('created_at', 'desc')
  228. ->simplePaginate($limit);
  229. }
  230. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  231. $res = $this->fractal->createData($fractal)->toArray();
  232. return response()->json($res);
  233. }
  234. public function homeTimelineApi(Request $request)
  235. {
  236. if(!Auth::check()) {
  237. return abort(403);
  238. }
  239. $this->validate($request,[
  240. 'page' => 'nullable|integer|max:40',
  241. 'min_id' => 'nullable|integer',
  242. 'max_id' => 'nullable|integer',
  243. 'limit' => 'nullable|integer|max:20'
  244. ]);
  245. $page = $request->input('page');
  246. $min = $request->input('min_id');
  247. $max = $request->input('max_id');
  248. $limit = $request->input('limit') ?? 10;
  249. // TODO: Use redis for timelines
  250. // $timeline = Timeline::build()->local();
  251. $pid = Auth::user()->profile->id;
  252. $following = Follower::whereProfileId($pid)->pluck('following_id');
  253. $following->push($pid)->toArray();
  254. $private = Profile::whereIsPrivate(true)->orWhereNotNull('status')->where('id', '!=', $pid)->pluck('id');
  255. $filters = UserFilter::whereUserId($pid)
  256. ->whereFilterableType('App\Profile')
  257. ->whereIn('filter_type', ['mute', 'block'])
  258. ->pluck('filterable_id')->toArray();
  259. $filtered = array_merge($private->toArray(), $filters);
  260. if($min || $max) {
  261. $dir = $min ? '>' : '<';
  262. $id = $min ?? $max;
  263. $timeline = Status::whereHas('media')
  264. ->where('id', $dir, $id)
  265. ->whereIn('profile_id', $following)
  266. ->whereNotIn('profile_id', $filtered)
  267. ->whereNull('in_reply_to_id')
  268. ->whereNull('reblog_of_id')
  269. ->whereVisibility('public')
  270. ->withCount(['comments', 'likes'])
  271. ->orderBy('created_at', 'desc')
  272. ->limit($limit)
  273. ->get();
  274. } else {
  275. $timeline = Status::whereHas('media')
  276. ->whereIn('profile_id', $following)
  277. ->whereNotIn('profile_id', $filtered)
  278. ->whereNull('in_reply_to_id')
  279. ->whereNull('reblog_of_id')
  280. ->whereVisibility('public')
  281. ->withCount(['comments', 'likes'])
  282. ->orderBy('created_at', 'desc')
  283. ->simplePaginate($limit);
  284. }
  285. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  286. $res = $this->fractal->createData($fractal)->toArray();
  287. return response()->json($res);
  288. }
  289. }