InternalApiController.php 11 KB

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