trypost/app/Models/SocialAccount.php
2026-01-14 22:13:44 -03:00

80 lines
2 KiB
PHP

<?php
namespace App\Models;
use App\Enums\SocialPlatform;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Storage;
class SocialAccount extends Model
{
/** @use HasFactory<\Database\Factories\SocialAccountFactory> */
use HasFactory, HasUuids;
protected $fillable = [
'workspace_id',
'platform',
'platform_user_id',
'username',
'display_name',
'avatar_url',
'access_token',
'refresh_token',
'token_expires_at',
'scopes',
'meta',
];
protected $hidden = [
'access_token',
'refresh_token',
];
protected function casts(): array
{
return [
'platform' => SocialPlatform::class,
'access_token' => 'encrypted',
'refresh_token' => 'encrypted',
'token_expires_at' => 'datetime',
'scopes' => 'array',
'meta' => 'array',
];
}
public function workspace(): BelongsTo
{
return $this->belongsTo(Workspace::class);
}
public function postPlatforms(): HasMany
{
return $this->hasMany(PostPlatform::class);
}
protected function isTokenExpired(): Attribute
{
return Attribute::make(
get: fn () => $this->token_expires_at && $this->token_expires_at->isPast(),
);
}
protected function isTokenExpiringSoon(): Attribute
{
return Attribute::make(
get: fn () => $this->token_expires_at && $this->token_expires_at->isBefore(now()->addHour()),
);
}
protected function avatarUrl(): Attribute
{
return Attribute::make(
get: fn (?string $value) => $value ? Storage::url($value) : null,
);
}
}