koel/app/Models/User.php

137 lines
4 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
use App\Casts\UserPreferencesCast;
use App\Exceptions\UserAlreadySubscribedToPodcast;
2024-03-30 16:49:25 +00:00
use App\Facades\License;
use App\Values\UserPreferences;
2023-08-20 22:35:58 +00:00
use Carbon\Carbon;
2022-06-10 10:47:46 +00:00
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-08-20 22:35:58 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2024-01-18 11:13:05 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
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;
use Illuminate\Support\Arr;
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
/**
* @property UserPreferences $preferences
* @property int $id
* @property bool $is_admin
* @property string $name
* @property string $email
* @property string $password
2024-03-30 16:49:25 +00:00
* @property-read bool $has_custom_avatar
2022-06-10 10:47:46 +00:00
* @property-read string $avatar
* @property Collection<array-key, Playlist> $playlists
* @property Collection<array-key, PlaylistFolder> $playlist_folders
2022-11-16 17:57:38 +00:00
* @property PersonalAccessToken $currentAccessToken
2023-08-20 22:35:58 +00:00
* @property ?Carbon $invitation_accepted_at
* @property ?User $invitedBy
* @property ?string $invitation_token
* @property ?Carbon $invited_at
* @property-read bool $is_prospect
* @property Collection<array-key, Playlist> $collaboratedPlaylists
2024-03-30 16:49:25 +00:00
* @property ?string $sso_provider
* @property ?string $sso_id
* @property bool $is_sso
* @property Collection<array-key, Podcast> $podcasts
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;
use HasFactory;
use Notifiable;
2016-09-26 06:30:00 +00:00
2015-12-13 04:42:28 +00:00
protected $guarded = ['id'];
2023-08-20 22:35:58 +00:00
protected $hidden = ['password', 'remember_token', 'created_at', 'updated_at', 'invitation_accepted_at'];
2022-06-10 10:47:46 +00:00
protected $appends = ['avatar'];
2015-12-13 04:42:28 +00:00
protected $casts = [
'is_admin' => 'bool',
'preferences' => UserPreferencesCast::class,
2015-12-13 04:42:28 +00:00
];
2023-08-20 22:35:58 +00:00
public function invitedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'invited_by_id');
}
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);
}
2024-05-19 05:49:42 +00:00
public function podcasts(): BelongsToMany
{
return $this->belongsToMany(Podcast::class)
->using(PodcastUserPivot::class)
->withTimestamps();
}
2024-01-18 11:13:05 +00:00
public function collaboratedPlaylists(): BelongsToMany
{
return $this->belongsToMany(Playlist::class, 'playlist_collaborators')->withTimestamps();
}
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
2024-05-19 05:49:42 +00:00
public function subscribedToPodcast(Podcast $podcast): bool
{
return $this->podcasts()->where('podcast_id', $podcast->id)->exists();
}
public function subscribeToPodcast(Podcast $podcast): void
{
throw_if($this->subscribedToPodcast($podcast), UserAlreadySubscribedToPodcast::make($this, $podcast));
$this->podcasts()->attach($podcast);
}
public function unsubscribeFromPodcast(Podcast $podcast): void
{
$this->podcasts()->detach($podcast);
}
2022-06-10 10:47:46 +00:00
protected function avatar(): Attribute
{
return Attribute::get(fn (): string => avatar_or_gravatar(Arr::get($this->attributes, 'avatar'), $this->email));
2022-06-10 10:47:46 +00:00
}
2024-03-30 16:49:25 +00:00
protected function hasCustomAvatar(): Attribute
{
return Attribute::get(fn (): bool => (bool) Arr::get($this->attributes, 'avatar'));
2024-03-30 16:49:25 +00:00
}
2023-08-20 22:35:58 +00:00
protected function isProspect(): Attribute
{
return Attribute::get(fn (): bool => (bool) $this->invitation_token);
}
2024-03-30 16:49:25 +00:00
protected function isSso(): Attribute
{
return Attribute::get(fn (): bool => License::isPlus() && $this->sso_provider);
}
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
{
return (bool) $this->preferences->lastFmSessionKey;
2015-12-20 12:17:35 +00:00
}
2015-12-13 04:42:28 +00:00
}