70 lines
1.6 KiB
PHP
70 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
protected $fillable = [
|
|
'username', 'password', 'status', 'comment', 'chat_id',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password', 'remember_token', 'bot_token',
|
|
];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
$bust = static function (): void {
|
|
app(\App\Services\FilterOptionsService::class)->forgetAgents();
|
|
};
|
|
static::saved($bust);
|
|
static::deleted($bust);
|
|
}
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'password' => 'hashed',
|
|
'status' => 'integer',
|
|
];
|
|
}
|
|
|
|
public function isEnabled(): bool
|
|
{
|
|
return (int) $this->status === 1;
|
|
}
|
|
|
|
public function channels(): HasMany
|
|
{
|
|
return $this->hasMany(Channel::class, 'user_id');
|
|
}
|
|
|
|
public function hasTelegramChat(): bool
|
|
{
|
|
return trim((string) ($this->chat_id ?? '')) !== '';
|
|
}
|
|
|
|
/** @deprecated Use hasTelegramChat(); kept for existing call sites. */
|
|
public function hasTelegramBot(): bool
|
|
{
|
|
return $this->hasTelegramChat();
|
|
}
|
|
|
|
public static function findEnabledByChatId(int|string|null $chatId): ?self
|
|
{
|
|
$chatId = trim((string) $chatId);
|
|
if ($chatId === '') {
|
|
return null;
|
|
}
|
|
|
|
return static::query()
|
|
->where('status', 1)
|
|
->where('chat_id', $chatId)
|
|
->orderBy('id')
|
|
->first();
|
|
}
|
|
}
|