From 3004c438f13030ed7af080f6bd365abbee85a76e Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 10 Oct 2024 02:00:55 -0600 Subject: [PATCH] Update Admin routes --- app/Http/Controllers/AdminController.php | 54 ++++++++++++++++++++++-- routes/web-admin.php | 2 + 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index 3e292037c..c25e25beb 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -13,6 +13,7 @@ use App\Http\Controllers\Admin\AdminReportController; use App\Http\Controllers\Admin\AdminSettingsController; use App\Http\Controllers\Admin\AdminUserController; use App\Instance; +use App\Mail\AdminMessageResponse; use App\Models\CustomEmoji; use App\Newsroom; use App\OauthClient; @@ -29,6 +30,7 @@ use Cache; use DB; use Illuminate\Http\Request; use Illuminate\Validation\Rule; +use Mail; use Storage; class AdminController extends Controller @@ -221,18 +223,62 @@ class AdminController extends Controller public function messagesHome(Request $request) { - $messages = Contact::orderByDesc('id')->paginate(10); + $this->validate($request, [ + 'sort' => 'sometimes|string|in:all,open,closed', + ]); + $sort = $request->input('sort', 'open'); - return view('admin.messages.home', compact('messages')); + $messages = Contact::when($sort, function ($query, $sort) { + if ($sort === 'open') { + $query->whereNull('read_at'); + } + if ($sort === 'closed') { + $query->whereNotNull('read_at'); + } + }) + ->orderByDesc('id') + ->paginate(10) + ->withQueryString(); + + return view('admin.messages.home', compact('messages', 'sort')); } public function messagesShow(Request $request, $id) { $message = Contact::findOrFail($id); + $user = User::whereNull('status')->findOrFail($message->user_id); return view('admin.messages.show', compact('message')); } + public function messagesReply(Request $request, $id) + { + $this->validate($request, [ + 'message' => 'required|string|min:1|max:500', + ]); + + $message = Contact::whereNull('responded_at')->findOrFail($id); + $message->response = $request->input('message'); + $message->read_at = now(); + $message->responded_at = now(); + $message->save(); + + Mail::to($message->user->email)->send(new AdminMessageResponse($message)); + + return redirect('/i/admin/messages/home')->with('status', 'Sent response to '.$message->user->username); + } + + public function messagesReplyPreview(Request $request, $id) + { + $this->validate($request, [ + 'message' => 'required|string|min:1|max:500', + ]); + + $message = Contact::whereNull('read_at')->findOrFail($id); + + return new AdminMessageResponse($message); + } + public function messagesMarkRead(Request $request) { $this->validate($request, [ @@ -245,7 +291,9 @@ class AdminController extends Controller } $message->read_at = now(); $message->save(); + $request->session()->flash('status', 'Marked response from '.$message->user->username.' as read!'); + return ['status' => 200]; } public function newsroomHome(Request $request) @@ -355,7 +403,7 @@ class AdminController extends Controller if (Newsroom::whereSlug($slug)->exists()) { $slug = $slug.'-'.str_random(4); } - $news = new Newsroom(); + $news = new Newsroom; $fields = [ 'title' => 'string', 'summary' => 'string', diff --git a/routes/web-admin.php b/routes/web-admin.php index bb206aaaa..3d5b1cfd9 100644 --- a/routes/web-admin.php +++ b/routes/web-admin.php @@ -77,6 +77,8 @@ Route::domain(config('pixelfed.domain.admin'))->prefix('i/admin')->group(functio Route::get('messages/home', 'AdminController@messagesHome')->name('admin.messages'); Route::get('messages/show/{id}', 'AdminController@messagesShow'); Route::post('messages/mark-read', 'AdminController@messagesMarkRead'); + Route::post('messages/show/{id}', 'AdminController@messagesReply'); + Route::post('messages/preview/{id}', 'AdminController@messagesReplyPreview'); Route::redirect('site-news', '/i/admin/newsroom'); Route::get('newsroom', 'AdminController@newsroomHome')->name('admin.newsroom.home'); Route::get('newsroom/create', 'AdminController@newsroomCreate')->name('admin.newsroom.create');