From 2f5f22571f9116536a7186ad47e604f84da56dd6 Mon Sep 17 00:00:00 2001 From: sodu Date: Wed, 1 Feb 2023 02:39:39 +0500 Subject: [PATCH 01/10] feature #827 --- app/Enums/NotificationTypes.php | 38 ++++++++++++++++++ ...dd_notifications_column_to_users_table.php | 33 ++++++++++++++++ .../forms/inputs/checkbox.blade.php | 2 +- .../settings/notification_settings.blade.php | 39 +++++++++++++++++++ .../views/users/settings/settings.blade.php | 1 + 5 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 app/Enums/NotificationTypes.php create mode 100755 database/migrations/2023_01_31_190506_add_notifications_column_to_users_table.php create mode 100644 resources/views/users/settings/notification_settings.blade.php diff --git a/app/Enums/NotificationTypes.php b/app/Enums/NotificationTypes.php new file mode 100644 index 000000000..e0698bc76 --- /dev/null +++ b/app/Enums/NotificationTypes.php @@ -0,0 +1,38 @@ + MentionNotification::class, + self::REPLY => NewReplyNotification::class, + }; + } + + public function label(): string + { + return match($this) + { + self::MENTION => "Mention", + self::REPLY => "Reply", + }; + } + + public static function getTypes(): array + { + return [ + self::MENTION->value => self::MENTION->label(), + self::REPLY->value => self::REPLY->label(), + ]; + } +} \ No newline at end of file diff --git a/database/migrations/2023_01_31_190506_add_notifications_column_to_users_table.php b/database/migrations/2023_01_31_190506_add_notifications_column_to_users_table.php new file mode 100755 index 000000000..a0c25f183 --- /dev/null +++ b/database/migrations/2023_01_31_190506_add_notifications_column_to_users_table.php @@ -0,0 +1,33 @@ +json('notifications') + ->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('notifications'); + }); + } +}; diff --git a/resources/views/components/forms/inputs/checkbox.blade.php b/resources/views/components/forms/inputs/checkbox.blade.php index 30b9203fe..0a573aa53 100644 --- a/resources/views/components/forms/inputs/checkbox.blade.php +++ b/resources/views/components/forms/inputs/checkbox.blade.php @@ -1,6 +1,6 @@
- +
From 6171dd968470aa85d4f991beda54d6245c3db8ca Mon Sep 17 00:00:00 2001 From: sodu Date: Wed, 1 Feb 2023 04:57:43 +0500 Subject: [PATCH 02/10] feature #827 --- app/Enums/NotificationTypes.php | 4 +- .../NotificationSettingsController.php | 22 ++++++++ .../SaveNotificationSettingsRequest.php | 15 ++++++ app/Jobs/SaveNotificationSettings.php | 31 +++++++++++ app/Models/User.php | 18 +++++++ .../forms/inputs/checkbox.blade.php | 2 +- .../settings/notification_settings.blade.php | 52 +++++++++++-------- routes/web.php | 2 + 8 files changed, 120 insertions(+), 26 deletions(-) create mode 100644 app/Http/Controllers/Settings/NotificationSettingsController.php create mode 100644 app/Http/Requests/SaveNotificationSettingsRequest.php create mode 100644 app/Jobs/SaveNotificationSettings.php diff --git a/app/Enums/NotificationTypes.php b/app/Enums/NotificationTypes.php index e0698bc76..ab1f8190d 100644 --- a/app/Enums/NotificationTypes.php +++ b/app/Enums/NotificationTypes.php @@ -31,8 +31,8 @@ public function label(): string public static function getTypes(): array { return [ - self::MENTION->value => self::MENTION->label(), - self::REPLY->value => self::REPLY->label(), + self::MENTION->value => self::MENTION, + self::REPLY->value => self::REPLY, ]; } } \ No newline at end of file diff --git a/app/Http/Controllers/Settings/NotificationSettingsController.php b/app/Http/Controllers/Settings/NotificationSettingsController.php new file mode 100644 index 000000000..c2fe5cab7 --- /dev/null +++ b/app/Http/Controllers/Settings/NotificationSettingsController.php @@ -0,0 +1,22 @@ +middleware(Authenticate::class); + } + + public function store(SaveNotificationSettingsRequest $request) + { + $this->dispatchSync(new SaveNotificationSettings($user = $request->user(), (array) $request->validated('notification_types'))); + return redirect()->route('settings.profile'); + } +} diff --git a/app/Http/Requests/SaveNotificationSettingsRequest.php b/app/Http/Requests/SaveNotificationSettingsRequest.php new file mode 100644 index 000000000..2768394df --- /dev/null +++ b/app/Http/Requests/SaveNotificationSettingsRequest.php @@ -0,0 +1,15 @@ + 'string' + ]; + } +} diff --git a/app/Jobs/SaveNotificationSettings.php b/app/Jobs/SaveNotificationSettings.php new file mode 100644 index 000000000..bb78a69a6 --- /dev/null +++ b/app/Jobs/SaveNotificationSettings.php @@ -0,0 +1,31 @@ +allowedNotifications); + if (!empty($not_allowed_notifications)) { + $notifications = []; + foreach ($not_allowed_notifications as $type) { + $notifications[] = [$type => false]; + } + $this->user->notifications = $notifications; + $this->user->save(); + } + else { + $this->user->notifications = NULL; + $this->user->save(); + } + } +} diff --git a/app/Models/User.php b/app/Models/User.php index f06783a61..54d84688e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -12,6 +12,8 @@ use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Auth; use Laravel\Sanctum\HasApiTokens; +use App\Enums\NotificationTypes; +use Illuminate\Support\Arr; final class User extends Authenticatable implements MustVerifyEmail { @@ -52,6 +54,10 @@ final class User extends Authenticatable implements MustVerifyEmail 'banned_reason', ]; + protected $casts = [ + 'notifications' => 'array' + ]; + /** * {@inheritdoc} */ @@ -351,4 +357,16 @@ public function scopeWithUsersWhoArentBlockedBy(Builder $query, User $user) $query->where('user_id', $user->getKey()); }); } + + public function isNotificationAllowed(string $notification_class): bool + { + if (!empty($this->notifications)) { + foreach (Arr::collapse($this->notifications) as $notification_type => $value) { + if (NotificationTypes::from($notification_type)->getClass() == $notification_class) { + return FALSE; + } + } + } + return TRUE; + } } diff --git a/resources/views/components/forms/inputs/checkbox.blade.php b/resources/views/components/forms/inputs/checkbox.blade.php index 0a573aa53..f65354d63 100644 --- a/resources/views/components/forms/inputs/checkbox.blade.php +++ b/resources/views/components/forms/inputs/checkbox.blade.php @@ -1,6 +1,6 @@
- +