1
0

PublicApiController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. ->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. case 'private':
  163. $user = Auth::check() ? Auth::user() : false;
  164. if($user && $profile->is_private) {
  165. $follows = Follower::whereProfileId($user->profile->id)
  166. ->whereFollowingId($profile->id)
  167. ->exists();
  168. if($follows == false && $profile->id !== $user->profile->id) {
  169. abort(404);
  170. }
  171. }
  172. break;
  173. case 'direct':
  174. abort(404);
  175. break;
  176. case 'draft':
  177. abort(404);
  178. break;
  179. default:
  180. abort(404);
  181. break;
  182. }
  183. }
  184. public function publicTimelineApi(Request $request)
  185. {
  186. if(!Auth::check()) {
  187. return abort(403);
  188. }
  189. $this->validate($request,[
  190. 'page' => 'nullable|integer|max:40',
  191. 'min_id' => 'nullable|integer',
  192. 'max_id' => 'nullable|integer',
  193. 'limit' => 'nullable|integer|max:20'
  194. ]);
  195. $page = $request->input('page');
  196. $min = $request->input('min_id');
  197. $max = $request->input('max_id');
  198. $limit = $request->input('limit') ?? 10;
  199. // TODO: Use redis for timelines
  200. // $timeline = Timeline::build()->local();
  201. $pid = Auth::user()->profile->id;
  202. $private = Profile::whereIsPrivate(true)->orWhereNotNull('status')->where('id', '!=', $pid)->pluck('id');
  203. $filters = UserFilter::whereUserId($pid)
  204. ->whereFilterableType('App\Profile')
  205. ->whereIn('filter_type', ['mute', 'block'])
  206. ->pluck('filterable_id')->toArray();
  207. $filtered = array_merge($private->toArray(), $filters);
  208. if($min || $max) {
  209. $dir = $min ? '>' : '<';
  210. $id = $min ?? $max;
  211. $timeline = Status::whereHas('media')
  212. ->where('id', $dir, $id)
  213. ->whereNotIn('profile_id', $filtered)
  214. ->whereNull('in_reply_to_id')
  215. ->whereNull('reblog_of_id')
  216. ->whereVisibility('public')
  217. ->withCount(['comments', 'likes'])
  218. ->orderBy('created_at', 'desc')
  219. ->limit($limit)
  220. ->get();
  221. } else {
  222. $timeline = Status::whereHas('media')
  223. ->whereNotIn('profile_id', $filtered)
  224. ->whereNull('in_reply_to_id')
  225. ->whereNull('reblog_of_id')
  226. ->whereVisibility('public')
  227. ->withCount(['comments', 'likes'])
  228. ->orderBy('created_at', 'desc')
  229. ->simplePaginate($limit);
  230. }
  231. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  232. $res = $this->fractal->createData($fractal)->toArray();
  233. return response()->json($res);
  234. }
  235. public function homeTimelineApi(Request $request)
  236. {
  237. if(!Auth::check()) {
  238. return abort(403);
  239. }
  240. $this->validate($request,[
  241. 'page' => 'nullable|integer|max:40',
  242. 'min_id' => 'nullable|integer',
  243. 'max_id' => 'nullable|integer',
  244. 'limit' => 'nullable|integer|max:20'
  245. ]);
  246. $page = $request->input('page');
  247. $min = $request->input('min_id');
  248. $max = $request->input('max_id');
  249. $limit = $request->input('limit') ?? 10;
  250. // TODO: Use redis for timelines
  251. // $timeline = Timeline::build()->local();
  252. $pid = Auth::user()->profile->id;
  253. $following = Follower::whereProfileId($pid)->pluck('following_id');
  254. $following->push($pid)->toArray();
  255. $private = Profile::whereIsPrivate(true)->orWhereNotNull('status')->where('id', '!=', $pid)->pluck('id');
  256. $filters = UserFilter::whereUserId($pid)
  257. ->whereFilterableType('App\Profile')
  258. ->whereIn('filter_type', ['mute', 'block'])
  259. ->pluck('filterable_id')->toArray();
  260. $filtered = array_merge($private->toArray(), $filters);
  261. if($min || $max) {
  262. $dir = $min ? '>' : '<';
  263. $id = $min ?? $max;
  264. $timeline = Status::whereHas('media')
  265. ->where('id', $dir, $id)
  266. ->whereIn('profile_id', $following)
  267. ->whereNotIn('profile_id', $filtered)
  268. ->whereNull('in_reply_to_id')
  269. ->whereNull('reblog_of_id')
  270. ->whereIn('visibility',['public', 'unlisted', 'private'])
  271. ->withCount(['comments', 'likes'])
  272. ->orderBy('created_at', 'desc')
  273. ->limit($limit)
  274. ->get();
  275. } else {
  276. $timeline = Status::whereHas('media')
  277. ->whereIn('profile_id', $following)
  278. ->whereNotIn('profile_id', $filtered)
  279. ->whereNull('in_reply_to_id')
  280. ->whereNull('reblog_of_id')
  281. ->whereIn('visibility',['public', 'unlisted', 'private'])
  282. ->withCount(['comments', 'likes'])
  283. ->orderBy('created_at', 'desc')
  284. ->simplePaginate($limit);
  285. }
  286. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  287. $res = $this->fractal->createData($fractal)->toArray();
  288. return response()->json($res);
  289. }
  290. }