AdminController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. return redirect('/i/admin/reports/autospam');
  123. }
  124. $status = $appeal->status;
  125. $status->is_nsfw = $meta->is_nsfw;
  126. $status->scope = 'public';
  127. $status->visibility = 'public';
  128. $status->save();
  129. $appeal->appeal_handled_at = now();
  130. $appeal->save();
  131. return redirect('/i/admin/reports/autospam');
  132. }
  133. public function updateAppeal(Request $request, $id)
  134. {
  135. $this->validate($request, [
  136. 'action' => 'required|in:dismiss,approve'
  137. ]);
  138. $action = $request->input('action');
  139. $appeal = AccountInterstitial::whereNotNull('appeal_requested_at')
  140. ->whereNull('appeal_handled_at')
  141. ->findOrFail($id);
  142. if($action == 'dismiss') {
  143. $appeal->appeal_handled_at = now();
  144. $appeal->save();
  145. return redirect('/i/admin/reports/appeals');
  146. }
  147. switch ($appeal->type) {
  148. case 'post.cw':
  149. $status = $appeal->status;
  150. $status->is_nsfw = false;
  151. $status->save();
  152. break;
  153. case 'post.unlist':
  154. $status = $appeal->status;
  155. $status->scope = 'public';
  156. $status->visibility = 'public';
  157. $status->save();
  158. break;
  159. default:
  160. # code...
  161. break;
  162. }
  163. $appeal->appeal_handled_at = now();
  164. $appeal->save();
  165. return redirect('/i/admin/reports/appeals');
  166. }
  167. public function profiles(Request $request)
  168. {
  169. $this->validate($request, [
  170. 'search' => 'nullable|string|max:250',
  171. 'filter' => [
  172. 'nullable',
  173. 'string',
  174. Rule::in(['all', 'local', 'remote'])
  175. ]
  176. ]);
  177. $search = $request->input('search');
  178. $filter = $request->input('filter');
  179. $limit = 12;
  180. $profiles = Profile::select('id','username')
  181. ->whereNull('status')
  182. ->when($search, function($q, $search) {
  183. return $q->where('username', 'like', "%$search%");
  184. })->when($filter, function($q, $filter) {
  185. if($filter == 'local') {
  186. return $q->whereNull('domain');
  187. }
  188. if($filter == 'remote') {
  189. return $q->whereNotNull('domain');
  190. }
  191. return $q;
  192. })->orderByDesc('id')
  193. ->simplePaginate($limit);
  194. return view('admin.profiles.home', compact('profiles'));
  195. }
  196. public function profileShow(Request $request, $id)
  197. {
  198. $profile = Profile::findOrFail($id);
  199. $user = $profile->user;
  200. return view('admin.profiles.edit', compact('profile', 'user'));
  201. }
  202. public function appsHome(Request $request)
  203. {
  204. $filter = $request->input('filter');
  205. if(in_array($filter, ['revoked'])) {
  206. $apps = OauthClient::with('user')
  207. ->whereNotNull('user_id')
  208. ->whereRevoked(true)
  209. ->orderByDesc('id')
  210. ->paginate(10);
  211. } else {
  212. $apps = OauthClient::with('user')
  213. ->whereNotNull('user_id')
  214. ->orderByDesc('id')
  215. ->paginate(10);
  216. }
  217. return view('admin.apps.home', compact('apps'));
  218. }
  219. public function hashtagsHome(Request $request)
  220. {
  221. $hashtags = Hashtag::orderByDesc('id')->paginate(10);
  222. return view('admin.hashtags.home', compact('hashtags'));
  223. }
  224. public function messagesHome(Request $request)
  225. {
  226. $messages = Contact::orderByDesc('id')->paginate(10);
  227. return view('admin.messages.home', compact('messages'));
  228. }
  229. public function messagesShow(Request $request, $id)
  230. {
  231. $message = Contact::findOrFail($id);
  232. return view('admin.messages.show', compact('message'));
  233. }
  234. public function messagesMarkRead(Request $request)
  235. {
  236. $this->validate($request, [
  237. 'id' => 'required|integer|min:1'
  238. ]);
  239. $id = $request->input('id');
  240. $message = Contact::findOrFail($id);
  241. if($message->read_at) {
  242. return;
  243. }
  244. $message->read_at = now();
  245. $message->save();
  246. return;
  247. }
  248. public function newsroomHome(Request $request)
  249. {
  250. $newsroom = Newsroom::latest()->paginate(10);
  251. return view('admin.newsroom.home', compact('newsroom'));
  252. }
  253. public function newsroomCreate(Request $request)
  254. {
  255. return view('admin.newsroom.create');
  256. }
  257. public function newsroomEdit(Request $request, $id)
  258. {
  259. $news = Newsroom::findOrFail($id);
  260. return view('admin.newsroom.edit', compact('news'));
  261. }
  262. public function newsroomDelete(Request $request, $id)
  263. {
  264. $news = Newsroom::findOrFail($id);
  265. $news->delete();
  266. return redirect('/i/admin/newsroom');
  267. }
  268. public function newsroomUpdate(Request $request, $id)
  269. {
  270. $this->validate($request, [
  271. 'title' => 'required|string|min:1|max:100',
  272. 'summary' => 'nullable|string|max:200',
  273. 'body' => 'nullable|string'
  274. ]);
  275. $changed = false;
  276. $changedFields = [];
  277. $news = Newsroom::findOrFail($id);
  278. $fields = [
  279. 'title' => 'string',
  280. 'summary' => 'string',
  281. 'body' => 'string',
  282. 'category' => 'string',
  283. 'show_timeline' => 'boolean',
  284. 'auth_only' => 'boolean',
  285. 'show_link' => 'boolean',
  286. 'force_modal' => 'boolean',
  287. 'published' => 'published'
  288. ];
  289. foreach($fields as $field => $type) {
  290. switch ($type) {
  291. case 'string':
  292. if($request->{$field} != $news->{$field}) {
  293. if($field == 'title') {
  294. $news->slug = str_slug($request->{$field});
  295. }
  296. $news->{$field} = $request->{$field};
  297. $changed = true;
  298. array_push($changedFields, $field);
  299. }
  300. break;
  301. case 'boolean':
  302. $state = $request->{$field} == 'on' ? true : false;
  303. if($state != $news->{$field}) {
  304. $news->{$field} = $state;
  305. $changed = true;
  306. array_push($changedFields, $field);
  307. }
  308. break;
  309. case 'published':
  310. $state = $request->{$field} == 'on' ? true : false;
  311. $published = $news->published_at != null;
  312. if($state != $published) {
  313. $news->published_at = $state ? now() : null;
  314. $changed = true;
  315. array_push($changedFields, $field);
  316. }
  317. break;
  318. }
  319. }
  320. if($changed) {
  321. $news->save();
  322. }
  323. $redirect = $news->published_at ? $news->permalink() : $news->editUrl();
  324. return redirect($redirect);
  325. }
  326. public function newsroomStore(Request $request)
  327. {
  328. $this->validate($request, [
  329. 'title' => 'required|string|min:1|max:100',
  330. 'summary' => 'nullable|string|max:200',
  331. 'body' => 'nullable|string'
  332. ]);
  333. $changed = false;
  334. $changedFields = [];
  335. $news = new Newsroom();
  336. $fields = [
  337. 'title' => 'string',
  338. 'summary' => 'string',
  339. 'body' => 'string',
  340. 'category' => 'string',
  341. 'show_timeline' => 'boolean',
  342. 'auth_only' => 'boolean',
  343. 'show_link' => 'boolean',
  344. 'force_modal' => 'boolean',
  345. 'published' => 'published'
  346. ];
  347. foreach($fields as $field => $type) {
  348. switch ($type) {
  349. case 'string':
  350. if($request->{$field} != $news->{$field}) {
  351. if($field == 'title') {
  352. $news->slug = str_slug($request->{$field});
  353. }
  354. $news->{$field} = $request->{$field};
  355. $changed = true;
  356. array_push($changedFields, $field);
  357. }
  358. break;
  359. case 'boolean':
  360. $state = $request->{$field} == 'on' ? true : false;
  361. if($state != $news->{$field}) {
  362. $news->{$field} = $state;
  363. $changed = true;
  364. array_push($changedFields, $field);
  365. }
  366. break;
  367. case 'published':
  368. $state = $request->{$field} == 'on' ? true : false;
  369. $published = $news->published_at != null;
  370. if($state != $published) {
  371. $news->published_at = $state ? now() : null;
  372. $changed = true;
  373. array_push($changedFields, $field);
  374. }
  375. break;
  376. }
  377. }
  378. if($changed) {
  379. $news->save();
  380. }
  381. $redirect = $news->published_at ? $news->permalink() : $news->editUrl();
  382. return redirect($redirect);
  383. }
  384. }