AdminController.php 14 KB

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