X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/80865b30a5d6a82e86d21e272ae58977d4430a64..refs/pull/234/head:/app/Services/ViewService.php diff --git a/app/Services/ViewService.php b/app/Services/ViewService.php index 75ffd21dc..1a9ee5f70 100644 --- a/app/Services/ViewService.php +++ b/app/Services/ViewService.php @@ -1,6 +1,5 @@ view = $view; - $this->user = auth()->user(); - $this->restrictionService = $restrictionService; + $this->user = user(); + $this->permissionService = $permissionService; } /** @@ -47,28 +46,27 @@ class ViewService return 1; } - /** * Get the entities with the most views. * @param int $count * @param int $page - * @param bool|false $filterModel + * @param bool|false|array $filterModel */ public function getPopular($count = 10, $page = 0, $filterModel = false) { $skipCount = $count * $page; - $query = $this->restrictionService->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type') - ->select('id', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count')) + $query = $this->permissionService->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type') + ->select('*', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count')) ->groupBy('viewable_id', 'viewable_type') ->orderBy('view_count', 'desc'); - if ($filterModel) $query->where('viewable_type', '=', get_class($filterModel)); + if ($filterModel && is_array($filterModel)) { + $query->whereIn('viewable_type', $filterModel); + } else if ($filterModel) { + $query->where('viewable_type', '=', get_class($filterModel)); + }; - $views = $query->with('viewable')->skip($skipCount)->take($count)->get(); - $viewedEntities = $views->map(function ($item) { - return $item->viewable()->getResults(); - }); - return $viewedEntities; + return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable'); } /** @@ -81,21 +79,18 @@ class ViewService public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false) { if ($this->user === null) return collect(); - $skipCount = $count * $page; - $query = $this->restrictionService + + $query = $this->permissionService ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type'); if ($filterModel) $query = $query->where('viewable_type', '=', get_class($filterModel)); - $query = $query->where('user_id', '=', auth()->user()->id); + $query = $query->where('user_id', '=', user()->id); - $views = $query->with('viewable')->orderBy('updated_at', 'desc')->skip($skipCount)->take($count)->get(); - $viewedEntities = $views->map(function ($item) { - return $item->viewable; - }); - return $viewedEntities; + $viewables = $query->with('viewable')->orderBy('updated_at', 'desc') + ->skip($count * $page)->take($count)->get()->pluck('viewable'); + return $viewables; } - /** * Reset all view counts by deleting all views. */ @@ -104,5 +99,4 @@ class ViewService $this->view->truncate(); } - } \ No newline at end of file