]> BookStack Code Mirror - bookstack/blob - app/Activity/Notifications/Handlers/CommentCreationNotificationHandler.php
Notifications: Added logic and classes for remaining notification types
[bookstack] / app / Activity / Notifications / Handlers / CommentCreationNotificationHandler.php
1 <?php
2
3 namespace BookStack\Activity\Notifications\Handlers;
4
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;
12
13 class CommentCreationNotificationHandler extends BaseNotificationHandler
14 {
15     public function handle(string $activityType, Loggable|string $detail, User $user): void
16     {
17         if (!($detail instanceof Comment)) {
18             throw new \InvalidArgumentException("Detail for comment creation notifications must be a comment");
19         }
20
21         // Main watchers
22         $page = $detail->entity;
23         $watchers = new EntityWatchers($page, WatchLevels::COMMENTS);
24         $watcherIds = $watchers->getWatcherUserIds();
25
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;
31             }
32         }
33
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;
40             }
41         }
42
43         $this->sendNotificationToUserIds(CommentCreationNotification::class, $watcherIds, $user, $page);
44     }
45 }