2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2022-07-06 11:05:21 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2020-11-14 16:57:25 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-08-05 16:56:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2018-11-03 23:25:08 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2022-06-10 10:47:46 +00:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2018-08-29 06:30:39 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2020-12-23 10:53:00 +00:00
|
|
|
use Laravel\Scout\Searchable;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-22 20:11:22 +00:00
|
|
|
* @property string $path
|
|
|
|
* @property string $title
|
|
|
|
* @property Album $album
|
|
|
|
* @property Artist $artist
|
|
|
|
* @property float $length
|
|
|
|
* @property string $lyrics
|
|
|
|
* @property int $track
|
|
|
|
* @property int $disc
|
|
|
|
* @property int $album_id
|
|
|
|
* @property string $id
|
|
|
|
* @property int $artist_id
|
|
|
|
* @property int $mtime
|
2020-12-22 23:01:49 +00:00
|
|
|
* @property int $contributing_artist_id
|
2019-08-05 10:57:36 +00:00
|
|
|
*
|
2019-08-05 10:56:48 +00:00
|
|
|
* @method static self updateOrCreate(array $where, array $params)
|
|
|
|
* @method static Builder select(string $string)
|
|
|
|
* @method static Builder inDirectory(string $path)
|
2019-10-23 10:45:57 +00:00
|
|
|
* @method static self first()
|
2021-06-05 10:47:56 +00:00
|
|
|
* @method static Builder orderBy(...$args)
|
2020-09-06 18:21:39 +00:00
|
|
|
* @method static int count()
|
2021-06-04 15:19:33 +00:00
|
|
|
* @method static self|Collection|null find($id)
|
2020-09-07 20:43:23 +00:00
|
|
|
* @method static Builder take(int $count)
|
2022-06-10 10:47:46 +00:00
|
|
|
* @method static float|int sum(string $column)
|
2022-07-06 16:08:55 +00:00
|
|
|
* @method static Builder latest(string $column = 'created_at')
|
2022-07-07 10:45:47 +00:00
|
|
|
* @method static Builder where(...$params)
|
|
|
|
* @method static Song findOrFail(string $id)
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
class Song extends Model
|
|
|
|
{
|
2020-11-14 16:57:25 +00:00
|
|
|
use HasFactory;
|
2020-12-23 10:53:00 +00:00
|
|
|
use Searchable;
|
2016-09-26 07:32:16 +00:00
|
|
|
use SupportsDeleteWhereIDsNotIn;
|
2021-12-06 16:12:47 +00:00
|
|
|
use SupportsS3;
|
2016-09-26 07:32:16 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
public $incrementing = false;
|
2015-12-13 04:42:28 +00:00
|
|
|
protected $guarded = [];
|
|
|
|
|
2022-07-06 11:05:21 +00:00
|
|
|
protected $hidden = ['updated_at', 'path', 'mtime'];
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2015-12-20 18:09:34 +00:00
|
|
|
protected $casts = [
|
2015-12-21 02:18:00 +00:00
|
|
|
'length' => 'float',
|
2016-03-22 08:22:39 +00:00
|
|
|
'mtime' => 'int',
|
2016-03-28 13:18:09 +00:00
|
|
|
'track' => 'int',
|
2017-12-03 10:02:31 +00:00
|
|
|
'disc' => 'int',
|
2015-12-20 18:09:34 +00:00
|
|
|
];
|
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
protected $keyType = 'string';
|
2018-11-03 23:25:08 +00:00
|
|
|
|
2021-06-05 10:47:56 +00:00
|
|
|
public function artist(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Artist::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function album(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Album::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function playlists(): BelongsToMany
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Playlist::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function interactions(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(Interaction::class);
|
|
|
|
}
|
|
|
|
|
2016-02-02 07:47:00 +00:00
|
|
|
/**
|
|
|
|
* Scope a query to only include songs in a given directory.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function scopeInDirectory(Builder $query, string $path): Builder
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
|
|
|
// Make sure the path ends with a directory separator.
|
2020-12-22 20:11:22 +00:00
|
|
|
$path = rtrim(trim($path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
2016-02-02 07:47:00 +00:00
|
|
|
|
|
|
|
return $query->where('path', 'LIKE', "$path%");
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:05:21 +00:00
|
|
|
protected function title(): Attribute
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-07-06 11:05:21 +00:00
|
|
|
return new Attribute(
|
|
|
|
get: fn (?string $value) => $value ?: pathinfo($this->path, PATHINFO_FILENAME),
|
|
|
|
set: static fn (string $value) => html_entity_decode($value)
|
|
|
|
);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
public static function withMeta(User $scopedUser, ?Builder $query = null): Builder
|
|
|
|
{
|
|
|
|
$query ??= static::query();
|
|
|
|
|
|
|
|
return $query
|
|
|
|
->with('artist', 'album', 'album.artist')
|
|
|
|
->leftJoin('interactions', static function (JoinClause $join) use ($scopedUser): void {
|
|
|
|
$join->on('interactions.song_id', '=', 'songs.id')
|
|
|
|
->where('interactions.user_id', $scopedUser->id);
|
|
|
|
})
|
|
|
|
->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'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeWithMeta(Builder $query, User $scopedUser): Builder
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-06-10 10:47:46 +00:00
|
|
|
return static::withMeta($scopedUser, $query);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
2016-04-17 15:38:06 +00:00
|
|
|
|
2020-12-23 10:53:00 +00:00
|
|
|
/** @return array<mixed> */
|
|
|
|
public function toSearchableArray(): array
|
|
|
|
{
|
|
|
|
$array = [
|
|
|
|
'id' => $this->id,
|
|
|
|
'title' => $this->title,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!$this->artist->is_unknown && !$this->artist->is_various) {
|
|
|
|
$array['artist'] = $this->artist->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
public function __toString(): string
|
2017-06-24 20:46:55 +00:00
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|