2015-12-13 12:42:28 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-06-04 18:19:34 +02:00
|
|
|
use App\Casts\UserPreferencesCast;
|
2024-05-31 13:40:34 +08:00
|
|
|
use App\Exceptions\UserAlreadySubscribedToPodcast;
|
2024-03-30 17:49:25 +01:00
|
|
|
use App\Facades\License;
|
2021-06-04 18:19:34 +02:00
|
|
|
use App\Values\UserPreferences;
|
2023-08-21 00:35:58 +02:00
|
|
|
use Carbon\Carbon;
|
2022-06-10 12:47:46 +02:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2022-08-10 16:56:01 +02:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2020-11-14 17:57:25 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2023-08-21 00:35:58 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2024-01-18 12:13:05 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2017-08-05 18:44:38 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2015-12-27 16:12:10 +07:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
2016-09-26 14:30:00 +08:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2024-03-22 21:34:32 +01:00
|
|
|
use Illuminate\Support\Arr;
|
2020-09-06 20:21:39 +02:00
|
|
|
use Laravel\Sanctum\HasApiTokens;
|
2022-11-16 18:57:38 +01:00
|
|
|
use Laravel\Sanctum\PersonalAccessToken;
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2016-02-03 23:39:15 +08:00
|
|
|
/**
|
2024-09-15 15:33:59 +02:00
|
|
|
* @property ?Carbon $invitation_accepted_at
|
|
|
|
* @property ?Carbon $invited_at
|
|
|
|
* @property ?User $invitedBy
|
|
|
|
* @property ?string $invitation_token
|
|
|
|
* @property Collection<array-key, Playlist> $collaboratedPlaylists
|
|
|
|
* @property Collection<array-key, Playlist> $playlists
|
|
|
|
* @property Collection<array-key, PlaylistFolder> $playlist_folders
|
|
|
|
* @property Collection<array-key, Podcast> $podcasts
|
|
|
|
* @property PersonalAccessToken $currentAccessToken
|
2021-06-04 18:19:34 +02:00
|
|
|
* @property UserPreferences $preferences
|
|
|
|
* @property bool $is_admin
|
2024-09-15 15:33:59 +02:00
|
|
|
* @property int $id
|
2019-10-23 12:45:57 +02:00
|
|
|
* @property string $email
|
2024-09-15 15:33:59 +02:00
|
|
|
* @property string $name
|
2019-10-23 12:45:57 +02:00
|
|
|
* @property string $password
|
2024-09-15 15:33:59 +02:00
|
|
|
* @property-read ?string $sso_id
|
|
|
|
* @property-read ?string $sso_provider
|
|
|
|
* @property-read bool $connected_to_lastfm Whether the user is connected to Last.fm
|
2024-03-30 17:49:25 +01:00
|
|
|
* @property-read bool $has_custom_avatar
|
2023-08-21 00:35:58 +02:00
|
|
|
* @property-read bool $is_prospect
|
2024-09-15 15:33:59 +02:00
|
|
|
* @property-read bool $is_sso
|
|
|
|
* @property-read string $avatar
|
2016-02-03 23:39:15 +08:00
|
|
|
*/
|
2015-12-27 16:12:10 +07:00
|
|
|
class User extends Authenticatable
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
2020-09-06 20:21:39 +02:00
|
|
|
use HasApiTokens;
|
2020-11-14 17:57:25 +01:00
|
|
|
use HasFactory;
|
|
|
|
use Notifiable;
|
2016-09-26 14:30:00 +08:00
|
|
|
|
2015-12-13 12:42:28 +08:00
|
|
|
protected $guarded = ['id'];
|
2023-08-21 00:35:58 +02:00
|
|
|
protected $hidden = ['password', 'remember_token', 'created_at', 'updated_at', 'invitation_accepted_at'];
|
2022-06-10 12:47:46 +02:00
|
|
|
protected $appends = ['avatar'];
|
2021-06-04 18:19:34 +02:00
|
|
|
|
2015-12-13 12:42:28 +08:00
|
|
|
protected $casts = [
|
|
|
|
'is_admin' => 'bool',
|
2021-06-04 18:19:34 +02:00
|
|
|
'preferences' => UserPreferencesCast::class,
|
2015-12-13 12:42:28 +08:00
|
|
|
];
|
|
|
|
|
2023-08-21 00:35:58 +02:00
|
|
|
public function invitedBy(): BelongsTo
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class, 'invited_by_id');
|
|
|
|
}
|
|
|
|
|
2018-08-24 17:27:19 +02:00
|
|
|
public function playlists(): HasMany
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
|
|
|
return $this->hasMany(Playlist::class);
|
|
|
|
}
|
|
|
|
|
2024-05-19 13:49:42 +08:00
|
|
|
public function podcasts(): BelongsToMany
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Podcast::class)
|
|
|
|
->using(PodcastUserPivot::class)
|
|
|
|
->withTimestamps();
|
|
|
|
}
|
|
|
|
|
2024-01-18 12:13:05 +01:00
|
|
|
public function collaboratedPlaylists(): BelongsToMany
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Playlist::class, 'playlist_collaborators')->withTimestamps();
|
|
|
|
}
|
|
|
|
|
2022-08-10 16:56:01 +02:00
|
|
|
public function playlist_folders(): HasMany // @phpcs:ignore
|
|
|
|
{
|
|
|
|
return $this->hasMany(PlaylistFolder::class);
|
|
|
|
}
|
|
|
|
|
2018-08-24 17:27:19 +02:00
|
|
|
public function interactions(): HasMany
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
|
|
|
return $this->hasMany(Interaction::class);
|
|
|
|
}
|
2015-12-20 20:17:35 +08:00
|
|
|
|
2024-05-19 13:49:42 +08:00
|
|
|
public function subscribedToPodcast(Podcast $podcast): bool
|
|
|
|
{
|
|
|
|
return $this->podcasts()->where('podcast_id', $podcast->id)->exists();
|
|
|
|
}
|
|
|
|
|
2024-05-31 13:40:34 +08:00
|
|
|
public function subscribeToPodcast(Podcast $podcast): void
|
|
|
|
{
|
|
|
|
throw_if($this->subscribedToPodcast($podcast), UserAlreadySubscribedToPodcast::make($this, $podcast));
|
|
|
|
|
|
|
|
$this->podcasts()->attach($podcast);
|
|
|
|
}
|
|
|
|
|
2024-05-31 22:51:10 +08:00
|
|
|
public function unsubscribeFromPodcast(Podcast $podcast): void
|
|
|
|
{
|
|
|
|
$this->podcasts()->detach($podcast);
|
|
|
|
}
|
|
|
|
|
2022-06-10 12:47:46 +02:00
|
|
|
protected function avatar(): Attribute
|
|
|
|
{
|
2024-10-06 21:21:30 +02:00
|
|
|
return Attribute::get(fn (): string => avatar_or_gravatar(Arr::get($this->attributes, 'avatar'), $this->email))
|
|
|
|
->shouldCache();
|
2022-06-10 12:47:46 +02:00
|
|
|
}
|
|
|
|
|
2024-03-30 17:49:25 +01:00
|
|
|
protected function hasCustomAvatar(): Attribute
|
|
|
|
{
|
2024-10-06 21:21:30 +02:00
|
|
|
return Attribute::get(fn (): bool => (bool) Arr::get($this->attributes, 'avatar'))
|
|
|
|
->shouldCache();
|
2024-03-30 17:49:25 +01:00
|
|
|
}
|
|
|
|
|
2023-08-21 00:35:58 +02:00
|
|
|
protected function isProspect(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::get(fn (): bool => (bool) $this->invitation_token);
|
|
|
|
}
|
|
|
|
|
2024-03-30 17:49:25 +01:00
|
|
|
protected function isSso(): Attribute
|
|
|
|
{
|
2024-10-06 21:21:30 +02:00
|
|
|
return Attribute::get(fn (): bool => License::isPlus() && $this->sso_provider)->shouldCache();
|
2024-03-30 17:49:25 +01:00
|
|
|
}
|
|
|
|
|
2024-09-15 15:33:59 +02:00
|
|
|
protected function connectedToLastfm(): Attribute
|
2018-09-04 13:25:24 +07:00
|
|
|
{
|
2024-10-06 21:21:30 +02:00
|
|
|
return Attribute::get(fn (): bool => (bool) $this->preferences->lastFmSessionKey)->shouldCache();
|
2015-12-20 20:17:35 +08:00
|
|
|
}
|
2015-12-13 12:42:28 +08:00
|
|
|
}
|