InternalApiController.php 11 KB

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