]> BookStack Code Mirror - bookstack/blobdiff - app/Activity/Notifications/Handlers/PageUpdateNotificationHandler.php
Customization: Added parent tag classes
[bookstack] / app / Activity / Notifications / Handlers / PageUpdateNotificationHandler.php
index bbd189d52b19bc07e0facd4ae04ca3f8d4197726..744aba18f631782e38b739874a3f8e5e44b3c34b 100644 (file)
@@ -2,13 +2,50 @@
 
 namespace BookStack\Activity\Notifications\Handlers;
 
+use BookStack\Activity\ActivityType;
+use BookStack\Activity\Models\Activity;
 use BookStack\Activity\Models\Loggable;
+use BookStack\Activity\Notifications\Messages\PageUpdateNotification;
+use BookStack\Activity\Tools\EntityWatchers;
+use BookStack\Activity\WatchLevels;
+use BookStack\Entities\Models\Page;
+use BookStack\Settings\UserNotificationPreferences;
 use BookStack\Users\Models\User;
 
-class PageUpdateNotificationHandler implements NotificationHandler
+class PageUpdateNotificationHandler extends BaseNotificationHandler
 {
-    public function handle(string $activityType, Loggable|string $detail, User $user): void
+    public function handle(Activity $activity, Loggable|string $detail, User $user): void
     {
-        // TODO
+        if (!($detail instanceof Page)) {
+            throw new \InvalidArgumentException("Detail for page update notifications must be a page");
+        }
+
+        // Get last update from activity
+        $lastUpdate = $detail->activity()
+            ->where('type', '=', ActivityType::PAGE_UPDATE)
+            ->where('id', '!=', $activity->id)
+            ->latest('created_at')
+            ->first();
+
+        // Return if the same user has already updated the page in the last 15 mins
+        if ($lastUpdate && $lastUpdate->user_id === $user->id) {
+            if ($lastUpdate->created_at->gt(now()->subMinutes(15))) {
+                return;
+            }
+        }
+
+        // Get active watchers
+        $watchers = new EntityWatchers($detail, WatchLevels::UPDATES);
+        $watcherIds = $watchers->getWatcherUserIds();
+
+        // Add page owner if preferences allow
+        if (!$watchers->isUserIgnoring($detail->owned_by) && $detail->ownedBy) {
+            $userNotificationPrefs = new UserNotificationPreferences($detail->ownedBy);
+            if ($userNotificationPrefs->notifyOnOwnPageChanges()) {
+                $watcherIds[] = $detail->owned_by;
+            }
+        }
+
+        $this->sendNotificationToUserIds(PageUpdateNotification::class, $watcherIds, $user, $detail, $detail);
     }
 }