2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-06-04 16:19:34 +00:00
|
|
|
use App\Casts\UserPreferencesCast;
|
|
|
|
use App\Values\UserPreferences;
|
2022-06-10 10:47:46 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2022-08-10 14:56:01 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2020-11-14 16:57:25 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2017-08-05 17:44:38 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2015-12-27 09:12:10 +00:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
2016-09-26 06:30:00 +00:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2020-09-06 18:21:39 +00:00
|
|
|
use Laravel\Sanctum\HasApiTokens;
|
2022-11-16 17:57:38 +00:00
|
|
|
use Laravel\Sanctum\PersonalAccessToken;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-02-03 15:39:15 +00:00
|
|
|
/**
|
2021-06-04 16:19:34 +00:00
|
|
|
* @property UserPreferences $preferences
|
|
|
|
* @property int $id
|
|
|
|
* @property bool $is_admin
|
2022-07-27 15:32:36 +00:00
|
|
|
* @property ?string $lastfm_session_key
|
2021-05-21 17:14:00 +00:00
|
|
|
* @property string $name
|
2019-10-23 10:45:57 +00:00
|
|
|
* @property string $email
|
|
|
|
* @property string $password
|
2022-06-10 10:47:46 +00:00
|
|
|
* @property-read string $avatar
|
2022-08-10 14:56:01 +00:00
|
|
|
* @property Collection|array<array-key, Playlist> $playlists
|
|
|
|
* @property Collection|array<array-key, PlaylistFolder> $playlist_folders
|
2022-11-16 17:57:38 +00:00
|
|
|
* @property PersonalAccessToken $currentAccessToken
|
2016-02-03 15:39:15 +00:00
|
|
|
*/
|
2015-12-27 09:12:10 +00:00
|
|
|
class User extends Authenticatable
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
use HasApiTokens;
|
2020-11-14 16:57:25 +00:00
|
|
|
use HasFactory;
|
|
|
|
use Notifiable;
|
2016-09-26 06:30:00 +00:00
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
protected $guarded = ['id'];
|
2021-06-04 16:19:34 +00:00
|
|
|
protected $hidden = ['password', 'remember_token', 'created_at', 'updated_at'];
|
2022-06-10 10:47:46 +00:00
|
|
|
protected $appends = ['avatar'];
|
2021-06-04 16:19:34 +00:00
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
protected $casts = [
|
|
|
|
'is_admin' => 'bool',
|
2021-06-04 16:19:34 +00:00
|
|
|
'preferences' => UserPreferencesCast::class,
|
2015-12-13 04:42:28 +00:00
|
|
|
];
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function playlists(): HasMany
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
return $this->hasMany(Playlist::class);
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:56:01 +00:00
|
|
|
public function playlist_folders(): HasMany // @phpcs:ignore
|
|
|
|
{
|
|
|
|
return $this->hasMany(PlaylistFolder::class);
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function interactions(): HasMany
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
return $this->hasMany(Interaction::class);
|
|
|
|
}
|
2015-12-20 12:17:35 +00:00
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
protected function avatar(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::get(
|
|
|
|
fn () => sprintf('https://www.gravatar.com/avatar/%s?s=192&d=robohash', md5($this->email))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-04 06:25:24 +00:00
|
|
|
/**
|
2022-07-18 11:00:37 +00:00
|
|
|
* Get the user's Last.fm session key.
|
2018-09-04 06:25:24 +00:00
|
|
|
*/
|
2022-07-18 11:00:37 +00:00
|
|
|
protected function lastfmSessionKey(): Attribute
|
2018-09-04 06:25:24 +00:00
|
|
|
{
|
2022-07-18 11:00:37 +00:00
|
|
|
return Attribute::get(fn (): ?string => $this->preferences->lastFmSessionKey);
|
2018-09-04 06:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-18 11:00:37 +00:00
|
|
|
* Determine if the user is connected to Last.fm.
|
2018-09-04 06:25:24 +00:00
|
|
|
*/
|
2022-07-18 11:00:37 +00:00
|
|
|
public function connectedToLastfm(): bool
|
2018-09-04 06:25:24 +00:00
|
|
|
{
|
2022-07-18 11:00:37 +00:00
|
|
|
return (bool) $this->lastfm_session_key;
|
2015-12-20 12:17:35 +00:00
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|