Let users adjust AI-generated slides in place via async job and Echo, while applying workspace brand, background, and text colors to image prompts. Autofill swaps site text/background colors for the image palette, and regeneration is blocked on finalized posts with safer job cleanup. Co-authored-by: Cursor <cursoragent@cursor.com>
74 lines
2.4 KiB
PHP
74 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
use App\Enums\Media\Source;
|
|
use App\Enums\Post\Status as PostStatus;
|
|
use App\Http\Requests\App\Ai\RegeneratePostMediaImageRequest;
|
|
use App\Jobs\Ai\RegeneratePostMediaImage;
|
|
use App\Models\Post;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Str;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class PostAiRegenerateMediaController extends Controller
|
|
{
|
|
public function regenerate(RegeneratePostMediaImageRequest $request, Post $post, string $mediaId): JsonResponse
|
|
{
|
|
$this->authorize('update', $post);
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
$terminalStatuses = [
|
|
PostStatus::Published,
|
|
PostStatus::PartiallyPublished,
|
|
PostStatus::Failed,
|
|
PostStatus::Publishing,
|
|
];
|
|
|
|
if (in_array($post->status, $terminalStatuses, true)) {
|
|
return response()->json([
|
|
'message' => __('posts.cannot_edit_finalized'),
|
|
], Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
}
|
|
|
|
$gate = Gate::inspect('useAi', $workspace->account);
|
|
if ($gate->denied()) {
|
|
return response()->json(['message' => $gate->message()], Response::HTTP_PAYMENT_REQUIRED);
|
|
}
|
|
|
|
$mediaItem = collect($post->media ?? [])
|
|
->first(fn ($item) => data_get($item, 'id') === $mediaId);
|
|
|
|
if (! is_array($mediaItem)) {
|
|
return response()->json([
|
|
'message' => __('posts.ai.image_regenerate.errors.media_not_found'),
|
|
], Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
if (data_get($mediaItem, 'source') !== Source::Ai->value) {
|
|
return response()->json([
|
|
'message' => __('posts.ai.image_regenerate.errors.not_ai_media'),
|
|
], Response::HTTP_UNPROCESSABLE_ENTITY);
|
|
}
|
|
|
|
$regenerationId = (string) Str::uuid();
|
|
|
|
RegeneratePostMediaImage::dispatch(
|
|
workspaceId: $workspace->id,
|
|
postId: $post->id,
|
|
userId: $request->user()->id,
|
|
mediaId: $mediaId,
|
|
regenerationId: $regenerationId,
|
|
instruction: $request->string('instruction')->toString(),
|
|
);
|
|
|
|
return response()->json([
|
|
'regeneration_id' => $regenerationId,
|
|
'channel' => "user.{$request->user()->id}.ai-media.{$regenerationId}",
|
|
], Response::HTTP_ACCEPTED);
|
|
}
|
|
}
|