InternalApiController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\{
  5. DirectMessage,
  6. DiscoverCategory,
  7. Hashtag,
  8. Follower,
  9. Like,
  10. Media,
  11. Notification,
  12. Profile,
  13. StatusHashtag,
  14. Status,
  15. UserFilter,
  16. };
  17. use Auth,Cache;
  18. use Carbon\Carbon;
  19. use League\Fractal;
  20. use App\Transformer\Api\{
  21. AccountTransformer,
  22. StatusTransformer,
  23. };
  24. use App\Util\Media\Filter;
  25. use App\Jobs\StatusPipeline\NewStatusPipeline;
  26. use League\Fractal\Serializer\ArraySerializer;
  27. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  28. use Illuminate\Validation\Rule;
  29. use Illuminate\Support\Str;
  30. class InternalApiController extends Controller
  31. {
  32. protected $fractal;
  33. public function __construct()
  34. {
  35. $this->middleware('auth');
  36. $this->fractal = new Fractal\Manager();
  37. $this->fractal->setSerializer(new ArraySerializer());
  38. }
  39. // deprecated v2 compose api
  40. public function compose(Request $request)
  41. {
  42. return redirect('/');
  43. }
  44. // deprecated
  45. public function discover(Request $request)
  46. {
  47. $profile = Auth::user()->profile;
  48. $pid = $profile->id;
  49. $following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(60), function() use ($pid) {
  50. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  51. });
  52. $filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(60), function() use($pid) {
  53. return UserFilter::whereUserId($pid)
  54. ->whereFilterableType('App\Profile')
  55. ->whereIn('filter_type', ['mute', 'block'])
  56. ->pluck('filterable_id')->toArray();
  57. });
  58. $following = array_merge($following, $filters);
  59. $posts = Status::select('id', 'caption', 'profile_id')
  60. ->whereHas('media')
  61. ->whereIsNsfw(false)
  62. ->whereVisibility('public')
  63. ->whereNotIn('profile_id', $following)
  64. ->with('media')
  65. ->orderBy('created_at', 'desc')
  66. ->take(21)
  67. ->get();
  68. $res = [
  69. 'posts' => $posts->map(function($post) {
  70. return [
  71. 'url' => $post->url(),
  72. 'thumb' => $post->thumb(),
  73. ];
  74. })
  75. ];
  76. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  77. }
  78. public function discoverPosts(Request $request)
  79. {
  80. $profile = Auth::user()->profile;
  81. $pid = $profile->id;
  82. $following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(15), function() use ($pid) {
  83. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  84. });
  85. $filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(15), function() use($pid) {
  86. $private = Profile::whereIsPrivate(true)
  87. ->orWhere('unlisted', true)
  88. ->orWhere('status', '!=', null)
  89. ->pluck('id')
  90. ->toArray();
  91. $filters = UserFilter::whereUserId($pid)
  92. ->whereFilterableType('App\Profile')
  93. ->whereIn('filter_type', ['mute', 'block'])
  94. ->pluck('filterable_id')
  95. ->toArray();
  96. return array_merge($private, $filters);
  97. });
  98. $following = array_merge($following, $filters);
  99. $posts = Status::select('id', 'caption', 'profile_id')
  100. ->whereNull('uri')
  101. ->whereHas('media')
  102. ->whereHas('profile', function($q) {
  103. return $q->whereNull('status');
  104. })
  105. ->whereIsNsfw(false)
  106. ->whereVisibility('public')
  107. ->whereNotIn('profile_id', $following)
  108. ->with('media')
  109. ->orderBy('created_at', 'desc')
  110. ->take(21)
  111. ->get();
  112. $res = [
  113. 'posts' => $posts->map(function($post) {
  114. return [
  115. 'url' => $post->url(),
  116. 'thumb' => $post->thumb(),
  117. ];
  118. })
  119. ];
  120. return response()->json($res);
  121. }
  122. public function directMessage(Request $request, $profileId, $threadId)
  123. {
  124. $profile = Auth::user()->profile;
  125. if($profileId != $profile->id) {
  126. abort(403);
  127. }
  128. $msg = DirectMessage::whereToId($profile->id)
  129. ->orWhere('from_id',$profile->id)
  130. ->findOrFail($threadId);
  131. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  132. ->whereIn('from_id', [$profile->id,$msg->from_id])
  133. ->orderBy('created_at', 'asc')
  134. ->paginate(30);
  135. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  136. }
  137. public function notificationMarkAllRead(Request $request)
  138. {
  139. $profile = Auth::user()->profile;
  140. $notifications = Notification::whereProfileId($profile->id)->get();
  141. foreach($notifications as $n) {
  142. $n->read_at = Carbon::now();
  143. $n->save();
  144. }
  145. return;
  146. }
  147. public function statusReplies(Request $request, int $id)
  148. {
  149. $parent = Status::findOrFail($id);
  150. $children = Status::whereInReplyToId($parent->id)
  151. ->orderBy('created_at', 'desc')
  152. ->take(3)
  153. ->get();
  154. $resource = new Fractal\Resource\Collection($children, new StatusTransformer());
  155. $res = $this->fractal->createData($resource)->toArray();
  156. return response()->json($res);
  157. }
  158. public function stories(Request $request)
  159. {
  160. }
  161. public function discoverCategories(Request $request)
  162. {
  163. $categories = DiscoverCategory::whereActive(true)->orderBy('order')->take(10)->get();
  164. $res = $categories->map(function($item) {
  165. return [
  166. 'name' => $item->name,
  167. 'url' => $item->url(),
  168. 'thumb' => $item->thumb()
  169. ];
  170. });
  171. return response()->json($res);
  172. }
  173. public function modAction(Request $request)
  174. {
  175. abort_unless(Auth::user()->is_admin, 403);
  176. $this->validate($request, [
  177. 'action' => [
  178. 'required',
  179. 'string',
  180. Rule::in([
  181. 'autocw',
  182. 'noautolink',
  183. 'unlisted',
  184. 'disable',
  185. 'suspend'
  186. ])
  187. ],
  188. 'item_id' => 'required|integer|min:1',
  189. 'item_type' => [
  190. 'required',
  191. 'string',
  192. Rule::in(['status'])
  193. ]
  194. ]);
  195. $action = $request->input('action');
  196. $item_id = $request->input('item_id');
  197. $item_type = $request->input('item_type');
  198. switch($action) {
  199. case 'autocw':
  200. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  201. $profile->cw = true;
  202. $profile->save();
  203. break;
  204. case 'noautolink':
  205. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  206. $profile->no_autolink = true;
  207. $profile->save();
  208. break;
  209. case 'unlisted':
  210. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  211. $profile->unlisted = true;
  212. $profile->save();
  213. break;
  214. case 'disable':
  215. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  216. $user = $profile->user;
  217. $profile->status = 'disabled';
  218. $user->status = 'disabled';
  219. $profile->save();
  220. $user->save();
  221. break;
  222. case 'suspend':
  223. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  224. $user = $profile->user;
  225. $profile->status = 'suspended';
  226. $user->status = 'suspended';
  227. $profile->save();
  228. $user->save();
  229. break;
  230. default:
  231. # code...
  232. break;
  233. }
  234. Cache::forget('profiles:private');
  235. return ['msg' => 200];
  236. }
  237. public function composePost(Request $request)
  238. {
  239. $this->validate($request, [
  240. 'caption' => 'nullable|string',
  241. 'media.*' => 'required',
  242. 'media.*.id' => 'required|integer|min:1',
  243. 'media.*.filter_class' => 'nullable|alpha_dash|max:30',
  244. 'media.*.license' => 'nullable|string|max:80',
  245. 'cw' => 'nullable|boolean',
  246. 'visibility' => 'required|string|in:public,private,unlisted|min:2|max:10'
  247. ]);
  248. if(config('costar.enabled') == true) {
  249. $blockedKeywords = config('costar.keyword.block');
  250. if($blockedKeywords !== null && $request->caption) {
  251. $keywords = config('costar.keyword.block');
  252. foreach($keywords as $kw) {
  253. if(Str::contains($request->caption, $kw) == true) {
  254. abort(400, 'Invalid object');
  255. }
  256. }
  257. }
  258. }
  259. $profile = Auth::user()->profile;
  260. $visibility = $request->input('visibility');
  261. $medias = $request->input('media');
  262. $attachments = [];
  263. $status = new Status;
  264. $mimes = [];
  265. $cw = $request->input('cw');
  266. foreach($medias as $k => $media) {
  267. if($k + 1 > config('pixelfed.max_album_length')) {
  268. continue;
  269. }
  270. $m = Media::findOrFail($media['id']);
  271. if($m->profile_id !== $profile->id || $m->status_id) {
  272. abort(403, 'Invalid media id');
  273. }
  274. $m->filter_class = in_array($media['filter_class'], Filter::classes()) ? $media['filter_class'] : null;
  275. $m->license = $media['license'];
  276. $m->caption = isset($media['alt']) ? strip_tags($media['alt']) : null;
  277. $m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
  278. if($cw == true || $profile->cw == true) {
  279. $m->is_nsfw = $cw;
  280. $status->is_nsfw = $cw;
  281. }
  282. $m->save();
  283. $attachments[] = $m;
  284. array_push($mimes, $m->mime);
  285. }
  286. $status->caption = strip_tags($request->caption);
  287. $status->scope = 'draft';
  288. $status->profile_id = $profile->id;
  289. $status->save();
  290. foreach($attachments as $media) {
  291. $media->status_id = $status->id;
  292. $media->save();
  293. }
  294. $visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility;
  295. $cw = $profile->cw == true ? true : $cw;
  296. $status->is_nsfw = $cw;
  297. $status->visibility = $visibility;
  298. $status->scope = $visibility;
  299. $status->type = StatusController::mimeTypeCheck($mimes);
  300. $status->save();
  301. NewStatusPipeline::dispatch($status);
  302. Cache::forget('user:account:id:'.$profile->user_id);
  303. return $status->url();
  304. }
  305. }