BookStack/app/Entities/Queries/QueryRecentlyViewed.php

44 lines
1.1 KiB
PHP
Raw Normal View History

2021-06-26 11:23:15 -04:00
<?php
namespace BookStack\Entities\Queries;
2023-05-17 12:56:55 -04:00
use BookStack\Activity\Models\View;
use BookStack\Entities\Tools\MixedEntityListLoader;
use BookStack\Permissions\PermissionApplicator;
use Illuminate\Support\Collection;
class QueryRecentlyViewed
{
public function __construct(
protected PermissionApplicator $permissions,
protected MixedEntityListLoader $listLoader,
) {
}
public function run(int $count, int $page): Collection
{
$user = user();
if ($user->isGuest()) {
return collect();
}
$query = $this->permissions->restrictEntityRelationQuery(
View::query(),
'views',
'viewable_id',
'viewable_type'
)
->orderBy('views.updated_at', 'desc')
->where('user_id', '=', user()->id);
$views = $query
->skip(($page - 1) * $count)
->take($count)
->get();
$this->listLoader->loadIntoRelations($views->all(), 'viewable', false);
return $views->pluck('viewable')->filter();
}
}