Code-review surfaced two correctness bugs and a security gap that
needed to land before merging.
- UpdatePost::execute disabled every platform when called without
a `platforms` key. PublishPostTool relied on that path, so every
publish-via-MCP queued a job whose handler then found nothing
enabled to publish to. Wrap the platform toggle in
`Arr::has($data, 'platforms')` (matches the existing label_ids
guard a few lines up). Add a regression assertion to
`PostPublishToolTest::publish post immediate dispatches PublishPost
job` that the previously-enabled platform stays enabled.
- StorePostRequest declared rules for only `platforms`,
`scheduled_at`, and `status`. `validated()` then stripped
`content`, `media`, and `label_ids`, so REST `POST /api/posts`
silently created empty drafts. Added rules for content / media /
label_ids (with workspace-scoped `Rule::exists` for labels) and
dropped the unused `status` field — REST callers transition state
via `PUT /posts/{id}`. Removed the dead `platforms.*.content`
rule. Added a feature test that asserts content + media + labels
roundtrip on create, plus a regression that an `is_active=false`
social_account is rejected at validation.
- CreatePost::execute now syncs label_ids itself so REST and MCP
share the behavior. Removed the duplicate sync from CreatePostTool.
- MCP UpdatePostTool didn't scope `platforms.*.id` to the post being
updated, drifting from the REST UpdatePostRequest which adds
`Rule::exists('post_platforms','id')->where('post_id', ...)`. Now
it loads the post first (failing fast with `Post not found.` if
the workspace check rejects), then uses the same Rule::exists.
- MediaAttacher fetched any URL the caller passed, including
loopback / link-local / private targets — classic SSRF pivot.
Now `isPublicHttpUrl` rejects non-http(s) schemes, restricted IP
ranges, and DNS hostnames whose A/AAAA records resolve into those
ranges (covers DNS rebinding). Bypassed under
`app()->runningUnitTests()` so `Http::fake()` keeps working.
Streaming the response body lets us abort early once we exceed
MAX_BYTES instead of buffering the full payload first; redirects
are disabled so a 200→302 trick can't bypass the host check.
- The `media[]` JSON column had a lost-update race in
`attachFromUrls`: read `$post->media`, mutate in PHP, write back.
Two concurrent calls clobbered each other. Now wrapped in a
transaction with `lockForUpdate()`.
- ESLint: `resources/js/actions/**` and `resources/js/routes/**`
are auto-generated by Wayfinder on every build. Their import
order matches PHP scan order, not alphabetical, so import/order
fought eslint-fix forever. Added them to ignores.
89 lines
4 KiB
PHP
89 lines
4 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\Enums\PostPlatform\ContentType;
|
|
use App\Http\Resources\Api\PostResource;
|
|
use App\Models\Post;
|
|
use App\Rules\ContentTypeMatchesPostPlatform;
|
|
use Illuminate\Contracts\JsonSchema\JsonSchema;
|
|
use Illuminate\Validation\Rule;
|
|
use Laravel\Mcp\Request;
|
|
use Laravel\Mcp\Response;
|
|
use Laravel\Mcp\ResponseFactory;
|
|
use Laravel\Mcp\Server\Attributes\Description;
|
|
use Laravel\Mcp\Server\Tool;
|
|
|
|
#[Description('Update a draft post — content, media, scheduled_at, labels, and which platforms are enabled. Cannot edit a post that has already been published.')]
|
|
class UpdatePostTool extends Tool
|
|
{
|
|
public function handle(Request $request): Response|ResponseFactory
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
$postId = data_get($request->all(), 'post_id');
|
|
$post = is_string($postId) ? Post::where('workspace_id', $workspace->id)->find($postId) : null;
|
|
|
|
if (! $post) {
|
|
return Response::error('Post not found.');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'post_id' => ['required', 'uuid'],
|
|
'content' => ['nullable', 'string', 'max:63206'],
|
|
'scheduled_at' => ['nullable', 'date', 'after:now'],
|
|
'status' => ['sometimes', 'string', Rule::in([Status::Draft->value, Status::Scheduled->value])],
|
|
'label_ids' => ['sometimes', 'array'],
|
|
'label_ids.*' => ['uuid', Rule::exists('workspace_labels', 'id')->where('workspace_id', $workspace->id)],
|
|
'platforms' => ['sometimes', 'array'],
|
|
'platforms.*.id' => [
|
|
'required',
|
|
'uuid',
|
|
Rule::exists('post_platforms', 'id')->where('post_id', $post->id),
|
|
],
|
|
'platforms.*.content_type' => ['sometimes', 'string', Rule::in(array_column(ContentType::cases(), 'value')), new ContentTypeMatchesPostPlatform],
|
|
'platforms.*.meta' => ['sometimes', 'array'],
|
|
]);
|
|
|
|
$payload = collect($validated)->except('post_id')->all();
|
|
|
|
$result = UpdatePost::execute($workspace, $post, $payload);
|
|
|
|
if (data_get($result, 'action') === PostAction::AlreadyPublished) {
|
|
return Response::error('Cannot edit a published post.');
|
|
}
|
|
|
|
/** @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 update.'),
|
|
'content' => $schema->string()->description('New caption/text body.'),
|
|
'scheduled_at' => $schema->string()->description('ISO 8601 datetime in the future. Required if status is "scheduled".'),
|
|
'status' => $schema->string()
|
|
->enum([Status::Draft->value, Status::Scheduled->value])
|
|
->description('Post status. Use "draft" to keep editing, "scheduled" to schedule the post. Use publish-post-tool for immediate publish.'),
|
|
'label_ids' => $schema->array()
|
|
->items($schema->string())
|
|
->description('Workspace label IDs to attach (replaces existing labels).'),
|
|
'platforms' => $schema->array()
|
|
->items($schema->object(fn ($p) => [
|
|
'id' => $p->string()->required()->description('UUID of the post_platform row (from get-post-tool / list-posts-tool).'),
|
|
'content_type' => $p->string()->description('New content_type for this platform.'),
|
|
'meta' => $p->object()->description('Per-platform metadata override (e.g. aspect_ratio, board_id for Pinterest).'),
|
|
]))
|
|
->description('Platforms to enable for publishing. Any platform NOT listed will be disabled. Pass an empty array to disable all.'),
|
|
];
|
|
}
|
|
}
|