Files
daily-meal-api/app/Models/User.php
T
leonm df4f523089
CI / 🧪 Tests Laravel (push) Successful in 2m7s
CI / 🐳 Build & Push Image (push) Successful in 42s
feat: add persistent mobile notifications
2026-08-11 16:46:05 +02:00

272 lines
7.6 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 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\Cashier\Billable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable implements FilamentUser, HasAvatar, HasLocalePreference, MustVerifyEmail
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use Billable, 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',
'bio',
'account_verified_at',
'certification_purchased_at',
'trial_ends_at',
'daily_calorie_goal',
'daily_protein_goal',
'daily_carbs_goal',
'daily_fats_goal',
'physical_activity_level',
'weight_goal',
'pace_preference',
'sex',
'date_of_birth',
'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',
'daily_calorie_goal' => 'integer',
'daily_protein_goal' => 'float',
'daily_carbs_goal' => 'float',
'daily_fats_goal' => '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',
'terms_accepted_at' => 'datetime',
'social_notifications_enabled' => 'boolean',
'engagement_reminders_enabled' => 'boolean',
'suspended_at' => 'datetime',
'trial_ends_at' => 'datetime',
];
}
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 mealPosts(): HasMany
{
return $this->hasMany(MealPosts::class);
}
public function aiUsages(): HasMany
{
return $this->hasMany(AiUsage::class);
}
public function paymentTransactions(): HasMany
{
return $this->hasMany(PaymentTransaction::class);
}
public function identityVerificationRequest(): HasOne
{
return $this->hasOne(IdentityVerificationRequest::class);
}
public function workouts(): HasMany
{
return $this->hasMany(WorkoutSessions::class);
}
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->subscribed('default') || $this->onGenericTrial();
}
public function analysisAccessLevel(): string
{
if ($this->subscribed('default')) {
return 'subscribed';
}
return $this->onGenericTrial() ? 'trial' : 'free';
}
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;
}
}