trypost/app/Models/PostPlatform.php
Paulo Castellano 3f6032c152 fix(facebook): empty-message rejection + state consistency + no re-publish on terminal
Production incident: a customer's Facebook Page post failed with 'The post
is empty. Please enter a message to share.' (error code 197) and ended up
with a contradictory DB state (status=published + error_message=set).

Three independent bugs were uncovered:

A. FacebookPublisher sends 'message'/'description' as null when the user
   posts media without text. Graph API requires the key be omitted, not
   null. Fixed in publishSingleImagePost, publishMultiImagePost,
   publishVideoPost, publishReel.

B. markAsPublished/markAsFailed leak stale fields across transitions
   (a published row could retain error_message from a prior failure,
   vice-versa). Both transitions now explicitly clear the opposite
   side's fields.

C. status='failed' was editable in the UI and the backend, so users
   were re-clicking Publish, generating duplicate failure emails and
   the contradictory state from bug B. The frontend isReadOnly check
   and the UpdatePost backend guard now treat Published/PartiallyPublished/
   Failed/Publishing as terminal. To retry, the user duplicates the post.

11 new tests guarantee these can't regress silently: FB payload shape
per content type, PostPlatform field-clearing on transitions, and the
terminal-status block at the controller level.
2026-05-19 12:47:18 -03:00

122 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\PostPlatform\ContentType;
use App\Enums\PostPlatform\Status;
use App\Enums\SocialAccount\Platform as SocialPlatform;
use Database\Factories\PostPlatformFactory;
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;
class PostPlatform extends Model
{
/** @use HasFactory<PostPlatformFactory> */
use HasFactory, HasUuids;
protected $fillable = [
'post_id',
'social_account_id',
'enabled',
'platform',
'platform_name',
'platform_username',
'platform_avatar',
'content_type',
'status',
'platform_post_id',
'platform_url',
'error_message',
'error_context',
'published_at',
'meta',
];
protected function casts(): array
{
return [
'enabled' => 'boolean',
'platform' => SocialPlatform::class,
'content_type' => ContentType::class,
'status' => Status::class,
'published_at' => 'datetime',
'meta' => 'array',
'error_context' => 'array',
];
}
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;
}
public function markAsPublishing(): void
{
$this->update(['status' => Status::Publishing]);
}
public function markAsPublished(string $platformPostId, ?string $platformUrl = null): void
{
$now = now();
$this->update([
'status' => Status::Published,
'platform_post_id' => $platformPostId,
'platform_url' => $platformUrl,
'published_at' => $now,
'error_message' => null,
'error_context' => null,
]);
$this->socialAccount?->update(['last_used_at' => $now]);
}
public function markAsFailed(string $errorMessage, ?array $errorContext = null): void
{
$this->update([
'status' => Status::Failed,
'error_message' => $errorMessage,
'error_context' => $errorContext,
'platform_post_id' => null,
'platform_url' => null,
]);
}
}