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