InternalApiController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. $people = Profile::select('id', 'name', 'username')
  60. ->with('avatar')
  61. ->whereNull('status')
  62. ->orderByRaw('rand()')
  63. ->whereHas('statuses')
  64. ->whereNull('domain')
  65. ->whereNotIn('id', $following)
  66. ->whereIsPrivate(false)
  67. ->take(3)
  68. ->get();
  69. $posts = Status::select('id', 'caption', 'profile_id')
  70. ->whereHas('media')
  71. ->whereIsNsfw(false)
  72. ->whereVisibility('public')
  73. ->whereNotIn('profile_id', $following)
  74. ->with('media')
  75. ->orderBy('created_at', 'desc')
  76. ->take(21)
  77. ->get();
  78. $res = [
  79. 'people' => $people->map(function($profile) {
  80. return [
  81. 'id' => $profile->id,
  82. 'avatar' => $profile->avatarUrl(),
  83. 'name' => $profile->name,
  84. 'username' => $profile->username,
  85. 'url' => $profile->url(),
  86. ];
  87. }),
  88. 'posts' => $posts->map(function($post) {
  89. return [
  90. 'url' => $post->url(),
  91. 'thumb' => $post->thumb(),
  92. ];
  93. })
  94. ];
  95. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  96. }
  97. public function discoverPeople(Request $request)
  98. {
  99. $profile = Auth::user()->profile;
  100. $pid = $profile->id;
  101. $following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(60), function() use ($pid) {
  102. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  103. });
  104. $filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(60), function() use($pid) {
  105. return UserFilter::whereUserId($pid)
  106. ->whereFilterableType('App\Profile')
  107. ->whereIn('filter_type', ['mute', 'block'])
  108. ->pluck('filterable_id')->toArray();
  109. });
  110. $following = array_merge($following, $filters);
  111. $people = Profile::select('id', 'name', 'username')
  112. ->with('avatar')
  113. ->orderByRaw('rand()')
  114. ->whereHas('statuses')
  115. ->whereNull('domain')
  116. ->whereNotIn('id', $following)
  117. ->whereIsPrivate(false)
  118. ->take(3)
  119. ->get();
  120. $res = [
  121. 'people' => $people->map(function($profile) {
  122. return [
  123. 'id' => $profile->id,
  124. 'avatar' => $profile->avatarUrl(),
  125. 'name' => $profile->name,
  126. 'username' => $profile->username,
  127. 'url' => $profile->url(),
  128. ];
  129. })
  130. ];
  131. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  132. }
  133. public function discoverPosts(Request $request)
  134. {
  135. $profile = Auth::user()->profile;
  136. $pid = $profile->id;
  137. $following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(15), function() use ($pid) {
  138. return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
  139. });
  140. $filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(15), function() use($pid) {
  141. $private = Profile::whereIsPrivate(true)
  142. ->orWhere('unlisted', true)
  143. ->orWhere('status', '!=', null)
  144. ->pluck('id')
  145. ->toArray();
  146. $filters = UserFilter::whereUserId($pid)
  147. ->whereFilterableType('App\Profile')
  148. ->whereIn('filter_type', ['mute', 'block'])
  149. ->pluck('filterable_id')
  150. ->toArray();
  151. return array_merge($private, $filters);
  152. });
  153. $following = array_merge($following, $filters);
  154. $posts = Status::select('id', 'caption', 'profile_id')
  155. ->whereNull('uri')
  156. ->whereHas('media')
  157. ->whereHas('profile', function($q) {
  158. return $q->whereNull('status');
  159. })
  160. ->whereIsNsfw(false)
  161. ->whereVisibility('public')
  162. ->whereNotIn('profile_id', $following)
  163. ->with('media')
  164. ->orderBy('created_at', 'desc')
  165. ->take(21)
  166. ->get();
  167. $res = [
  168. 'posts' => $posts->map(function($post) {
  169. return [
  170. 'url' => $post->url(),
  171. 'thumb' => $post->thumb(),
  172. ];
  173. })
  174. ];
  175. return response()->json($res);
  176. }
  177. public function directMessage(Request $request, $profileId, $threadId)
  178. {
  179. $profile = Auth::user()->profile;
  180. if($profileId != $profile->id) {
  181. abort(403);
  182. }
  183. $msg = DirectMessage::whereToId($profile->id)
  184. ->orWhere('from_id',$profile->id)
  185. ->findOrFail($threadId);
  186. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  187. ->whereIn('from_id', [$profile->id,$msg->from_id])
  188. ->orderBy('created_at', 'asc')
  189. ->paginate(30);
  190. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  191. }
  192. public function notificationMarkAllRead(Request $request)
  193. {
  194. $profile = Auth::user()->profile;
  195. $notifications = Notification::whereProfileId($profile->id)->get();
  196. foreach($notifications as $n) {
  197. $n->read_at = Carbon::now();
  198. $n->save();
  199. }
  200. return;
  201. }
  202. public function statusReplies(Request $request, int $id)
  203. {
  204. $parent = Status::findOrFail($id);
  205. $children = Status::whereInReplyToId($parent->id)
  206. ->orderBy('created_at', 'desc')
  207. ->take(3)
  208. ->get();
  209. $resource = new Fractal\Resource\Collection($children, new StatusTransformer());
  210. $res = $this->fractal->createData($resource)->toArray();
  211. return response()->json($res);
  212. }
  213. public function stories(Request $request)
  214. {
  215. }
  216. public function discoverCategories(Request $request)
  217. {
  218. $categories = DiscoverCategory::whereActive(true)->orderBy('order')->take(10)->get();
  219. $res = $categories->map(function($item) {
  220. return [
  221. 'name' => $item->name,
  222. 'url' => $item->url(),
  223. 'thumb' => $item->thumb()
  224. ];
  225. });
  226. return response()->json($res);
  227. }
  228. public function modAction(Request $request)
  229. {
  230. abort_unless(Auth::user()->is_admin, 403);
  231. $this->validate($request, [
  232. 'action' => [
  233. 'required',
  234. 'string',
  235. Rule::in([
  236. 'autocw',
  237. 'noautolink',
  238. 'unlisted',
  239. 'disable',
  240. 'suspend'
  241. ])
  242. ],
  243. 'item_id' => 'required|integer|min:1',
  244. 'item_type' => [
  245. 'required',
  246. 'string',
  247. Rule::in(['status'])
  248. ]
  249. ]);
  250. $action = $request->input('action');
  251. $item_id = $request->input('item_id');
  252. $item_type = $request->input('item_type');
  253. switch($action) {
  254. case 'autocw':
  255. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  256. $profile->cw = true;
  257. $profile->save();
  258. break;
  259. case 'noautolink':
  260. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  261. $profile->no_autolink = true;
  262. $profile->save();
  263. break;
  264. case 'unlisted':
  265. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  266. $profile->unlisted = true;
  267. $profile->save();
  268. break;
  269. case 'disable':
  270. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  271. $user = $profile->user;
  272. $profile->status = 'disabled';
  273. $user->status = 'disabled';
  274. $profile->save();
  275. $user->save();
  276. break;
  277. case 'suspend':
  278. $profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
  279. $user = $profile->user;
  280. $profile->status = 'suspended';
  281. $user->status = 'suspended';
  282. $profile->save();
  283. $user->save();
  284. break;
  285. default:
  286. # code...
  287. break;
  288. }
  289. return ['msg' => 200];
  290. }
  291. public function composePost(Request $request)
  292. {
  293. $this->validate($request, [
  294. 'caption' => 'nullable|string',
  295. 'media.*' => 'required',
  296. 'media.*.id' => 'required|integer|min:1',
  297. 'media.*.filter_class' => 'nullable|alpha_dash|max:30',
  298. 'media.*.license' => 'nullable|string|max:80',
  299. 'cw' => 'nullable|boolean',
  300. 'visibility' => 'required|string|in:public,private,unlisted|min:2|max:10'
  301. ]);
  302. if(config('costar.enabled') == true) {
  303. $blockedKeywords = config('costar.keyword.block');
  304. if($blockedKeywords !== null && $request->caption) {
  305. $keywords = config('costar.keyword.block');
  306. foreach($keywords as $kw) {
  307. if(Str::contains($request->caption, $kw) == true) {
  308. abort(400, 'Invalid object');
  309. }
  310. }
  311. }
  312. }
  313. $profile = Auth::user()->profile;
  314. $visibility = $request->input('visibility');
  315. $medias = $request->input('media');
  316. $attachments = [];
  317. $status = new Status;
  318. $mimes = [];
  319. $cw = $request->input('cw');
  320. foreach($medias as $k => $media) {
  321. if($k + 1 > config('pixelfed.max_album_length')) {
  322. continue;
  323. }
  324. $m = Media::findOrFail($media['id']);
  325. if($m->profile_id !== $profile->id || $m->status_id) {
  326. abort(403, 'Invalid media id');
  327. }
  328. $m->filter_class = in_array($media['filter_class'], Filter::classes()) ? $media['filter_class'] : null;
  329. $m->license = $media['license'];
  330. $m->caption = isset($media['alt']) ? strip_tags($media['alt']) : null;
  331. $m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
  332. if($cw == true || $profile->cw == true) {
  333. $m->is_nsfw = $cw;
  334. $status->is_nsfw = $cw;
  335. }
  336. $m->save();
  337. $attachments[] = $m;
  338. array_push($mimes, $m->mime);
  339. }
  340. $status->caption = strip_tags($request->caption);
  341. $status->scope = 'draft';
  342. $status->profile_id = $profile->id;
  343. $status->save();
  344. foreach($attachments as $media) {
  345. $media->status_id = $status->id;
  346. $media->save();
  347. }
  348. $visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility;
  349. $cw = $profile->cw == true ? true : $cw;
  350. $status->is_nsfw = $cw;
  351. $status->visibility = $visibility;
  352. $status->scope = $visibility;
  353. $status->type = StatusController::mimeTypeCheck($mimes);
  354. $status->save();
  355. NewStatusPipeline::dispatch($status);
  356. return $status->url();
  357. }
  358. }