]> BookStack Code Mirror - bookstack/blobdiff - app/Services/ViewService.php
Polish translation
[bookstack] / app / Services / ViewService.php
index 849a164cf409995a596dd7113d34bebb47d50601..3285745ce4d8c5a3b1a9acc4adfa58ac77b50e29 100644 (file)
@@ -5,9 +5,7 @@ use BookStack\View;
 
 class ViewService
 {
-
     protected $view;
-    protected $user;
     protected $permissionService;
 
     /**
@@ -18,7 +16,6 @@ class ViewService
     public function __construct(View $view, PermissionService $permissionService)
     {
         $this->view = $view;
-        $this->user = auth()->user();
         $this->permissionService = $permissionService;
     }
 
@@ -29,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');
@@ -39,7 +37,7 @@ class ViewService
 
         // Otherwise create new view count
         $entity->views()->save($this->view->create([
-            'user_id' => $this->user->id,
+            'user_id' => $user->id,
             'views' => 1
         ]));
 
@@ -50,7 +48,7 @@ class ViewService
      * 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)
     {
@@ -60,7 +58,11 @@ class ViewService
             ->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));
+        };
 
         return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable');
     }
@@ -74,13 +76,14 @@ class ViewService
      */
     public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false)
     {
-        if ($this->user === null) return collect();
+        $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);
 
         $viewables = $query->with('viewable')->orderBy('updated_at', 'desc')
             ->skip($count * $page)->take($count)->get()->pluck('viewable');