InternalApiController.php 11 KB

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