InternalApiController.php 12 KB

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