]> BookStack Code Mirror - bookstack/blobdiff - app/Activity/Notifications/Handlers/CommentCreationNotificationHandler.php
Perms: Fixed some issues made when adding transactions
[bookstack] / app / Activity / Notifications / Handlers / CommentCreationNotificationHandler.php
index 67c30433903a0cf6e8eba10c58c934245323fcf4..bc12c85669f2b65adc03df99116443d79377b463 100644 (file)
@@ -2,13 +2,47 @@
 
 namespace BookStack\Activity\Notifications\Handlers;
 
+use BookStack\Activity\Models\Activity;
+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\Entities\Models\Page;
+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
+    public function handle(Activity $activity, 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
+        /** @var Page $page */
+        $page = $detail->entity;
+        $watchers = new EntityWatchers($page, WatchLevels::COMMENTS);
+        $watcherIds = $watchers->getWatcherUserIds();
+
+        // Page owner if user preferences allow
+        if (!$watchers->isUserIgnoring($page->owned_by) && $page->ownedBy) {
+            $userNotificationPrefs = new UserNotificationPreferences($page->ownedBy);
+            if ($userNotificationPrefs->notifyOnOwnPageComments()) {
+                $watcherIds[] = $page->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, $detail, $page);
     }
 }