3 namespace BookStack\Activity\Notifications\Handlers;
5 use BookStack\Activity\Models\Comment;
6 use BookStack\Activity\Models\Loggable;
7 use BookStack\Activity\Notifications\Messages\CommentCreationNotification;
8 use BookStack\Activity\Tools\EntityWatchers;
9 use BookStack\Activity\WatchLevels;
10 use BookStack\Settings\UserNotificationPreferences;
11 use BookStack\Users\Models\User;
13 class CommentCreationNotificationHandler extends BaseNotificationHandler
15 public function handle(string $activityType, Loggable|string $detail, User $user): void
17 if (!($detail instanceof Comment)) {
18 throw new \InvalidArgumentException("Detail for comment creation notifications must be a comment");
22 $page = $detail->entity;
23 $watchers = new EntityWatchers($page, WatchLevels::COMMENTS);
24 $watcherIds = $watchers->getWatcherUserIds();
26 // Page owner if user preferences allow
27 if (!$watchers->isUserIgnoring($detail->owned_by) && $detail->ownedBy) {
28 $userNotificationPrefs = new UserNotificationPreferences($detail->ownedBy);
29 if ($userNotificationPrefs->notifyOnOwnPageComments()) {
30 $watcherIds[] = $detail->owned_by;
34 // Parent comment creator if preferences allow
35 $parentComment = $detail->parent()->first();
36 if ($parentComment && !$watchers->isUserIgnoring($parentComment->created_by) && $parentComment->createdBy) {
37 $parentCommenterNotificationsPrefs = new UserNotificationPreferences($parentComment->createdBy);
38 if ($parentCommenterNotificationsPrefs->notifyOnCommentReplies()) {
39 $watcherIds[] = $parentComment->created_by;
43 $this->sendNotificationToUserIds(CommentCreationNotification::class, $watcherIds, $user, $page);