InternalApiController.php 12 KB

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