2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Queries;
|
2021-05-22 21:05:28 +08:00
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Activity\Models\Favourite;
|
2024-02-09 00:39:59 +08:00
|
|
|
use BookStack\Entities\Tools\MixedEntityListLoader;
|
|
|
|
use BookStack\Permissions\PermissionApplicator;
|
2021-05-22 21:05:28 +08:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
|
|
|
|
2024-02-09 00:39:59 +08:00
|
|
|
class QueryTopFavourites
|
2021-05-22 21:05:28 +08:00
|
|
|
{
|
2024-02-09 00:39:59 +08:00
|
|
|
public function __construct(
|
|
|
|
protected PermissionApplicator $permissions,
|
|
|
|
protected MixedEntityListLoader $listLoader,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2021-05-23 20:34:08 +08:00
|
|
|
public function run(int $count, int $skip = 0)
|
2021-05-22 21:05:28 +08:00
|
|
|
{
|
|
|
|
$user = user();
|
2023-09-16 20:18:35 +08:00
|
|
|
if ($user->isGuest()) {
|
2021-05-22 21:05:28 +08:00
|
|
|
return collect();
|
|
|
|
}
|
|
|
|
|
2024-02-09 00:39:59 +08:00
|
|
|
$query = $this->permissions
|
2022-07-17 02:54:25 +08:00
|
|
|
->restrictEntityRelationQuery(Favourite::query(), 'favourites', 'favouritable_id', 'favouritable_type')
|
2021-05-23 21:34:36 +08:00
|
|
|
->select('favourites.*')
|
|
|
|
->leftJoin('views', function (JoinClause $join) {
|
|
|
|
$join->on('favourites.favouritable_id', '=', 'views.viewable_id');
|
|
|
|
$join->on('favourites.favouritable_type', '=', 'views.viewable_type');
|
|
|
|
$join->where('views.user_id', '=', user()->id);
|
2021-05-22 21:05:28 +08:00
|
|
|
})
|
2021-05-23 21:34:36 +08:00
|
|
|
->orderBy('views.views', 'desc')
|
|
|
|
->where('favourites.user_id', '=', user()->id);
|
2021-05-22 21:05:28 +08:00
|
|
|
|
2024-02-04 22:39:01 +08:00
|
|
|
$favourites = $query
|
2021-05-23 20:34:08 +08:00
|
|
|
->skip($skip)
|
2021-05-22 21:05:28 +08:00
|
|
|
->take($count)
|
2024-02-04 22:39:01 +08:00
|
|
|
->get();
|
|
|
|
|
2024-02-09 00:39:59 +08:00
|
|
|
$this->listLoader->loadIntoRelations($favourites->all(), 'favouritable', false);
|
2024-02-04 22:39:01 +08:00
|
|
|
|
|
|
|
return $favourites->pluck('favouritable')->filter();
|
2021-05-22 21:05:28 +08:00
|
|
|
}
|
2021-05-23 20:34:08 +08:00
|
|
|
}
|