X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/0a06e2bce38284ca09340ac880010f45cb3d6f7a..refs/pull/2283/head:/app/Actions/ViewService.php diff --git a/app/Actions/ViewService.php b/app/Actions/ViewService.php index 532f31c42..aa75abb72 100644 --- a/app/Actions/ViewService.php +++ b/app/Actions/ViewService.php @@ -1,8 +1,10 @@ views()->save($this->view->create([ + $entity->views()->save($this->view->newInstance([ 'user_id' => $user->id, 'views' => 1 ])); @@ -59,12 +61,12 @@ class ViewService * @param string $action - used for permission checking * @return Collection */ - public function getPopular(int $count = 10, int $page = 0, $filterModels = null, string $action = 'view') + public function getPopular(int $count = 10, int $page = 0, array $filterModels = null, string $action = 'view') { $skipCount = $count * $page; $query = $this->permissionService ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type', $action) - ->select('*', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count')) + ->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count')) ->groupBy('viewable_id', 'viewable_type') ->orderBy('view_count', 'desc'); @@ -77,29 +79,26 @@ class ViewService /** * Get all recently viewed entities for the current user. - * @param int $count - * @param int $page - * @param Entity|bool $filterModel - * @return mixed */ - public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false) + public function getUserRecentlyViewed(int $count = 10, int $page = 1) { $user = user(); if ($user === null || $user->isDefault()) { return collect(); } - $query = $this->permissionService - ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type'); - - if ($filterModel) { - $query = $query->where('viewable_type', '=', $filterModel->getMorphClass()); + $all = collect(); + /** @var Entity $instance */ + foreach ($this->entityProvider->all() as $name => $instance) { + $items = $instance::visible()->withLastView() + ->orderBy('last_viewed_at', 'desc') + ->skip($count * ($page - 1)) + ->take($count) + ->get(); + $all = $all->concat($items); } - $query = $query->where('user_id', '=', $user->id); - $viewables = $query->with('viewable')->orderBy('updated_at', 'desc') - ->skip($count * $page)->take($count)->get()->pluck('viewable'); - return $viewables; + return $all->sortByDesc('last_viewed_at')->slice(0, $count); } /**