PR #82 made `meta.aspect_ratio` crop Facebook (and already Instagram) feed images at publish time, but the API and MCP surfaces only half-supported it: you couldn't set meta at creation, the value wasn't validated, and responses never returned it. This closes those gaps. - New `AspectRatio` enum is the single source of truth for the allowed ratios (1:1, 4:5, 16:9, original). App/API/MCP requests now validate via `Rule::enum(AspectRatio::class)` — an invalid ratio is rejected everywhere instead of silently center-cropping to square. - API `StorePostRequest` and MCP `CreatePostTool` now accept `platforms.*.meta`; `CreatePost` persists it. MCP create documents `meta` in its schema. - `Api\PostPlatformResource` now exposes `meta`, so API and MCP responses return the aspect_ratio (and other per-platform meta) a client set.
150 lines
6 KiB
PHP
150 lines
6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\App\Post;
|
|
|
|
use App\Enums\Media\Source;
|
|
use App\Enums\Post\Status;
|
|
use App\Enums\PostPlatform\AspectRatio;
|
|
use App\Enums\PostPlatform\ContentType;
|
|
use App\Enums\SocialAccount\Platform;
|
|
use App\Rules\ContentFitsPlatformLimits;
|
|
use App\Rules\ContentTypeCompatibleWithMedia;
|
|
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
|
|
{
|
|
$enforcesMediaCompatibility = 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(
|
|
$enforcesMediaCompatibility,
|
|
[new ContentFitsPlatformLimits($this->resolveSelectedPlatforms())]
|
|
),
|
|
],
|
|
'media' => ['sometimes', 'array'],
|
|
'media.*.id' => ['required', 'string'],
|
|
'media.*.path' => ['required', 'string', 'max:500'],
|
|
'media.*.url' => ['required', 'string', 'max:2048'],
|
|
'media.*.type' => ['sometimes', 'nullable', 'string', 'max:32'],
|
|
'media.*.mime_type' => ['sometimes', 'nullable', 'string', 'max:255'],
|
|
'media.*.original_filename' => ['sometimes', 'nullable', 'string', 'max:500'],
|
|
'media.*.size' => ['sometimes', 'nullable', 'integer'],
|
|
'media.*.meta' => ['sometimes', 'nullable', 'array'],
|
|
'media.*.source' => ['sometimes', 'nullable', 'string', Rule::in(array_column(Source::cases(), 'value'))],
|
|
'media.*.source_meta' => ['sometimes', 'nullable', 'array'],
|
|
'scheduled_at' => [
|
|
'sometimes',
|
|
'nullable',
|
|
'date',
|
|
Rule::when(
|
|
$this->input('status') === Status::Scheduled->value,
|
|
['after:now']
|
|
),
|
|
],
|
|
'platforms' => ['sometimes', 'array'],
|
|
'platforms.*.id' => ['required', 'uuid', Rule::exists('post_platforms', 'id')->where('post_id', $this->route('post')->id)],
|
|
'platforms.*.content_type' => [
|
|
$enforcesMediaCompatibility ? 'required' : 'sometimes',
|
|
'string',
|
|
Rule::in(array_column(ContentType::cases(), 'value')),
|
|
Rule::when($enforcesMediaCompatibility, [new ContentTypeCompatibleWithMedia]),
|
|
],
|
|
'platforms.*.meta' => ['nullable', 'array'],
|
|
'platforms.*.meta.aspect_ratio' => ['sometimes', 'nullable', 'string', Rule::enum(AspectRatio::class)],
|
|
'platforms.*.meta.privacy_level' => ['sometimes', 'nullable', 'string', Rule::in(['PUBLIC_TO_EVERYONE', 'MUTUAL_FOLLOW_FRIENDS', 'FOLLOWER_OF_CREATOR', 'SELF_ONLY'])],
|
|
'platforms.*.meta.auto_add_music' => ['sometimes', 'boolean'],
|
|
'platforms.*.meta.allow_comments' => ['sometimes', 'boolean'],
|
|
'platforms.*.meta.allow_duet' => ['sometimes', 'boolean'],
|
|
'platforms.*.meta.allow_stitch' => ['sometimes', 'boolean'],
|
|
'platforms.*.meta.is_aigc' => ['sometimes', 'boolean'],
|
|
'platforms.*.meta.disclose' => ['sometimes', 'boolean'],
|
|
'platforms.*.meta.brand_content_toggle' => ['sometimes', 'boolean'],
|
|
'platforms.*.meta.brand_organic_toggle' => ['sometimes', 'boolean'],
|
|
'platforms.*.meta.board_id' => ['sometimes', 'nullable', 'string'],
|
|
'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): void {
|
|
if (! $this->isPublishingOrScheduling()) {
|
|
return;
|
|
}
|
|
|
|
$platforms = $this->input('platforms', []);
|
|
$ids = collect($platforms)->pluck('id')->filter()->all();
|
|
|
|
$platformsById = $this->route('post')
|
|
->postPlatforms()
|
|
->whereIn('id', $ids)
|
|
->pluck('platform', 'id');
|
|
|
|
foreach ($platforms as $i => $platform) {
|
|
$platformEnum = $platformsById[data_get($platform, 'id')] ?? null;
|
|
if ($platformEnum === Platform::TikTok
|
|
&& blank(data_get($platform, 'meta.privacy_level'))) {
|
|
$validator->errors()->add(
|
|
"platforms.{$i}.meta.privacy_level",
|
|
trans('posts.form.tiktok.privacy_required'),
|
|
);
|
|
}
|
|
|
|
if ($platformEnum === Platform::Pinterest
|
|
&& blank(data_get($platform, 'meta.board_id'))) {
|
|
$validator->errors()->add(
|
|
"platforms.{$i}.meta.board_id",
|
|
trans('posts.form.pinterest.board_required'),
|
|
);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private function isPublishingOrScheduling(): bool
|
|
{
|
|
return in_array(
|
|
$this->input('status'),
|
|
[Status::Scheduled->value, Status::Publishing->value],
|
|
true,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int|string, Platform>
|
|
*/
|
|
private function resolveSelectedPlatforms(): Collection
|
|
{
|
|
$ids = collect($this->input('platforms', []))->pluck('id')->filter()->all();
|
|
if (empty($ids)) {
|
|
return collect();
|
|
}
|
|
|
|
return $this->route('post')
|
|
->postPlatforms()
|
|
->whereIn('id', $ids)
|
|
->pluck('platform', 'id');
|
|
}
|
|
}
|