X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/cd6572b61af2165133468d2562d04dffdca8fca8..refs/pull/986/head:/app/Services/ViewService.php diff --git a/app/Services/ViewService.php b/app/Services/ViewService.php index 3285745ce..cd869018c 100644 --- a/app/Services/ViewService.php +++ b/app/Services/ViewService.php @@ -27,7 +27,9 @@ class ViewService public function add(Entity $entity) { $user = user(); - if ($user === null || $user->isDefault()) return 0; + if ($user === null || $user->isDefault()) { + return 0; + } $view = $entity->views()->where('user_id', '=', $user->id)->first(); // Add view if model exists if ($view) { @@ -49,11 +51,13 @@ class ViewService * @param int $count * @param int $page * @param bool|false|array $filterModel + * @param string $action - used for permission checking + * @return */ - public function getPopular($count = 10, $page = 0, $filterModel = false) + public function getPopular($count = 10, $page = 0, $filterModel = false, $action = 'view') { $skipCount = $count * $page; - $query = $this->permissionService->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type') + $query = $this->permissionService->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type', $action) ->select('*', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count')) ->groupBy('viewable_id', 'viewable_type') ->orderBy('view_count', 'desc'); @@ -62,7 +66,7 @@ class ViewService $query->whereIn('viewable_type', $filterModel); } else if ($filterModel) { $query->where('viewable_type', '=', get_class($filterModel)); - }; + } return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable'); } @@ -77,12 +81,16 @@ class ViewService public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false) { $user = user(); - if ($user === null || $user->isDefault()) return collect(); + 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', '=', get_class($filterModel)); + if ($filterModel) { + $query = $query->where('viewable_type', '=', get_class($filterModel)); + } $query = $query->where('user_id', '=', $user->id); $viewables = $query->with('viewable')->orderBy('updated_at', 'desc') @@ -97,5 +105,4 @@ class ViewService { $this->view->truncate(); } - -} \ No newline at end of file +}