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;
|
2021-05-22 21:05:28 +08:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
|
|
|
|
|
|
|
class TopFavourites extends EntityQuery
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->permissionService()
|
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
|
|
|
|
2021-05-23 21:34:36 +08:00
|
|
|
return $query->with('favouritable')
|
2021-05-23 20:34:08 +08:00
|
|
|
->skip($skip)
|
2021-05-22 21:05:28 +08:00
|
|
|
->take($count)
|
|
|
|
->get()
|
2021-05-23 21:34:36 +08:00
|
|
|
->pluck('favouritable')
|
2021-05-22 21:05:28 +08:00
|
|
|
->filter();
|
|
|
|
}
|
2021-05-23 20:34:08 +08:00
|
|
|
}
|