InternalApiController.php 15 KB

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