From e97ae5572503eaa30203eb354c45cb02d370575a Mon Sep 17 00:00:00 2001 From: Jovan Jovanovic Date: Wed, 4 Aug 2021 22:01:50 +0200 Subject: [PATCH] Send notification to multiple users --- app/Http/Controllers/Admin/UserController.php | 42 +++++++-- .../Api/NotificationController.php | 21 ++--- composer.json | 1 + composer.lock | 72 +++++++++++++- resources/views/admin/users/index.blade.php | 2 + .../views/admin/users/notifications.blade.php | 93 ++++++++++++++++++- resources/views/layouts/main.blade.php | 6 ++ routes/api.php | 2 +- routes/web.php | 5 +- 9 files changed, 216 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 52057914..502e8f89 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -6,6 +6,7 @@ use App\Classes\Pterodactyl; use App\Http\Controllers\Controller; use App\Models\User; use App\Notifications\DynamicNotification; +use Spatie\QueryBuilder\QueryBuilder; use Exception; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; @@ -16,6 +17,7 @@ use Illuminate\Http\Response; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Notification; use Illuminate\Support\HtmlString; use Illuminate\Validation\Rule; use Illuminate\Validation\ValidationException; @@ -53,6 +55,28 @@ class UserController extends Controller ]); } + /** + * Get a JSON response of users. + * + * @return \Illuminate\Support\Collection|\App\models\User + */ + public function json(Request $request) + { + $users = QueryBuilder::for(User::query())->allowedFilters(['id', 'name', 'pterodactyl_id', 'email'])->paginate(25); + + if ($request->query('user_id')) { + $user = User::query()->findOrFail($request->input('user_id')); + $user->avatarUrl = $user->getAvatar(); + + return $user; + } + + return $users->map(function ($item) { + $item->avatarUrl = $item->getAvatar(); + + return $item; + }); + } /** * Show the form for editing the specified resource. * @@ -152,9 +176,7 @@ class UserController extends Controller */ public function notifications(User $user) { - return view('admin.users.notifications')->with([ - 'user' => $user - ]); + return view('admin.users.notifications'); } /** @@ -165,11 +187,14 @@ class UserController extends Controller * @return RedirectResponse * @throws Exception */ - public function notify(Request $request, User $user) + public function notify(Request $request) { $data = $request->validate([ "via" => "required|min:1|array", "via.*" => "required|string|in:mail,database", + "all" => "required_without:users|boolean", + "users" => "required_without:all|min:1|array", + "users.*" => "exists:users,id", "title" => "required|string|min:1", "content" => "required|string|min:1" ]); @@ -187,10 +212,10 @@ class UserController extends Controller ->subject($data["title"]) ->line(new HtmlString($data["content"])); } - $user->notify( - new DynamicNotification($data["via"], $database, $mail) - ); - return redirect()->route('admin.users.notifications', $user->id)->with('success', 'User notified!'); + $all = $data["all"] ?? false; + $users = $all ? User::all() : User::whereIn("id", $data["users"])->get(); + Notification::send($users, new DynamicNotification($data["via"], $database, $mail)); + return redirect()->route('admin.users.notifications')->with('success', 'Notification sent!'); } /** @@ -228,7 +253,6 @@ class UserController extends Controller -
' . csrf_field() . ' ' . method_field("DELETE") . ' diff --git a/app/Http/Controllers/Api/NotificationController.php b/app/Http/Controllers/Api/NotificationController.php index a5bf3084..2371e524 100644 --- a/app/Http/Controllers/Api/NotificationController.php +++ b/app/Http/Controllers/Api/NotificationController.php @@ -9,6 +9,7 @@ use App\Notifications\DynamicNotification; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Notifications\Messages\MailMessage; +use Illuminate\Support\Facades\Notification; use Illuminate\Support\HtmlString; use Spatie\ValidationRules\Rules\Delimited; @@ -56,34 +57,32 @@ class NotificationController extends Controller * @param int $userId * @return JsonResponse */ - public function send(Request $request, int $userId) + public function send(Request $request) { - $discordUser = DiscordUser::find($userId); - $user = $discordUser ? $discordUser->user : User::findOrFail($userId); - $data = $request->validate([ "via" => ["required", new Delimited("in:mail,database")], + "all" => "required_without:users|boolean", + "users" => ["required_without:all", new Delimited("exists:users,id")], "title" => "required|string|min:1", "content" => "required|string|min:1" ]); - $via = explode(',', $data["via"]); + $via = explode(",", $data["via"]); $mail = null; $database = null; - if (in_array('database', $via)) { + if (in_array("database", $via)) { $database = [ "title" => $data["title"], "content" => $data["content"] ]; } - if (in_array('mail', $via)) { + if (in_array("mail", $via)) { $mail = (new MailMessage) ->subject($data["title"]) ->line(new HtmlString($data["content"])); } - $user->notify( - new DynamicNotification($via, $database, $mail) - ); - + $all = $data["all"] ?? false; + $users = $all ? User::all() : User::whereIn("id", explode(",", $data["users"]))->get(); + Notification::send($users, new DynamicNotification($via, $database, $mail)); return response()->json(["message" => "Notification successfully sent."]); } diff --git a/composer.json b/composer.json index a4c8a987..3edec1b4 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "paypal/rest-api-sdk-php": "^1.14", "socialiteproviders/discord": "^4.1", "spatie/laravel-activitylog": "^3.16", + "spatie/laravel-query-builder": "^3.5", "spatie/laravel-validation-rules": "^3.0", "yajra/laravel-datatables-oracle": "~9.0" }, diff --git a/composer.lock b/composer.lock index 9a0f0594..14abe8de 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "eb7191a6b0476ec319915f6b98561af9", + "content-hash": "b3b61a46d5d4d6560d052cfda863d12c", "packages": [ { "name": "asm89/stack-cors", @@ -3460,6 +3460,76 @@ ], "time": "2021-03-02T16:49:06+00:00" }, + { + "name": "spatie/laravel-query-builder", + "version": "3.5.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-query-builder.git", + "reference": "4e5257be24139836dc092f618d7c73bcb1c00302" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-query-builder/zipball/4e5257be24139836dc092f618d7c73bcb1c00302", + "reference": "4e5257be24139836dc092f618d7c73bcb1c00302", + "shasum": "" + }, + "require": { + "illuminate/database": "^6.20.13|^7.30.4|^8.22.2", + "illuminate/http": "^6.20.13|7.30.4|^8.22.2", + "illuminate/support": "^6.20.13|7.30.4|^8.22.2", + "php": "^7.3|^8.0" + }, + "require-dev": { + "ext-json": "*", + "laravel/legacy-factories": "^1.0.4", + "mockery/mockery": "^1.4", + "orchestra/testbench": "^4.9|^5.8|^6.3", + "phpunit/phpunit": "^9.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\QueryBuilder\\QueryBuilderServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Spatie\\QueryBuilder\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Alex Vanderbist", + "email": "alex@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Easily build Eloquent queries from API requests", + "homepage": "https://github.com/spatie/laravel-query-builder", + "keywords": [ + "laravel-query-builder", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-query-builder/issues", + "source": "https://github.com/spatie/laravel-query-builder" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + } + ], + "time": "2021-07-05T14:17:44+00:00" + }, { "name": "spatie/laravel-validation-rules", "version": "3.0.0", diff --git a/resources/views/admin/users/index.blade.php b/resources/views/admin/users/index.blade.php index b5c81c5e..364bfdba 100644 --- a/resources/views/admin/users/index.blade.php +++ b/resources/views/admin/users/index.blade.php @@ -28,6 +28,8 @@
Users
+ Notify
diff --git a/resources/views/admin/users/notifications.blade.php b/resources/views/admin/users/notifications.blade.php index ba7806c6..e4390903 100644 --- a/resources/views/admin/users/notifications.blade.php +++ b/resources/views/admin/users/notifications.blade.php @@ -1,5 +1,4 @@ @extends('layouts.main') - @section('content')
@@ -13,7 +12,7 @@ + href="{{route('admin.users.notifications')}}">Notifications @@ -29,10 +28,31 @@
- + @csrf @method('POST') +
+
+ + +
+ +
+ @error('all') +
+ {{$message}} +
+ @enderror + @error('users') +
+ {{$message}} +
+ @enderror +
+

" + + "
" + + "
" + + "
" + + "" + + "
" + + "
" + ); + + $container.find(".select2-result-repository__username").text(data.name); + $container.find(".select2-result-repository__email").text(data.email); + + return $container; + {{-- return $('
\ + User Image \ + \ + ' + escapeHtml(data.name) +' \ + \ + ' + escapeHtml(data.email) + ' \ +
'); --}} + }, + templateSelection: function (data) { + return $('
\ + \ + User Image \ + \ + \ + ' + escapeHtml(data.name) +' \ + \ +
'); + } + }) + }) + + function toggleClass(id, className) { + document.getElementById(id).classList.toggle(className) + } + function escapeHtml(str) { + var div = document.createElement('div'); + div.appendChild(document.createTextNode(str)); + return div.innerHTML; + } diff --git a/resources/views/layouts/main.blade.php b/resources/views/layouts/main.blade.php index c9ff4bfd..9f22a2bd 100644 --- a/resources/views/layouts/main.blade.php +++ b/resources/views/layouts/main.blade.php @@ -19,6 +19,9 @@ {{-- datetimepicker --}} + {{-- select2 --}} + + @@ -344,6 +347,9 @@ + +