Files
hado/core/app/Models/User.php

66 lines
2.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use App\Enums\EventState;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class User extends Model
{
protected $guarded = [];
public function channels(): HasMany
{
return $this->hasMany(Channel::class);
}
public function events(): HasMany
{
return $this->hasMany(Event::class);
}
/** Событие пользователя по id; чужой id — «не найдено», а не 403. */
public function ownEvent(int $id): Event
{
return $this->events()->with('source')->whereKey($id)->firstOrFail();
}
/**
* Живые события в этих состояниях, по сроку.
*
* @param list<string> $states
*/
public function liveEvents(array $states): HasMany
{
return $this->events()->with('source')->whereIn('state', $states)->orderBy('due_date')->orderBy('due_instant');
}
/** Закрытые события для архива: done и expired, свежие первыми, не больше 100. */
public function archive(): HasMany
{
return $this->events()->with('source')
->whereIn('state', [EventState::Done->value, EventState::Expired->value])
->orderByDesc('updated_at')->limit(100);
}
/** «Сейчас» в поясе пользователя. */
public function now(): CarbonImmutable
{
return CarbonImmutable::now($this->tz);
}
/** Начало активных часов в данный календарный день. */
public function activeStart(CarbonImmutable $day): CarbonImmutable
{
return $day->startOfDay()->setTimeFromTimeString($this->quiet_end);
}
/** Конец активных часов (начало тихих) в данный календарный день. */
public function activeEnd(CarbonImmutable $day): CarbonImmutable
{
return $day->startOfDay()->setTimeFromTimeString($this->quiet_start);
}
}