trypost/app/Http/Controllers/App/PostAiRegenerateMediaController.php
Paulo Castellano 7854596579 refactor(posts): centralize post editing status checks with PostStatusGuard
- Replaced direct status checks in multiple controllers and actions with the PostStatusGuard utility, improving code readability and maintainability.
- Updated error messages to utilize a centralized method for consistency across the application.
- Removed the BrandImagePalette class, consolidating color resolution logic into the AiImageClient for better organization and type safety.
2026-05-21 19:27:19 -03:00

67 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\App;
use App\Actions\Post\PostStatusGuard;
use App\Enums\Media\Source;
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;
if (PostStatusGuard::blocksEditing($post)) {
return response()->json([
'message' => PostStatusGuard::editBlockedMessage(),
], 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);
}
}