AdminController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\{
  4. Contact,
  5. FailedJob,
  6. Hashtag,
  7. Instance,
  8. Media,
  9. Like,
  10. Newsroom,
  11. OauthClient,
  12. Profile,
  13. Report,
  14. Status,
  15. User
  16. };
  17. use DB, Cache;
  18. use Carbon\Carbon;
  19. use Illuminate\Http\Request;
  20. use App\Http\Controllers\Admin\{
  21. AdminDiscoverController,
  22. AdminInstanceController,
  23. AdminReportController,
  24. AdminMediaController,
  25. AdminSettingsController,
  26. AdminSupportController
  27. };
  28. use App\Util\Lexer\PrettyNumber;
  29. use Illuminate\Validation\Rule;
  30. class AdminController extends Controller
  31. {
  32. use AdminReportController,
  33. AdminDiscoverController,
  34. AdminMediaController,
  35. AdminSettingsController,
  36. AdminInstanceController;
  37. public function __construct()
  38. {
  39. $this->middleware('admin');
  40. $this->middleware('twofactor');
  41. }
  42. public function home()
  43. {
  44. $day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day(';
  45. $recent = Cache::remember('admin:dashboard:home:data:15min', now()->addMinutes(15), function() use ($day) {
  46. return [
  47. 'contact' => [
  48. 'count' => PrettyNumber::convert(Contact::whereNull('read_at')->count()),
  49. 'graph' => Contact::selectRaw('count(*) as count, '.$day.'created_at) as d')->groupBy('d')->whereNull('read_at')->whereBetween('created_at',[now()->subDays(14), now()])->orderBy('d')->pluck('count')
  50. ],
  51. 'failedjobs' => [
  52. 'count' => PrettyNumber::convert(FailedJob::where('failed_at', '>=', \Carbon\Carbon::now()->subDay())->count()),
  53. 'graph' => FailedJob::selectRaw('count(*) as count, '.$day.'failed_at) as d')->groupBy('d')->whereBetween('failed_at',[now()->subDays(14), now()])->orderBy('d')->pluck('count')
  54. ],
  55. 'reports' => [
  56. 'count' => PrettyNumber::convert(Report::whereNull('admin_seen')->count()),
  57. 'graph' => Report::selectRaw('count(*) as count, '.$day.'created_at) as d')->whereBetween('created_at',[now()->subDays(14), now()])->groupBy('d')->orderBy('d')->pluck('count')
  58. ],
  59. 'statuses' => [
  60. 'count' => PrettyNumber::convert(Status::whereNull('in_reply_to_id')->whereNull('reblog_of_id')->count()),
  61. 'graph' => Status::selectRaw('count(*) as count, '.$day.'created_at) as day')->whereBetween('created_at',[now()->subDays(14), now()])->groupBy('day')->orderBy('day')->pluck('count')
  62. ],
  63. 'replies' => [
  64. 'count' => PrettyNumber::convert(Status::whereNotNull('in_reply_to_id')->count()),
  65. 'graph' => Status::whereNotNull('in_reply_to_id')->selectRaw('count(*) as count, '.$day.'created_at) as day')->whereBetween('created_at',[now()->subDays(14), now()])->groupBy('day')->orderBy('day')->pluck('count')
  66. ],
  67. 'shares' => [
  68. 'count' => PrettyNumber::convert(Status::whereNotNull('reblog_of_id')->count()),
  69. 'graph' => Status::whereNotNull('reblog_of_id')->selectRaw('count(*) as count, '.$day.'created_at) as day')->whereBetween('created_at',[now()->subDays(14), now()])->groupBy('day')->orderBy('day')->pluck('count')
  70. ],
  71. 'likes' => [
  72. 'count' => PrettyNumber::convert(Like::count()),
  73. 'graph' => Like::selectRaw('count(*) as count, '.$day.'created_at) as day')->whereBetween('created_at',[now()->subDays(14), now()])->groupBy('day')->orderBy('day')->pluck('count')
  74. ],
  75. 'profiles' => [
  76. 'count' => PrettyNumber::convert(Profile::count()),
  77. 'graph' => Profile::selectRaw('count(*) as count, '.$day.'created_at) as day')->whereBetween('created_at',[now()->subDays(14), now()])->groupBy('day')->orderBy('day')->pluck('count')
  78. ],
  79. ];
  80. });
  81. $longer = Cache::remember('admin:dashboard:home:data:24hr', now()->addHours(24), function() use ($day) {
  82. return [
  83. 'users' => [
  84. 'count' => PrettyNumber::convert(User::count()),
  85. 'graph' => User::selectRaw('count(*) as count, '.$day.'created_at) as day')->whereBetween('created_at',[now()->subDays(14), now()])->groupBy('day')->orderBy('day')->pluck('count')
  86. ],
  87. 'instances' => [
  88. 'count' => PrettyNumber::convert(Instance::count()),
  89. 'graph' => Instance::selectRaw('count(*) as count, '.$day.'created_at) as day')->whereBetween('created_at',[now()->subDays(28), now()])->groupBy('day')->orderBy('day')->pluck('count')
  90. ],
  91. 'media' => [
  92. 'count' => PrettyNumber::convert(Media::count()),
  93. 'graph' => Media::selectRaw('count(*) as count, '.$day.'created_at) as day')->whereBetween('created_at',[now()->subDays(14), now()])->groupBy('day')->orderBy('day')->pluck('count')
  94. ],
  95. 'storage' => [
  96. 'count' => Media::sum('size'),
  97. 'graph' => Media::selectRaw('sum(size) as count, '.$day.'created_at) as day')->whereBetween('created_at',[now()->subDays(14), now()])->groupBy('day')->orderBy('day')->pluck('count')
  98. ]
  99. ];
  100. });
  101. $data = array_merge($recent, $longer);
  102. return view('admin.home', compact('data'));
  103. }
  104. public function users(Request $request)
  105. {
  106. $col = $request->query('col') ?? 'id';
  107. $dir = $request->query('dir') ?? 'desc';
  108. $users = User::select('id', 'username', 'status')->withCount('statuses')->orderBy($col, $dir)->simplePaginate(10);
  109. return view('admin.users.home', compact('users'));
  110. }
  111. public function editUser(Request $request, $id)
  112. {
  113. $user = User::findOrFail($id);
  114. $profile = $user->profile;
  115. return view('admin.users.edit', compact('user', 'profile'));
  116. }
  117. public function statuses(Request $request)
  118. {
  119. $statuses = Status::orderBy('id', 'desc')->simplePaginate(10);
  120. return view('admin.statuses.home', compact('statuses'));
  121. }
  122. public function showStatus(Request $request, $id)
  123. {
  124. $status = Status::findOrFail($id);
  125. return view('admin.statuses.show', compact('status'));
  126. }
  127. public function reports(Request $request)
  128. {
  129. $this->validate($request, [
  130. 'filter' => 'nullable|string|in:all,open,closed'
  131. ]);
  132. $filter = $request->input('filter');
  133. $reports = Report::orderBy('created_at','desc')
  134. ->when($filter, function($q, $filter) {
  135. return $filter == 'open' ?
  136. $q->whereNull('admin_seen') :
  137. $q->whereNotNull('admin_seen');
  138. })
  139. ->paginate(4);
  140. return view('admin.reports.home', compact('reports'));
  141. }
  142. public function showReport(Request $request, $id)
  143. {
  144. $report = Report::findOrFail($id);
  145. return view('admin.reports.show', compact('report'));
  146. }
  147. public function profiles(Request $request)
  148. {
  149. $this->validate($request, [
  150. 'search' => 'nullable|string|max:250',
  151. 'filter' => [
  152. 'nullable',
  153. 'string',
  154. Rule::in(['id','username','statuses_count','followers_count','likes_count'])
  155. ],
  156. 'order' => [
  157. 'nullable',
  158. 'string',
  159. Rule::in(['asc','desc'])
  160. ],
  161. 'layout' => [
  162. 'nullable',
  163. 'string',
  164. Rule::in(['card','list'])
  165. ],
  166. 'limit' => 'nullable|integer|min:1|max:50'
  167. ]);
  168. $search = $request->input('search');
  169. $filter = $request->input('filter');
  170. $order = $request->input('order') ?? 'desc';
  171. $limit = $request->input('limit') ?? 12;
  172. if($search) {
  173. $profiles = Profile::select('id','username')->where('username','like', "%$search%")->orderBy('id','desc')->simplePaginate($limit);
  174. } else if($filter && $order) {
  175. $profiles = Profile::select('id','username')->withCount(['likes','statuses','followers'])->orderBy($filter, $order)->simplePaginate($limit);
  176. } else {
  177. $profiles = Profile::select('id','username')->orderBy('id','desc')->simplePaginate($limit);
  178. }
  179. return view('admin.profiles.home', compact('profiles'));
  180. }
  181. public function profileShow(Request $request, $id)
  182. {
  183. $profile = Profile::findOrFail($id);
  184. $user = $profile->user;
  185. return view('admin.profiles.edit', compact('profile', 'user'));
  186. }
  187. public function appsHome(Request $request)
  188. {
  189. $filter = $request->input('filter');
  190. if(in_array($filter, ['revoked'])) {
  191. $apps = OauthClient::with('user')
  192. ->whereNotNull('user_id')
  193. ->whereRevoked(true)
  194. ->orderByDesc('id')
  195. ->paginate(10);
  196. } else {
  197. $apps = OauthClient::with('user')
  198. ->whereNotNull('user_id')
  199. ->orderByDesc('id')
  200. ->paginate(10);
  201. }
  202. return view('admin.apps.home', compact('apps'));
  203. }
  204. public function hashtagsHome(Request $request)
  205. {
  206. $hashtags = Hashtag::orderByDesc('id')->paginate(10);
  207. return view('admin.hashtags.home', compact('hashtags'));
  208. }
  209. public function messagesHome(Request $request)
  210. {
  211. $messages = Contact::orderByDesc('id')->paginate(10);
  212. return view('admin.messages.home', compact('messages'));
  213. }
  214. public function messagesShow(Request $request, $id)
  215. {
  216. $message = Contact::findOrFail($id);
  217. return view('admin.messages.show', compact('message'));
  218. }
  219. public function messagesMarkRead(Request $request)
  220. {
  221. $this->validate($request, [
  222. 'id' => 'required|integer|min:1'
  223. ]);
  224. $id = $request->input('id');
  225. $message = Contact::findOrFail($id);
  226. if($message->read_at) {
  227. return;
  228. }
  229. $message->read_at = now();
  230. $message->save();
  231. return;
  232. }
  233. public function newsroomHome(Request $request)
  234. {
  235. $newsroom = Newsroom::latest()->paginate(10);
  236. return view('admin.newsroom.home', compact('newsroom'));
  237. }
  238. public function newsroomCreate(Request $request)
  239. {
  240. return view('admin.newsroom.create');
  241. }
  242. public function newsroomEdit(Request $request, $id)
  243. {
  244. $news = Newsroom::findOrFail($id);
  245. return view('admin.newsroom.edit', compact('news'));
  246. }
  247. public function newsroomDelete(Request $request, $id)
  248. {
  249. $news = Newsroom::findOrFail($id);
  250. $news->delete();
  251. return redirect('/i/admin/newsroom');
  252. }
  253. public function newsroomUpdate(Request $request, $id)
  254. {
  255. $this->validate($request, [
  256. 'title' => 'required|string|min:1|max:100',
  257. 'summary' => 'nullable|string|max:200',
  258. 'body' => 'nullable|string'
  259. ]);
  260. $changed = false;
  261. $changedFields = [];
  262. $news = Newsroom::findOrFail($id);
  263. $fields = [
  264. 'title' => 'string',
  265. 'summary' => 'string',
  266. 'body' => 'string',
  267. 'category' => 'string',
  268. 'show_timeline' => 'boolean',
  269. 'auth_only' => 'boolean',
  270. 'show_link' => 'boolean',
  271. 'force_modal' => 'boolean',
  272. 'published' => 'published'
  273. ];
  274. foreach($fields as $field => $type) {
  275. switch ($type) {
  276. case 'string':
  277. if($request->{$field} != $news->{$field}) {
  278. if($field == 'title') {
  279. $news->slug = str_slug($request->{$field});
  280. }
  281. $news->{$field} = $request->{$field};
  282. $changed = true;
  283. array_push($changedFields, $field);
  284. }
  285. break;
  286. case 'boolean':
  287. $state = $request->{$field} == 'on' ? true : false;
  288. if($state != $news->{$field}) {
  289. $news->{$field} = $state;
  290. $changed = true;
  291. array_push($changedFields, $field);
  292. }
  293. break;
  294. case 'published':
  295. $state = $request->{$field} == 'on' ? true : false;
  296. $published = $news->published_at != null;
  297. if($state != $published) {
  298. $news->published_at = $state ? now() : null;
  299. $changed = true;
  300. array_push($changedFields, $field);
  301. }
  302. break;
  303. }
  304. }
  305. if($changed) {
  306. $news->save();
  307. }
  308. $redirect = $news->published_at ? $news->permalink() : $news->editUrl();
  309. return redirect($redirect);
  310. }
  311. public function newsroomStore(Request $request)
  312. {
  313. $this->validate($request, [
  314. 'title' => 'required|string|min:1|max:100',
  315. 'summary' => 'nullable|string|max:200',
  316. 'body' => 'nullable|string'
  317. ]);
  318. $changed = false;
  319. $changedFields = [];
  320. $news = new Newsroom();
  321. $fields = [
  322. 'title' => 'string',
  323. 'summary' => 'string',
  324. 'body' => 'string',
  325. 'category' => 'string',
  326. 'show_timeline' => 'boolean',
  327. 'auth_only' => 'boolean',
  328. 'show_link' => 'boolean',
  329. 'force_modal' => 'boolean',
  330. 'published' => 'published'
  331. ];
  332. foreach($fields as $field => $type) {
  333. switch ($type) {
  334. case 'string':
  335. if($request->{$field} != $news->{$field}) {
  336. if($field == 'title') {
  337. $news->slug = str_slug($request->{$field});
  338. }
  339. $news->{$field} = $request->{$field};
  340. $changed = true;
  341. array_push($changedFields, $field);
  342. }
  343. break;
  344. case 'boolean':
  345. $state = $request->{$field} == 'on' ? true : false;
  346. if($state != $news->{$field}) {
  347. $news->{$field} = $state;
  348. $changed = true;
  349. array_push($changedFields, $field);
  350. }
  351. break;
  352. case 'published':
  353. $state = $request->{$field} == 'on' ? true : false;
  354. $published = $news->published_at != null;
  355. if($state != $published) {
  356. $news->published_at = $state ? now() : null;
  357. $changed = true;
  358. array_push($changedFields, $field);
  359. }
  360. break;
  361. }
  362. }
  363. if($changed) {
  364. $news->save();
  365. }
  366. $redirect = $news->published_at ? $news->permalink() : $news->editUrl();
  367. return redirect($redirect);
  368. }
  369. }