trypost/app/Models/User.php

106 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\Notification\Type as NotificationType;
use App\Models\Traits\HasMedia;
use App\Models\Traits\HasWorkspace;
use Database\Factories\UserFactory;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
/** @use HasFactory<UserFactory> */
use HasFactory, HasMedia, HasUuids, HasWorkspace, Notifiable;
/**
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
'account_id',
'current_workspace_id',
'email_verified_at',
];
/**
* @var list<string>
*/
protected $hidden = [
'password',
'two_factor_secret',
'two_factor_recovery_codes',
'remember_token',
];
protected $appends = [
'has_photo',
'photo_url',
];
public function getHasPhotoAttribute(): bool
{
return $this->getFirstMedia('avatar') !== null;
}
public function getPhotoUrlAttribute(): ?string
{
return $this->getFirstMediaUrl('avatar');
}
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'two_factor_confirmed_at' => 'datetime',
];
}
public function notifications(): HasMany
{
return $this->hasMany(Notification::class);
}
public function notificationPreference(): HasOne
{
return $this->hasOne(NotificationPreference::class);
}
public function account(): BelongsTo
{
return $this->belongsTo(Account::class);
}
public function isAccountOwner(): bool
{
return $this->id === $this->account?->owner_id;
}
public function wantsEmailFor(NotificationType $type): bool
{
$preference = $this->notificationPreference;
if (! $preference) {
return true;
}
return match ($type) {
NotificationType::PostPublished => $preference->post_published,
NotificationType::PostFailed, NotificationType::PostPartiallyPublished => $preference->post_failed,
NotificationType::AccountDisconnected => $preference->account_disconnected,
default => true,
};
}
}