2022-08-09 18:45:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Builders;
|
|
|
|
|
2024-01-03 17:02:18 +00:00
|
|
|
use App\Facades\License;
|
2022-08-09 18:45:11 +00:00
|
|
|
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;
|
2022-08-09 18:45:11 +00:00
|
|
|
|
2024-01-03 17:02:18 +00:00
|
|
|
/**
|
|
|
|
* @method self logSql()
|
|
|
|
*/
|
2022-08-09 18:45:11 +00:00
|
|
|
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',
|
|
|
|
];
|
|
|
|
|
|
|
|
private const VALID_SORT_COLUMNS = [
|
|
|
|
'songs.title',
|
|
|
|
'songs.track',
|
|
|
|
'songs.length',
|
|
|
|
'songs.created_at',
|
|
|
|
'artists.name',
|
|
|
|
'albums.name',
|
|
|
|
];
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
public function inDirectory(string $path): static
|
|
|
|
{
|
|
|
|
// Make sure the path ends with a directory separator.
|
|
|
|
$path = rtrim(trim($path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
return $this->where('path', 'LIKE', "$path%");
|
|
|
|
}
|
|
|
|
|
2024-01-03 17:02:18 +00:00
|
|
|
public function withMetaFor(User $user, bool $requiresInteractions = false): static
|
2022-08-09 18:45:11 +00:00
|
|
|
{
|
2024-01-03 17:02:18 +00:00
|
|
|
$joinType = $requiresInteractions ? 'join' : 'leftJoin';
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
return $this
|
|
|
|
->with('artist', 'album', 'album.artist')
|
2024-01-03 17:02:18 +00:00
|
|
|
->$joinType('interactions', static function (JoinClause $join) use ($user): void {
|
|
|
|
$join->on('interactions.song_id', '=', 'songs.id')
|
|
|
|
->where('interactions.user_id', $user->id);
|
2022-08-09 18:45:11 +00:00
|
|
|
})
|
|
|
|
->join('albums', 'songs.album_id', '=', 'albums.id')
|
|
|
|
->join('artists', 'songs.artist_id', '=', 'artists.id')
|
|
|
|
->select(
|
|
|
|
'songs.*',
|
|
|
|
'albums.name',
|
|
|
|
'artists.name',
|
|
|
|
'interactions.liked',
|
|
|
|
'interactions.play_count'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-03 17:02:18 +00:00
|
|
|
public function accessibleBy(User $user, bool $withTableName = true): static
|
|
|
|
{
|
|
|
|
if (License::isCommunity()) {
|
|
|
|
// In the Community Edition, all songs are accessible by all users.
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->where(static function (Builder $query) use ($user, $withTableName): void {
|
|
|
|
$query->where(($withTableName ? 'songs.' : '') . 'is_public', true)
|
|
|
|
->orWhere(($withTableName ? 'songs.' : '') . 'owner_id', $user->id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sort(string $column, string $direction): static
|
|
|
|
{
|
|
|
|
$column = self::normalizeSortColumn($column);
|
|
|
|
|
|
|
|
Assert::oneOf($column, self::VALID_SORT_COLUMNS);
|
|
|
|
Assert::oneOf(strtolower($direction), ['asc', 'desc']);
|
|
|
|
|
|
|
|
$this->orderBy($column, $direction);
|
|
|
|
|
|
|
|
if ($column === 'artists.name') {
|
|
|
|
$this->orderBy('albums.name')
|
|
|
|
->orderBy('songs.disc')
|
|
|
|
->orderBy('songs.track')
|
|
|
|
->orderBy('songs.title');
|
|
|
|
} elseif ($column === 'albums.name') {
|
|
|
|
$this->orderBy('artists.name')
|
|
|
|
->orderBy('songs.disc')
|
|
|
|
->orderBy('songs.track')
|
|
|
|
->orderBy('songs.title');
|
|
|
|
} elseif ($column === 'track') {
|
|
|
|
$this->orderBy('songs.disc')
|
|
|
|
->orderBy('songs.track');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function normalizeSortColumn(string $column): string
|
|
|
|
{
|
|
|
|
return key_exists($column, self::SORT_COLUMNS_NORMALIZE_MAP)
|
|
|
|
? self::SORT_COLUMNS_NORMALIZE_MAP[$column]
|
|
|
|
: $column;
|
|
|
|
}
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
public function hostedOnS3(): static
|
|
|
|
{
|
|
|
|
return $this->where('path', 'LIKE', 's3://%');
|
|
|
|
}
|
|
|
|
}
|