AdminController.php 11 KB

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