AdminController.php 9.0 KB

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