AdminReportController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Cache;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Redis;
  7. use App\Services\AccountService;
  8. use App\Services\StatusService;
  9. use App\{
  10. AccountInterstitial,
  11. Contact,
  12. Hashtag,
  13. Newsroom,
  14. OauthClient,
  15. Profile,
  16. Report,
  17. Status,
  18. Story,
  19. User
  20. };
  21. use Illuminate\Validation\Rule;
  22. use App\Services\StoryService;
  23. trait AdminReportController
  24. {
  25. public function reports(Request $request)
  26. {
  27. $filter = $request->input('filter') == 'closed' ? 'closed' : 'open';
  28. $page = $request->input('page') ?? 1;
  29. $ai = Cache::remember('admin-dash:reports:ai-count', 3600, function() {
  30. return AccountInterstitial::whereNotNull('appeal_requested_at')->whereNull('appeal_handled_at')->count();
  31. });
  32. $spam = Cache::remember('admin-dash:reports:spam-count', 3600, function() {
  33. return AccountInterstitial::whereType('post.autospam')->whereNull('appeal_handled_at')->count();
  34. });
  35. $mailVerifications = Redis::scard('email:manual');
  36. if($filter == 'open' && $page == 1) {
  37. $reports = Cache::remember('admin-dash:reports:list-cache', 300, function() use($page, $filter) {
  38. return Report::whereHas('status')
  39. ->whereHas('reportedUser')
  40. ->whereHas('reporter')
  41. ->orderBy('created_at','desc')
  42. ->when($filter, function($q, $filter) {
  43. return $filter == 'open' ?
  44. $q->whereNull('admin_seen') :
  45. $q->whereNotNull('admin_seen');
  46. })
  47. ->paginate(6);
  48. });
  49. } else {
  50. $reports = Report::whereHas('status')
  51. ->whereHas('reportedUser')
  52. ->whereHas('reporter')
  53. ->orderBy('created_at','desc')
  54. ->when($filter, function($q, $filter) {
  55. return $filter == 'open' ?
  56. $q->whereNull('admin_seen') :
  57. $q->whereNotNull('admin_seen');
  58. })
  59. ->paginate(6);
  60. }
  61. return view('admin.reports.home', compact('reports', 'ai', 'spam', 'mailVerifications'));
  62. }
  63. public function showReport(Request $request, $id)
  64. {
  65. $report = Report::findOrFail($id);
  66. return view('admin.reports.show', compact('report'));
  67. }
  68. public function appeals(Request $request)
  69. {
  70. $appeals = AccountInterstitial::whereNotNull('appeal_requested_at')
  71. ->whereNull('appeal_handled_at')
  72. ->latest()
  73. ->paginate(6);
  74. return view('admin.reports.appeals', compact('appeals'));
  75. }
  76. public function showAppeal(Request $request, $id)
  77. {
  78. $appeal = AccountInterstitial::whereNotNull('appeal_requested_at')
  79. ->whereNull('appeal_handled_at')
  80. ->findOrFail($id);
  81. $meta = json_decode($appeal->meta);
  82. return view('admin.reports.show_appeal', compact('appeal', 'meta'));
  83. }
  84. public function spam(Request $request)
  85. {
  86. $this->validate($request, [
  87. 'tab' => 'sometimes|in:home,not-spam,spam,settings,custom,exemptions'
  88. ]);
  89. $tab = $request->input('tab', 'home');
  90. $openCount = Cache::remember('admin-dash:reports:spam-count', 3600, function() {
  91. return AccountInterstitial::whereType('post.autospam')
  92. ->whereNull('appeal_handled_at')
  93. ->count();
  94. });
  95. $monthlyCount = Cache::remember('admin-dash:reports:spam-count:30d', 43200, function() {
  96. return AccountInterstitial::whereType('post.autospam')
  97. ->where('created_at', '>', now()->subMonth())
  98. ->count();
  99. });
  100. $totalCount = Cache::remember('admin-dash:reports:spam-count:total', 43200, function() {
  101. return AccountInterstitial::whereType('post.autospam')->count();
  102. });
  103. $uncategorized = Cache::remember('admin-dash:reports:spam-sync', 3600, function() {
  104. return AccountInterstitial::whereType('post.autospam')
  105. ->whereIsSpam(null)
  106. ->whereNotNull('appeal_handled_at')
  107. ->exists();
  108. });
  109. $avg = Cache::remember('admin-dash:reports:spam-count:avg', 43200, function() {
  110. if(config('database.default') != 'mysql') {
  111. return 0;
  112. }
  113. return AccountInterstitial::selectRaw('*, count(id) as counter')
  114. ->whereType('post.autospam')
  115. ->groupBy('user_id')
  116. ->get()
  117. ->avg('counter');
  118. });
  119. $avgOpen = Cache::remember('admin-dash:reports:spam-count:avgopen', 43200, function() {
  120. if(config('database.default') != 'mysql') {
  121. return "0";
  122. }
  123. $seconds = AccountInterstitial::selectRaw('DATE(created_at) AS start_date, AVG(TIME_TO_SEC(TIMEDIFF(appeal_handled_at, created_at))) AS timediff')->whereType('post.autospam')->whereNotNull('appeal_handled_at')->where('created_at', '>', now()->subMonth())->get();
  124. if(!$seconds) {
  125. return "0";
  126. }
  127. $mins = floor($seconds->avg('timediff') / 60);
  128. if($mins < 60) {
  129. return $mins . ' min(s)';
  130. }
  131. if($mins < 2880) {
  132. return floor($mins / 60) . ' hour(s)';
  133. }
  134. return floor($mins / 60 / 24) . ' day(s)';
  135. });
  136. $avgCount = $totalCount && $avg ? floor($totalCount / $avg) : "0";
  137. if(in_array($tab, ['home', 'spam', 'not-spam'])) {
  138. $appeals = AccountInterstitial::whereType('post.autospam')
  139. ->when($tab, function($q, $tab) {
  140. switch($tab) {
  141. case 'home':
  142. return $q->whereNull('appeal_handled_at');
  143. break;
  144. case 'spam':
  145. return $q->whereIsSpam(true);
  146. break;
  147. case 'not-spam':
  148. return $q->whereIsSpam(false);
  149. break;
  150. }
  151. })
  152. ->latest()
  153. ->paginate(6);
  154. if($tab !== 'home') {
  155. $appeals = $appeals->appends(['tab' => $tab]);
  156. }
  157. } else {
  158. $appeals = new class {
  159. public function count() {
  160. return 0;
  161. }
  162. public function render() {
  163. return;
  164. }
  165. };
  166. }
  167. return view('admin.reports.spam', compact('tab', 'appeals', 'openCount', 'monthlyCount', 'totalCount', 'avgCount', 'avgOpen', 'uncategorized'));
  168. }
  169. public function showSpam(Request $request, $id)
  170. {
  171. $appeal = AccountInterstitial::whereType('post.autospam')
  172. ->findOrFail($id);
  173. $meta = json_decode($appeal->meta);
  174. return view('admin.reports.show_spam', compact('appeal', 'meta'));
  175. }
  176. public function fixUncategorizedSpam(Request $request)
  177. {
  178. if(Cache::get('admin-dash:reports:spam-sync-active')) {
  179. return redirect('/i/admin/reports/autospam');
  180. }
  181. Cache::put('admin-dash:reports:spam-sync-active', 1, 900);
  182. AccountInterstitial::chunk(500, function($reports) {
  183. foreach($reports as $report) {
  184. if($report->item_type != 'App\Status') {
  185. continue;
  186. }
  187. if($report->type != 'post.autospam') {
  188. continue;
  189. }
  190. if($report->is_spam != null) {
  191. continue;
  192. }
  193. $status = StatusService::get($report->item_id, false);
  194. if(!$status) {
  195. return;
  196. }
  197. $scope = $status['visibility'];
  198. $report->is_spam = $scope == 'unlisted';
  199. $report->in_violation = $report->is_spam;
  200. $report->severity_index = 1;
  201. $report->save();
  202. }
  203. });
  204. Cache::forget('admin-dash:reports:spam-sync');
  205. return redirect('/i/admin/reports/autospam');
  206. }
  207. public function updateSpam(Request $request, $id)
  208. {
  209. $this->validate($request, [
  210. 'action' => 'required|in:dismiss,approve,dismiss-all,approve-all'
  211. ]);
  212. $action = $request->input('action');
  213. $appeal = AccountInterstitial::whereType('post.autospam')
  214. ->whereNull('appeal_handled_at')
  215. ->findOrFail($id);
  216. $meta = json_decode($appeal->meta);
  217. $res = ['status' => 'success'];
  218. $now = now();
  219. Cache::forget('admin-dash:reports:spam-count:total');
  220. Cache::forget('admin-dash:reports:spam-count:30d');
  221. if($action == 'dismiss') {
  222. $appeal->is_spam = true;
  223. $appeal->appeal_handled_at = $now;
  224. $appeal->save();
  225. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  226. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  227. Cache::forget('admin-dash:reports:spam-count');
  228. return $res;
  229. }
  230. if($action == 'dismiss-all') {
  231. AccountInterstitial::whereType('post.autospam')
  232. ->whereItemType('App\Status')
  233. ->whereNull('appeal_handled_at')
  234. ->whereUserId($appeal->user_id)
  235. ->update(['appeal_handled_at' => $now, 'is_spam' => true]);
  236. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  237. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  238. Cache::forget('admin-dash:reports:spam-count');
  239. return $res;
  240. }
  241. if($action == 'approve-all') {
  242. AccountInterstitial::whereType('post.autospam')
  243. ->whereItemType('App\Status')
  244. ->whereNull('appeal_handled_at')
  245. ->whereUserId($appeal->user_id)
  246. ->get()
  247. ->each(function($report) use($meta) {
  248. $report->is_spam = false;
  249. $report->appeal_handled_at = now();
  250. $report->save();
  251. $status = Status::find($report->item_id);
  252. if($status) {
  253. $status->is_nsfw = $meta->is_nsfw;
  254. $status->scope = 'public';
  255. $status->visibility = 'public';
  256. $status->save();
  257. StatusService::del($status->id);
  258. }
  259. });
  260. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  261. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  262. Cache::forget('admin-dash:reports:spam-count');
  263. return $res;
  264. }
  265. $status = $appeal->status;
  266. $status->is_nsfw = $meta->is_nsfw;
  267. $status->scope = 'public';
  268. $status->visibility = 'public';
  269. $status->save();
  270. $appeal->is_spam = false;
  271. $appeal->appeal_handled_at = now();
  272. $appeal->save();
  273. StatusService::del($status->id);
  274. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  275. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  276. Cache::forget('admin-dash:reports:spam-count');
  277. return $res;
  278. }
  279. public function updateAppeal(Request $request, $id)
  280. {
  281. $this->validate($request, [
  282. 'action' => 'required|in:dismiss,approve'
  283. ]);
  284. $action = $request->input('action');
  285. $appeal = AccountInterstitial::whereNotNull('appeal_requested_at')
  286. ->whereNull('appeal_handled_at')
  287. ->findOrFail($id);
  288. if($action == 'dismiss') {
  289. $appeal->appeal_handled_at = now();
  290. $appeal->save();
  291. Cache::forget('admin-dash:reports:ai-count');
  292. return redirect('/i/admin/reports/appeals');
  293. }
  294. switch ($appeal->type) {
  295. case 'post.cw':
  296. $status = $appeal->status;
  297. $status->is_nsfw = false;
  298. $status->save();
  299. break;
  300. case 'post.unlist':
  301. $status = $appeal->status;
  302. $status->scope = 'public';
  303. $status->visibility = 'public';
  304. $status->save();
  305. break;
  306. default:
  307. # code...
  308. break;
  309. }
  310. $appeal->appeal_handled_at = now();
  311. $appeal->save();
  312. StatusService::del($status->id);
  313. Cache::forget('admin-dash:reports:ai-count');
  314. return redirect('/i/admin/reports/appeals');
  315. }
  316. public function updateReport(Request $request, $id)
  317. {
  318. $this->validate($request, [
  319. 'action' => 'required|string',
  320. ]);
  321. $action = $request->input('action');
  322. $actions = [
  323. 'ignore',
  324. 'cw',
  325. 'unlist',
  326. 'delete',
  327. 'shadowban',
  328. 'ban',
  329. ];
  330. if (!in_array($action, $actions)) {
  331. return abort(403);
  332. }
  333. $report = Report::findOrFail($id);
  334. $this->handleReportAction($report, $action);
  335. Cache::forget('admin-dash:reports:list-cache');
  336. return response()->json(['msg'=> 'Success']);
  337. }
  338. public function handleReportAction(Report $report, $action)
  339. {
  340. $item = $report->reported();
  341. $report->admin_seen = Carbon::now();
  342. switch ($action) {
  343. case 'ignore':
  344. $report->not_interested = true;
  345. break;
  346. case 'cw':
  347. Cache::forget('status:thumb:'.$item->id);
  348. $item->is_nsfw = true;
  349. $item->save();
  350. $report->nsfw = true;
  351. StatusService::del($item->id);
  352. break;
  353. case 'unlist':
  354. $item->visibility = 'unlisted';
  355. $item->save();
  356. Cache::forget('profiles:private');
  357. StatusService::del($item->id);
  358. break;
  359. case 'delete':
  360. // Todo: fire delete job
  361. $report->admin_seen = null;
  362. StatusService::del($item->id);
  363. break;
  364. case 'shadowban':
  365. // Todo: fire delete job
  366. $report->admin_seen = null;
  367. break;
  368. case 'ban':
  369. // Todo: fire delete job
  370. $report->admin_seen = null;
  371. break;
  372. default:
  373. $report->admin_seen = null;
  374. break;
  375. }
  376. $report->save();
  377. return $this;
  378. }
  379. protected function actionMap()
  380. {
  381. return [
  382. '1' => 'ignore',
  383. '2' => 'cw',
  384. '3' => 'unlist',
  385. '4' => 'delete',
  386. '5' => 'shadowban',
  387. '6' => 'ban'
  388. ];
  389. }
  390. public function bulkUpdateReport(Request $request)
  391. {
  392. $this->validate($request, [
  393. 'action' => 'required|integer|min:1|max:10',
  394. 'ids' => 'required|array'
  395. ]);
  396. $action = $this->actionMap()[$request->input('action')];
  397. $ids = $request->input('ids');
  398. $reports = Report::whereIn('id', $ids)->whereNull('admin_seen')->get();
  399. foreach($reports as $report) {
  400. $this->handleReportAction($report, $action);
  401. }
  402. $res = [
  403. 'message' => 'Success',
  404. 'code' => 200
  405. ];
  406. return response()->json($res);
  407. }
  408. public function reportMailVerifications(Request $request)
  409. {
  410. $ids = Redis::smembers('email:manual');
  411. $ignored = Redis::smembers('email:manual-ignored');
  412. $reports = [];
  413. if($ids) {
  414. $reports = collect($ids)
  415. ->filter(function($id) use($ignored) {
  416. return !in_array($id, $ignored);
  417. })
  418. ->map(function($id) {
  419. $account = AccountService::get($id);
  420. $user = User::whereProfileId($id)->first();
  421. if(!$user) {
  422. return [];
  423. }
  424. $account['email'] = $user->email;
  425. return $account;
  426. })
  427. ->filter(function($res) {
  428. return isset($res['id']);
  429. })
  430. ->values();
  431. }
  432. return view('admin.reports.mail_verification', compact('reports', 'ignored'));
  433. }
  434. public function reportMailVerifyIgnore(Request $request)
  435. {
  436. $id = $request->input('id');
  437. Redis::sadd('email:manual-ignored', $id);
  438. return redirect('/i/admin/reports');
  439. }
  440. public function reportMailVerifyApprove(Request $request)
  441. {
  442. $id = $request->input('id');
  443. $user = User::whereProfileId($id)->firstOrFail();
  444. Redis::srem('email:manual', $id);
  445. Redis::srem('email:manual-ignored', $id);
  446. $user->email_verified_at = now();
  447. $user->save();
  448. return redirect('/i/admin/reports');
  449. }
  450. public function reportMailVerifyClearIgnored(Request $request)
  451. {
  452. Redis::del('email:manual-ignored');
  453. return [200];
  454. }
  455. }