]> BookStack Code Mirror - bookstack/blob - app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php
Notifications: Added logic and classes for remaining notification types
[bookstack] / app / Activity / Notifications / Handlers / PageUpdateNotificationHandler.php
1 <?php
2
3 namespace BookStack\Activity\Notifications\Handlers;
4
5 use BookStack\Activity\Models\Loggable;
6 use BookStack\Activity\Notifications\Messages\PageUpdateNotification;
7 use BookStack\Activity\Tools\EntityWatchers;
8 use BookStack\Activity\WatchLevels;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Settings\UserNotificationPreferences;
11 use BookStack\Users\Models\User;
12
13 class PageUpdateNotificationHandler extends BaseNotificationHandler
14 {
15     public function handle(string $activityType, Loggable|string $detail, User $user): void
16     {
17         if (!($detail instanceof Page)) {
18             throw new \InvalidArgumentException("Detail for page update notifications must be a page");
19         }
20
21         $watchers = new EntityWatchers($detail, WatchLevels::UPDATES);
22         $watcherIds = $watchers->getWatcherUserIds();
23
24         if (!$watchers->isUserIgnoring($detail->owned_by) && $detail->ownedBy) {
25             $userNotificationPrefs = new UserNotificationPreferences($detail->ownedBy);
26             if ($userNotificationPrefs->notifyOnOwnPageChanges()) {
27                 $watcherIds[] = $detail->owned_by;
28             }
29         }
30
31         $this->sendNotificationToUserIds(PageUpdateNotification::class, $watcherIds, $user, $detail);
32     }
33 }