AdminReportController.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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\DB;
  7. use Illuminate\Support\Facades\Redis;
  8. use App\Services\AccountService;
  9. use App\Services\StatusService;
  10. use App\{
  11. AccountInterstitial,
  12. Contact,
  13. Hashtag,
  14. Newsroom,
  15. Notification,
  16. OauthClient,
  17. Profile,
  18. Report,
  19. Status,
  20. Story,
  21. User
  22. };
  23. use Illuminate\Validation\Rule;
  24. use App\Services\StoryService;
  25. use App\Services\ModLogService;
  26. use App\Jobs\DeletePipeline\DeleteAccountPipeline;
  27. use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
  28. use App\Jobs\DeletePipeline\DeleteRemoteStatusPipeline;
  29. use App\Jobs\StatusPipeline\StatusDelete;
  30. use App\Http\Resources\AdminReport;
  31. use App\Http\Resources\AdminSpamReport;
  32. use App\Services\NotificationService;
  33. use App\Services\PublicTimelineService;
  34. use App\Services\NetworkTimelineService;
  35. trait AdminReportController
  36. {
  37. public function reports(Request $request)
  38. {
  39. $filter = $request->input('filter') == 'closed' ? 'closed' : 'open';
  40. $page = $request->input('page') ?? 1;
  41. $ai = Cache::remember('admin-dash:reports:ai-count', 3600, function() {
  42. return AccountInterstitial::whereNotNull('appeal_requested_at')->whereNull('appeal_handled_at')->count();
  43. });
  44. $spam = Cache::remember('admin-dash:reports:spam-count', 3600, function() {
  45. return AccountInterstitial::whereType('post.autospam')->whereNull('appeal_handled_at')->count();
  46. });
  47. $mailVerifications = Redis::scard('email:manual');
  48. if($filter == 'open' && $page == 1) {
  49. $reports = Cache::remember('admin-dash:reports:list-cache', 300, function() use($page, $filter) {
  50. return 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. } else {
  62. $reports = Report::whereHas('status')
  63. ->whereHas('reportedUser')
  64. ->whereHas('reporter')
  65. ->orderBy('created_at','desc')
  66. ->when($filter, function($q, $filter) {
  67. return $filter == 'open' ?
  68. $q->whereNull('admin_seen') :
  69. $q->whereNotNull('admin_seen');
  70. })
  71. ->paginate(6);
  72. }
  73. return view('admin.reports.home', compact('reports', 'ai', 'spam', 'mailVerifications'));
  74. }
  75. public function showReport(Request $request, $id)
  76. {
  77. $report = Report::with('status')->findOrFail($id);
  78. if($request->has('ref') && $request->input('ref') == 'email') {
  79. return redirect('/i/admin/reports?tab=report&id=' . $report->id);
  80. }
  81. return view('admin.reports.show', compact('report'));
  82. }
  83. public function appeals(Request $request)
  84. {
  85. $appeals = AccountInterstitial::whereNotNull('appeal_requested_at')
  86. ->whereNull('appeal_handled_at')
  87. ->latest()
  88. ->paginate(6);
  89. return view('admin.reports.appeals', compact('appeals'));
  90. }
  91. public function showAppeal(Request $request, $id)
  92. {
  93. $appeal = AccountInterstitial::whereNotNull('appeal_requested_at')
  94. ->whereNull('appeal_handled_at')
  95. ->findOrFail($id);
  96. $meta = json_decode($appeal->meta);
  97. return view('admin.reports.show_appeal', compact('appeal', 'meta'));
  98. }
  99. public function spam(Request $request)
  100. {
  101. $this->validate($request, [
  102. 'tab' => 'sometimes|in:home,not-spam,spam,settings,custom,exemptions'
  103. ]);
  104. $tab = $request->input('tab', 'home');
  105. $openCount = Cache::remember('admin-dash:reports:spam-count', 3600, function() {
  106. return AccountInterstitial::whereType('post.autospam')
  107. ->whereNull('appeal_handled_at')
  108. ->count();
  109. });
  110. $monthlyCount = Cache::remember('admin-dash:reports:spam-count:30d', 43200, function() {
  111. return AccountInterstitial::whereType('post.autospam')
  112. ->where('created_at', '>', now()->subMonth())
  113. ->count();
  114. });
  115. $totalCount = Cache::remember('admin-dash:reports:spam-count:total', 43200, function() {
  116. return AccountInterstitial::whereType('post.autospam')->count();
  117. });
  118. $uncategorized = Cache::remember('admin-dash:reports:spam-sync', 3600, function() {
  119. return AccountInterstitial::whereType('post.autospam')
  120. ->whereIsSpam(null)
  121. ->whereNotNull('appeal_handled_at')
  122. ->exists();
  123. });
  124. $avg = Cache::remember('admin-dash:reports:spam-count:avg', 43200, function() {
  125. if(config('database.default') != 'mysql') {
  126. return 0;
  127. }
  128. return AccountInterstitial::selectRaw('*, count(id) as counter')
  129. ->whereType('post.autospam')
  130. ->groupBy('user_id')
  131. ->get()
  132. ->avg('counter');
  133. });
  134. $avgOpen = Cache::remember('admin-dash:reports:spam-count:avgopen', 43200, function() {
  135. if(config('database.default') != 'mysql') {
  136. return "0";
  137. }
  138. $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();
  139. if(!$seconds) {
  140. return "0";
  141. }
  142. $mins = floor($seconds->avg('timediff') / 60);
  143. if($mins < 60) {
  144. return $mins . ' min(s)';
  145. }
  146. if($mins < 2880) {
  147. return floor($mins / 60) . ' hour(s)';
  148. }
  149. return floor($mins / 60 / 24) . ' day(s)';
  150. });
  151. $avgCount = $totalCount && $avg ? floor($totalCount / $avg) : "0";
  152. if(in_array($tab, ['home', 'spam', 'not-spam'])) {
  153. $appeals = AccountInterstitial::whereType('post.autospam')
  154. ->when($tab, function($q, $tab) {
  155. switch($tab) {
  156. case 'home':
  157. return $q->whereNull('appeal_handled_at');
  158. break;
  159. case 'spam':
  160. return $q->whereIsSpam(true);
  161. break;
  162. case 'not-spam':
  163. return $q->whereIsSpam(false);
  164. break;
  165. }
  166. })
  167. ->latest()
  168. ->paginate(6);
  169. if($tab !== 'home') {
  170. $appeals = $appeals->appends(['tab' => $tab]);
  171. }
  172. } else {
  173. $appeals = new class {
  174. public function count() {
  175. return 0;
  176. }
  177. public function render() {
  178. return;
  179. }
  180. };
  181. }
  182. return view('admin.reports.spam', compact('tab', 'appeals', 'openCount', 'monthlyCount', 'totalCount', 'avgCount', 'avgOpen', 'uncategorized'));
  183. }
  184. public function showSpam(Request $request, $id)
  185. {
  186. $appeal = AccountInterstitial::whereType('post.autospam')
  187. ->findOrFail($id);
  188. if($request->has('ref') && $request->input('ref') == 'email') {
  189. return redirect('/i/admin/reports?tab=autospam&id=' . $appeal->id);
  190. }
  191. $meta = json_decode($appeal->meta);
  192. return view('admin.reports.show_spam', compact('appeal', 'meta'));
  193. }
  194. public function fixUncategorizedSpam(Request $request)
  195. {
  196. if(Cache::get('admin-dash:reports:spam-sync-active')) {
  197. return redirect('/i/admin/reports/autospam');
  198. }
  199. Cache::put('admin-dash:reports:spam-sync-active', 1, 900);
  200. AccountInterstitial::chunk(500, function($reports) {
  201. foreach($reports as $report) {
  202. if($report->item_type != 'App\Status') {
  203. continue;
  204. }
  205. if($report->type != 'post.autospam') {
  206. continue;
  207. }
  208. if($report->is_spam != null) {
  209. continue;
  210. }
  211. $status = StatusService::get($report->item_id, false);
  212. if(!$status) {
  213. return;
  214. }
  215. $scope = $status['visibility'];
  216. $report->is_spam = $scope == 'unlisted';
  217. $report->in_violation = $report->is_spam;
  218. $report->severity_index = 1;
  219. $report->save();
  220. }
  221. });
  222. Cache::forget('admin-dash:reports:spam-sync');
  223. return redirect('/i/admin/reports/autospam');
  224. }
  225. public function updateSpam(Request $request, $id)
  226. {
  227. $this->validate($request, [
  228. 'action' => 'required|in:dismiss,approve,dismiss-all,approve-all,delete-account,mark-spammer'
  229. ]);
  230. $action = $request->input('action');
  231. $appeal = AccountInterstitial::whereType('post.autospam')
  232. ->whereNull('appeal_handled_at')
  233. ->findOrFail($id);
  234. $meta = json_decode($appeal->meta);
  235. $res = ['status' => 'success'];
  236. $now = now();
  237. Cache::forget('admin-dash:reports:spam-count:total');
  238. Cache::forget('admin-dash:reports:spam-count:30d');
  239. if($action == 'delete-account') {
  240. if(config('pixelfed.account_deletion') == false) {
  241. abort(404);
  242. }
  243. $user = User::findOrFail($appeal->user_id);
  244. $profile = $user->profile;
  245. if($user->is_admin == true) {
  246. $mid = $request->user()->id;
  247. abort_if($user->id < $mid, 403);
  248. }
  249. $ts = now()->addMonth();
  250. $user->status = 'delete';
  251. $profile->status = 'delete';
  252. $user->delete_after = $ts;
  253. $profile->delete_after = $ts;
  254. $user->save();
  255. $profile->save();
  256. ModLogService::boot()
  257. ->objectUid($user->id)
  258. ->objectId($user->id)
  259. ->objectType('App\User::class')
  260. ->user($request->user())
  261. ->action('admin.user.delete')
  262. ->accessLevel('admin')
  263. ->save();
  264. Cache::forget('profiles:private');
  265. DeleteAccountPipeline::dispatch($user);
  266. return;
  267. }
  268. if($action == 'dismiss') {
  269. $appeal->is_spam = true;
  270. $appeal->appeal_handled_at = $now;
  271. $appeal->save();
  272. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  273. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  274. Cache::forget('admin-dash:reports:spam-count');
  275. return $res;
  276. }
  277. if($action == 'dismiss-all') {
  278. AccountInterstitial::whereType('post.autospam')
  279. ->whereItemType('App\Status')
  280. ->whereNull('appeal_handled_at')
  281. ->whereUserId($appeal->user_id)
  282. ->update(['appeal_handled_at' => $now, 'is_spam' => true]);
  283. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  284. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  285. Cache::forget('admin-dash:reports:spam-count');
  286. return $res;
  287. }
  288. if($action == 'approve-all') {
  289. AccountInterstitial::whereType('post.autospam')
  290. ->whereItemType('App\Status')
  291. ->whereNull('appeal_handled_at')
  292. ->whereUserId($appeal->user_id)
  293. ->get()
  294. ->each(function($report) use($meta) {
  295. $report->is_spam = false;
  296. $report->appeal_handled_at = now();
  297. $report->save();
  298. $status = Status::find($report->item_id);
  299. if($status) {
  300. $status->is_nsfw = $meta->is_nsfw;
  301. $status->scope = 'public';
  302. $status->visibility = 'public';
  303. $status->save();
  304. StatusService::del($status->id, true);
  305. }
  306. });
  307. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  308. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  309. Cache::forget('admin-dash:reports:spam-count');
  310. return $res;
  311. }
  312. if($action == 'mark-spammer') {
  313. AccountInterstitial::whereType('post.autospam')
  314. ->whereItemType('App\Status')
  315. ->whereNull('appeal_handled_at')
  316. ->whereUserId($appeal->user_id)
  317. ->update(['appeal_handled_at' => $now, 'is_spam' => true]);
  318. $pro = Profile::whereUserId($appeal->user_id)->firstOrFail();
  319. $pro->update([
  320. 'unlisted' => true,
  321. 'cw' => true,
  322. 'no_autolink' => true
  323. ]);
  324. Status::whereProfileId($pro->id)
  325. ->get()
  326. ->each(function($report) {
  327. $status->is_nsfw = $meta->is_nsfw;
  328. $status->scope = 'public';
  329. $status->visibility = 'public';
  330. $status->save();
  331. StatusService::del($status->id, true);
  332. });
  333. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  334. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  335. Cache::forget('admin-dash:reports:spam-count');
  336. return $res;
  337. }
  338. $status = $appeal->status;
  339. $status->is_nsfw = $meta->is_nsfw;
  340. $status->scope = 'public';
  341. $status->visibility = 'public';
  342. $status->save();
  343. $appeal->is_spam = false;
  344. $appeal->appeal_handled_at = now();
  345. $appeal->save();
  346. StatusService::del($status->id);
  347. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  348. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  349. Cache::forget('admin-dash:reports:spam-count');
  350. return $res;
  351. }
  352. public function updateAppeal(Request $request, $id)
  353. {
  354. $this->validate($request, [
  355. 'action' => 'required|in:dismiss,approve'
  356. ]);
  357. $action = $request->input('action');
  358. $appeal = AccountInterstitial::whereNotNull('appeal_requested_at')
  359. ->whereNull('appeal_handled_at')
  360. ->findOrFail($id);
  361. if($action == 'dismiss') {
  362. $appeal->appeal_handled_at = now();
  363. $appeal->save();
  364. Cache::forget('admin-dash:reports:ai-count');
  365. return redirect('/i/admin/reports/appeals');
  366. }
  367. switch ($appeal->type) {
  368. case 'post.cw':
  369. $status = $appeal->status;
  370. $status->is_nsfw = false;
  371. $status->save();
  372. break;
  373. case 'post.unlist':
  374. $status = $appeal->status;
  375. $status->scope = 'public';
  376. $status->visibility = 'public';
  377. $status->save();
  378. break;
  379. default:
  380. # code...
  381. break;
  382. }
  383. $appeal->appeal_handled_at = now();
  384. $appeal->save();
  385. StatusService::del($status->id, true);
  386. Cache::forget('admin-dash:reports:ai-count');
  387. return redirect('/i/admin/reports/appeals');
  388. }
  389. public function updateReport(Request $request, $id)
  390. {
  391. $this->validate($request, [
  392. 'action' => 'required|string',
  393. ]);
  394. $action = $request->input('action');
  395. $actions = [
  396. 'ignore',
  397. 'cw',
  398. 'unlist',
  399. 'delete',
  400. 'shadowban',
  401. 'ban',
  402. ];
  403. if (!in_array($action, $actions)) {
  404. return abort(403);
  405. }
  406. $report = Report::findOrFail($id);
  407. $this->handleReportAction($report, $action);
  408. Cache::forget('admin-dash:reports:list-cache');
  409. return response()->json(['msg'=> 'Success']);
  410. }
  411. public function handleReportAction(Report $report, $action)
  412. {
  413. $item = $report->reported();
  414. $report->admin_seen = Carbon::now();
  415. switch ($action) {
  416. case 'ignore':
  417. $report->not_interested = true;
  418. break;
  419. case 'cw':
  420. Cache::forget('status:thumb:'.$item->id);
  421. $item->is_nsfw = true;
  422. $item->save();
  423. $report->nsfw = true;
  424. StatusService::del($item->id, true);
  425. break;
  426. case 'unlist':
  427. $item->visibility = 'unlisted';
  428. $item->save();
  429. Cache::forget('profiles:private');
  430. StatusService::del($item->id, true);
  431. break;
  432. case 'delete':
  433. // Todo: fire delete job
  434. $report->admin_seen = null;
  435. StatusService::del($item->id, true);
  436. break;
  437. case 'shadowban':
  438. // Todo: fire delete job
  439. $report->admin_seen = null;
  440. break;
  441. case 'ban':
  442. // Todo: fire delete job
  443. $report->admin_seen = null;
  444. break;
  445. default:
  446. $report->admin_seen = null;
  447. break;
  448. }
  449. $report->save();
  450. return $this;
  451. }
  452. protected function actionMap()
  453. {
  454. return [
  455. '1' => 'ignore',
  456. '2' => 'cw',
  457. '3' => 'unlist',
  458. '4' => 'delete',
  459. '5' => 'shadowban',
  460. '6' => 'ban'
  461. ];
  462. }
  463. public function bulkUpdateReport(Request $request)
  464. {
  465. $this->validate($request, [
  466. 'action' => 'required|integer|min:1|max:10',
  467. 'ids' => 'required|array'
  468. ]);
  469. $action = $this->actionMap()[$request->input('action')];
  470. $ids = $request->input('ids');
  471. $reports = Report::whereIn('id', $ids)->whereNull('admin_seen')->get();
  472. foreach($reports as $report) {
  473. $this->handleReportAction($report, $action);
  474. }
  475. $res = [
  476. 'message' => 'Success',
  477. 'code' => 200
  478. ];
  479. return response()->json($res);
  480. }
  481. public function reportMailVerifications(Request $request)
  482. {
  483. $ids = Redis::smembers('email:manual');
  484. $ignored = Redis::smembers('email:manual-ignored');
  485. $reports = [];
  486. if($ids) {
  487. $reports = collect($ids)
  488. ->filter(function($id) use($ignored) {
  489. return !in_array($id, $ignored);
  490. })
  491. ->map(function($id) {
  492. $user = User::whereProfileId($id)->first();
  493. if(!$user || $user->email_verified_at) {
  494. return [];
  495. }
  496. $account = AccountService::get($id, true);
  497. if(!$account) {
  498. return [];
  499. }
  500. $account['email'] = $user->email;
  501. return $account;
  502. })
  503. ->filter(function($res) {
  504. return $res && isset($res['id']);
  505. })
  506. ->values();
  507. }
  508. return view('admin.reports.mail_verification', compact('reports', 'ignored'));
  509. }
  510. public function reportMailVerifyIgnore(Request $request)
  511. {
  512. $id = $request->input('id');
  513. Redis::sadd('email:manual-ignored', $id);
  514. return redirect('/i/admin/reports');
  515. }
  516. public function reportMailVerifyApprove(Request $request)
  517. {
  518. $id = $request->input('id');
  519. $user = User::whereProfileId($id)->firstOrFail();
  520. Redis::srem('email:manual', $id);
  521. Redis::srem('email:manual-ignored', $id);
  522. $user->email_verified_at = now();
  523. $user->save();
  524. return redirect('/i/admin/reports');
  525. }
  526. public function reportMailVerifyClearIgnored(Request $request)
  527. {
  528. Redis::del('email:manual-ignored');
  529. return [200];
  530. }
  531. public function reportsStats(Request $request)
  532. {
  533. $stats = [
  534. 'total' => Report::count(),
  535. 'open' => Report::whereNull('admin_seen')->count(),
  536. 'closed' => Report::whereNotNull('admin_seen')->count(),
  537. 'autospam' => AccountInterstitial::whereType('post.autospam')->count(),
  538. 'autospam_open' => AccountInterstitial::whereType('post.autospam')->whereNull(['appeal_handled_at'])->count(),
  539. 'appeals' => AccountInterstitial::whereNotNull('appeal_requested_at')->whereNull('appeal_handled_at')->count(),
  540. 'email_verification_requests' => Redis::scard('email:manual')
  541. ];
  542. return $stats;
  543. }
  544. public function reportsApiAll(Request $request)
  545. {
  546. $filter = $request->input('filter') == 'closed' ? 'closed' : 'open';
  547. $reports = AdminReport::collection(
  548. Report::orderBy('id','desc')
  549. ->when($filter, function($q, $filter) {
  550. return $filter == 'open' ?
  551. $q->whereNull('admin_seen') :
  552. $q->whereNotNull('admin_seen');
  553. })
  554. ->groupBy(['object_id', 'object_type'])
  555. ->cursorPaginate(6)
  556. ->withQueryString()
  557. );
  558. return $reports;
  559. }
  560. public function reportsApiGet(Request $request, $id)
  561. {
  562. $report = Report::findOrFail($id);
  563. return new AdminReport($report);
  564. }
  565. public function reportsApiHandle(Request $request)
  566. {
  567. $this->validate($request, [
  568. 'object_id' => 'required',
  569. 'object_type' => 'required',
  570. 'id' => 'required',
  571. 'action' => 'required|in:ignore,nsfw,unlist,private,delete',
  572. 'action_type' => 'required|in:post,profile'
  573. ]);
  574. $report = Report::whereObjectId($request->input('object_id'))->findOrFail($request->input('id'));
  575. if($request->input('action_type') === 'profile') {
  576. return $this->reportsHandleProfileAction($report, $request->input('action'));
  577. } else if($request->input('action_type') === 'post') {
  578. return $this->reportsHandleStatusAction($report, $request->input('action'));
  579. }
  580. return $report;
  581. }
  582. protected function reportsHandleProfileAction($report, $action)
  583. {
  584. switch($action) {
  585. case 'ignore':
  586. Report::whereObjectId($report->object_id)
  587. ->whereObjectType($report->object_type)
  588. ->update([
  589. 'admin_seen' => now()
  590. ]);
  591. return [200];
  592. break;
  593. case 'nsfw':
  594. if($report->object_type === 'App\Profile') {
  595. $profile = Profile::find($report->object_id);
  596. } else if($report->object_type === 'App\Status') {
  597. $status = Status::find($report->object_id);
  598. if(!$status) {
  599. return [200];
  600. }
  601. $profile = Profile::find($status->profile_id);
  602. }
  603. if(!$profile) {
  604. return;
  605. }
  606. abort_if($profile->user && $profile->user->is_admin, 400, 'Cannot moderate an admin account.');
  607. $profile->cw = true;
  608. $profile->save();
  609. foreach(Status::whereProfileId($profile->id)->cursor() as $status) {
  610. $status->is_nsfw = true;
  611. $status->save();
  612. StatusService::del($status->id);
  613. PublicTimelineService::rem($status->id);
  614. }
  615. ModLogService::boot()
  616. ->objectUid($profile->id)
  617. ->objectId($profile->id)
  618. ->objectType('App\Profile::class')
  619. ->user(request()->user())
  620. ->action('admin.user.moderate')
  621. ->metadata([
  622. 'action' => 'cw',
  623. 'message' => 'Success!'
  624. ])
  625. ->accessLevel('admin')
  626. ->save();
  627. Report::whereObjectId($report->object_id)
  628. ->whereObjectType($report->object_type)
  629. ->update([
  630. 'nsfw' => true,
  631. 'admin_seen' => now()
  632. ]);
  633. return [200];
  634. break;
  635. case 'unlist':
  636. if($report->object_type === 'App\Profile') {
  637. $profile = Profile::find($report->object_id);
  638. } else if($report->object_type === 'App\Status') {
  639. $status = Status::find($report->object_id);
  640. if(!$status) {
  641. return [200];
  642. }
  643. $profile = Profile::find($status->profile_id);
  644. }
  645. if(!$profile) {
  646. return;
  647. }
  648. abort_if($profile->user && $profile->user->is_admin, 400, 'Cannot moderate an admin account.');
  649. $profile->unlisted = true;
  650. $profile->save();
  651. foreach(Status::whereProfileId($profile->id)->whereScope('public')->cursor() as $status) {
  652. $status->scope = 'unlisted';
  653. $status->visibility = 'unlisted';
  654. $status->save();
  655. StatusService::del($status->id);
  656. PublicTimelineService::rem($status->id);
  657. }
  658. ModLogService::boot()
  659. ->objectUid($profile->id)
  660. ->objectId($profile->id)
  661. ->objectType('App\Profile::class')
  662. ->user(request()->user())
  663. ->action('admin.user.moderate')
  664. ->metadata([
  665. 'action' => 'unlisted',
  666. 'message' => 'Success!'
  667. ])
  668. ->accessLevel('admin')
  669. ->save();
  670. Report::whereObjectId($report->object_id)
  671. ->whereObjectType($report->object_type)
  672. ->update([
  673. 'admin_seen' => now()
  674. ]);
  675. return [200];
  676. break;
  677. case 'private':
  678. if($report->object_type === 'App\Profile') {
  679. $profile = Profile::find($report->object_id);
  680. } else if($report->object_type === 'App\Status') {
  681. $status = Status::find($report->object_id);
  682. if(!$status) {
  683. return [200];
  684. }
  685. $profile = Profile::find($status->profile_id);
  686. }
  687. if(!$profile) {
  688. return;
  689. }
  690. abort_if($profile->user && $profile->user->is_admin, 400, 'Cannot moderate an admin account.');
  691. $profile->unlisted = true;
  692. $profile->save();
  693. foreach(Status::whereProfileId($profile->id)->cursor() as $status) {
  694. $status->scope = 'private';
  695. $status->visibility = 'private';
  696. $status->save();
  697. StatusService::del($status->id);
  698. PublicTimelineService::rem($status->id);
  699. }
  700. ModLogService::boot()
  701. ->objectUid($profile->id)
  702. ->objectId($profile->id)
  703. ->objectType('App\Profile::class')
  704. ->user(request()->user())
  705. ->action('admin.user.moderate')
  706. ->metadata([
  707. 'action' => 'private',
  708. 'message' => 'Success!'
  709. ])
  710. ->accessLevel('admin')
  711. ->save();
  712. Report::whereObjectId($report->object_id)
  713. ->whereObjectType($report->object_type)
  714. ->update([
  715. 'admin_seen' => now()
  716. ]);
  717. return [200];
  718. break;
  719. case 'delete':
  720. if(config('pixelfed.account_deletion') == false) {
  721. abort(404);
  722. }
  723. if($report->object_type === 'App\Profile') {
  724. $profile = Profile::find($report->object_id);
  725. } else if($report->object_type === 'App\Status') {
  726. $status = Status::find($report->object_id);
  727. if(!$status) {
  728. return [200];
  729. }
  730. $profile = Profile::find($status->profile_id);
  731. }
  732. if(!$profile) {
  733. return;
  734. }
  735. abort_if($profile->user && $profile->user->is_admin, 400, 'Cannot delete an admin account.');
  736. $ts = now()->addMonth();
  737. if($profile->user_id) {
  738. $user = $profile->user;
  739. abort_if($user->is_admin, 403, 'You cannot delete admin accounts.');
  740. $user->status = 'delete';
  741. $user->delete_after = $ts;
  742. $user->save();
  743. }
  744. $profile->status = 'delete';
  745. $profile->delete_after = $ts;
  746. $profile->save();
  747. ModLogService::boot()
  748. ->objectUid($profile->id)
  749. ->objectId($profile->id)
  750. ->objectType('App\Profile::class')
  751. ->user(request()->user())
  752. ->action('admin.user.delete')
  753. ->accessLevel('admin')
  754. ->save();
  755. Report::whereObjectId($report->object_id)
  756. ->whereObjectType($report->object_type)
  757. ->update([
  758. 'admin_seen' => now()
  759. ]);
  760. if($profile->user_id) {
  761. DB::table('oauth_access_tokens')->whereUserId($user->id)->delete();
  762. DB::table('oauth_auth_codes')->whereUserId($user->id)->delete();
  763. $user->email = $user->id;
  764. $user->password = '';
  765. $user->status = 'delete';
  766. $user->save();
  767. $profile->status = 'delete';
  768. $profile->delete_after = now()->addMonth();
  769. $profile->save();
  770. AccountService::del($profile->id);
  771. DeleteAccountPipeline::dispatch($user)->onQueue('high');
  772. } else {
  773. $profile->status = 'delete';
  774. $profile->delete_after = now()->addMonth();
  775. $profile->save();
  776. AccountService::del($profile->id);
  777. DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('high');
  778. }
  779. return [200];
  780. break;
  781. }
  782. }
  783. protected function reportsHandleStatusAction($report, $action)
  784. {
  785. switch($action) {
  786. case 'ignore':
  787. Report::whereObjectId($report->object_id)
  788. ->whereObjectType($report->object_type)
  789. ->update([
  790. 'admin_seen' => now()
  791. ]);
  792. return [200];
  793. break;
  794. case 'nsfw':
  795. $status = Status::find($report->object_id);
  796. if(!$status) {
  797. return [200];
  798. }
  799. abort_if($status->profile->user && $status->profile->user->is_admin, 400, 'Cannot moderate an admin account post.');
  800. $status->is_nsfw = true;
  801. $status->save();
  802. StatusService::del($status->id);
  803. ModLogService::boot()
  804. ->objectUid($status->profile_id)
  805. ->objectId($status->profile_id)
  806. ->objectType('App\Status::class')
  807. ->user(request()->user())
  808. ->action('admin.status.moderate')
  809. ->metadata([
  810. 'action' => 'cw',
  811. 'message' => 'Success!'
  812. ])
  813. ->accessLevel('admin')
  814. ->save();
  815. Report::whereObjectId($report->object_id)
  816. ->whereObjectType($report->object_type)
  817. ->update([
  818. 'nsfw' => true,
  819. 'admin_seen' => now()
  820. ]);
  821. return [200];
  822. break;
  823. case 'private':
  824. $status = Status::find($report->object_id);
  825. if(!$status) {
  826. return [200];
  827. }
  828. abort_if($status->profile->user && $status->profile->user->is_admin, 400, 'Cannot moderate an admin account post.');
  829. $status->scope = 'private';
  830. $status->visibility = 'private';
  831. $status->save();
  832. StatusService::del($status->id);
  833. PublicTimelineService::rem($status->id);
  834. ModLogService::boot()
  835. ->objectUid($status->profile_id)
  836. ->objectId($status->profile_id)
  837. ->objectType('App\Status::class')
  838. ->user(request()->user())
  839. ->action('admin.status.moderate')
  840. ->metadata([
  841. 'action' => 'private',
  842. 'message' => 'Success!'
  843. ])
  844. ->accessLevel('admin')
  845. ->save();
  846. Report::whereObjectId($report->object_id)
  847. ->whereObjectType($report->object_type)
  848. ->update([
  849. 'admin_seen' => now()
  850. ]);
  851. return [200];
  852. break;
  853. case 'unlist':
  854. $status = Status::find($report->object_id);
  855. if(!$status) {
  856. return [200];
  857. }
  858. abort_if($status->profile->user && $status->profile->user->is_admin, 400, 'Cannot moderate an admin account post.');
  859. if($status->scope === 'public') {
  860. $status->scope = 'unlisted';
  861. $status->visibility = 'unlisted';
  862. $status->save();
  863. StatusService::del($status->id);
  864. PublicTimelineService::rem($status->id);
  865. }
  866. ModLogService::boot()
  867. ->objectUid($status->profile_id)
  868. ->objectId($status->profile_id)
  869. ->objectType('App\Status::class')
  870. ->user(request()->user())
  871. ->action('admin.status.moderate')
  872. ->metadata([
  873. 'action' => 'unlist',
  874. 'message' => 'Success!'
  875. ])
  876. ->accessLevel('admin')
  877. ->save();
  878. Report::whereObjectId($report->object_id)
  879. ->whereObjectType($report->object_type)
  880. ->update([
  881. 'admin_seen' => now()
  882. ]);
  883. return [200];
  884. break;
  885. case 'delete':
  886. $status = Status::find($report->object_id);
  887. if(!$status) {
  888. return [200];
  889. }
  890. $profile = $status->profile;
  891. abort_if($profile->user && $profile->user->is_admin, 400, 'Cannot delete an admin account post.');
  892. StatusService::del($status->id);
  893. if($profile->user_id != null && $profile->domain == null) {
  894. PublicTimelineService::del($status->id);
  895. StatusDelete::dispatch($status)->onQueue('high');
  896. } else {
  897. NetworkTimelineService::del($status->id);
  898. DeleteRemoteStatusPipeline::dispatch($status)->onQueue('high');
  899. }
  900. Report::whereObjectId($report->object_id)
  901. ->whereObjectType($report->object_type)
  902. ->update([
  903. 'admin_seen' => now()
  904. ]);
  905. return [200];
  906. break;
  907. }
  908. }
  909. public function reportsApiSpamAll(Request $request)
  910. {
  911. $tab = $request->input('tab', 'home');
  912. $appeals = AdminSpamReport::collection(
  913. AccountInterstitial::orderBy('id', 'desc')
  914. ->whereType('post.autospam')
  915. ->whereNull('appeal_handled_at')
  916. ->cursorPaginate(6)
  917. ->withQueryString()
  918. );
  919. return $appeals;
  920. }
  921. public function reportsApiSpamHandle(Request $request)
  922. {
  923. $this->validate($request, [
  924. 'id' => 'required',
  925. 'action' => 'required|in:mark-read,mark-not-spam,mark-all-read,mark-all-not-spam,delete-profile',
  926. ]);
  927. $action = $request->input('action');
  928. abort_if(
  929. $action === 'delete-profile' &&
  930. !config('pixelfed.account_deletion'),
  931. 404,
  932. "Cannot delete profile, account_deletion is disabled.\n\n Set `ACCOUNT_DELETION=true` in .env and re-cache config."
  933. );
  934. $report = AccountInterstitial::with('user')
  935. ->whereType('post.autospam')
  936. ->whereNull('appeal_handled_at')
  937. ->findOrFail($request->input('id'));
  938. $this->reportsHandleSpamAction($report, $action);
  939. Cache::forget('admin-dash:reports:spam-count');
  940. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $report->user->profile_id);
  941. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $report->user->profile_id);
  942. PublicTimelineService::warmCache(true, 400);
  943. return [$action, $report];
  944. }
  945. public function reportsHandleSpamAction($appeal, $action)
  946. {
  947. $meta = json_decode($appeal->meta);
  948. if($action == 'mark-read') {
  949. $appeal->is_spam = true;
  950. $appeal->appeal_handled_at = now();
  951. $appeal->save();
  952. }
  953. if($action == 'mark-not-spam') {
  954. $status = $appeal->status;
  955. $status->is_nsfw = $meta->is_nsfw;
  956. $status->scope = 'public';
  957. $status->visibility = 'public';
  958. $status->save();
  959. $appeal->is_spam = false;
  960. $appeal->appeal_handled_at = now();
  961. $appeal->save();
  962. Notification::whereAction('autospam.warning')
  963. ->whereProfileId($appeal->user->profile_id)
  964. ->get()
  965. ->each(function($n) use($appeal) {
  966. NotificationService::del($appeal->user->profile_id, $n->id);
  967. $n->forceDelete();
  968. });
  969. StatusService::del($status->id);
  970. }
  971. if($action == 'mark-all-read') {
  972. AccountInterstitial::whereType('post.autospam')
  973. ->whereItemType('App\Status')
  974. ->whereNull('appeal_handled_at')
  975. ->whereUserId($appeal->user_id)
  976. ->update([
  977. 'appeal_handled_at' => now(),
  978. 'is_spam' => true
  979. ]);
  980. }
  981. if($action == 'mark-all-not-spam') {
  982. AccountInterstitial::whereType('post.autospam')
  983. ->whereItemType('App\Status')
  984. ->whereUserId($appeal->user_id)
  985. ->get()
  986. ->each(function($report) use($meta) {
  987. $report->is_spam = false;
  988. $report->appeal_handled_at = now();
  989. $report->save();
  990. $status = Status::find($report->item_id);
  991. if($status) {
  992. $status->is_nsfw = $meta->is_nsfw;
  993. $status->scope = 'public';
  994. $status->visibility = 'public';
  995. $status->save();
  996. StatusService::del($status->id);
  997. }
  998. Notification::whereAction('autospam.warning')
  999. ->whereProfileId($report->user->profile_id)
  1000. ->get()
  1001. ->each(function($n) use($report) {
  1002. NotificationService::del($report->user->profile_id, $n->id);
  1003. $n->forceDelete();
  1004. });
  1005. });
  1006. }
  1007. if($action == 'delete-profile') {
  1008. $user = User::findOrFail($appeal->user_id);
  1009. $profile = $user->profile;
  1010. if($user->is_admin == true) {
  1011. $mid = request()->user()->id;
  1012. abort_if($user->id < $mid, 403, 'You cannot delete an admin account.');
  1013. }
  1014. $ts = now()->addMonth();
  1015. $user->status = 'delete';
  1016. $profile->status = 'delete';
  1017. $user->delete_after = $ts;
  1018. $profile->delete_after = $ts;
  1019. $user->save();
  1020. $profile->save();
  1021. $appeal->appeal_handled_at = now();
  1022. $appeal->save();
  1023. ModLogService::boot()
  1024. ->objectUid($user->id)
  1025. ->objectId($user->id)
  1026. ->objectType('App\User::class')
  1027. ->user(request()->user())
  1028. ->action('admin.user.delete')
  1029. ->accessLevel('admin')
  1030. ->save();
  1031. Cache::forget('profiles:private');
  1032. DeleteAccountPipeline::dispatch($user);
  1033. }
  1034. }
  1035. public function reportsApiSpamGet(Request $request, $id)
  1036. {
  1037. $report = AccountInterstitial::findOrFail($id);
  1038. return new AdminSpamReport($report);
  1039. }
  1040. }