InternalApiController.php 15 KB

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