koel/app/Builders/SongBuilder.php

149 lines
4.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Builders;
2024-01-03 17:02:18 +00:00
use App\Facades\License;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
2024-01-03 17:02:18 +00:00
use Webmozart\Assert\Assert;
2024-01-03 17:02:18 +00:00
/**
* @method self logSql()
*/
class SongBuilder extends Builder
{
2024-01-03 17:02:18 +00:00
public const SORT_COLUMNS_NORMALIZE_MAP = [
'title' => 'songs.title',
'track' => 'songs.track',
'length' => 'songs.length',
'created_at' => 'songs.created_at',
'disc' => 'songs.disc',
'artist_name' => 'artists.name',
'album_name' => 'albums.name',
2024-05-19 05:49:42 +00:00
'podcast_title' => 'podcasts.title',
'podcast_author' => 'podcasts.author',
2024-01-03 17:02:18 +00:00
];
private const VALID_SORT_COLUMNS = [
'songs.title',
'songs.track',
'songs.length',
'songs.created_at',
'artists.name',
'albums.name',
2024-05-19 05:49:42 +00:00
'podcasts.title',
'podcasts.author',
2024-01-03 17:02:18 +00:00
];
private User $user;
public function inDirectory(string $path): self
{
// Make sure the path ends with a directory separator.
$path = rtrim(trim($path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
return $this->where('path', 'LIKE', "$path%");
}
public function withMeta(bool $requiresInteractions = false): self
{
$joinClosure = function (JoinClause $join): void {
$join->on('interactions.song_id', 'songs.id')->where('interactions.user_id', $this->user->id);
};
2024-01-03 17:02:18 +00:00
return $this
->with('artist', 'album', 'album.artist')
->when(
$requiresInteractions,
static fn (self $query) => $query->join('interactions', $joinClosure),
static fn (self $query) => $query->leftJoin('interactions', $joinClosure)
)
2024-05-19 05:49:42 +00:00
->leftJoin('albums', 'songs.album_id', 'albums.id')
->leftJoin('artists', 'songs.artist_id', 'artists.id')
->select(
'songs.*',
'albums.name',
'artists.name',
'interactions.liked',
'interactions.play_count'
);
}
public function accessible(): self
2024-01-03 17:02:18 +00:00
{
if (License::isCommunity()) {
// In the Community Edition, all songs are accessible by all users.
return $this;
}
// We want to alias both podcasts and podcast_user tables to avoid possible conflicts with other joins.
return $this->leftJoin('podcasts as podcasts_a11y', 'songs.podcast_id', 'podcasts_a11y.id')
->leftJoin('podcast_user as podcast_user_a11y', function (JoinClause $join): void {
$join->on('podcasts_a11y.id', 'podcast_user_a11y.podcast_id')
->where('podcast_user_a11y.user_id', $this->user->id);
})
->where(function (Builder $query): void {
// Songs must be public or owned by the user.
$query->where('songs.is_public', true)
->orWhere('songs.owner_id', $this->user->id);
})->whereNot(static function (Builder $query): void {
// Episodes must belong to a podcast that the user is not subscribed to.
$query->whereNotNull('songs.podcast_id')
->whereNull('podcast_user_a11y.podcast_id');
});
2024-01-03 17:02:18 +00:00
}
2024-05-19 05:49:42 +00:00
private function sortByOneColumn(string $column, string $direction): self
2024-01-03 17:02:18 +00:00
{
$column = self::normalizeSortColumn($column);
Assert::oneOf($column, self::VALID_SORT_COLUMNS);
Assert::oneOf(strtolower($direction), ['asc', 'desc']);
2024-05-19 05:49:42 +00:00
return $this
->orderBy($column, $direction)
->when($column === 'artists.name', static fn (self $query) => $query->orderBy('albums.name')
2024-01-03 17:02:18 +00:00
->orderBy('songs.disc')
->orderBy('songs.track')
->orderBy('songs.title'))
->when($column === 'albums.name', static fn (self $query) => $query->orderBy('artists.name')
2024-01-03 17:02:18 +00:00
->orderBy('songs.disc')
->orderBy('songs.track')
->orderBy('songs.title'))
->when($column === 'track', static fn (self $query) => $query->orderBy('songs.disc')
->orderBy('songs.track'));
2024-01-03 17:02:18 +00:00
}
2024-05-19 05:49:42 +00:00
public function sort(array $columns, string $direction): self
{
$this->leftJoin('podcasts', 'songs.podcast_id', 'podcasts.id');
foreach ($columns as $column) {
$this->sortByOneColumn($column, $direction);
}
return $this;
}
2024-01-03 17:02:18 +00:00
private static function normalizeSortColumn(string $column): string
{
return key_exists($column, self::SORT_COLUMNS_NORMALIZE_MAP)
? self::SORT_COLUMNS_NORMALIZE_MAP[$column]
: $column;
}
public function storedOnCloud(): self
{
return $this->whereNotNull('storage')
->where('storage', '!=', '');
}
2024-05-19 05:49:42 +00:00
public function forUser(User $user): self
2024-05-19 05:49:42 +00:00
{
$this->user = $user;
return $this;
2024-05-19 05:49:42 +00:00
}
}