InternalApiController.php 15 KB

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