diff --git a/app/Contact.php b/app/Contact.php index afbabc58e..4d9bc56e8 100644 --- a/app/Contact.php +++ b/app/Contact.php @@ -13,6 +13,6 @@ class Contact extends Model public function adminUrl() { - return url('/i/admin/contact/show/' . $this->id); + return url('/i/admin/messages/show/' . $this->id); } } diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index a8bb4e489..bbfbbfdd2 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\{ + Contact, FailedJob, Hashtag, Instance, @@ -47,6 +48,10 @@ class AdminController extends Controller $data = Cache::remember('admin:dashboard:home:data', now()->addMinutes(15), function() { $day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day('; return [ + 'contact' => [ + 'count' => PrettyNumber::convert(Contact::whereNull('read_at')->count()), + 'graph' => Contact::selectRaw('count(*) as count, '.$day.'created_at) as day')->whereNull('read_at')->whereBetween('created_at',[now()->subDays(14), now()])->groupBy('day')->orderBy('day')->pluck('count') + ], 'failedjobs' => [ 'count' => PrettyNumber::convert(FailedJob::where('failed_at', '>=', \Carbon\Carbon::now()->subDay())->count()), 'graph' => FailedJob::selectRaw('count(*) as count, '.$day.'failed_at) as d')->groupBy('d')->whereBetween('failed_at',[now()->subDays(24), now()])->orderBy('d')->pluck('count') @@ -248,4 +253,30 @@ class AdminController extends Controller return view('admin.hashtags.home', compact('hashtags')); } + public function messagesHome(Request $request) + { + $messages = Contact::orderByDesc('id')->paginate(10); + return view('admin.messages.home', compact('messages')); + } + + public function messagesShow(Request $request, $id) + { + $message = Contact::findOrFail($id); + return view('admin.messages.show', compact('message')); + } + + public function messagesMarkRead(Request $request) + { + $this->validate($request, [ + 'id' => 'required|integer|min:1' + ]); + $id = $request->input('id'); + $message = Contact::findOrFail($id); + if($message->read_at) { + return; + } + $message->read_at = now(); + $message->save(); + return; + } } diff --git a/app/Http/Controllers/ContactController.php b/app/Http/Controllers/ContactController.php index da1591fe3..e95553021 100644 --- a/app/Http/Controllers/ContactController.php +++ b/app/Http/Controllers/ContactController.php @@ -5,16 +5,19 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Auth; use App\Contact; +use App\Jobs\ContactPipeline\ContactPipeline; class ContactController extends Controller { public function show(Request $request) { + abort_if(!config('instance.email') && !config('instance.contact.enabled'), 404); return view('site.contact'); } public function store(Request $request) { + abort_if(!config('instance.contact.enabled'), 404); abort_if(!Auth::check(), 403); $this->validate($request, [ @@ -26,11 +29,12 @@ class ContactController extends Controller $request_response = $request->input('request_response') == 'on' ? true : false; $user = Auth::user(); + $max = config('instance.contact.max_per_day'); $contact = Contact::whereUserId($user->id) - ->whereDate('created_at', '>', now()->subDays(1)) + ->whereDate('created_at', '>', now()->subDays($max)) ->count(); - if($contact >= 2) { + if($contact >= $max) { return redirect()->back()->with('error', 'You have recently sent a message. Please try again later.'); } @@ -40,6 +44,8 @@ class ContactController extends Controller $contact->message = $message; $contact->save(); + ContactPipeline::dispatchNow($contact); + return redirect()->back()->with('status', 'Success - Your message has been sent to admins.'); } } diff --git a/app/Jobs/ContactPipeline/ContactPipeline.php b/app/Jobs/ContactPipeline/ContactPipeline.php new file mode 100644 index 000000000..635367051 --- /dev/null +++ b/app/Jobs/ContactPipeline/ContactPipeline.php @@ -0,0 +1,44 @@ +contact = $contact; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $contact = $this->contact; + $email = config('instance.email'); + if(config('instance.contact.enabled') == false || $contact->read_at !== null || filter_var($email, FILTER_VALIDATE_EMAIL) == false) { + return; + } + Mail::to($email)->send(new ContactAdmin($contact)); + } +} diff --git a/config/instance.php b/config/instance.php index 53d6242c7..3842de358 100644 --- a/config/instance.php +++ b/config/instance.php @@ -2,4 +2,9 @@ return [ 'email' => env('INSTANCE_CONTACT_EMAIL'), + + 'contact' => [ + 'enabled' => env('INSTANCE_CONTACT_FORM', false), + 'max_per_day' => env('INSTANCE_CONTACT_MAX_PER_DAY', 1), + ], ]; \ No newline at end of file diff --git a/resources/lang/en/site.php b/resources/lang/en/site.php index 70b9e862d..f28f57459 100644 --- a/resources/lang/en/site.php +++ b/resources/lang/en/site.php @@ -12,5 +12,7 @@ return [ 'l10nWip' => 'We’re still working on localization support', 'currentLocale' => 'Current locale', 'selectLocale' => 'Select one of the supported languages', + 'contact' => 'Contact', + 'contact-us' => 'Contact Us', ]; diff --git a/resources/views/admin/apps/home.blade.php b/resources/views/admin/apps/home.blade.php index 8aaeb1170..aa8dfc0f2 100644 --- a/resources/views/admin/apps/home.blade.php +++ b/resources/views/admin/apps/home.blade.php @@ -17,7 +17,8 @@
# | @@ -45,4 +46,5 @@ @endforeach
---|
Alerts
-0
+New Messages
+{{$data['contact']['count']}}
# | +User | +Message | +Created | +
---|---|---|---|
+ + {{$msg->id}} + + | +{{$msg->user->username}} | +{{str_limit($msg->message, 40)}} | +{{$msg->created_at->diffForHumans()}} | +
Response Requested
+ @endif +Sent {{$message->created_at->diffForHumans()}}
+{{$message->message}}
+