From 93fd869ba36cb8e74690af95d28236084d159b63 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sun, 16 May 2021 10:49:37 +0100 Subject: [PATCH] Started refactoring of view service Phasing out the view service from being a generic 'service' class, moving the core create/delete methods into the model. The idea is that the existing query work will need to interlink with the favourite system so maybe we have a (or many composable) query building classes rather than mixing query building and create/delete work as per the old service. --- app/Actions/View.php | 39 ++++++++++++++++++++ app/Actions/ViewService.php | 36 ------------------ app/Console/Commands/ClearViews.php | 3 +- app/Entities/Models/Entity.php | 3 +- app/Http/Controllers/BookController.php | 3 +- app/Http/Controllers/BookshelfController.php | 3 +- app/Http/Controllers/ChapterController.php | 3 +- app/Http/Controllers/PageController.php | 3 +- app/Interfaces/Viewable.php | 11 ++++++ 9 files changed, 62 insertions(+), 42 deletions(-) create mode 100644 app/Interfaces/Viewable.php diff --git a/app/Actions/View.php b/app/Actions/View.php index c5ec6a38d..62e03d9f4 100644 --- a/app/Actions/View.php +++ b/app/Actions/View.php @@ -1,8 +1,19 @@ 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->save(['views' => $view->views + 1]); + + return $view->views; + } + + /** + * Clear all views from the system. + */ + public static function clearAll() + { + static::query()->truncate(); + } } diff --git a/app/Actions/ViewService.php b/app/Actions/ViewService.php index a4e620d4b..febc93af3 100644 --- a/app/Actions/ViewService.php +++ b/app/Actions/ViewService.php @@ -1,7 +1,6 @@ entityProvider = $entityProvider; } - /** - * Add a view to the given entity. - * @param \BookStack\Entities\Models\Entity $entity - * @return int - */ - public function add(Entity $entity) - { - $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'); - return $view->views; - } - - // Otherwise create new view count - $entity->views()->save($this->view->newInstance([ - 'user_id' => $user->id, - 'views' => 1 - ])); - - return 1; - } - /** * Get the entities with the most views. * @param int $count @@ -106,12 +78,4 @@ class ViewService return $all->sortByDesc('last_viewed_at')->slice(0, $count); } - - /** - * Reset all view counts by deleting all views. - */ - public function resetAll() - { - $this->view->truncate(); - } } diff --git a/app/Console/Commands/ClearViews.php b/app/Console/Commands/ClearViews.php index 35356210b..693d93639 100644 --- a/app/Console/Commands/ClearViews.php +++ b/app/Console/Commands/ClearViews.php @@ -2,6 +2,7 @@ namespace BookStack\Console\Commands; +use BookStack\Actions\View; use Illuminate\Console\Command; class ClearViews extends Command @@ -36,7 +37,7 @@ class ClearViews extends Command */ public function handle() { - \Views::resetAll(); + View::clearAll(); $this->comment('Views cleared'); } } diff --git a/app/Entities/Models/Entity.php b/app/Entities/Models/Entity.php index be63cec2b..561876769 100644 --- a/app/Entities/Models/Entity.php +++ b/app/Entities/Models/Entity.php @@ -12,6 +12,7 @@ use BookStack\Entities\Tools\SlugGenerator; use BookStack\Facades\Permissions; use BookStack\Interfaces\Favouritable; use BookStack\Interfaces\Sluggable; +use BookStack\Interfaces\Viewable; use BookStack\Model; use BookStack\Traits\HasCreatorAndUpdater; use BookStack\Traits\HasOwner; @@ -40,7 +41,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @method static Builder withLastView() * @method static Builder withViewCount() */ -abstract class Entity extends Model implements Sluggable, Favouritable +abstract class Entity extends Model implements Sluggable, Favouritable, Viewable { use SoftDeletes; use HasCreatorAndUpdater; diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.php index 59c205d0a..d111f9f0a 100644 --- a/app/Http/Controllers/BookController.php +++ b/app/Http/Controllers/BookController.php @@ -2,6 +2,7 @@ use Activity; use BookStack\Actions\ActivityType; +use BookStack\Actions\View; use BookStack\Entities\Tools\BookContents; use BookStack\Entities\Models\Bookshelf; use BookStack\Entities\Tools\PermissionsUpdater; @@ -112,7 +113,7 @@ class BookController extends Controller $bookChildren = (new BookContents($book))->getTree(true); $bookParentShelves = $book->shelves()->visible()->get(); - Views::add($book); + View::incrementFor($book); if ($request->has('shelf')) { $this->entityContextManager->setShelfContext(intval($request->get('shelf'))); } diff --git a/app/Http/Controllers/BookshelfController.php b/app/Http/Controllers/BookshelfController.php index 03b3cad54..b4795db09 100644 --- a/app/Http/Controllers/BookshelfController.php +++ b/app/Http/Controllers/BookshelfController.php @@ -1,6 +1,7 @@ values() ->all(); - Views::add($shelf); + View::incrementFor($shelf); $this->entityContextManager->setShelfContext($shelf->id); $view = setting()->getForCurrentUser('bookshelf_view_type'); diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php index 1d69df2a2..fbef81582 100644 --- a/app/Http/Controllers/ChapterController.php +++ b/app/Http/Controllers/ChapterController.php @@ -1,5 +1,6 @@ book))->getTree(); $pages = $chapter->getVisiblePages(); - Views::add($chapter); + View::incrementFor($chapter); $this->setPageTitle($chapter->getShortName()); return view('chapters.show', [ diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index 30d33ad48..769ea8e69 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -1,5 +1,6 @@ load(['comments.createdBy']); } - Views::add($page); + View::incrementFor($page); $this->setPageTitle($page->getShortName()); return view('pages.show', [ 'page' => $page, diff --git a/app/Interfaces/Viewable.php b/app/Interfaces/Viewable.php new file mode 100644 index 000000000..4a7b6a013 --- /dev/null +++ b/app/Interfaces/Viewable.php @@ -0,0 +1,11 @@ +