AdminController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\{
  4. AccountInterstitial,
  5. Contact,
  6. Hashtag,
  7. Newsroom,
  8. OauthClient,
  9. Profile,
  10. Report,
  11. Status,
  12. Story,
  13. User
  14. };
  15. use DB, Cache;
  16. use Carbon\Carbon;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Support\Facades\Redis;
  19. use App\Http\Controllers\Admin\{
  20. AdminDiscoverController,
  21. AdminInstanceController,
  22. AdminReportController,
  23. AdminMediaController,
  24. AdminSettingsController,
  25. AdminSupportController,
  26. AdminUserController
  27. };
  28. use Illuminate\Validation\Rule;
  29. use App\Services\AdminStatsService;
  30. use App\Services\StatusService;
  31. use App\Services\StoryService;
  32. class AdminController extends Controller
  33. {
  34. use AdminReportController,
  35. AdminDiscoverController,
  36. AdminMediaController,
  37. AdminSettingsController,
  38. AdminInstanceController,
  39. AdminUserController;
  40. public function __construct()
  41. {
  42. $this->middleware('admin');
  43. $this->middleware('dangerzone');
  44. $this->middleware('twofactor');
  45. }
  46. public function home()
  47. {
  48. $data = AdminStatsService::get();
  49. return view('admin.home', compact('data'));
  50. }
  51. public function statuses(Request $request)
  52. {
  53. $statuses = Status::orderBy('id', 'desc')->cursorPaginate(10);
  54. $data = $statuses->map(function($status) {
  55. return StatusService::get($status->id, false);
  56. })
  57. ->filter(function($s) {
  58. return $s;
  59. })
  60. ->toArray();
  61. return view('admin.statuses.home', compact('statuses', 'data'));
  62. }
  63. public function showStatus(Request $request, $id)
  64. {
  65. $status = Status::findOrFail($id);
  66. return view('admin.statuses.show', compact('status'));
  67. }
  68. public function reports(Request $request)
  69. {
  70. $filter = $request->input('filter') == 'closed' ? 'closed' : 'open';
  71. $page = $request->input('page') ?? 1;
  72. $ai = Cache::remember('admin-dash:reports:ai-count', 3600, function() {
  73. return AccountInterstitial::whereNotNull('appeal_requested_at')->whereNull('appeal_handled_at')->count();
  74. });
  75. $spam = Cache::remember('admin-dash:reports:spam-count', 3600, function() {
  76. return AccountInterstitial::whereType('post.autospam')->whereNull('appeal_handled_at')->count();
  77. });
  78. $mailVerifications = Redis::scard('email:manual');
  79. if($filter == 'open' && $page == 1) {
  80. $reports = Cache::remember('admin-dash:reports:list-cache', 300, function() use($page, $filter) {
  81. return Report::whereHas('status')
  82. ->whereHas('reportedUser')
  83. ->whereHas('reporter')
  84. ->orderBy('created_at','desc')
  85. ->when($filter, function($q, $filter) {
  86. return $filter == 'open' ?
  87. $q->whereNull('admin_seen') :
  88. $q->whereNotNull('admin_seen');
  89. })
  90. ->paginate(6);
  91. });
  92. } else {
  93. $reports = Report::whereHas('status')
  94. ->whereHas('reportedUser')
  95. ->whereHas('reporter')
  96. ->orderBy('created_at','desc')
  97. ->when($filter, function($q, $filter) {
  98. return $filter == 'open' ?
  99. $q->whereNull('admin_seen') :
  100. $q->whereNotNull('admin_seen');
  101. })
  102. ->paginate(6);
  103. }
  104. return view('admin.reports.home', compact('reports', 'ai', 'spam', 'mailVerifications'));
  105. }
  106. public function showReport(Request $request, $id)
  107. {
  108. $report = Report::findOrFail($id);
  109. return view('admin.reports.show', compact('report'));
  110. }
  111. public function appeals(Request $request)
  112. {
  113. $appeals = AccountInterstitial::whereNotNull('appeal_requested_at')
  114. ->whereNull('appeal_handled_at')
  115. ->latest()
  116. ->paginate(6);
  117. return view('admin.reports.appeals', compact('appeals'));
  118. }
  119. public function showAppeal(Request $request, $id)
  120. {
  121. $appeal = AccountInterstitial::whereNotNull('appeal_requested_at')
  122. ->whereNull('appeal_handled_at')
  123. ->findOrFail($id);
  124. $meta = json_decode($appeal->meta);
  125. return view('admin.reports.show_appeal', compact('appeal', 'meta'));
  126. }
  127. public function spam(Request $request)
  128. {
  129. $appeals = AccountInterstitial::whereType('post.autospam')
  130. ->whereNull('appeal_handled_at')
  131. ->latest()
  132. ->paginate(6);
  133. return view('admin.reports.spam', compact('appeals'));
  134. }
  135. public function showSpam(Request $request, $id)
  136. {
  137. $appeal = AccountInterstitial::whereType('post.autospam')
  138. ->whereNull('appeal_handled_at')
  139. ->findOrFail($id);
  140. $meta = json_decode($appeal->meta);
  141. return view('admin.reports.show_spam', compact('appeal', 'meta'));
  142. }
  143. public function updateSpam(Request $request, $id)
  144. {
  145. $this->validate($request, [
  146. 'action' => 'required|in:dismiss,approve'
  147. ]);
  148. $action = $request->input('action');
  149. $appeal = AccountInterstitial::whereType('post.autospam')
  150. ->whereNull('appeal_handled_at')
  151. ->findOrFail($id);
  152. $meta = json_decode($appeal->meta);
  153. if($action == 'dismiss') {
  154. $appeal->appeal_handled_at = now();
  155. $appeal->save();
  156. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  157. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  158. Cache::forget('admin-dash:reports:spam-count');
  159. return redirect('/i/admin/reports/autospam');
  160. }
  161. $status = $appeal->status;
  162. $status->is_nsfw = $meta->is_nsfw;
  163. $status->scope = 'public';
  164. $status->visibility = 'public';
  165. $status->save();
  166. $appeal->appeal_handled_at = now();
  167. $appeal->save();
  168. StatusService::del($status->id);
  169. Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id);
  170. Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id);
  171. Cache::forget('admin-dash:reports:spam-count');
  172. return redirect('/i/admin/reports/autospam');
  173. }
  174. public function updateAppeal(Request $request, $id)
  175. {
  176. $this->validate($request, [
  177. 'action' => 'required|in:dismiss,approve'
  178. ]);
  179. $action = $request->input('action');
  180. $appeal = AccountInterstitial::whereNotNull('appeal_requested_at')
  181. ->whereNull('appeal_handled_at')
  182. ->findOrFail($id);
  183. if($action == 'dismiss') {
  184. $appeal->appeal_handled_at = now();
  185. $appeal->save();
  186. Cache::forget('admin-dash:reports:ai-count');
  187. return redirect('/i/admin/reports/appeals');
  188. }
  189. switch ($appeal->type) {
  190. case 'post.cw':
  191. $status = $appeal->status;
  192. $status->is_nsfw = false;
  193. $status->save();
  194. break;
  195. case 'post.unlist':
  196. $status = $appeal->status;
  197. $status->scope = 'public';
  198. $status->visibility = 'public';
  199. $status->save();
  200. break;
  201. default:
  202. # code...
  203. break;
  204. }
  205. $appeal->appeal_handled_at = now();
  206. $appeal->save();
  207. StatusService::del($status->id);
  208. Cache::forget('admin-dash:reports:ai-count');
  209. return redirect('/i/admin/reports/appeals');
  210. }
  211. public function profiles(Request $request)
  212. {
  213. $this->validate($request, [
  214. 'search' => 'nullable|string|max:250',
  215. 'filter' => [
  216. 'nullable',
  217. 'string',
  218. Rule::in(['all', 'local', 'remote'])
  219. ]
  220. ]);
  221. $search = $request->input('search');
  222. $filter = $request->input('filter');
  223. $limit = 12;
  224. $profiles = Profile::select('id','username')
  225. ->whereNull('status')
  226. ->when($search, function($q, $search) {
  227. return $q->where('username', 'like', "%$search%");
  228. })->when($filter, function($q, $filter) {
  229. if($filter == 'local') {
  230. return $q->whereNull('domain');
  231. }
  232. if($filter == 'remote') {
  233. return $q->whereNotNull('domain');
  234. }
  235. return $q;
  236. })->orderByDesc('id')
  237. ->simplePaginate($limit);
  238. return view('admin.profiles.home', compact('profiles'));
  239. }
  240. public function profileShow(Request $request, $id)
  241. {
  242. $profile = Profile::findOrFail($id);
  243. $user = $profile->user;
  244. return view('admin.profiles.edit', compact('profile', 'user'));
  245. }
  246. public function appsHome(Request $request)
  247. {
  248. $filter = $request->input('filter');
  249. if(in_array($filter, ['revoked'])) {
  250. $apps = OauthClient::with('user')
  251. ->whereNotNull('user_id')
  252. ->whereRevoked(true)
  253. ->orderByDesc('id')
  254. ->paginate(10);
  255. } else {
  256. $apps = OauthClient::with('user')
  257. ->whereNotNull('user_id')
  258. ->orderByDesc('id')
  259. ->paginate(10);
  260. }
  261. return view('admin.apps.home', compact('apps'));
  262. }
  263. public function hashtagsHome(Request $request)
  264. {
  265. $hashtags = Hashtag::orderByDesc('id')->paginate(10);
  266. return view('admin.hashtags.home', compact('hashtags'));
  267. }
  268. public function messagesHome(Request $request)
  269. {
  270. $messages = Contact::orderByDesc('id')->paginate(10);
  271. return view('admin.messages.home', compact('messages'));
  272. }
  273. public function messagesShow(Request $request, $id)
  274. {
  275. $message = Contact::findOrFail($id);
  276. return view('admin.messages.show', compact('message'));
  277. }
  278. public function messagesMarkRead(Request $request)
  279. {
  280. $this->validate($request, [
  281. 'id' => 'required|integer|min:1'
  282. ]);
  283. $id = $request->input('id');
  284. $message = Contact::findOrFail($id);
  285. if($message->read_at) {
  286. return;
  287. }
  288. $message->read_at = now();
  289. $message->save();
  290. return;
  291. }
  292. public function newsroomHome(Request $request)
  293. {
  294. $newsroom = Newsroom::latest()->paginate(10);
  295. return view('admin.newsroom.home', compact('newsroom'));
  296. }
  297. public function newsroomCreate(Request $request)
  298. {
  299. return view('admin.newsroom.create');
  300. }
  301. public function newsroomEdit(Request $request, $id)
  302. {
  303. $news = Newsroom::findOrFail($id);
  304. return view('admin.newsroom.edit', compact('news'));
  305. }
  306. public function newsroomDelete(Request $request, $id)
  307. {
  308. $news = Newsroom::findOrFail($id);
  309. $news->delete();
  310. return redirect('/i/admin/newsroom');
  311. }
  312. public function newsroomUpdate(Request $request, $id)
  313. {
  314. $this->validate($request, [
  315. 'title' => 'required|string|min:1|max:100',
  316. 'summary' => 'nullable|string|max:200',
  317. 'body' => 'nullable|string'
  318. ]);
  319. $changed = false;
  320. $changedFields = [];
  321. $news = Newsroom::findOrFail($id);
  322. $fields = [
  323. 'title' => 'string',
  324. 'summary' => 'string',
  325. 'body' => 'string',
  326. 'category' => 'string',
  327. 'show_timeline' => 'boolean',
  328. 'auth_only' => 'boolean',
  329. 'show_link' => 'boolean',
  330. 'force_modal' => 'boolean',
  331. 'published' => 'published'
  332. ];
  333. foreach($fields as $field => $type) {
  334. switch ($type) {
  335. case 'string':
  336. if($request->{$field} != $news->{$field}) {
  337. if($field == 'title') {
  338. $news->slug = str_slug($request->{$field});
  339. }
  340. $news->{$field} = $request->{$field};
  341. $changed = true;
  342. array_push($changedFields, $field);
  343. }
  344. break;
  345. case 'boolean':
  346. $state = $request->{$field} == 'on' ? true : false;
  347. if($state != $news->{$field}) {
  348. $news->{$field} = $state;
  349. $changed = true;
  350. array_push($changedFields, $field);
  351. }
  352. break;
  353. case 'published':
  354. $state = $request->{$field} == 'on' ? true : false;
  355. $published = $news->published_at != null;
  356. if($state != $published) {
  357. $news->published_at = $state ? now() : null;
  358. $changed = true;
  359. array_push($changedFields, $field);
  360. }
  361. break;
  362. }
  363. }
  364. if($changed) {
  365. $news->save();
  366. }
  367. $redirect = $news->published_at ? $news->permalink() : $news->editUrl();
  368. return redirect($redirect);
  369. }
  370. public function newsroomStore(Request $request)
  371. {
  372. $this->validate($request, [
  373. 'title' => 'required|string|min:1|max:100',
  374. 'summary' => 'nullable|string|max:200',
  375. 'body' => 'nullable|string'
  376. ]);
  377. $changed = false;
  378. $changedFields = [];
  379. $news = new Newsroom();
  380. $fields = [
  381. 'title' => 'string',
  382. 'summary' => 'string',
  383. 'body' => 'string',
  384. 'category' => 'string',
  385. 'show_timeline' => 'boolean',
  386. 'auth_only' => 'boolean',
  387. 'show_link' => 'boolean',
  388. 'force_modal' => 'boolean',
  389. 'published' => 'published'
  390. ];
  391. foreach($fields as $field => $type) {
  392. switch ($type) {
  393. case 'string':
  394. if($request->{$field} != $news->{$field}) {
  395. if($field == 'title') {
  396. $news->slug = str_slug($request->{$field});
  397. }
  398. $news->{$field} = $request->{$field};
  399. $changed = true;
  400. array_push($changedFields, $field);
  401. }
  402. break;
  403. case 'boolean':
  404. $state = $request->{$field} == 'on' ? true : false;
  405. if($state != $news->{$field}) {
  406. $news->{$field} = $state;
  407. $changed = true;
  408. array_push($changedFields, $field);
  409. }
  410. break;
  411. case 'published':
  412. $state = $request->{$field} == 'on' ? true : false;
  413. $published = $news->published_at != null;
  414. if($state != $published) {
  415. $news->published_at = $state ? now() : null;
  416. $changed = true;
  417. array_push($changedFields, $field);
  418. }
  419. break;
  420. }
  421. }
  422. if($changed) {
  423. $news->save();
  424. }
  425. $redirect = $news->published_at ? $news->permalink() : $news->editUrl();
  426. return redirect($redirect);
  427. }
  428. public function diagnosticsHome(Request $request)
  429. {
  430. return view('admin.diagnostics.home');
  431. }
  432. public function diagnosticsDecrypt(Request $request)
  433. {
  434. $this->validate($request, [
  435. 'payload' => 'required'
  436. ]);
  437. $key = 'exception_report:';
  438. $decrypted = decrypt($request->input('payload'));
  439. if(!starts_with($decrypted, $key)) {
  440. abort(403, 'Can only decrypt error diagnostics');
  441. }
  442. $res = [
  443. 'decrypted' => substr($decrypted, strlen($key))
  444. ];
  445. return response()->json($res);
  446. }
  447. public function stories(Request $request)
  448. {
  449. $stories = Story::with('profile')->latest()->paginate(10);
  450. $stats = StoryService::adminStats();
  451. return view('admin.stories.home', compact('stories', 'stats'));
  452. }
  453. }