1
0

AdminReportController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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. return [$action, $report];
  943. }
  944. public function reportsHandleSpamAction($appeal, $action)
  945. {
  946. $meta = json_decode($appeal->meta);
  947. if($action == 'mark-read') {
  948. $appeal->is_spam = true;
  949. $appeal->appeal_handled_at = now();
  950. $appeal->save();
  951. PublicTimelineService::del($appeal->item_id);
  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. StatusService::get($status->id);
  971. PublicTimelineService::add($status->id);
  972. }
  973. if($action == 'mark-all-read') {
  974. AccountInterstitial::whereType('post.autospam')
  975. ->whereItemType('App\Status')
  976. ->whereNull('appeal_handled_at')
  977. ->whereUserId($appeal->user_id)
  978. ->update([
  979. 'appeal_handled_at' => now(),
  980. 'is_spam' => true
  981. ]);
  982. }
  983. if($action == 'mark-all-not-spam') {
  984. AccountInterstitial::whereType('post.autospam')
  985. ->whereItemType('App\Status')
  986. ->whereUserId($appeal->user_id)
  987. ->get()
  988. ->each(function($report) use($meta) {
  989. $report->is_spam = false;
  990. $report->appeal_handled_at = now();
  991. $report->save();
  992. $status = Status::find($report->item_id);
  993. if($status) {
  994. $status->is_nsfw = $meta->is_nsfw;
  995. $status->scope = 'public';
  996. $status->visibility = 'public';
  997. $status->save();
  998. StatusService::del($status->id);
  999. }
  1000. Notification::whereAction('autospam.warning')
  1001. ->whereProfileId($report->user->profile_id)
  1002. ->get()
  1003. ->each(function($n) use($report) {
  1004. NotificationService::del($report->user->profile_id, $n->id);
  1005. $n->forceDelete();
  1006. });
  1007. });
  1008. }
  1009. if($action == 'delete-profile') {
  1010. $user = User::findOrFail($appeal->user_id);
  1011. $profile = $user->profile;
  1012. if($user->is_admin == true) {
  1013. $mid = request()->user()->id;
  1014. abort_if($user->id < $mid, 403, 'You cannot delete an admin account.');
  1015. }
  1016. $ts = now()->addMonth();
  1017. $user->status = 'delete';
  1018. $profile->status = 'delete';
  1019. $user->delete_after = $ts;
  1020. $profile->delete_after = $ts;
  1021. $user->save();
  1022. $profile->save();
  1023. $appeal->appeal_handled_at = now();
  1024. $appeal->save();
  1025. ModLogService::boot()
  1026. ->objectUid($user->id)
  1027. ->objectId($user->id)
  1028. ->objectType('App\User::class')
  1029. ->user(request()->user())
  1030. ->action('admin.user.delete')
  1031. ->accessLevel('admin')
  1032. ->save();
  1033. Cache::forget('profiles:private');
  1034. DeleteAccountPipeline::dispatch($user);
  1035. }
  1036. }
  1037. public function reportsApiSpamGet(Request $request, $id)
  1038. {
  1039. $report = AccountInterstitial::findOrFail($id);
  1040. return new AdminSpamReport($report);
  1041. }
  1042. }