From ecab2c8e42ae11485bb3c0fcce1a51df1bc6b118 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 5 Aug 2023 14:19:23 +0100 Subject: [PATCH] Notifications: Added logic and classes for remaining notification types --- app/Activity/Models/Comment.php | 8 ++++ .../Handlers/BaseNotificationHandler.php | 45 +++++++++++++++++++ .../CommentCreationNotificationHandler.php | 35 ++++++++++++++- .../PageCreationNotificationHandler.php | 25 +---------- .../PageUpdateNotificationHandler.php | 23 +++++++++- .../Messages/CommentCreationNotification.php | 32 +++++++++++++ .../Messages/PageUpdateNotification.php | 29 ++++++++++++ app/Activity/Tools/EntityWatchers.php | 5 +++ 8 files changed, 175 insertions(+), 27 deletions(-) create mode 100644 app/Activity/Notifications/Handlers/BaseNotificationHandler.php create mode 100644 app/Activity/Notifications/Messages/CommentCreationNotification.php create mode 100644 app/Activity/Notifications/Messages/PageUpdateNotification.php diff --git a/app/Activity/Models/Comment.php b/app/Activity/Models/Comment.php index 7aea6124a..72098a3c3 100644 --- a/app/Activity/Models/Comment.php +++ b/app/Activity/Models/Comment.php @@ -32,6 +32,14 @@ class Comment extends Model implements Loggable return $this->morphTo('entity'); } + /** + * Get the parent comment this is in reply to (if existing). + */ + public function parent() + { + return $this->belongsTo(Comment::class); + } + /** * Check if a comment has been updated since creation. */ diff --git a/app/Activity/Notifications/Handlers/BaseNotificationHandler.php b/app/Activity/Notifications/Handlers/BaseNotificationHandler.php new file mode 100644 index 000000000..e0b3f3ceb --- /dev/null +++ b/app/Activity/Notifications/Handlers/BaseNotificationHandler.php @@ -0,0 +1,45 @@ + $notification + * @param int[] $userIds + */ + protected function sendNotificationToUserIds(string $notification, array $userIds, User $initiator, Entity $relatedModel): void + { + $users = User::query()->whereIn('id', array_unique($userIds))->get(); + + foreach ($users as $user) { + // Prevent sending to the user that initiated the activity + if ($user->id === $initiator->id) { + continue; + } + + // Prevent sending of the user does not have notification permissions + if (!$user->can('receive-notifications')) { + continue; + } + + // Prevent sending if the user does not have access to the related content + if (!$this->permissionApplicator->checkOwnableUserAccess($relatedModel, 'view')) { + continue; + } + + // Send the notification + $user->notify(new $notification($relatedModel, $initiator)); + } + } +} diff --git a/app/Activity/Notifications/Handlers/CommentCreationNotificationHandler.php b/app/Activity/Notifications/Handlers/CommentCreationNotificationHandler.php index 67c304339..27d61307a 100644 --- a/app/Activity/Notifications/Handlers/CommentCreationNotificationHandler.php +++ b/app/Activity/Notifications/Handlers/CommentCreationNotificationHandler.php @@ -2,13 +2,44 @@ namespace BookStack\Activity\Notifications\Handlers; +use BookStack\Activity\Models\Comment; use BookStack\Activity\Models\Loggable; +use BookStack\Activity\Notifications\Messages\CommentCreationNotification; +use BookStack\Activity\Tools\EntityWatchers; +use BookStack\Activity\WatchLevels; +use BookStack\Settings\UserNotificationPreferences; use BookStack\Users\Models\User; -class CommentCreationNotificationHandler implements NotificationHandler +class CommentCreationNotificationHandler extends BaseNotificationHandler { public function handle(string $activityType, Loggable|string $detail, User $user): void { - // TODO + if (!($detail instanceof Comment)) { + throw new \InvalidArgumentException("Detail for comment creation notifications must be a comment"); + } + + // Main watchers + $page = $detail->entity; + $watchers = new EntityWatchers($page, WatchLevels::COMMENTS); + $watcherIds = $watchers->getWatcherUserIds(); + + // Page owner if user preferences allow + if (!$watchers->isUserIgnoring($detail->owned_by) && $detail->ownedBy) { + $userNotificationPrefs = new UserNotificationPreferences($detail->ownedBy); + if ($userNotificationPrefs->notifyOnOwnPageComments()) { + $watcherIds[] = $detail->owned_by; + } + } + + // Parent comment creator if preferences allow + $parentComment = $detail->parent()->first(); + if ($parentComment && !$watchers->isUserIgnoring($parentComment->created_by) && $parentComment->createdBy) { + $parentCommenterNotificationsPrefs = new UserNotificationPreferences($parentComment->createdBy); + if ($parentCommenterNotificationsPrefs->notifyOnCommentReplies()) { + $watcherIds[] = $parentComment->created_by; + } + } + + $this->sendNotificationToUserIds(CommentCreationNotification::class, $watcherIds, $user, $page); } } diff --git a/app/Activity/Notifications/Handlers/PageCreationNotificationHandler.php b/app/Activity/Notifications/Handlers/PageCreationNotificationHandler.php index 8f19b3558..e9aca2f23 100644 --- a/app/Activity/Notifications/Handlers/PageCreationNotificationHandler.php +++ b/app/Activity/Notifications/Handlers/PageCreationNotificationHandler.php @@ -7,38 +7,17 @@ use BookStack\Activity\Notifications\Messages\PageCreationNotification; use BookStack\Activity\Tools\EntityWatchers; use BookStack\Activity\WatchLevels; use BookStack\Entities\Models\Page; -use BookStack\Permissions\PermissionApplicator; use BookStack\Users\Models\User; -class PageCreationNotificationHandler implements NotificationHandler +class PageCreationNotificationHandler extends BaseNotificationHandler { public function handle(string $activityType, Loggable|string $detail, User $user): void { if (!($detail instanceof Page)) { throw new \InvalidArgumentException("Detail for page create notifications must be a page"); } - // No user-level preferences to care about here. - // Possible Scenarios: - // ✅ User watching parent chapter - // ✅ User watching parent book - // ❌ User ignoring parent book - // ❌ User ignoring parent chapter - // ❌ User watching parent book, ignoring chapter - // ✅ User watching parent book, watching chapter - // ❌ User ignoring parent book, ignoring chapter - // ✅ User ignoring parent book, watching chapter - // Get all relevant watchers $watchers = new EntityWatchers($detail, WatchLevels::NEW); - $users = User::query()->whereIn('id', $watchers->getWatcherUserIds())->get(); - - // TODO - Clean this up, likely abstract to base class - // TODO - Prevent sending to current user - $permissions = app()->make(PermissionApplicator::class); - foreach ($users as $user) { - if ($user->can('receive-notifications') && $permissions->checkOwnableUserAccess($detail, 'view')) { - $user->notify(new PageCreationNotification($detail, $user)); - } - } + $this->sendNotificationToUserIds(PageCreationNotification::class, $watchers->getWatcherUserIds(), $user, $detail); } } diff --git a/app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php b/app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php index bbd189d52..5a2bf4e9c 100644 --- a/app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php +++ b/app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php @@ -3,12 +3,31 @@ namespace BookStack\Activity\Notifications\Handlers; use BookStack\Activity\Models\Loggable; +use BookStack\Activity\Notifications\Messages\PageUpdateNotification; +use BookStack\Activity\Tools\EntityWatchers; +use BookStack\Activity\WatchLevels; +use BookStack\Entities\Models\Page; +use BookStack\Settings\UserNotificationPreferences; use BookStack\Users\Models\User; -class PageUpdateNotificationHandler implements NotificationHandler +class PageUpdateNotificationHandler extends BaseNotificationHandler { public function handle(string $activityType, Loggable|string $detail, User $user): void { - // TODO + if (!($detail instanceof Page)) { + throw new \InvalidArgumentException("Detail for page update notifications must be a page"); + } + + $watchers = new EntityWatchers($detail, WatchLevels::UPDATES); + $watcherIds = $watchers->getWatcherUserIds(); + + if (!$watchers->isUserIgnoring($detail->owned_by) && $detail->ownedBy) { + $userNotificationPrefs = new UserNotificationPreferences($detail->ownedBy); + if ($userNotificationPrefs->notifyOnOwnPageChanges()) { + $watcherIds[] = $detail->owned_by; + } + } + + $this->sendNotificationToUserIds(PageUpdateNotification::class, $watcherIds, $user, $detail); } } diff --git a/app/Activity/Notifications/Messages/CommentCreationNotification.php b/app/Activity/Notifications/Messages/CommentCreationNotification.php new file mode 100644 index 000000000..817eb7b84 --- /dev/null +++ b/app/Activity/Notifications/Messages/CommentCreationNotification.php @@ -0,0 +1,32 @@ +detail; + /** @var Page $page */ + $page = $comment->entity; + + return (new MailMessage()) + ->subject("New Comment on Page: " . $page->getShortName()) + ->line("A user has commented on a page in " . setting('app-name') . ':') + ->line("Page Name: " . $page->name) + ->line("Commenter: " . $this->user->name) + ->line("Comment: " . strip_tags($comment->html)) + ->action('View Comment', $page->getUrl('#comment' . $comment->local_id)) + ->line(new LinkedMailMessageLine( + url('/preferences/notifications'), + 'This notification was sent to you because :link cover this type of activity for this item.', + 'your notification preferences', + )); + } +} diff --git a/app/Activity/Notifications/Messages/PageUpdateNotification.php b/app/Activity/Notifications/Messages/PageUpdateNotification.php new file mode 100644 index 000000000..f29f50dde --- /dev/null +++ b/app/Activity/Notifications/Messages/PageUpdateNotification.php @@ -0,0 +1,29 @@ +detail; + + return (new MailMessage()) + ->subject("Updated Page: " . $page->getShortName()) + ->line("A page has been updated in " . setting('app-name') . ':') + ->line("Page Name: " . $page->name) + ->line("Updated By: " . $this->user->name) + ->line("To prevent a mass of notifications, for a while you won't be sent notifications for further edits to this page by the same editor.") + ->action('View Page', $page->getUrl()) + ->line(new LinkedMailMessageLine( + url('/preferences/notifications'), + 'This notification was sent to you because :link cover this type of activity for this item.', + 'your notification preferences', + )); + } +} diff --git a/app/Activity/Tools/EntityWatchers.php b/app/Activity/Tools/EntityWatchers.php index 38ba8c591..1ab53cb1c 100644 --- a/app/Activity/Tools/EntityWatchers.php +++ b/app/Activity/Tools/EntityWatchers.php @@ -32,6 +32,11 @@ class EntityWatchers return $this->watchers; } + public function isUserIgnoring(int $userId): bool + { + return in_array($userId, $this->ignorers); + } + protected function build(): void { $watches = $this->getRelevantWatches();