X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/ffc1aa873ed8912c1400d83191d2b1485e59fca3..refs/pull/3616/head:/app/Actions/View.php diff --git a/app/Actions/View.php b/app/Actions/View.php index e9841293b..16961bd91 100644 --- a/app/Actions/View.php +++ b/app/Actions/View.php @@ -1,18 +1,58 @@ -morphTo(); } + + /** + * Increment the current user's view count for the given viewable model. + */ + public static function incrementFor(Viewable $viewable): int + { + $user = user(); + if (is_null($user) || $user->isDefault()) { + return 0; + } + + /** @var View $view */ + $view = $viewable->views()->firstOrNew([ + 'user_id' => $user->id, + ], ['views' => 0]); + + $view->forceFill(['views' => $view->views + 1])->save(); + + return $view->views; + } + + /** + * Clear all views from the system. + */ + public static function clearAll() + { + static::query()->truncate(); + } }