]> BookStack Code Mirror - bookstack/blobdiff - app/Activity/Tools/UserEntityWatchOptions.php
ZIP Imports: Added API examples, finished testing
[bookstack] / app / Activity / Tools / UserEntityWatchOptions.php
index 26d830851c51e2e5263ca2cdca5445d73ab12126..559d7903d6f510671ae01c74a61409b0de93c875 100644 (file)
@@ -22,7 +22,7 @@ class UserEntityWatchOptions
 
     public function canWatch(): bool
     {
-        return $this->user->can('receive-notifications') && !$this->user->isDefault();
+        return $this->user->can('receive-notifications') && !$this->user->isGuest();
     }
 
     public function getWatchLevel(): string
@@ -51,15 +51,20 @@ class UserEntityWatchOptions
         return null;
     }
 
-    public function updateWatchLevel(string $level): void
+    public function updateLevelByName(string $level): void
     {
         $levelValue = WatchLevels::levelNameToValue($level);
-        if ($levelValue < 0) {
+        $this->updateLevelByValue($levelValue);
+    }
+
+    public function updateLevelByValue(int $level): void
+    {
+        if ($level < 0) {
             $this->remove();
             return;
         }
 
-        $this->updateLevel($levelValue);
+        $this->updateLevel($level);
     }
 
     public function getWatchMap(): array
@@ -76,14 +81,16 @@ class UserEntityWatchOptions
             $entities[] = $this->entity->chapter;
         }
 
-        $query = Watch::query()->where(function (Builder $subQuery) use ($entities) {
-            foreach ($entities as $entity) {
-                $subQuery->orWhere(function (Builder $whereQuery) use ($entity) {
-                    $whereQuery->where('watchable_type', '=', $entity->getMorphClass())
+        $query = Watch::query()
+            ->where('user_id', '=', $this->user->id)
+            ->where(function (Builder $subQuery) use ($entities) {
+                foreach ($entities as $entity) {
+                    $subQuery->orWhere(function (Builder $whereQuery) use ($entity) {
+                        $whereQuery->where('watchable_type', '=', $entity->getMorphClass())
                         ->where('watchable_id', '=', $entity->id);
-                });
-            }
-        });
+                    });
+                }
+            });
 
         $this->watchMap = $query->get(['watchable_type', 'level'])
             ->pluck('level', 'watchable_type')