InternalApiController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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(
  100. 'id',
  101. 'caption',
  102. 'profile_id',
  103. 'type'
  104. )
  105. ->whereNull('uri')
  106. ->whereHas('media')
  107. ->whereHas('profile', function($q) {
  108. return $q->whereNull('status');
  109. })
  110. ->whereIsNsfw(false)
  111. ->whereVisibility('public')
  112. ->whereNotIn('profile_id', $following)
  113. ->with('media')
  114. ->orderBy('created_at', 'desc')
  115. ->take(21)
  116. ->get();
  117. $res = [
  118. 'posts' => $posts->map(function($post) {
  119. return [
  120. 'type' => $post->type,
  121. 'url' => $post->url(),
  122. 'thumb' => $post->thumb(),
  123. ];
  124. })
  125. ];
  126. return response()->json($res);
  127. }
  128. public function directMessage(Request $request, $profileId, $threadId)
  129. {
  130. $profile = Auth::user()->profile;
  131. if($profileId != $profile->id) {
  132. abort(403);
  133. }
  134. $msg = DirectMessage::whereToId($profile->id)
  135. ->orWhere('from_id',$profile->id)
  136. ->findOrFail($threadId);
  137. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  138. ->whereIn('from_id', [$profile->id,$msg->from_id])
  139. ->orderBy('created_at', 'asc')
  140. ->paginate(30);
  141. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  142. }
  143. public function notificationMarkAllRead(Request $request)
  144. {
  145. $profile = Auth::user()->profile;
  146. $notifications = Notification::whereProfileId($profile->id)->get();
  147. foreach($notifications as $n) {
  148. $n->read_at = Carbon::now();
  149. $n->save();
  150. }
  151. return;
  152. }
  153. public function statusReplies(Request $request, int $id)
  154. {
  155. $parent = Status::findOrFail($id);
  156. $children = Status::whereInReplyToId($parent->id)
  157. ->orderBy('created_at', 'desc')
  158. ->take(3)
  159. ->get();
  160. $resource = new Fractal\Resource\Collection($children, new StatusTransformer());
  161. $res = $this->fractal->createData($resource)->toArray();
  162. return response()->json($res);
  163. }
  164. public function stories(Request $request)
  165. {
  166. }
  167. public function discoverCategories(Request $request)
  168. {
  169. $categories = DiscoverCategory::whereActive(true)->orderBy('order')->take(10)->get();
  170. $res = $categories->map(function($item) {
  171. return [
  172. 'name' => $item->name,
  173. 'url' => $item->url(),
  174. 'thumb' => $item->thumb()
  175. ];
  176. });
  177. return response()->json($res);
  178. }
  179. public function modAction(Request $request)
  180. {
  181. abort_unless(Auth::user()->is_admin, 403);
  182. $this->validate($request, [
  183. 'action' => [
  184. 'required',
  185. 'string',
  186. Rule::in([
  187. 'autocw',
  188. 'noautolink',
  189. 'unlisted',
  190. 'disable',
  191. 'suspend'
  192. ])
  193. ],
  194. 'item_id' => 'required|integer|min:1',
  195. 'item_type' => [
  196. 'required',
  197. 'string',
  198. Rule::in(['status'])
  199. ]
  200. ]);
  201. $action = $request->input('action');
  202. $item_id = $request->input('item_id');
  203. $item_type = $request->input('item_type');
  204. switch($action) {
  205. case 'autocw':
  206. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  207. $profile->cw = true;
  208. $profile->save();
  209. break;
  210. case 'noautolink':
  211. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  212. $profile->no_autolink = true;
  213. $profile->save();
  214. break;
  215. case 'unlisted':
  216. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  217. $profile->unlisted = true;
  218. $profile->save();
  219. break;
  220. case 'disable':
  221. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  222. $user = $profile->user;
  223. $profile->status = 'disabled';
  224. $user->status = 'disabled';
  225. $profile->save();
  226. $user->save();
  227. break;
  228. case 'suspend':
  229. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  230. $user = $profile->user;
  231. $profile->status = 'suspended';
  232. $user->status = 'suspended';
  233. $profile->save();
  234. $user->save();
  235. break;
  236. default:
  237. # code...
  238. break;
  239. }
  240. Cache::forget('profiles:private');
  241. return ['msg' => 200];
  242. }
  243. public function composePost(Request $request)
  244. {
  245. $this->validate($request, [
  246. 'caption' => 'nullable|string',
  247. 'media.*' => 'required',
  248. 'media.*.id' => 'required|integer|min:1',
  249. 'media.*.filter_class' => 'nullable|alpha_dash|max:30',
  250. 'media.*.license' => 'nullable|string|max:80',
  251. 'cw' => 'nullable|boolean',
  252. 'visibility' => 'required|string|in:public,private,unlisted|min:2|max:10'
  253. ]);
  254. if(config('costar.enabled') == true) {
  255. $blockedKeywords = config('costar.keyword.block');
  256. if($blockedKeywords !== null && $request->caption) {
  257. $keywords = config('costar.keyword.block');
  258. foreach($keywords as $kw) {
  259. if(Str::contains($request->caption, $kw) == true) {
  260. abort(400, 'Invalid object');
  261. }
  262. }
  263. }
  264. }
  265. $profile = Auth::user()->profile;
  266. $visibility = $request->input('visibility');
  267. $medias = $request->input('media');
  268. $attachments = [];
  269. $status = new Status;
  270. $mimes = [];
  271. $cw = $request->input('cw');
  272. foreach($medias as $k => $media) {
  273. if($k + 1 > config('pixelfed.max_album_length')) {
  274. continue;
  275. }
  276. $m = Media::findOrFail($media['id']);
  277. if($m->profile_id !== $profile->id || $m->status_id) {
  278. abort(403, 'Invalid media id');
  279. }
  280. $m->filter_class = in_array($media['filter_class'], Filter::classes()) ? $media['filter_class'] : null;
  281. $m->license = $media['license'];
  282. $m->caption = isset($media['alt']) ? strip_tags($media['alt']) : null;
  283. $m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
  284. if($cw == true || $profile->cw == true) {
  285. $m->is_nsfw = $cw;
  286. $status->is_nsfw = $cw;
  287. }
  288. $m->save();
  289. $attachments[] = $m;
  290. array_push($mimes, $m->mime);
  291. }
  292. $status->caption = strip_tags($request->caption);
  293. $status->scope = 'draft';
  294. $status->profile_id = $profile->id;
  295. $status->save();
  296. foreach($attachments as $media) {
  297. $media->status_id = $status->id;
  298. $media->save();
  299. }
  300. $visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility;
  301. $cw = $profile->cw == true ? true : $cw;
  302. $status->is_nsfw = $cw;
  303. $status->visibility = $visibility;
  304. $status->scope = $visibility;
  305. $status->type = StatusController::mimeTypeCheck($mimes);
  306. $status->save();
  307. NewStatusPipeline::dispatch($status);
  308. Cache::forget('user:account:id:'.$profile->user_id);
  309. return $status->url();
  310. }
  311. }