]> BookStack Code Mirror - bookstack/blob - app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Activity / Notifications / Handlers / PageUpdateNotificationHandler.php
1 <?php
2
3 namespace BookStack\Activity\Notifications\Handlers;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Activity;
7 use BookStack\Activity\Models\Loggable;
8 use BookStack\Activity\Notifications\Messages\PageUpdateNotification;
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;
14
15 class PageUpdateNotificationHandler extends BaseNotificationHandler
16 {
17     public function handle(Activity $activity, Loggable|string $detail, User $user): void
18     {
19         if (!($detail instanceof Page)) {
20             throw new \InvalidArgumentException("Detail for page update notifications must be a page");
21         }
22
23         // Get last update from activity
24         $lastUpdate = $detail->activity()
25             ->where('type', '=', ActivityType::PAGE_UPDATE)
26             ->where('id', '!=', $activity->id)
27             ->latest('created_at')
28             ->first();
29
30         // Return if the same user has already updated the page in the last 15 mins
31         if ($lastUpdate && $lastUpdate->user_id === $user->id) {
32             if ($lastUpdate->created_at->gt(now()->subMinutes(15))) {
33                 return;
34             }
35         }
36
37         // Get active watchers
38         $watchers = new EntityWatchers($detail, WatchLevels::UPDATES);
39         $watcherIds = $watchers->getWatcherUserIds();
40
41         // Add page owner if preferences allow
42         if (!$watchers->isUserIgnoring($detail->owned_by) && $detail->ownedBy) {
43             $userNotificationPrefs = new UserNotificationPreferences($detail->ownedBy);
44             if ($userNotificationPrefs->notifyOnOwnPageChanges()) {
45                 $watcherIds[] = $detail->owned_by;
46             }
47         }
48
49         $this->sendNotificationToUserIds(PageUpdateNotification::class, $watcherIds, $user, $detail, $detail);
50     }
51 }