*/ public function getRecentlyAdded(int $count = 6, ?User $user = null): Collection { return Album::query() ->isStandard() ->accessibleBy($user ?? $this->auth->user()) ->groupBy('albums.id') ->distinct() ->latest('albums.created_at') ->limit($count) ->get('albums.*'); } /** @return Collection|array */ public function getMostPlayed(int $count = 6, ?User $user = null): Collection { /** @var ?User $user */ $user ??= $this->auth->user(); $query = Album::query() ->isStandard() ->accessibleBy($user); if (License::isCommunity()) { // if the license is Plus, accessibleBy() would have already joined the songs table // and we don't want to join it twice $query->leftJoin('songs', 'albums.id', 'songs.album_id'); } return $query->join('interactions', static function (JoinClause $join) use ($user): void { $join->on('songs.id', 'interactions.song_id')->where('interactions.user_id', $user->id); }) ->groupBy('albums.id') ->distinct() ->orderByDesc('play_count') ->limit($count) ->get('albums.*'); } /** @return Collection|array */ public function getMany(array $ids, bool $inThatOrder = false, ?User $user = null): Collection { $albums = Album::query() ->isStandard() ->accessibleBy($user ?? auth()->user()) ->whereIn('albums.id', $ids) ->groupBy('albums.id') ->distinct() ->get('albums.*'); return $inThatOrder ? $albums->orderByArray($ids) : $albums; } /** @return Collection|array */ public function getByArtist(Artist $artist, ?User $user = null): Collection { return Album::query() ->accessibleBy($user ?? $this->auth->user()) ->where('albums.artist_id', $artist->id) ->orWhereIn('albums.id', $artist->songs()->pluck('album_id')) ->orderBy('albums.name') ->groupBy('albums.id') ->distinct() ->get('albums.*'); } public function paginate(?User $user = null): Paginator { return Album::query() ->accessibleBy($user ?? $this->auth->user()) ->isStandard() ->orderBy('albums.name') ->groupBy('albums.id') ->distinct() ->select('albums.*') ->simplePaginate(21); } }