]> BookStack Code Mirror - bookstack/blobdiff - app/Services/ViewService.php
Finished migration of last angular code
[bookstack] / app / Services / ViewService.php
index 75ffd21dcd68bfa6ef49502fc3ab8a6084e67dba..770a9e39ffef3387ba10efc975b8749e662e5a5c 100644 (file)
@@ -1,26 +1,22 @@
 <?php namespace BookStack\Services;
 
-
 use BookStack\Entity;
 use BookStack\View;
 
 class ViewService
 {
-
     protected $view;
-    protected $user;
-    protected $restrictionService;
+    protected $permissionService;
 
     /**
      * ViewService constructor.
      * @param View $view
-     * @param RestrictionService $restrictionService
+     * @param PermissionService $permissionService
      */
-    public function __construct(View $view, RestrictionService $restrictionService)
+    public function __construct(View $view, PermissionService $permissionService)
     {
         $this->view = $view;
-        $this->user = auth()->user();
-        $this->restrictionService = $restrictionService;
+        $this->permissionService = $permissionService;
     }
 
     /**
@@ -30,8 +26,9 @@ class ViewService
      */
     public function add(Entity $entity)
     {
-        if ($this->user === null) return 0;
-        $view = $entity->views()->where('user_id', '=', $this->user->id)->first();
+        $user = user();
+        if ($user === null || $user->isDefault()) return 0;
+        $view = $entity->views()->where('user_id', '=', $user->id)->first();
         // Add view if model exists
         if ($view) {
             $view->increment('views');
@@ -40,35 +37,34 @@ class ViewService
 
         // Otherwise create new view count
         $entity->views()->save($this->view->create([
-            'user_id' => $this->user->id,
+            'user_id' => $user->id,
             'views' => 1
         ]));
 
         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');
     }
 
     /**
@@ -80,22 +76,20 @@ class ViewService
      */
     public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false)
     {
-        if ($this->user === null) return collect();
-        $skipCount = $count * $page;
-        $query = $this->restrictionService
+        $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', '=', 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 +98,4 @@ class ViewService
         $this->view->truncate();
     }
 
-
 }
\ No newline at end of file