X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/bf8e7f3393d48e6300c4d8775daeb40d55ea2017..refs/pull/3630/head:/app/Actions/View.php diff --git a/app/Actions/View.php b/app/Actions/View.php index c5ec6a38d..16961bd91 100644 --- a/app/Actions/View.php +++ b/app/Actions/View.php @@ -1,11 +1,23 @@ -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(); + } }