2026-05-03 12:36:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
|
|
|
|
|
|
use App\Http\Requests\App\Ai\StartPostCreationRequest;
|
|
|
|
|
use App\Jobs\Ai\StreamPostCreation;
|
|
|
|
|
use App\Models\SocialAccount;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2026-05-08 21:30:11 +00:00
|
|
|
use Illuminate\Http\Request;
|
2026-05-03 22:36:26 +00:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2026-05-03 12:36:50 +00:00
|
|
|
use Illuminate\Support\Str;
|
2026-05-08 21:30:11 +00:00
|
|
|
use Inertia\Inertia;
|
|
|
|
|
use Inertia\Response as InertiaResponse;
|
2026-05-03 12:36:50 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
|
|
class PostAiCreateController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function start(StartPostCreationRequest $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
|
|
|
|
$this->authorize('createPost', $workspace);
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
$gate = Gate::inspect('useAi', $workspace->account);
|
|
|
|
|
if ($gate->denied()) {
|
|
|
|
|
return response()->json(['message' => $gate->message()], Response::HTTP_PAYMENT_REQUIRED);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 12:36:50 +00:00
|
|
|
$socialAccountId = $request->input('social_account_id');
|
|
|
|
|
|
|
|
|
|
if ($socialAccountId) {
|
|
|
|
|
$owned = SocialAccount::where('id', $socialAccountId)
|
|
|
|
|
->where('workspace_id', $workspace->id)
|
|
|
|
|
->exists();
|
|
|
|
|
|
|
|
|
|
if (! $owned) {
|
|
|
|
|
abort(Response::HTTP_FORBIDDEN);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$creationId = (string) Str::uuid();
|
|
|
|
|
|
|
|
|
|
StreamPostCreation::dispatch(
|
|
|
|
|
userId: $request->user()->id,
|
|
|
|
|
creationId: $creationId,
|
|
|
|
|
workspaceId: $workspace->id,
|
|
|
|
|
format: $request->string('format')->toString(),
|
|
|
|
|
socialAccountId: $socialAccountId,
|
|
|
|
|
imageCount: (int) $request->input('image_count', 0),
|
|
|
|
|
prompt: $request->string('prompt')->toString(),
|
2026-05-06 20:21:30 +00:00
|
|
|
date: $request->input('date'),
|
2026-06-17 20:23:33 +00:00
|
|
|
template: $request->input('template', 'image_card'),
|
2026-05-03 12:36:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'creation_id' => $creationId,
|
2026-05-08 21:30:11 +00:00
|
|
|
'channel' => "user.{$request->user()->id}.ai-creation.{$creationId}",
|
2026-05-03 12:36:50 +00:00
|
|
|
], Response::HTTP_ACCEPTED);
|
|
|
|
|
}
|
2026-05-08 21:30:11 +00:00
|
|
|
|
|
|
|
|
public function loading(Request $request, string $creationId): InertiaResponse
|
|
|
|
|
{
|
|
|
|
|
return Inertia::render('posts/ai/Loading', [
|
|
|
|
|
'creationId' => $creationId,
|
|
|
|
|
'channel' => "user.{$request->user()->id}.ai-creation.{$creationId}",
|
|
|
|
|
'imageCount' => (int) $request->query('images', '0'),
|
|
|
|
|
'format' => (string) $request->query('format', ''),
|
|
|
|
|
'prompt' => (string) $request->query('prompt', ''),
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-05-03 12:36:50 +00:00
|
|
|
}
|