X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/ee7e1122d325b1807f993e4f0cacc40fbbc806fb..refs/pull/2902/head:/app/Actions/ActivityService.php diff --git a/app/Actions/ActivityService.php b/app/Actions/ActivityService.php index be9f656c3..dce7dc7b2 100644 --- a/app/Actions/ActivityService.php +++ b/app/Actions/ActivityService.php @@ -1,10 +1,13 @@ -activity = $activity; $this->permissionService = $permissionService; - $this->user = user(); } /** @@ -32,14 +33,31 @@ 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. */ protected function newActivityForUser(string $type): Activity { return $this->activity->newInstance()->forceFill([ - 'key' => strtolower($type), - 'user_id' => $this->user->id, + 'type' => strtolower($type), + 'user_id' => user()->id, ]); } @@ -51,9 +69,9 @@ class ActivityService public function removeEntity(Entity $entity) { $entity->activity()->update([ - 'extra' => $entity->name, - 'entity_id' => 0, - 'entity_type' => '', + 'detail' => $entity->name, + 'entity_id' => null, + 'entity_type' => null, ]); } @@ -63,7 +81,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) @@ -83,10 +101,10 @@ class ActivityService $queryIds = [$entity->getMorphClass() => [$entity->id]]; if ($entity->isA('book')) { - $queryIds[(new Chapter)->getMorphClass()] = $entity->chapters()->visible()->pluck('id'); + $queryIds[(new Chapter())->getMorphClass()] = $entity->chapters()->visible()->pluck('id'); } if ($entity->isA('book') || $entity->isA('chapter')) { - $queryIds[(new Page)->getMorphClass()] = $entity->pages()->visible()->pluck('id'); + $queryIds[(new Page())->getMorphClass()] = $entity->pages()->visible()->pluck('id'); } $query = $this->activity->newQuery(); @@ -116,7 +134,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) @@ -128,7 +146,9 @@ class ActivityService /** * Filters out similar activity. + * * @param Activity[] $activities + * * @return array */ protected function filterSimilar(iterable $activities): array @@ -150,9 +170,9 @@ class ActivityService /** * Flashes a notification message to the session if an appropriate message is available. */ - protected function setNotification(string $activityKey) + protected function setNotification(string $type) { - $notificationTextKey = 'activities.' . $activityKey . '_notification'; + $notificationTextKey = 'activities.' . $type . '_notification'; if (trans()->has($notificationTextKey)) { $message = trans($notificationTextKey); session()->flash('success', $message); @@ -170,7 +190,7 @@ class ActivityService return; } - $message = str_replace("%u", $username, $message); + $message = str_replace('%u', $username, $message); $channel = config('logging.failed_login.channel'); Log::channel($channel)->warning($message); }