trypost/app/Mcp/Tools/Post/PublishPostTool.php
Paulo Castellano edec58af81 refactor(post): remove now-dead PostAction::AlreadyPublished
UpdatePost::execute used to return AlreadyPublished for the Published
short-circuit. This PR widened the short-circuit to four terminal
statuses and consolidated them under PostAction::Finalized — so the
old enum case stopped being emitted, and every caller already had a
defensive in_array([AlreadyPublished, Finalized], ...).

Audit before removal: nothing emits AlreadyPublished anymore (only
UpdatePost::execute returns Actions, and it returns Finalized for
the whole terminal set), no test references the case, and no string
'already_published' exists elsewhere in app/resources/tests/lang.

- Drop the enum case
- Simplify the three in_array checks to a direct === Finalized
- Delete the dead App/PostController branch that flashed the old
  cannot_edit_published message (its successor branch with
  cannot_edit_finalized stays). The old i18n key is left in lang/
  for now — orphan but harmless, can ressuscitate if a similar
  flash is added back.
2026-05-19 13:06:13 -03:00

68 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Mcp\Tools\Post;
use App\Actions\Post\UpdatePost;
use App\Enums\Post\Action as PostAction;
use App\Enums\Post\Status;
use App\Http\Resources\Api\PostResource;
use App\Models\Post;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\ResponseFactory;
use Laravel\Mcp\Server\Attributes\Description;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\Annotations\IsDestructive;
#[IsDestructive]
#[Description('Publish a draft post — either immediately or scheduled for a future time. The post must already have at least one enabled platform. Use update-post-tool first to set content/platforms.')]
class PublishPostTool extends Tool
{
public function handle(Request $request): Response|ResponseFactory
{
$workspace = $request->user()->currentWorkspace;
$validated = $request->validate([
'post_id' => ['required', 'uuid'],
'scheduled_at' => ['nullable', 'date', 'after:now'],
]);
$post = Post::where('workspace_id', $workspace->id)->find(data_get($validated, 'post_id'));
if (! $post) {
return Response::error('Post not found.');
}
if (! $post->postPlatforms()->where('enabled', true)->exists()) {
return Response::error('Post has no enabled platforms. Use update-post-tool to enable at least one platform first.');
}
$scheduledAt = data_get($validated, 'scheduled_at');
$result = UpdatePost::execute($workspace, $post, [
'status' => $scheduledAt ? Status::Scheduled->value : Status::Publishing->value,
'scheduled_at' => $scheduledAt,
]);
if (data_get($result, 'action') === PostAction::Finalized) {
return Response::error('Post is already published or in a terminal state.');
}
/** @var Post $updated */
$updated = data_get($result, 'post');
$updated->load(['postPlatforms.socialAccount', 'labels']);
return Response::structured((new PostResource($updated))->resolve());
}
public function schema(JsonSchema $schema): array
{
return [
'post_id' => $schema->string()->required()->description('UUID of the post to publish.'),
'scheduled_at' => $schema->string()->description('ISO 8601 datetime in the future. If provided, the post is queued for that time. If omitted, publishing starts immediately.'),
];
}
}