Make the document (PDF) exclusivity validation — previously web-only — also apply when scheduling/publishing via the public API and MCP, so a misconfigured post can't slip through these entry points. - ContentTypeCompatibleWithMedia: stored-media fallback for partial updates + a stored-state assertStoredPostCompatible(Post) - MCP PublishPostTool: assert stored-state compatibility before publish (the media-side mirror of assertStoredPostPublishable) - API UpdatePostRequest: validate each platform's effective content_type against effective media on schedule/publish (covers publishing without resubmitting content_type) - MCP UpdatePostTool: apply the rule on schedule with stored-media fallback - Tests: API + MCP happy + rejection paths, plus rule fallback/precedence units
152 lines
5.5 KiB
PHP
152 lines
5.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Api\Post;
|
|
|
|
use App\Enums\Post\Status;
|
|
use App\Enums\PostPlatform\ContentType;
|
|
use App\Enums\SocialAccount\Platform;
|
|
use App\Models\Post;
|
|
use App\Models\PostPlatform;
|
|
use App\Rules\ContentFitsPlatformLimits;
|
|
use App\Rules\ContentTypeCompatibleWithMedia;
|
|
use App\Rules\ContentTypeMatchesPostPlatform;
|
|
use App\Support\PostPlatformMetaRules;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class UpdatePostRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
$enforcesPlatformLimits = in_array(
|
|
$this->input('status'),
|
|
[Status::Scheduled->value, Status::Publishing->value],
|
|
true,
|
|
);
|
|
|
|
return [
|
|
'status' => ['required', 'string', Rule::in([Status::Draft->value, Status::Scheduled->value, Status::Publishing->value])],
|
|
'content' => [
|
|
'nullable',
|
|
'string',
|
|
'max:10000',
|
|
Rule::when(
|
|
$enforcesPlatformLimits,
|
|
[new ContentFitsPlatformLimits($this->resolveSelectedPlatforms())]
|
|
),
|
|
],
|
|
'media' => ['sometimes', 'array'],
|
|
'platforms' => ['sometimes', 'array'],
|
|
'platforms.*.id' => ['required', 'uuid', Rule::exists('post_platforms', 'id')->where('post_id', $this->route('post') instanceof Post ? $this->route('post')->id : $this->route('post'))],
|
|
'platforms.*.content_type' => [
|
|
'sometimes',
|
|
'string',
|
|
Rule::in(array_column(ContentType::cases(), 'value')),
|
|
new ContentTypeMatchesPostPlatform,
|
|
],
|
|
...PostPlatformMetaRules::rules(),
|
|
'scheduled_at' => [
|
|
'nullable',
|
|
'date',
|
|
Rule::when(
|
|
$this->input('status') === Status::Scheduled->value,
|
|
['after:now']
|
|
),
|
|
],
|
|
'label_ids' => ['sometimes', 'array'],
|
|
'label_ids.*' => ['uuid', Rule::exists('workspace_labels', 'id')->where('workspace_id', $this->user()->currentWorkspace->id)],
|
|
];
|
|
}
|
|
|
|
public function withValidator(Validator $validator): void
|
|
{
|
|
$validator->after(function (Validator $validator): void {
|
|
if (! in_array($this->input('status'), [Status::Scheduled->value, Status::Publishing->value], true)) {
|
|
return;
|
|
}
|
|
|
|
$this->addMediaCompatibilityErrors($validator);
|
|
|
|
$platformsById = $this->resolveSelectedPlatforms();
|
|
|
|
PostPlatformMetaRules::addRequiredOnPublishErrors(
|
|
$validator,
|
|
$this->input('platforms', []),
|
|
fn ($platform) => $platformsById[data_get($platform, 'id')] ?? null,
|
|
);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* On publish/schedule, validate every platform's *effective* content_type
|
|
* (resubmitted in this request, or its stored value) against the *effective*
|
|
* media (the request's media when sent, otherwise the post's stored media).
|
|
* This closes the gap where a client publishes a misconfigured post — e.g. a
|
|
* PDF on a regular LinkedIn post — without resubmitting content_type, which a
|
|
* field-level rule on `platforms.*.content_type` would skip.
|
|
*/
|
|
private function addMediaCompatibilityErrors(Validator $validator): void
|
|
{
|
|
$routePost = $this->route('post');
|
|
$post = $routePost instanceof Post ? $routePost : Post::find($routePost);
|
|
|
|
if (! $post) {
|
|
return;
|
|
}
|
|
|
|
$media = $this->has('media') ? (array) $this->input('media', []) : (array) ($post->media ?? []);
|
|
|
|
$entries = $this->has('platforms')
|
|
? collect($this->input('platforms', []))->map(fn ($platform, $index) => [
|
|
'key' => "platforms.{$index}.content_type",
|
|
'content_type' => data_get($platform, 'content_type')
|
|
?? $post->postPlatforms()->where('id', data_get($platform, 'id'))->first()?->content_type?->value,
|
|
])
|
|
: $post->postPlatforms()->where('enabled', true)->get()->values()->map(fn ($postPlatform, $index) => [
|
|
'key' => "platforms.{$index}.content_type",
|
|
'content_type' => $postPlatform->content_type?->value,
|
|
]);
|
|
|
|
foreach ($entries as $entry) {
|
|
if (data_get($entry, 'content_type') === null) {
|
|
continue;
|
|
}
|
|
|
|
(new ContentTypeCompatibleWithMedia($media))->validate(
|
|
$entry['key'],
|
|
(string) $entry['content_type'],
|
|
function (string $message) use ($validator, $entry): void {
|
|
$validator->errors()->add($entry['key'], $message);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int|string, Platform>
|
|
*/
|
|
private function resolveSelectedPlatforms(): Collection
|
|
{
|
|
$ids = collect($this->input('platforms', []))->pluck('id')->filter()->all();
|
|
if (empty($ids)) {
|
|
return collect();
|
|
}
|
|
|
|
$post = $this->route('post');
|
|
$postId = $post instanceof Post ? $post->id : $post;
|
|
|
|
return PostPlatform::query()
|
|
->where('post_id', $postId)
|
|
->whereIn('id', $ids)
|
|
->pluck('platform', 'id');
|
|
}
|
|
}
|