From 54f415afeb2897b2d7be1272bef160b77ed2630a Mon Sep 17 00:00:00 2001 From: Arthur Monney Date: Fri, 10 Dec 2021 15:58:16 +0100 Subject: [PATCH 1/5] :wheelchair: correction de l'enregistrement des username dans le bon format --- app/Actions/Fortify/CreateNewUser.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/Actions/Fortify/CreateNewUser.php b/app/Actions/Fortify/CreateNewUser.php index 9def52e5..92419947 100644 --- a/app/Actions/Fortify/CreateNewUser.php +++ b/app/Actions/Fortify/CreateNewUser.php @@ -5,6 +5,7 @@ use App\Models\User; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; +use Illuminate\Support\Str; use Illuminate\Validation\Rule; use Laravel\Fortify\Contracts\CreatesNewUsers; @@ -35,6 +36,7 @@ public function create(array $input): User 'string', 'min:6', 'max:20', + 'alpha_dash', Rule::unique(User::class, 'username'), ], 'password' => $this->passwordRules(), @@ -43,9 +45,9 @@ public function create(array $input): User return User::create([ 'name' => $input['name'], 'email' => $input['email'], - 'username' => $input['username'], - 'opt_in' => isset($input['opt_in']), + 'username' => Str::lower($input['username']), 'password' => Hash::make($input['password']), + 'opt_in' => isset($input['opt_in']), ]); } } From f4f7e8d8b44cc52a0fdfe8f4439987f63d86e1e1 Mon Sep 17 00:00:00 2001 From: Arthur Monney Date: Fri, 10 Dec 2021 19:12:35 +0100 Subject: [PATCH 2/5] :sparkles: mis en place des notifications --- app/Http/Controllers/ReplyAbleController.php | 15 +++ app/Http/Livewire/NotificationCount.php | 29 ++++++ app/Http/Livewire/NotificationIndicator.php | 31 ++++++ app/Http/Livewire/Notifications.php | 54 ++++++++++ app/Notifications/YouWereMentioned.php | 1 + app/Policies/NotificationPolicy.php | 19 ++++ app/Providers/AuthServiceProvider.php | 3 + app/helpers.php | 16 +++ .../notifications/new_comment.blade.php | 30 ++++++ .../notifications/new_mention.blade.php | 33 +++++++ .../notifications/new_reply.blade.php | 30 ++++++ resources/views/layouts/_nav.blade.php | 5 +- .../livewire/notification-count.blade.php | 3 + .../livewire/notification-indicator.blade.php | 2 + .../views/livewire/notifications.blade.php | 99 +++++++++++++++++++ resources/views/user/notifications.blade.php | 20 ++++ routes/web.php | 7 ++ 17 files changed, 395 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/ReplyAbleController.php create mode 100644 app/Http/Livewire/NotificationCount.php create mode 100644 app/Http/Livewire/NotificationIndicator.php create mode 100644 app/Http/Livewire/Notifications.php create mode 100644 app/Policies/NotificationPolicy.php create mode 100644 resources/views/components/notifications/new_comment.blade.php create mode 100644 resources/views/components/notifications/new_mention.blade.php create mode 100644 resources/views/components/notifications/new_reply.blade.php create mode 100644 resources/views/livewire/notification-count.blade.php create mode 100644 resources/views/livewire/notification-indicator.blade.php create mode 100644 resources/views/livewire/notifications.blade.php create mode 100644 resources/views/user/notifications.blade.php diff --git a/app/Http/Controllers/ReplyAbleController.php b/app/Http/Controllers/ReplyAbleController.php new file mode 100644 index 00000000..862a8529 --- /dev/null +++ b/app/Http/Controllers/ReplyAbleController.php @@ -0,0 +1,15 @@ +where('replyable_type', $type)->firstOrFail(); + + return redirect(route_to_reply_able($reply->replyAble)); + } +} diff --git a/app/Http/Livewire/NotificationCount.php b/app/Http/Livewire/NotificationCount.php new file mode 100644 index 00000000..959f2ed8 --- /dev/null +++ b/app/Http/Livewire/NotificationCount.php @@ -0,0 +1,29 @@ + 'updateCount', + ]; + + public function updateCount(int $count): int + { + return $count; + } + + public function render() + { + $this->count = Auth::user()->unreadNotifications()->count(); + + return view('livewire.notification-count', [ + 'count' => $this->count, + ]); + } +} diff --git a/app/Http/Livewire/NotificationIndicator.php b/app/Http/Livewire/NotificationIndicator.php new file mode 100644 index 00000000..03ec3e47 --- /dev/null +++ b/app/Http/Livewire/NotificationIndicator.php @@ -0,0 +1,31 @@ + 'setHasNotification', + ]; + + public function setHasNotification(int $count): bool + { + return $count > 0; + } + + public function render() + { + $this->hasNotification = $this->setHasNotification( + Auth::user()->unreadNotifications()->count(), + ); + + return view('livewire.notification-indicator', [ + 'hasNotification' => $this->hasNotification + ]); + } +} diff --git a/app/Http/Livewire/Notifications.php b/app/Http/Livewire/Notifications.php new file mode 100644 index 00000000..a9ca19e1 --- /dev/null +++ b/app/Http/Livewire/Notifications.php @@ -0,0 +1,54 @@ +notificationId); + } + + public function markAsRead(string $notificationId): void + { + $this->notificationId = $notificationId; + + $this->authorize(NotificationPolicy::MARK_AS_READ, $this->notification); + + $this->notification->markAsRead(); + + $this->notification()->success('Notification', 'Cette notification a été marquée comme lue.'); + + $this->emit('NotificationMarkedAsRead', Auth::user()->unreadNotifications()->count()); + } + + public function render() + { + return view('livewire.notifications', [ + 'notifications' => Auth::user() + ->unreadNotifications() + ->take(10) + ->get() + ->groupBy( + fn ($notification) => Carbon::parse($notification->created_at)->format('M, Y') + ), + ]); + } +} diff --git a/app/Notifications/YouWereMentioned.php b/app/Notifications/YouWereMentioned.php index 59b206e4..5fa83c6b 100644 --- a/app/Notifications/YouWereMentioned.php +++ b/app/Notifications/YouWereMentioned.php @@ -39,6 +39,7 @@ public function toArray($notifiable): array 'author_name' => $this->reply->author->name, 'author_username' => $this->reply->author->username, 'author_photo' => $this->reply->author->profile_photo_url, + 'replyable_id' => $this->reply->replyable_id, 'replyable_type' => $this->reply->replyable_type, 'replyable_subject' => $this->reply->replyAble->replyAbleSubject(), ]; diff --git a/app/Policies/NotificationPolicy.php b/app/Policies/NotificationPolicy.php new file mode 100644 index 00000000..c64dc1ef --- /dev/null +++ b/app/Policies/NotificationPolicy.php @@ -0,0 +1,19 @@ +notifiable->is($user); + } +} diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index e054b304..ee6353b8 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -8,9 +8,11 @@ use App\Models\Thread; use App\Policies\ArticlePolicy; use App\Policies\DiscussionPolicy; +use App\Policies\NotificationPolicy; use App\Policies\ReplyPolicy; use App\Policies\ThreadPolicy; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; +use Illuminate\Notifications\DatabaseNotification as Notification; use Illuminate\Support\Facades\Gate; class AuthServiceProvider extends ServiceProvider @@ -25,6 +27,7 @@ class AuthServiceProvider extends ServiceProvider Thread::class => ThreadPolicy::class, Reply::class => ReplyPolicy::class, Discussion::class => DiscussionPolicy::class, + Notification::class => NotificationPolicy::class, ]; /** diff --git a/app/helpers.php b/app/helpers.php index 1ad28e5a..812a407f 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -79,3 +79,19 @@ function getFilter(string $key, array $filters = [], string $default = 'recent') return in_array($filter, $filters) ? $filter : $default; } } + +if (! function_exists('route_to_reply_able')) { + /** + * Returns the route for the replyAble. + */ + function route_to_reply_able(mixed $replyAble) + { + if ($replyAble instanceof App\Models\Thread) { + return route('forum.show', $replyAble->slug()); + } + + if ($replyAble instanceof App\Models\Discussion) { + return route('discussions.show', $replyAble->slug()); + } + } +} diff --git a/resources/views/components/notifications/new_comment.blade.php b/resources/views/components/notifications/new_comment.blade.php new file mode 100644 index 00000000..dd722069 --- /dev/null +++ b/resources/views/components/notifications/new_comment.blade.php @@ -0,0 +1,30 @@ +@php($data = $notification->data) + +
  • +
    + +
    +
    +
    + + + +
    +

    Un commentaire a été ajoutée dans la conversation "{{ $data['replyable_subject'] }}".

    +

    + +

    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    +
  • diff --git a/resources/views/components/notifications/new_mention.blade.php b/resources/views/components/notifications/new_mention.blade.php new file mode 100644 index 00000000..0ecdd6fc --- /dev/null +++ b/resources/views/components/notifications/new_mention.blade.php @@ -0,0 +1,33 @@ +@php($data = $notification->data) + +
  • +
    + +
    +
    +
    + + + +
    +

    + {{ $data['author_name'] }} + vous a mentionné dans "{{ $data['replyable_subject'] }}". +

    +

    + +

    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    +
  • diff --git a/resources/views/components/notifications/new_reply.blade.php b/resources/views/components/notifications/new_reply.blade.php new file mode 100644 index 00000000..7d722b4b --- /dev/null +++ b/resources/views/components/notifications/new_reply.blade.php @@ -0,0 +1,30 @@ +@php($data = $notification->data) + +
  • +
    + +
    +
    +
    + + + +
    +

    Une nouvelle réponse a été ajoutée au sujet "{{ $data['replyable_subject'] }}".

    +

    + +

    +
    +
    +
    + +
    +
    + +
    +
    +
    +
    +
  • diff --git a/resources/views/layouts/_nav.blade.php b/resources/views/layouts/_nav.blade.php index 8930e7b3..44be6747 100644 --- a/resources/views/layouts/_nav.blade.php +++ b/resources/views/layouts/_nav.blade.php @@ -155,10 +155,11 @@ class="absolute z-10 -ml-4 mt-3 transform w-screen max-w-md lg:max-w-3xl lg:ml-0