AdminReportController.php 33 KB

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