From 2f92967a9c6c6aded094c84a67ed050862ba276c Mon Sep 17 00:00:00 2001 From: Jovan Jovanovic Date: Tue, 3 Aug 2021 00:10:59 +0200 Subject: [PATCH] Add notifications api --- .../Api/NotificationController.php | 27 +++++-- app/Notifications/DynamicNotification.php | 36 +++++---- composer.json | 5 +- composer.lock | 73 ++++++++++++++++++- 4 files changed, 118 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/Api/NotificationController.php b/app/Http/Controllers/Api/NotificationController.php index 84e89547..8ba3570a 100644 --- a/app/Http/Controllers/Api/NotificationController.php +++ b/app/Http/Controllers/Api/NotificationController.php @@ -8,6 +8,9 @@ use App\Models\User; use App\Notifications\DynamicNotification; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Illuminate\Notifications\Messages\MailMessage; +use Illuminate\Support\HtmlString; +use Spatie\ValidationRules\Rules\Delimited; class NotificationController extends Controller { @@ -58,13 +61,27 @@ class NotificationController extends Controller $discordUser = DiscordUser::find($userId); $user = $discordUser ? $discordUser->user : User::findOrFail($userId); - $body = $request->validate([ - "title" => "required:string|min:0", - "content" => "required:string|min:0" + $via = $request->validate([ + "via" => ["required", new Delimited("in:mail,database")] ]); - + $via = explode(',', $via["via"]); + $mail = null; + $database = null; + if (in_array('database', $via)) { + $database = $request->validate([ + "title" => "required_if:database,true|string|min:1", + "content" => "required_if:database,true|string|min:1" + ]); + } + if (in_array('mail', $via)) { + $data = $request->validate([ + "subject" => "required|string|min:1", + "body" => "required|string|min:1" + ]); + $mail = (new MailMessage)->subject($data["subject"])->line(new HtmlString($data["body"])); + } $user->notify( - new DynamicNotification($body["title"], $body["content"]) + new DynamicNotification($via, $database, $mail) ); return response()->json(["message" => "Notification successfully sent."]); diff --git a/app/Notifications/DynamicNotification.php b/app/Notifications/DynamicNotification.php index 0075de13..6e24c329 100644 --- a/app/Notifications/DynamicNotification.php +++ b/app/Notifications/DynamicNotification.php @@ -10,24 +10,29 @@ class DynamicNotification extends Notification { use Queueable; /** - * @var string + * @var array */ - private $title; + private $via; /** - * @var string + * @var array */ - private $content; - + private $database; + /** + * @var MailMessage + */ + private $mail; /** * Create a new notification instance. * - * @param string $title - * @param string $content + * @param array $via + * @param array $database + * @param MailMessage $mail */ - public function __construct($title, $content) + public function __construct($via, $database, $mail) { - $this->title = $title; - $this->content = $content; + $this->via = $via; + $this->database = $database; + $this->mail = $mail; } /** @@ -38,9 +43,13 @@ class DynamicNotification extends Notification */ public function via() { - return ['database']; + return $this->via; } + public function toMail() + { + return $this->mail; + } /** * Get the array representation of the notification. * @@ -49,9 +58,6 @@ class DynamicNotification extends Notification */ public function toArray() { - return [ - 'title' => $this->title, - 'content' => $this->content, - ]; + return $this->database; } } diff --git a/composer.json b/composer.json index cc28d042..a4c8a987 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "license": "MIT", "require": { "php": "^8.0|^7.4", + "ext-intl": "*", "biscolab/laravel-recaptcha": "^5.0", "doctrine/dbal": "^3.1", "fideloper/proxy": "^4.4", @@ -22,8 +23,8 @@ "paypal/rest-api-sdk-php": "^1.14", "socialiteproviders/discord": "^4.1", "spatie/laravel-activitylog": "^3.16", - "yajra/laravel-datatables-oracle": "~9.0", - "ext-intl": "*" + "spatie/laravel-validation-rules": "^3.0", + "yajra/laravel-datatables-oracle": "~9.0" }, "require-dev": { "facade/ignition": "^2.5", diff --git a/composer.lock b/composer.lock index 48624e80..9a0f0594 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": "dda32482531d11fdf6adc74fcba74715", + "content-hash": "eb7191a6b0476ec319915f6b98561af9", "packages": [ { "name": "asm89/stack-cors", @@ -3460,6 +3460,77 @@ ], "time": "2021-03-02T16:49:06+00:00" }, + { + "name": "spatie/laravel-validation-rules", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-validation-rules.git", + "reference": "43e15a70fb6148b0128d7981b0c0f3e99463b9fb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-validation-rules/zipball/43e15a70fb6148b0128d7981b0c0f3e99463b9fb", + "reference": "43e15a70fb6148b0128d7981b0c0f3e99463b9fb", + "shasum": "" + }, + "require": { + "illuminate/support": "^6.0|^7.0|^8.0", + "php": "^7.3|^8.0" + }, + "require-dev": { + "laravel/legacy-factories": "^1.0.4", + "myclabs/php-enum": "^1.6", + "orchestra/testbench": "^4.5|^5.0|^6.0", + "phpunit/phpunit": "^9.3", + "spatie/enum": "^2.2|^3.0" + }, + "suggest": { + "league/iso3166": "Needed for the CountryCode rule" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\ValidationRules\\ValidationRulesServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Spatie\\ValidationRules\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "A set of useful Laravel validation rules", + "homepage": "https://github.com/spatie/laravel-validation-rules", + "keywords": [ + "laravel-validation-rules", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-validation-rules/issues", + "source": "https://github.com/spatie/laravel-validation-rules/tree/3.0.0" + }, + "funding": [ + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2020-11-30T15:23:31+00:00" + }, { "name": "swiftmailer/swiftmailer", "version": "v6.2.7",