InternalApiController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\{
  5. AccountInterstitial,
  6. Bookmark,
  7. DirectMessage,
  8. DiscoverCategory,
  9. Hashtag,
  10. Follower,
  11. Like,
  12. Media,
  13. MediaTag,
  14. Notification,
  15. Profile,
  16. StatusHashtag,
  17. Status,
  18. User,
  19. UserFilter,
  20. };
  21. use Auth,Cache;
  22. use Illuminate\Support\Facades\Redis;
  23. use Carbon\Carbon;
  24. use League\Fractal;
  25. use App\Transformer\Api\{
  26. AccountTransformer,
  27. StatusTransformer,
  28. // StatusMediaContainerTransformer,
  29. };
  30. use App\Util\Media\Filter;
  31. use App\Jobs\StatusPipeline\NewStatusPipeline;
  32. use App\Jobs\ModPipeline\HandleSpammerPipeline;
  33. use League\Fractal\Serializer\ArraySerializer;
  34. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  35. use Illuminate\Validation\Rule;
  36. use Illuminate\Support\Str;
  37. use App\Services\MediaTagService;
  38. use App\Services\ModLogService;
  39. use App\Services\PublicTimelineService;
  40. use App\Services\SnowflakeService;
  41. use App\Services\StatusService;
  42. use App\Services\UserFilterService;
  43. use App\Services\DiscoverService;
  44. use App\Services\BookmarkService;
  45. class InternalApiController extends Controller
  46. {
  47. protected $fractal;
  48. public function __construct()
  49. {
  50. $this->middleware('auth');
  51. $this->fractal = new Fractal\Manager();
  52. $this->fractal->setSerializer(new ArraySerializer());
  53. }
  54. // deprecated v2 compose api
  55. public function compose(Request $request)
  56. {
  57. return redirect('/');
  58. }
  59. // deprecated
  60. public function discover(Request $request)
  61. {
  62. return;
  63. }
  64. public function discoverPosts(Request $request)
  65. {
  66. $pid = $request->user()->profile_id;
  67. $filters = UserFilterService::filters($pid);
  68. $forYou = DiscoverService::getForYou();
  69. $posts = $forYou->take(50)->map(function($post) {
  70. return StatusService::get($post);
  71. })
  72. ->filter(function($post) use($filters) {
  73. return $post &&
  74. isset($post['account']) &&
  75. isset($post['account']['id']) &&
  76. !in_array($post['account']['id'], $filters);
  77. })
  78. ->take(12)
  79. ->values();
  80. return response()->json(compact('posts'));
  81. }
  82. public function directMessage(Request $request, $profileId, $threadId)
  83. {
  84. $profile = Auth::user()->profile;
  85. if($profileId != $profile->id) {
  86. abort(403);
  87. }
  88. $msg = DirectMessage::whereToId($profile->id)
  89. ->orWhere('from_id',$profile->id)
  90. ->findOrFail($threadId);
  91. $thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
  92. ->whereIn('from_id', [$profile->id,$msg->from_id])
  93. ->orderBy('created_at', 'asc')
  94. ->paginate(30);
  95. return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
  96. }
  97. public function statusReplies(Request $request, int $id)
  98. {
  99. $this->validate($request, [
  100. 'limit' => 'nullable|int|min:1|max:6'
  101. ]);
  102. $parent = Status::whereScope('public')->findOrFail($id);
  103. $limit = $request->input('limit') ?? 3;
  104. $children = Status::whereInReplyToId($parent->id)
  105. ->orderBy('created_at', 'desc')
  106. ->take($limit)
  107. ->get();
  108. $resource = new Fractal\Resource\Collection($children, new StatusTransformer());
  109. $res = $this->fractal->createData($resource)->toArray();
  110. return response()->json($res);
  111. }
  112. public function stories(Request $request)
  113. {
  114. }
  115. public function discoverCategories(Request $request)
  116. {
  117. $categories = DiscoverCategory::whereActive(true)->orderBy('order')->take(10)->get();
  118. $res = $categories->map(function($item) {
  119. return [
  120. 'name' => $item->name,
  121. 'url' => $item->url(),
  122. 'thumb' => $item->thumb()
  123. ];
  124. });
  125. return response()->json($res);
  126. }
  127. public function modAction(Request $request)
  128. {
  129. abort_unless(Auth::user()->is_admin, 400);
  130. $this->validate($request, [
  131. 'action' => [
  132. 'required',
  133. 'string',
  134. Rule::in([
  135. 'addcw',
  136. 'remcw',
  137. 'unlist',
  138. 'spammer'
  139. ])
  140. ],
  141. 'item_id' => 'required|integer|min:1',
  142. 'item_type' => [
  143. 'required',
  144. 'string',
  145. Rule::in(['profile', 'status'])
  146. ]
  147. ]);
  148. $action = $request->input('action');
  149. $item_id = $request->input('item_id');
  150. $item_type = $request->input('item_type');
  151. $status = Status::findOrFail($item_id);
  152. $author = User::whereProfileId($status->profile_id)->first();
  153. abort_if($author && $author->is_admin, 422, 'Cannot moderate administrator accounts');
  154. switch($action) {
  155. case 'addcw':
  156. $status->is_nsfw = true;
  157. $status->save();
  158. ModLogService::boot()
  159. ->user(Auth::user())
  160. ->objectUid($status->profile->user_id)
  161. ->objectId($status->id)
  162. ->objectType('App\Status::class')
  163. ->action('admin.status.moderate')
  164. ->metadata([
  165. 'action' => 'cw',
  166. 'message' => 'Success!'
  167. ])
  168. ->accessLevel('admin')
  169. ->save();
  170. if($status->uri == null) {
  171. $media = $status->media;
  172. $ai = new AccountInterstitial;
  173. $ai->user_id = $status->profile->user_id;
  174. $ai->type = 'post.cw';
  175. $ai->view = 'account.moderation.post.cw';
  176. $ai->item_type = 'App\Status';
  177. $ai->item_id = $status->id;
  178. $ai->has_media = (bool) $media->count();
  179. $ai->blurhash = $media->count() ? $media->first()->blurhash : null;
  180. $ai->meta = json_encode([
  181. 'caption' => $status->caption,
  182. 'created_at' => $status->created_at,
  183. 'type' => $status->type,
  184. 'url' => $status->url(),
  185. 'is_nsfw' => $status->is_nsfw,
  186. 'scope' => $status->scope,
  187. 'reblog' => $status->reblog_of_id,
  188. 'likes_count' => $status->likes_count,
  189. 'reblogs_count' => $status->reblogs_count,
  190. ]);
  191. $ai->save();
  192. $u = $status->profile->user;
  193. $u->has_interstitial = true;
  194. $u->save();
  195. }
  196. break;
  197. case 'remcw':
  198. $status->is_nsfw = false;
  199. $status->save();
  200. ModLogService::boot()
  201. ->user(Auth::user())
  202. ->objectUid($status->profile->user_id)
  203. ->objectId($status->id)
  204. ->objectType('App\Status::class')
  205. ->action('admin.status.moderate')
  206. ->metadata([
  207. 'action' => 'remove_cw',
  208. 'message' => 'Success!'
  209. ])
  210. ->accessLevel('admin')
  211. ->save();
  212. if($status->uri == null) {
  213. $ai = AccountInterstitial::whereUserId($status->profile->user_id)
  214. ->whereType('post.cw')
  215. ->whereItemId($status->id)
  216. ->whereItemType('App\Status')
  217. ->first();
  218. $ai->delete();
  219. }
  220. break;
  221. case 'unlist':
  222. $status->scope = $status->visibility = 'unlisted';
  223. $status->save();
  224. PublicTimelineService::del($status->id);
  225. ModLogService::boot()
  226. ->user(Auth::user())
  227. ->objectUid($status->profile->user_id)
  228. ->objectId($status->id)
  229. ->objectType('App\Status::class')
  230. ->action('admin.status.moderate')
  231. ->metadata([
  232. 'action' => 'unlist',
  233. 'message' => 'Success!'
  234. ])
  235. ->accessLevel('admin')
  236. ->save();
  237. if($status->uri == null) {
  238. $media = $status->media;
  239. $ai = new AccountInterstitial;
  240. $ai->user_id = $status->profile->user_id;
  241. $ai->type = 'post.unlist';
  242. $ai->view = 'account.moderation.post.unlist';
  243. $ai->item_type = 'App\Status';
  244. $ai->item_id = $status->id;
  245. $ai->has_media = (bool) $media->count();
  246. $ai->blurhash = $media->count() ? $media->first()->blurhash : null;
  247. $ai->meta = json_encode([
  248. 'caption' => $status->caption,
  249. 'created_at' => $status->created_at,
  250. 'type' => $status->type,
  251. 'url' => $status->url(),
  252. 'is_nsfw' => $status->is_nsfw,
  253. 'scope' => $status->scope,
  254. 'reblog' => $status->reblog_of_id,
  255. 'likes_count' => $status->likes_count,
  256. 'reblogs_count' => $status->reblogs_count,
  257. ]);
  258. $ai->save();
  259. $u = $status->profile->user;
  260. $u->has_interstitial = true;
  261. $u->save();
  262. }
  263. break;
  264. case 'spammer':
  265. HandleSpammerPipeline::dispatch($status->profile);
  266. ModLogService::boot()
  267. ->user(Auth::user())
  268. ->objectUid($status->profile->user_id)
  269. ->objectId($status->id)
  270. ->objectType('App\User::class')
  271. ->action('admin.status.moderate')
  272. ->metadata([
  273. 'action' => 'spammer',
  274. 'message' => 'Success!'
  275. ])
  276. ->accessLevel('admin')
  277. ->save();
  278. break;
  279. }
  280. StatusService::del($status->id, true);
  281. return ['msg' => 200];
  282. }
  283. public function composePost(Request $request)
  284. {
  285. abort(400, 'Endpoint deprecated');
  286. }
  287. public function bookmarks(Request $request)
  288. {
  289. $pid = $request->user()->profile_id;
  290. $res = Bookmark::whereProfileId($pid)
  291. ->orderByDesc('created_at')
  292. ->simplePaginate(10)
  293. ->map(function($bookmark) use($pid) {
  294. $status = StatusService::get($bookmark->status_id, false);
  295. $status['bookmarked_at'] = $bookmark->created_at->format('c');
  296. if($status) {
  297. BookmarkService::add($pid, $status['id']);
  298. }
  299. return $status;
  300. })
  301. ->filter(function($bookmark) {
  302. return $bookmark && isset($bookmark['id']);
  303. })
  304. ->values();
  305. return response()->json($res);
  306. }
  307. public function accountStatuses(Request $request, $id)
  308. {
  309. $this->validate($request, [
  310. 'only_media' => 'nullable',
  311. 'pinned' => 'nullable',
  312. 'exclude_replies' => 'nullable',
  313. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  314. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  315. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  316. 'limit' => 'nullable|integer|min:1|max:24'
  317. ]);
  318. $profile = Profile::whereNull('status')->findOrFail($id);
  319. $limit = $request->limit ?? 9;
  320. $max_id = $request->max_id;
  321. $min_id = $request->min_id;
  322. $scope = $request->only_media == true ?
  323. ['photo', 'photo:album', 'video', 'video:album'] :
  324. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  325. if($profile->is_private) {
  326. if(!Auth::check()) {
  327. return response()->json([]);
  328. }
  329. $pid = Auth::user()->profile->id;
  330. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  331. $following = Follower::whereProfileId($pid)->pluck('following_id');
  332. return $following->push($pid)->toArray();
  333. });
  334. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  335. } else {
  336. if(Auth::check()) {
  337. $pid = Auth::user()->profile->id;
  338. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  339. $following = Follower::whereProfileId($pid)->pluck('following_id');
  340. return $following->push($pid)->toArray();
  341. });
  342. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  343. } else {
  344. $visibility = ['public', 'unlisted'];
  345. }
  346. }
  347. $dir = $min_id ? '>' : '<';
  348. $id = $min_id ?? $max_id;
  349. $timeline = Status::select(
  350. 'id',
  351. 'uri',
  352. 'caption',
  353. 'rendered',
  354. 'profile_id',
  355. 'type',
  356. 'in_reply_to_id',
  357. 'reblog_of_id',
  358. 'is_nsfw',
  359. 'likes_count',
  360. 'reblogs_count',
  361. 'scope',
  362. 'local',
  363. 'created_at',
  364. 'updated_at'
  365. )->whereProfileId($profile->id)
  366. ->whereIn('type', $scope)
  367. ->where('id', $dir, $id)
  368. ->whereIn('visibility', $visibility)
  369. ->latest()
  370. ->limit($limit)
  371. ->get();
  372. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  373. $res = $this->fractal->createData($resource)->toArray();
  374. return response()->json($res);
  375. }
  376. public function remoteProfile(Request $request, $id)
  377. {
  378. return redirect('/i/web/profile/' . $id);
  379. }
  380. public function remoteStatus(Request $request, $profileId, $statusId)
  381. {
  382. return redirect('/i/web/post/' . $statusId);
  383. }
  384. public function requestEmailVerification(Request $request)
  385. {
  386. $pid = $request->user()->profile_id;
  387. $exists = Redis::sismember('email:manual', $pid);
  388. return view('account.email.request_verification', compact('exists'));
  389. }
  390. public function requestEmailVerificationStore(Request $request)
  391. {
  392. $pid = $request->user()->profile_id;
  393. Redis::sadd('email:manual', $pid);
  394. return redirect('/i/verify-email')->with(['status' => 'Successfully sent manual verification request!']);
  395. }
  396. }