This commit is contained in:
ct
2025-07-04 14:55:56 +08:00
parent b5ac848ba2
commit b3ffc261a3
8 changed files with 320 additions and 4 deletions

View File

@@ -4,6 +4,7 @@
use App\Helpers\FirstParty\Credits\CreditsService;
use App\Jobs\GenerateMemeJob;
use App\Models\UserMemeGeneration;
use Illuminate\Http\Request;
use App\Models\Category;
use Auth;
@@ -16,6 +17,23 @@ public function generateMeme(Request $request)
{
$user = Auth::user();
// Check if user has an active job
$activeJobId = Cache::get("user_active_job_{$user->id}");
if ($activeJobId) {
$activeGeneration = UserMemeGeneration::where('job_id', $activeJobId)
->where('user_id', $user->id)
->whereIn('status', ['pending', 'processing'])
->first();
if ($activeGeneration) {
return response()->json([
'error' => [
'message' => 'You already have a meme generation in progress. Please wait for it to complete.',
],
], 400);
}
}
if (!CreditsService::canSpend($user->id, 2)) {
return response()->json([
'error' => [
@@ -28,6 +46,18 @@ public function generateMeme(Request $request)
$jobId = Str::uuid()->toString();
// Create database record
$generation = UserMemeGeneration::create([
'user_id' => $user->id,
'job_id' => $jobId,
'prompt' => $request->prompt,
'status' => 'pending',
'credits_to_be_charged' => 2,
'credits_are_processed' => false,
]);
// Set active job in cache
Cache::put("user_active_job_{$user->id}", $jobId, 300);
Cache::put("meme_job_status_{$jobId}", 'pending', 300);
$job = new GenerateMemeJob($user->id, $request->prompt, $jobId);
@@ -89,6 +119,104 @@ public function checkMemeJobStatus(Request $request)
return response()->json($response);
}
public function getActiveJob()
{
$user = Auth::user();
$activeJobId = Cache::get("user_active_job_{$user->id}");
if (!$activeJobId) {
return response()->json([
'success' => [
'data' => null,
],
]);
}
$generation = UserMemeGeneration::where('job_id', $activeJobId)
->where('user_id', $user->id)
->with('meme.meme_media', 'meme.background_media')
->first();
if (!$generation) {
// Clean up stale cache
Cache::forget("user_active_job_{$user->id}");
return response()->json([
'success' => [
'data' => null,
],
]);
}
$response = [
'success' => [
'data' => [
'job_id' => $generation->job_id,
'status' => $generation->status,
'prompt' => $generation->prompt,
'created_at' => $generation->created_at,
],
],
];
// If completed, include the meme result
if ($generation->status === 'completed' && $generation->meme) {
$meme = $generation->meme;
$meme_media = $generation->meme->meme_media;
$response['success']['data']['result'] = [
'generate' => [
'info' => $meme,
'caption' => $meme->caption,
'meme' => $meme_media,
'background' => $meme->background_media,
],
];
}
return response()->json($response);
}
public function getMemeHistory()
{
$user = Auth::user();
$generations = UserMemeGeneration::where('user_id', $user->id)
->with('meme.meme_media', 'meme.background_media')
->orderBy('created_at', 'desc')
->limit(20)
->get();
$history = $generations->map(function ($generation) {
$data = [
'job_id' => $generation->job_id,
'prompt' => $generation->prompt,
'status' => $generation->status,
'created_at' => $generation->created_at,
];
if ($generation->status === 'completed' && $generation->meme) {
$meme = $generation->meme;
$data['meme'] = [
'info' => $meme,
'caption' => $meme->caption,
'meme_media' => $meme->meme_media,
'background_media' => $meme->background_media,
];
}
return $data;
});
return response()->json([
'success' => [
'data' => [
'history' => $history,
],
],
]);
}
public function aiHints()
{
$categories = Category::whereNotNull('keywords')

View File

@@ -2,8 +2,10 @@
namespace App\Jobs;
use App\Helpers\FirstParty\Credits\CreditsService;
use App\Helpers\FirstParty\Meme\MemeGenerator;
use App\Models\User;
use App\Models\UserMemeGeneration;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@@ -23,7 +25,7 @@ class GenerateMemeJob implements ShouldQueue
protected $prompt;
protected $jobId;
public function __construct(int $userId, string $prompt, string $jobId = null)
public function __construct(int $userId, string $prompt, ?string $jobId)
{
$this->userId = $userId;
$this->prompt = $prompt;
@@ -33,12 +35,27 @@ public function __construct(int $userId, string $prompt, string $jobId = null)
public function handle(): void
{
$userGeneration = UserMemeGeneration::where('job_id', $this->jobId)->first();
if (!$userGeneration) {
throw new \Exception("User generation record not found for job {$this->jobId}");
}
try {
// Update status to processing
$userGeneration->update(['status' => 'processing']);
Cache::put("meme_job_status_{$this->jobId}", 'processing', 300);
$meme = MemeGenerator::generateMemeByKeyword($this->prompt);
$meme_media = MemeGenerator::getSuitableMemeMedia($meme, 2);
// Update the generation record with the meme
$userGeneration->update([
'meme_id' => $meme->id,
'status' => 'completed',
'credits_are_processed' => true,
]);
$result = [
'generate' => [
'info' => $meme,
@@ -50,15 +67,61 @@ public function handle(): void
Cache::put("meme_job_result_{$this->jobId}", $result, 300);
Cache::put("meme_job_status_{$this->jobId}", 'completed', 300);
// Clear active job from cache
Cache::forget("user_active_job_{$this->userId}");
} catch (\Exception $e) {
// Handle failure with credit refund
if (!$userGeneration->credits_are_processed) {
if ($userGeneration->credits_to_be_charged > 0) {
CreditsService::depositAlacarte(
$userGeneration->user_id,
$userGeneration->credits_to_be_charged,
'Refunded credits for failed generation due to provider issue.'
);
}
$userGeneration->update([
'credits_are_processed' => true,
'status' => 'failed',
]);
}
Cache::put("meme_job_status_{$this->jobId}", 'failed', 300);
Cache::put("meme_job_error_{$this->jobId}", $e->getMessage(), 300);
// Clear active job from cache
Cache::forget("user_active_job_{$this->userId}");
throw $e;
}
}
public function failed(\Throwable $exception): void
{
$userGeneration = UserMemeGeneration::where('job_id', $this->jobId)->first();
if ($userGeneration && !$userGeneration->credits_are_processed) {
if ($userGeneration->credits_to_be_charged > 0) {
CreditsService::depositAlacarte(
$userGeneration->user_id,
$userGeneration->credits_to_be_charged,
'Refunded credits for failed generation due to system error.'
);
}
$userGeneration->update([
'credits_are_processed' => true,
'status' => 'failed',
]);
}
Cache::put("meme_job_status_{$this->jobId}", 'failed', 300);
Cache::forget("user_active_job_{$this->userId}");
}
public function getJobId(): string
{
return $this->jobId;
}
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class UserMemeGeneration extends Model
{
protected $fillable = [
'user_id',
'meme_id',
'job_id',
'prompt',
'status',
'credits_to_be_charged',
'credits_are_processed',
];
protected $casts = [
'credits_are_processed' => 'boolean',
'credits_to_be_charged' => 'integer',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function meme(): BelongsTo
{
return $this->belongsTo(Meme::class);
}
}