]> BookStack Code Mirror - bookstack/blobdiff - app/Actions/ActivityService.php
Extend /users API endpoint
[bookstack] / app / Actions / ActivityService.php
index fb4a739cd27dbcedcef17e23b759f0747f3ab3ce..73f827e70e329186d9a8fc8d2ecf6749f919da6c 100644 (file)
@@ -2,9 +2,10 @@
 
 use BookStack\Auth\Permissions\PermissionService;
 use BookStack\Auth\User;
-use BookStack\Entities\Chapter;
-use BookStack\Entities\Entity;
-use BookStack\Entities\Page;
+use BookStack\Entities\Models\Chapter;
+use BookStack\Entities\Models\Entity;
+use BookStack\Entities\Models\Page;
+use BookStack\Interfaces\Loggable;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Relations\Relation;
 use Illuminate\Support\Facades\Log;
@@ -30,6 +31,22 @@ class ActivityService
         $this->setNotification($type);
     }
 
+    /**
+     * Add a generic activity event to the database.
+     * @param string|Loggable $detail
+     */
+    public function add(string $type, $detail = '')
+    {
+        if ($detail instanceof Loggable) {
+            $detail = $detail->logDescriptor();
+        }
+
+        $activity = $this->newActivityForUser($type);
+        $activity->detail = $detail;
+        $activity->save();
+        $this->setNotification($type);
+    }
+
     /**
      * Get a new activity instance for the current user.
      */
@@ -61,7 +78,7 @@ class ActivityService
     public function latest(int $count = 20, int $page = 0): array
     {
         $activityList = $this->permissionService
-            ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
+            ->filterRestrictedEntityRelations($this->activity->newQuery(), 'activities', 'entity_id', 'entity_type')
             ->orderBy('created_at', 'desc')
             ->with(['user', 'entity'])
             ->skip($count * $page)
@@ -114,7 +131,7 @@ class ActivityService
     public function userActivity(User $user, int $count = 20, int $page = 0): array
     {
         $activityList = $this->permissionService
-            ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
+            ->filterRestrictedEntityRelations($this->activity->newQuery(), 'activities', 'entity_id', 'entity_type')
             ->orderBy('created_at', 'desc')
             ->where('user_id', '=', $user->id)
             ->skip($count * $page)