AdminController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\{
  4. Contact,
  5. Hashtag,
  6. Newsroom,
  7. OauthClient,
  8. Profile,
  9. Report,
  10. Status,
  11. User
  12. };
  13. use DB, Cache;
  14. use Carbon\Carbon;
  15. use Illuminate\Http\Request;
  16. use App\Http\Controllers\Admin\{
  17. AdminDiscoverController,
  18. AdminInstanceController,
  19. AdminReportController,
  20. AdminMediaController,
  21. AdminSettingsController,
  22. AdminSupportController,
  23. AdminUserController
  24. };
  25. use Illuminate\Validation\Rule;
  26. use App\Services\AdminStatsService;
  27. class AdminController extends Controller
  28. {
  29. use AdminReportController,
  30. AdminDiscoverController,
  31. AdminMediaController,
  32. AdminSettingsController,
  33. AdminInstanceController,
  34. AdminUserController;
  35. public function __construct()
  36. {
  37. $this->middleware('admin');
  38. $this->middleware('dangerzone');
  39. $this->middleware('twofactor');
  40. }
  41. public function home()
  42. {
  43. $data = AdminStatsService::get();
  44. return view('admin.home', compact('data'));
  45. }
  46. public function statuses(Request $request)
  47. {
  48. $statuses = Status::orderBy('id', 'desc')->simplePaginate(10);
  49. return view('admin.statuses.home', compact('statuses'));
  50. }
  51. public function showStatus(Request $request, $id)
  52. {
  53. $status = Status::findOrFail($id);
  54. return view('admin.statuses.show', compact('status'));
  55. }
  56. public function reports(Request $request)
  57. {
  58. $this->validate($request, [
  59. 'filter' => 'nullable|string|in:all,open,closed'
  60. ]);
  61. $filter = $request->input('filter');
  62. $reports = Report::orderBy('created_at','desc')
  63. ->when($filter, function($q, $filter) {
  64. return $filter == 'open' ?
  65. $q->whereNull('admin_seen') :
  66. $q->whereNotNull('admin_seen');
  67. })
  68. ->paginate(4);
  69. return view('admin.reports.home', compact('reports'));
  70. }
  71. public function showReport(Request $request, $id)
  72. {
  73. $report = Report::findOrFail($id);
  74. return view('admin.reports.show', compact('report'));
  75. }
  76. public function profiles(Request $request)
  77. {
  78. $this->validate($request, [
  79. 'search' => 'nullable|string|max:250',
  80. 'filter' => [
  81. 'nullable',
  82. 'string',
  83. Rule::in(['all', 'local', 'remote'])
  84. ]
  85. ]);
  86. $search = $request->input('search');
  87. $filter = $request->input('filter');
  88. $limit = 12;
  89. $profiles = Profile::select('id','username')
  90. ->whereNull('status')
  91. ->when($search, function($q, $search) {
  92. return $q->where('username', 'like', "%$search%");
  93. })->when($filter, function($q, $filter) {
  94. if($filter == 'local') {
  95. return $q->whereNull('domain');
  96. }
  97. if($filter == 'remote') {
  98. return $q->whereNotNull('domain');
  99. }
  100. return $q;
  101. })->orderByDesc('id')
  102. ->simplePaginate($limit);
  103. return view('admin.profiles.home', compact('profiles'));
  104. }
  105. public function profileShow(Request $request, $id)
  106. {
  107. $profile = Profile::findOrFail($id);
  108. $user = $profile->user;
  109. return view('admin.profiles.edit', compact('profile', 'user'));
  110. }
  111. public function appsHome(Request $request)
  112. {
  113. $filter = $request->input('filter');
  114. if(in_array($filter, ['revoked'])) {
  115. $apps = OauthClient::with('user')
  116. ->whereNotNull('user_id')
  117. ->whereRevoked(true)
  118. ->orderByDesc('id')
  119. ->paginate(10);
  120. } else {
  121. $apps = OauthClient::with('user')
  122. ->whereNotNull('user_id')
  123. ->orderByDesc('id')
  124. ->paginate(10);
  125. }
  126. return view('admin.apps.home', compact('apps'));
  127. }
  128. public function hashtagsHome(Request $request)
  129. {
  130. $hashtags = Hashtag::orderByDesc('id')->paginate(10);
  131. return view('admin.hashtags.home', compact('hashtags'));
  132. }
  133. public function messagesHome(Request $request)
  134. {
  135. $messages = Contact::orderByDesc('id')->paginate(10);
  136. return view('admin.messages.home', compact('messages'));
  137. }
  138. public function messagesShow(Request $request, $id)
  139. {
  140. $message = Contact::findOrFail($id);
  141. return view('admin.messages.show', compact('message'));
  142. }
  143. public function messagesMarkRead(Request $request)
  144. {
  145. $this->validate($request, [
  146. 'id' => 'required|integer|min:1'
  147. ]);
  148. $id = $request->input('id');
  149. $message = Contact::findOrFail($id);
  150. if($message->read_at) {
  151. return;
  152. }
  153. $message->read_at = now();
  154. $message->save();
  155. return;
  156. }
  157. public function newsroomHome(Request $request)
  158. {
  159. $newsroom = Newsroom::latest()->paginate(10);
  160. return view('admin.newsroom.home', compact('newsroom'));
  161. }
  162. public function newsroomCreate(Request $request)
  163. {
  164. return view('admin.newsroom.create');
  165. }
  166. public function newsroomEdit(Request $request, $id)
  167. {
  168. $news = Newsroom::findOrFail($id);
  169. return view('admin.newsroom.edit', compact('news'));
  170. }
  171. public function newsroomDelete(Request $request, $id)
  172. {
  173. $news = Newsroom::findOrFail($id);
  174. $news->delete();
  175. return redirect('/i/admin/newsroom');
  176. }
  177. public function newsroomUpdate(Request $request, $id)
  178. {
  179. $this->validate($request, [
  180. 'title' => 'required|string|min:1|max:100',
  181. 'summary' => 'nullable|string|max:200',
  182. 'body' => 'nullable|string'
  183. ]);
  184. $changed = false;
  185. $changedFields = [];
  186. $news = Newsroom::findOrFail($id);
  187. $fields = [
  188. 'title' => 'string',
  189. 'summary' => 'string',
  190. 'body' => 'string',
  191. 'category' => 'string',
  192. 'show_timeline' => 'boolean',
  193. 'auth_only' => 'boolean',
  194. 'show_link' => 'boolean',
  195. 'force_modal' => 'boolean',
  196. 'published' => 'published'
  197. ];
  198. foreach($fields as $field => $type) {
  199. switch ($type) {
  200. case 'string':
  201. if($request->{$field} != $news->{$field}) {
  202. if($field == 'title') {
  203. $news->slug = str_slug($request->{$field});
  204. }
  205. $news->{$field} = $request->{$field};
  206. $changed = true;
  207. array_push($changedFields, $field);
  208. }
  209. break;
  210. case 'boolean':
  211. $state = $request->{$field} == 'on' ? true : false;
  212. if($state != $news->{$field}) {
  213. $news->{$field} = $state;
  214. $changed = true;
  215. array_push($changedFields, $field);
  216. }
  217. break;
  218. case 'published':
  219. $state = $request->{$field} == 'on' ? true : false;
  220. $published = $news->published_at != null;
  221. if($state != $published) {
  222. $news->published_at = $state ? now() : null;
  223. $changed = true;
  224. array_push($changedFields, $field);
  225. }
  226. break;
  227. }
  228. }
  229. if($changed) {
  230. $news->save();
  231. }
  232. $redirect = $news->published_at ? $news->permalink() : $news->editUrl();
  233. return redirect($redirect);
  234. }
  235. public function newsroomStore(Request $request)
  236. {
  237. $this->validate($request, [
  238. 'title' => 'required|string|min:1|max:100',
  239. 'summary' => 'nullable|string|max:200',
  240. 'body' => 'nullable|string'
  241. ]);
  242. $changed = false;
  243. $changedFields = [];
  244. $news = new Newsroom();
  245. $fields = [
  246. 'title' => 'string',
  247. 'summary' => 'string',
  248. 'body' => 'string',
  249. 'category' => 'string',
  250. 'show_timeline' => 'boolean',
  251. 'auth_only' => 'boolean',
  252. 'show_link' => 'boolean',
  253. 'force_modal' => 'boolean',
  254. 'published' => 'published'
  255. ];
  256. foreach($fields as $field => $type) {
  257. switch ($type) {
  258. case 'string':
  259. if($request->{$field} != $news->{$field}) {
  260. if($field == 'title') {
  261. $news->slug = str_slug($request->{$field});
  262. }
  263. $news->{$field} = $request->{$field};
  264. $changed = true;
  265. array_push($changedFields, $field);
  266. }
  267. break;
  268. case 'boolean':
  269. $state = $request->{$field} == 'on' ? true : false;
  270. if($state != $news->{$field}) {
  271. $news->{$field} = $state;
  272. $changed = true;
  273. array_push($changedFields, $field);
  274. }
  275. break;
  276. case 'published':
  277. $state = $request->{$field} == 'on' ? true : false;
  278. $published = $news->published_at != null;
  279. if($state != $published) {
  280. $news->published_at = $state ? now() : null;
  281. $changed = true;
  282. array_push($changedFields, $field);
  283. }
  284. break;
  285. }
  286. }
  287. if($changed) {
  288. $news->save();
  289. }
  290. $redirect = $news->published_at ? $news->permalink() : $news->editUrl();
  291. return redirect($redirect);
  292. }
  293. }