3 namespace BookStack\Activity\Notifications\Handlers;
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;
15 class PageUpdateNotificationHandler extends BaseNotificationHandler
17 public function handle(Activity $activity, Loggable|string $detail, User $user): void
19 if (!($detail instanceof Page)) {
20 throw new \InvalidArgumentException("Detail for page update notifications must be a page");
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')
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))) {
37 // Get active watchers
38 $watchers = new EntityWatchers($detail, WatchLevels::UPDATES);
39 $watcherIds = $watchers->getWatcherUserIds();
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;
49 $this->sendNotificationToUserIds(PageUpdateNotification::class, $watcherIds, $user, $detail, $detail);