X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/712ccd23c4738e6a59a10f31ff654743fbc61879..refs/pull/3012/head:/app/Actions/ActivityService.php diff --git a/app/Actions/ActivityService.php b/app/Actions/ActivityService.php index fb4a739cd..bc7a6b6b7 100644 --- a/app/Actions/ActivityService.php +++ b/app/Actions/ActivityService.php @@ -1,10 +1,13 @@ -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 { + $ip = request()->ip() ?? ''; + return $this->activity->newInstance()->forceFill([ 'type' => strtolower($type), - 'user_id' => user()->id, + 'user_id' => user()->id, + 'ip' => config('app.env') === 'demo' ? '127.0.0.1' : $ip, ]); } @@ -50,8 +73,8 @@ class ActivityService { $entity->activity()->update([ 'detail' => $entity->name, - 'entity_id' => null, - 'entity_type' => null, + 'entity_id' => null, + 'entity_type' => null, ]); } @@ -61,7 +84,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) @@ -81,10 +104,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(); @@ -114,7 +137,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) @@ -126,7 +149,9 @@ class ActivityService /** * Filters out similar activity. + * * @param Activity[] $activities + * * @return array */ protected function filterSimilar(iterable $activities): array @@ -168,7 +193,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); }