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