AdminReportController.php 34 KB

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