trypost/app/Models/PostPlatform.php

119 lines
3.1 KiB
PHP
Raw Normal View History

2026-01-15 01:13:44 +00:00
<?php
declare(strict_types=1);
2026-01-15 01:13:44 +00:00
namespace App\Models;
use App\Enums\PostPlatform\ContentType;
use App\Enums\PostPlatform\Status;
use App\Enums\SocialAccount\Platform as SocialPlatform;
use Database\Factories\PostPlatformFactory;
2026-01-15 01:13:44 +00:00
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\Support\Facades\Storage;
2026-01-15 01:13:44 +00:00
class PostPlatform extends Model
{
/** @use HasFactory<PostPlatformFactory> */
use HasFactory, HasUuids;
2026-01-15 01:13:44 +00:00
protected $fillable = [
'post_id',
'social_account_id',
2026-01-15 17:24:39 +00:00
'enabled',
2026-01-15 01:13:44 +00:00
'platform',
'platform_name',
'platform_username',
'platform_avatar',
'content_type',
2026-01-15 01:13:44 +00:00
'status',
'platform_post_id',
'platform_url',
'error_message',
'error_context',
2026-01-15 01:13:44 +00:00
'published_at',
'meta',
];
protected function casts(): array
{
return [
2026-01-15 17:24:39 +00:00
'enabled' => 'boolean',
2026-01-15 01:13:44 +00:00
'platform' => SocialPlatform::class,
'content_type' => ContentType::class,
'status' => Status::class,
2026-01-15 01:13:44 +00:00
'published_at' => 'datetime',
'meta' => 'array',
'error_context' => 'array',
2026-01-15 01:13:44 +00:00
];
}
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
public function socialAccount(): BelongsTo
{
return $this->belongsTo(SocialAccount::class);
}
/**
* Get display name, falling back to snapshot if account was deleted.
*/
public function getDisplayNameAttribute(): string
{
return $this->socialAccount?->display_name ?? $this->platform_name ?? $this->platform->label();
}
/**
* Get username, falling back to snapshot if account was deleted.
*/
public function getDisplayUsernameAttribute(): ?string
{
return $this->socialAccount?->username ?? $this->platform_username;
}
/**
* Get avatar URL, falling back to snapshot if account was deleted.
*/
public function getDisplayAvatarAttribute(): ?string
{
if ($this->socialAccount?->avatar_url) {
return $this->socialAccount->avatar_url;
}
return $this->platform_avatar ? Storage::url($this->platform_avatar) : null;
}
2026-01-15 01:13:44 +00:00
public function markAsPublishing(): void
{
$this->update(['status' => Status::Publishing]);
2026-01-15 01:13:44 +00:00
}
public function markAsPublished(string $platformPostId, ?string $platformUrl = null): void
{
$now = now();
2026-01-15 01:13:44 +00:00
$this->update([
'status' => Status::Published,
2026-01-15 01:13:44 +00:00
'platform_post_id' => $platformPostId,
'platform_url' => $platformUrl,
'published_at' => $now,
2026-01-15 01:13:44 +00:00
]);
$this->socialAccount?->update(['last_used_at' => $now]);
2026-01-15 01:13:44 +00:00
}
public function markAsFailed(string $errorMessage, ?array $errorContext = null): void
2026-01-15 01:13:44 +00:00
{
$this->update([
'status' => Status::Failed,
2026-01-15 01:13:44 +00:00
'error_message' => $errorMessage,
'error_context' => $errorContext,
2026-01-15 01:13:44 +00:00
]);
}
}