314 lines
8.7 KiB
PHP
314 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\PacePreference;
|
|
use App\Enums\PhysicalActivityLevel;
|
|
use App\Enums\UserRole;
|
|
use App\Enums\UserSex;
|
|
use App\Enums\WeightGoal;
|
|
use Database\Factories\UserFactory;
|
|
use Filament\Models\Contracts\FilamentUser;
|
|
use Filament\Models\Contracts\HasAvatar;
|
|
use Filament\Panel;
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Contracts\Translation\HasLocalePreference;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
class User extends Authenticatable implements FilamentUser, HasAvatar, HasLocalePreference, MustVerifyEmail
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use HasApiTokens, HasFactory, HasUlids, Notifiable, SoftDeletes;
|
|
|
|
protected $attributes = [
|
|
'social_notifications_enabled' => true,
|
|
'engagement_reminders_enabled' => true,
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'email',
|
|
'locale',
|
|
'role',
|
|
'email_verified_at',
|
|
'password',
|
|
'avatar_url',
|
|
'height',
|
|
'target_weight',
|
|
'bio',
|
|
'account_verified_at',
|
|
'certification_purchased_at',
|
|
'subscription_product_id',
|
|
'subscription_store',
|
|
'subscription_expires_at',
|
|
'subscription_is_trial',
|
|
'physical_activity_level',
|
|
'weight_goal',
|
|
'pace_preference',
|
|
'sex',
|
|
'date_of_birth',
|
|
'onboarding_completed_at',
|
|
'nutrition_estimate_accepted_at',
|
|
'terms_accepted_at',
|
|
'social_notifications_enabled',
|
|
'engagement_reminders_enabled',
|
|
'suspended_at',
|
|
'suspended_by',
|
|
'suspended_reason',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'account_verified_at' => 'datetime',
|
|
'certification_purchased_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
'height' => 'integer',
|
|
'target_weight' => 'float',
|
|
'physical_activity_level' => PhysicalActivityLevel::class,
|
|
'weight_goal' => WeightGoal::class,
|
|
'pace_preference' => PacePreference::class,
|
|
'sex' => UserSex::class,
|
|
'role' => UserRole::class,
|
|
'date_of_birth' => 'immutable_date',
|
|
'onboarding_completed_at' => 'datetime',
|
|
'nutrition_estimate_accepted_at' => 'datetime',
|
|
'terms_accepted_at' => 'datetime',
|
|
'social_notifications_enabled' => 'boolean',
|
|
'engagement_reminders_enabled' => 'boolean',
|
|
'suspended_at' => 'datetime',
|
|
'subscription_expires_at' => 'datetime',
|
|
'subscription_is_trial' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public function getFilamentAvatarUrl(): ?string
|
|
{
|
|
return $this->avatar_url ? Storage::url($this->avatar_url) : null;
|
|
}
|
|
|
|
public function deviceTokens(): HasMany
|
|
{
|
|
return $this->hasMany(DeviceToken::class);
|
|
}
|
|
|
|
public function socialAccounts(): HasMany
|
|
{
|
|
return $this->hasMany(SocialAccount::class);
|
|
}
|
|
|
|
public function mealPosts(): HasMany
|
|
{
|
|
return $this->hasMany(MealPosts::class);
|
|
}
|
|
|
|
public function aiUsages(): HasMany
|
|
{
|
|
return $this->hasMany(AiUsage::class);
|
|
}
|
|
|
|
public function identityVerificationRequest(): HasOne
|
|
{
|
|
return $this->hasOne(IdentityVerificationRequest::class);
|
|
}
|
|
|
|
public function workouts(): HasMany
|
|
{
|
|
return $this->hasMany(WorkoutSessions::class);
|
|
}
|
|
|
|
public function nutritionPlans(): HasMany
|
|
{
|
|
return $this->hasMany(NutritionPlan::class);
|
|
}
|
|
|
|
|
|
public function activeNutritionPlan(): HasOne
|
|
{
|
|
return $this->hasOne(NutritionPlan::class)
|
|
->where('is_active', true)
|
|
->latestOfMany('effective_from');
|
|
}
|
|
|
|
public function weightEntries(): HasMany
|
|
{
|
|
return $this->hasMany(WeightEntry::class);
|
|
}
|
|
|
|
public function latestWeightEntry(): HasOne
|
|
{
|
|
return $this->hasOne(WeightEntry::class)->latestOfMany('measured_at');
|
|
}
|
|
|
|
public function weeklyReviews(): HasMany
|
|
{
|
|
return $this->hasMany(WeeklyReview::class);
|
|
}
|
|
|
|
public function hasCompletedNutritionOnboarding(): bool
|
|
{
|
|
return $this->onboarding_completed_at !== null;
|
|
}
|
|
|
|
public function stravaConnection(): HasOne
|
|
{
|
|
return $this->hasOne(StravaConnection::class);
|
|
}
|
|
|
|
public function reportsMade(): HasMany
|
|
{
|
|
return $this->hasMany(Report::class, 'reporter_id');
|
|
}
|
|
|
|
public function receivedReports(): MorphMany
|
|
{
|
|
return $this->morphMany(Report::class, 'reportable');
|
|
}
|
|
|
|
public function followers(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(self::class, 'follows', 'following_id', 'follower_id')
|
|
->withPivot('status')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function following(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(self::class, 'follows', 'follower_id', 'following_id')
|
|
->withPivot('status')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function blockedUsers(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(self::class, 'user_blocks', 'blocker_id', 'blocked_id')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function blockedByUsers(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(self::class, 'user_blocks', 'blocked_id', 'blocker_id')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function scopeWithoutBlockingRelationshipWith(Builder $query, User $user): Builder
|
|
{
|
|
return $query
|
|
->whereDoesntHave('blockedByUsers', fn (Builder $blockedByQuery): Builder => $blockedByQuery->whereKey($user->getKey()))
|
|
->whereDoesntHave('blockedUsers', fn (Builder $blockedQuery): Builder => $blockedQuery->whereKey($user->getKey()));
|
|
}
|
|
|
|
public function hasBlockingRelationshipWith(User $user): bool
|
|
{
|
|
return $this->blockedUsers()->whereKey($user->getKey())->exists()
|
|
|| $this->blockedByUsers()->whereKey($user->getKey())->exists();
|
|
}
|
|
|
|
public function moderationCase(): MorphOne
|
|
{
|
|
return $this->morphOne(ModerationCase::class, 'caseable');
|
|
}
|
|
|
|
public function assignedModerationCases(): HasMany
|
|
{
|
|
return $this->hasMany(ModerationCase::class, 'assigned_to');
|
|
}
|
|
|
|
public function resolvedModerationCases(): HasMany
|
|
{
|
|
return $this->hasMany(ModerationCase::class, 'resolved_by');
|
|
}
|
|
|
|
public function suspendedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(self::class, 'suspended_by');
|
|
}
|
|
|
|
public function isSuspended(): bool
|
|
{
|
|
return $this->suspended_at !== null;
|
|
}
|
|
|
|
public function canAnalyzeMeals(): bool
|
|
{
|
|
return $this->hasActiveSubscription();
|
|
}
|
|
|
|
public function analysisAccessLevel(): string
|
|
{
|
|
if (! $this->hasActiveSubscription()) {
|
|
return 'free';
|
|
}
|
|
|
|
return $this->subscription_is_trial ? 'trial' : 'subscribed';
|
|
}
|
|
|
|
public function hasActiveSubscription(): bool
|
|
{
|
|
return $this->subscription_product_id !== null
|
|
&& $this->subscription_expires_at?->isFuture() === true;
|
|
}
|
|
|
|
public function hasPurchasedCertification(): bool
|
|
{
|
|
return $this->certification_purchased_at !== null;
|
|
}
|
|
|
|
public function routeNotificationForExpoPush(): array
|
|
{
|
|
return $this->deviceTokens()
|
|
->orderBy('id')
|
|
->pluck('expo_push_token')
|
|
->all();
|
|
}
|
|
|
|
public function preferredLocale(): ?string
|
|
{
|
|
return $this->locale ?: 'en';
|
|
}
|
|
|
|
public function isAdmin(): bool
|
|
{
|
|
return $this->role === UserRole::ADMIN;
|
|
}
|
|
|
|
public function canAccessPanel(Panel $panel): bool
|
|
{
|
|
return $this->role === UserRole::ADMIN || $this->role === UserRole::MODERATOR;
|
|
}
|
|
}
|