This commit is contained in:
ct
2025-07-16 15:49:30 +08:00
parent d4c5fb5589
commit 6c8a69173e
24 changed files with 594 additions and 64 deletions

View File

@@ -0,0 +1,51 @@
<?php
if (! function_exists('get_current_ip')) {
function get_current_ip($ip_override = null)
{
if (! is_null($ip_override)) {
return $ip_override;
}
$ip = null;
if (app()->environment() == 'local') {
return config('platform.general.dev_default_ip');
}
if (getenv('HTTP_CF_CONNECTING_IP')) {
$ip = getenv('HTTP_CF_CONNECTING_IP');
} elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
if (! is_empty($ip)) {
return $ip;
}
$ip_add_set = null;
if (getenv('HTTP_CLIENT_IP')) {
$ip_add_set = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ip_add_set = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_X_FORWARDED')) {
$ip_add_set = getenv('HTTP_X_FORWARDED');
} elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip_add_set = getenv('HTTP_FORWARDED_FOR');
} elseif (getenv('HTTP_FORWARDED')) {
$ip_add_set = getenv('HTTP_FORWARDED');
} elseif (getenv('REMOTE_ADDR')) {
$ip_add_set = getenv('REMOTE_ADDR');
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip_add_set = $_SERVER['REMOTE_ADDR'];
} else {
$ip_add_set = '127.0.0.0';
}
$ip_add_set = explode(',', $ip_add_set);
return $ip_add_set[0];
}
}

View File

@@ -4,3 +4,4 @@
include 'cast_helpers.php'; include 'cast_helpers.php';
include 'generation_helpers.php'; include 'generation_helpers.php';
include 'user_access_helpers.php'; include 'user_access_helpers.php';
include 'geo_helper.php';

View File

@@ -6,6 +6,7 @@
use App\Models\BackgroundMedia; use App\Models\BackgroundMedia;
use App\Models\Meme; use App\Models\Meme;
use App\Models\MemeMedia; use App\Models\MemeMedia;
use App\Services\TrackingAnalyticsService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class FrontMediaController extends Controller class FrontMediaController extends Controller
@@ -61,6 +62,16 @@ public function searchMemes(Request $request)
$query = $request->input('query', ''); $query = $request->input('query', '');
$limit = 30; $limit = 30;
// Track the search if there's a query
if (! empty($query)) {
try {
$trackingService = app(TrackingAnalyticsService::class);
$trackingService->trackSearch('meme', $query);
} catch (\Exception $e) {
// Silently ignore tracking errors
}
}
if (empty($query)) { if (empty($query)) {
// Return random memes if no search query // Return random memes if no search query
$memes = MemeMedia::where('type', 'video')->where('sub_type', 'overlay')->take($limit)->inRandomOrder()->get(); $memes = MemeMedia::where('type', 'video')->where('sub_type', 'overlay')->take($limit)->inRandomOrder()->get();
@@ -108,6 +119,16 @@ public function searchBackgrounds(Request $request)
$query = $request->input('query', ''); $query = $request->input('query', '');
$limit = 30; $limit = 30;
// Track the search if there's a query
if (! empty($query)) {
try {
$trackingService = app(TrackingAnalyticsService::class);
$trackingService->trackSearch('background', $query);
} catch (\Exception $e) {
// Silently ignore tracking errors
}
}
if (empty($query)) { if (empty($query)) {
// Return random backgrounds if no search query // Return random backgrounds if no search query
$backgrounds = BackgroundMedia::where('status', 'completed')->take($limit)->inRandomOrder()->get(); $backgrounds = BackgroundMedia::where('status', 'completed')->take($limit)->inRandomOrder()->get();
@@ -134,4 +155,99 @@ public function searchBackgrounds(Request $request)
], ],
]); ]);
} }
public function selectMeme(Request $request)
{
try {
$request->validate([
'meme_ids' => 'required|string',
]);
// Decode the hashid to get the actual database ID
$memeMediaId = hashids_decode($request->meme_ids);
if ($memeMediaId) {
// Get the meme media for the name
$memeMedia = MemeMedia::find($memeMediaId);
if ($memeMedia) {
// Track the content selection
$trackingService = app(TrackingAnalyticsService::class);
$trackingService->trackContentSelection(
'meme',
$memeMediaId,
$memeMedia->name,
'browse'
);
}
}
} catch (\Exception $e) {
// Silently ignore all errors
}
return response()->json(['success' => true]);
}
public function selectBackground(Request $request)
{
try {
$request->validate([
'background_ids' => 'required|string',
]);
// Decode the hashid to get the actual database ID
$backgroundMediaId = hashids_decode($request->background_ids);
if ($backgroundMediaId) {
// Get the background media for the name
$backgroundMedia = BackgroundMedia::find($backgroundMediaId);
if ($backgroundMedia) {
// Track the content selection
$trackingService = app(TrackingAnalyticsService::class);
$trackingService->trackContentSelection(
'background',
$backgroundMediaId,
$backgroundMedia->prompt ?? 'Background',
'browse'
);
}
}
} catch (\Exception $e) {
// Silently ignore all errors
}
return response()->json(['success' => true]);
}
public function saveMeme(Request $request)
{
try {
$request->validate([
'meme_media_ids' => 'nullable|string',
'background_media_ids' => 'nullable|string',
'caption_texts' => 'required|array',
'caption_texts.*' => 'string',
'is_premium_export' => 'boolean',
]);
// Convert hashids to actual database IDs
$memeMediaId = $request->meme_media_ids ? hashids_decode($request->meme_media_ids) : null;
$backgroundMediaId = $request->background_media_ids ? hashids_decode($request->background_media_ids) : null;
// Track the save action
$trackingService = app(TrackingAnalyticsService::class);
$trackingService->trackSave(
null, // meme_id
$memeMediaId,
$backgroundMediaId,
$request->caption_texts,
$request->is_premium_export ?? false
);
} catch (\Exception $e) {
// Silently ignore all errors
}
return response()->json(['success' => true]);
}
} }

View File

@@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\ExportToken; use App\Models\ExportToken;
use App\Services\TrackingAnalyticsService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@@ -11,6 +12,17 @@ class UserExportController extends Controller
{ {
public function premiumExportRequest(Request $request) public function premiumExportRequest(Request $request)
{ {
$request->validate([
'meme_media_ids' => 'nullable|string', // Accept hashid string
'background_media_ids' => 'nullable|string', // Accept hashid string
'caption_texts' => 'required|array',
'caption_texts.*' => 'string',
]);
// Convert hashids to actual database IDs
$memeMediaId = $request->meme_media_ids ? hashids_decode($request->meme_media_ids) : null;
$backgroundMediaId = $request->background_media_ids ? hashids_decode($request->background_media_ids) : null;
$user = Auth::user(); $user = Auth::user();
$user->load('user_usage'); $user->load('user_usage');
@@ -33,9 +45,22 @@ public function premiumExportRequest(Request $request)
'token' => Str::uuid()->toString(), 'token' => Str::uuid()->toString(),
'is_premium' => true, 'is_premium' => true,
'credits_reserved' => 1, 'credits_reserved' => 1,
'metadata' => [
'meme_media_id' => $memeMediaId,
'background_media_id' => $backgroundMediaId,
'caption_texts' => $request->caption_texts,
],
'expires_at' => now()->addMinutes(30), 'expires_at' => now()->addMinutes(30),
]); ]);
// Track the export
try {
$trackingService = app(TrackingAnalyticsService::class);
$trackingService->trackExport(null, $memeMediaId, $backgroundMediaId, $request->caption_texts);
} catch (\Exception $e) {
// Silently ignore tracking errors
}
$user->user_usage->refresh(); $user->user_usage->refresh();
return response()->json([ return response()->json([
@@ -92,6 +117,17 @@ public function premiumExportComplete(Request $request)
public function basicExportRequest(Request $request) public function basicExportRequest(Request $request)
{ {
$request->validate([
'meme_media_ids' => 'nullable|string', // Accept hashid string
'background_media_ids' => 'nullable|string', // Accept hashid string
'caption_texts' => 'required|array',
'caption_texts.*' => 'string',
]);
// Convert hashids to actual database IDs
$memeMediaId = $request->meme_media_ids ? hashids_decode($request->meme_media_ids) : null;
$backgroundMediaId = $request->background_media_ids ? hashids_decode($request->background_media_ids) : null;
// No authentication required for basic exports // No authentication required for basic exports
// Create export token (expires in 30 minutes) // Create export token (expires in 30 minutes)
$token = ExportToken::create([ $token = ExportToken::create([
@@ -100,8 +136,21 @@ public function basicExportRequest(Request $request)
'is_premium' => false, 'is_premium' => false,
'credits_reserved' => 0, // No credits for basic exports 'credits_reserved' => 0, // No credits for basic exports
'expires_at' => now()->addMinutes(30), 'expires_at' => now()->addMinutes(30),
'metadata' => [
'meme_media_id' => $memeMediaId,
'background_media_id' => $backgroundMediaId,
'caption_texts' => $request->caption_texts,
],
]); ]);
// Track the export
try {
$trackingService = app(TrackingAnalyticsService::class);
$trackingService->trackExport(null, $memeMediaId, $backgroundMediaId, $request->caption_texts);
} catch (\Exception $e) {
// Silently ignore tracking errors
}
return response()->json([ return response()->json([
'success' => [ 'success' => [
'data' => [ 'data' => [

View File

@@ -21,7 +21,6 @@ public function __construct(
private int $contentId, private int $contentId,
private string $contentName, private string $contentName,
private string $selectionMethod, private string $selectionMethod,
private ?string $searchQuery,
private Carbon $actionAt, private Carbon $actionAt,
private ?string $userAgent, private ?string $userAgent,
private ?string $ipAddress, private ?string $ipAddress,
@@ -39,7 +38,6 @@ public function handle(): void
'content_id' => $this->contentId, 'content_id' => $this->contentId,
'content_name' => $this->contentName, 'content_name' => $this->contentName,
'selection_method' => $this->selectionMethod, 'selection_method' => $this->selectionMethod,
'search_query' => $this->searchQuery,
'action_at' => $this->actionAt, 'action_at' => $this->actionAt,
'user_agent' => $this->userAgent, 'user_agent' => $this->userAgent,
'ip_address' => $this->ipAddress, 'ip_address' => $this->ipAddress,

View File

@@ -21,8 +21,6 @@ public function __construct(
private ?int $memeMediaId, private ?int $memeMediaId,
private ?int $backgroundMediaId, private ?int $backgroundMediaId,
private array $captionTexts, private array $captionTexts,
private string $exportFormat,
private string $exportQuality,
private Carbon $actionAt, private Carbon $actionAt,
private ?string $userAgent, private ?string $userAgent,
private ?string $ipAddress, private ?string $ipAddress,
@@ -31,30 +29,25 @@ public function __construct(
$this->onQueue('tracking'); $this->onQueue('tracking');
} }
public function handle(): int public function handle(): void
{ {
try { try {
$trackingExport = TrackingExport::create([ TrackingExport::create([
'device_id' => $this->deviceId, 'device_id' => $this->deviceId,
'meme_id' => $this->memeId, 'meme_id' => $this->memeId,
'meme_media_id' => $this->memeMediaId, 'meme_media_id' => $this->memeMediaId,
'background_media_id' => $this->backgroundMediaId, 'background_media_id' => $this->backgroundMediaId,
'caption_texts' => $this->captionTexts, 'caption_texts' => $this->captionTexts,
'export_format' => $this->exportFormat,
'export_quality' => $this->exportQuality,
'export_status' => 'initiated', 'export_status' => 'initiated',
'action_at' => $this->actionAt, 'action_at' => $this->actionAt,
'user_agent' => $this->userAgent, 'user_agent' => $this->userAgent,
'ip_address' => $this->ipAddress, 'ip_address' => $this->ipAddress,
'platform' => $this->platform, 'platform' => $this->platform,
]); ]);
return $trackingExport->id;
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error('Failed to track export', [ Log::error('Failed to track export', [
'device_id' => $this->deviceId, 'device_id' => $this->deviceId,
'meme_id' => $this->memeId, 'meme_id' => $this->memeId,
'export_format' => $this->exportFormat,
'error' => $e->getMessage(), 'error' => $e->getMessage(),
]); ]);
@@ -67,7 +60,6 @@ public function failed(\Throwable $exception): void
Log::error('TrackExportJob failed permanently', [ Log::error('TrackExportJob failed permanently', [
'device_id' => $this->deviceId, 'device_id' => $this->deviceId,
'meme_id' => $this->memeId, 'meme_id' => $this->memeId,
'export_format' => $this->exportFormat,
'exception' => $exception->getMessage(), 'exception' => $exception->getMessage(),
]); ]);
} }

71
app/Jobs/TrackSaveJob.php Normal file
View File

@@ -0,0 +1,71 @@
<?php
namespace App\Jobs;
use App\Models\TrackingSave;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class TrackSaveJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
private string $deviceId,
private ?int $memeId,
private ?int $memeMediaId,
private ?int $backgroundMediaId,
private array $captionTexts,
private bool $isPremiumExport,
private Carbon $actionAt,
private ?string $userAgent,
private ?string $ipAddress,
private string $platform
) {
$this->onQueue('tracking');
}
public function handle(): void
{
try {
TrackingSave::create([
'device_id' => $this->deviceId,
'meme_id' => $this->memeId,
'meme_media_id' => $this->memeMediaId,
'background_media_id' => $this->backgroundMediaId,
'caption_texts' => $this->captionTexts,
'is_premium_export' => $this->isPremiumExport,
'action_at' => $this->actionAt,
'user_agent' => $this->userAgent,
'ip_address' => $this->ipAddress,
'platform' => $this->platform,
]);
} catch (\Exception $e) {
Log::error('Failed to track save', [
'device_id' => $this->deviceId,
'meme_id' => $this->memeId,
'meme_media_id' => $this->memeMediaId,
'background_media_id' => $this->backgroundMediaId,
'error' => $e->getMessage(),
]);
throw $e;
}
}
public function failed(\Throwable $exception): void
{
Log::error('TrackSaveJob failed permanently', [
'device_id' => $this->deviceId,
'meme_id' => $this->memeId,
'meme_media_id' => $this->memeMediaId,
'background_media_id' => $this->backgroundMediaId,
'exception' => $exception->getMessage(),
]);
}
}

View File

@@ -15,12 +15,14 @@ class ExportToken extends Model
'token', 'token',
'is_premium', 'is_premium',
'credits_reserved', 'credits_reserved',
'metadata',
'expires_at', 'expires_at',
'used_at', 'used_at',
]; ];
protected $casts = [ protected $casts = [
'is_premium' => 'boolean', 'is_premium' => 'boolean',
'metadata' => 'array',
'expires_at' => 'datetime', 'expires_at' => 'datetime',
'used_at' => 'datetime', 'used_at' => 'datetime',
]; ];

View File

@@ -17,7 +17,6 @@ class TrackingContentSelection extends Model
'content_type', 'content_type',
'content_id', 'content_id',
'content_name', 'content_name',
'search_query',
'selection_method', 'selection_method',
'action_at', 'action_at',
]; ];

View File

@@ -19,8 +19,6 @@ class TrackingExport extends Model
'meme_media_id', 'meme_media_id',
'background_media_id', 'background_media_id',
'caption_texts', 'caption_texts',
'export_format',
'export_quality',
'export_status', 'export_status',
'error_message', 'error_message',
'action_at', 'action_at',
@@ -37,7 +35,6 @@ class TrackingExport extends Model
protected $attributes = [ protected $attributes = [
'platform' => 'web', 'platform' => 'web',
'export_quality' => 'standard',
'export_status' => 'initiated', 'export_status' => 'initiated',
]; ];

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class TrackingSave extends Model
{
use HasFactory;
protected $table = 'track_saves';
protected $fillable = [
'device_id',
'user_agent',
'ip_address',
'platform',
'meme_id',
'meme_media_id',
'background_media_id',
'caption_texts',
'is_premium_export',
'action_at',
];
protected $casts = [
'caption_texts' => 'array',
'is_premium_export' => 'boolean',
'action_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
protected $attributes = [
'platform' => 'web',
'is_premium_export' => false,
];
/**
* Get the related meme
*/
public function meme()
{
return $this->belongsTo(Meme::class, 'meme_id');
}
/**
* Get the related meme media
*/
public function memeMedia()
{
return $this->belongsTo(MemeMedia::class, 'meme_media_id');
}
/**
* Get the related background media
*/
public function backgroundMedia()
{
return $this->belongsTo(BackgroundMedia::class, 'background_media_id');
}
}

View File

@@ -4,6 +4,7 @@
use App\Jobs\TrackContentSelectionJob; use App\Jobs\TrackContentSelectionJob;
use App\Jobs\TrackExportJob; use App\Jobs\TrackExportJob;
use App\Jobs\TrackSaveJob;
use App\Jobs\TrackSearchJob; use App\Jobs\TrackSearchJob;
use App\Jobs\UpdateExportStatusJob; use App\Jobs\UpdateExportStatusJob;
use Carbon\Carbon; use Carbon\Carbon;
@@ -11,9 +12,9 @@
class TrackingAnalyticsService class TrackingAnalyticsService
{ {
/** /**
* Track a search action * Track a search action with full context
*/ */
public function trackSearch( public function trackSearchWithContext(
string $deviceId, string $deviceId,
string $searchType, string $searchType,
string $searchQuery, string $searchQuery,
@@ -36,15 +37,14 @@ public function trackSearch(
} }
/** /**
* Track a content selection action * Track a content selection action with full context
*/ */
public function trackContentSelection( public function trackContentSelectionWithContext(
string $deviceId, string $deviceId,
string $contentType, string $contentType,
int $contentId, int $contentId,
string $contentName, string $contentName,
string $selectionMethod, string $selectionMethod,
?string $searchQuery = null,
?Carbon $actionAt = null, ?Carbon $actionAt = null,
?string $userAgent = null, ?string $userAgent = null,
?string $ipAddress = null, ?string $ipAddress = null,
@@ -56,7 +56,6 @@ public function trackContentSelection(
$contentId, $contentId,
$contentName, $contentName,
$selectionMethod, $selectionMethod,
$searchQuery,
$actionAt ?? now(), $actionAt ?? now(),
$userAgent, $userAgent,
$ipAddress, $ipAddress,
@@ -65,29 +64,54 @@ public function trackContentSelection(
} }
/** /**
* Track an export action * Track an export action with full context
*/ */
public function trackExport( public function trackExportWithContext(
string $deviceId, string $deviceId,
?int $memeId, ?int $memeId,
?int $memeMediaId, ?int $memeMediaId,
?int $backgroundMediaId, ?int $backgroundMediaId,
array $captionTexts, array $captionTexts,
string $exportFormat,
string $exportQuality = 'standard',
?Carbon $actionAt = null, ?Carbon $actionAt = null,
?string $userAgent = null, ?string $userAgent = null,
?string $ipAddress = null, ?string $ipAddress = null,
string $platform = 'web' string $platform = 'web'
): int { ): void {
return TrackExportJob::dispatchSync( TrackExportJob::dispatch(
$deviceId, $deviceId,
$memeId, $memeId,
$memeMediaId, $memeMediaId,
$backgroundMediaId, $backgroundMediaId,
$captionTexts, $captionTexts,
$exportFormat, $actionAt ?? now(),
$exportQuality, $userAgent,
$ipAddress,
$platform
);
}
/**
* Track a save action with full context
*/
public function trackSaveWithContext(
string $deviceId,
?int $memeId,
?int $memeMediaId,
?int $backgroundMediaId,
array $captionTexts,
bool $isPremiumExport,
?Carbon $actionAt = null,
?string $userAgent = null,
?string $ipAddress = null,
string $platform = 'web'
): void {
TrackSaveJob::dispatch(
$deviceId,
$memeId,
$memeMediaId,
$backgroundMediaId,
$captionTexts,
$isPremiumExport,
$actionAt ?? now(), $actionAt ?? now(),
$userAgent, $userAgent,
$ipAddress, $ipAddress,
@@ -121,7 +145,7 @@ public function getDeviceContext(): array
return [ return [
'user_agent' => $request->userAgent(), 'user_agent' => $request->userAgent(),
'ip_address' => $request->ip(), 'ip_address' => get_current_ip(),
'platform' => 'web', // Default for now, can be enhanced for mobile detection 'platform' => 'web', // Default for now, can be enhanced for mobile detection
]; ];
} }
@@ -145,14 +169,14 @@ public function generateDeviceId(): string
} }
/** /**
* Quick track methods with auto device context * Track methods with auto device context
*/ */
public function quickTrackSearch(string $searchType, string $searchQuery, ?array $searchFilters = null): void public function trackSearch(string $searchType, string $searchQuery, ?array $searchFilters = null): void
{ {
$context = $this->getDeviceContext(); $context = $this->getDeviceContext();
$deviceId = $this->generateDeviceId(); $deviceId = $this->generateDeviceId();
$this->trackSearch( $this->trackSearchWithContext(
$deviceId, $deviceId,
$searchType, $searchType,
$searchQuery, $searchQuery,
@@ -164,18 +188,17 @@ public function quickTrackSearch(string $searchType, string $searchQuery, ?array
); );
} }
public function quickTrackContentSelection(string $contentType, int $contentId, string $contentName, string $selectionMethod, ?string $searchQuery = null): void public function trackContentSelection(string $contentType, int $contentId, string $contentName, string $selectionMethod): void
{ {
$context = $this->getDeviceContext(); $context = $this->getDeviceContext();
$deviceId = $this->generateDeviceId(); $deviceId = $this->generateDeviceId();
$this->trackContentSelection( $this->trackContentSelectionWithContext(
$deviceId, $deviceId,
$contentType, $contentType,
$contentId, $contentId,
$contentName, $contentName,
$selectionMethod, $selectionMethod,
$searchQuery,
null, null,
$context['user_agent'], $context['user_agent'],
$context['ip_address'], $context['ip_address'],
@@ -183,19 +206,36 @@ public function quickTrackContentSelection(string $contentType, int $contentId,
); );
} }
public function quickTrackExport(?int $memeId, ?int $memeMediaId, ?int $backgroundMediaId, array $captionTexts, string $exportFormat, string $exportQuality = 'standard'): int public function trackExport(?int $memeId, ?int $memeMediaId, ?int $backgroundMediaId, array $captionTexts): void
{ {
$context = $this->getDeviceContext(); $context = $this->getDeviceContext();
$deviceId = $this->generateDeviceId(); $deviceId = $this->generateDeviceId();
return $this->trackExport( $this->trackExportWithContext(
$deviceId, $deviceId,
$memeId, $memeId,
$memeMediaId, $memeMediaId,
$backgroundMediaId, $backgroundMediaId,
$captionTexts, $captionTexts,
$exportFormat, null,
$exportQuality, $context['user_agent'],
$context['ip_address'],
$context['platform']
);
}
public function trackSave(?int $memeId, ?int $memeMediaId, ?int $backgroundMediaId, array $captionTexts, bool $isPremiumExport = false): void
{
$context = $this->getDeviceContext();
$deviceId = $this->generateDeviceId();
$this->trackSaveWithContext(
$deviceId,
$memeId,
$memeMediaId,
$backgroundMediaId,
$captionTexts,
$isPremiumExport,
null, null,
$context['user_agent'], $context['user_agent'],
$context['ip_address'], $context['ip_address'],

View File

@@ -4,4 +4,11 @@
'enable_test_routes' => env('ENABLE_TEST_ROUTES', false), 'enable_test_routes' => env('ENABLE_TEST_ROUTES', false),
'authed_route_redirect' => 'home', 'authed_route_redirect' => 'home',
'dev_default_ip' => env('DEV_DEFAULT_IP', '127.0.0.1'),
'dev_default_location' => env('DEV_DEFAULT_LOCATION', 'US'),
'ip_override' => env('IP_OVERRIDE', null),
]; ];

View File

@@ -24,7 +24,6 @@ public function up(): void
$table->enum('content_type', ['meme', 'background']); $table->enum('content_type', ['meme', 'background']);
$table->unsignedBigInteger('content_id'); $table->unsignedBigInteger('content_id');
$table->string('content_name'); $table->string('content_name');
$table->text('search_query')->nullable();
$table->enum('selection_method', ['search', 'browse', 'featured', 'recent']); $table->enum('selection_method', ['search', 'browse', 'featured', 'recent']);
// Timestamps // Timestamps

View File

@@ -25,8 +25,6 @@ public function up(): void
$table->unsignedBigInteger('meme_media_id')->nullable(); $table->unsignedBigInteger('meme_media_id')->nullable();
$table->unsignedBigInteger('background_media_id')->nullable(); $table->unsignedBigInteger('background_media_id')->nullable();
$table->json('caption_texts'); $table->json('caption_texts');
$table->enum('export_format', ['mov', 'webm', 'gif', 'webp']);
$table->enum('export_quality', ['standard', 'premium'])->default('standard');
$table->enum('export_status', ['initiated', 'processing', 'completed', 'failed'])->default('initiated'); $table->enum('export_status', ['initiated', 'processing', 'completed', 'failed'])->default('initiated');
$table->text('error_message')->nullable(); $table->text('error_message')->nullable();
@@ -43,7 +41,6 @@ public function up(): void
// Indexes // Indexes
$table->index(['device_id', 'action_at']); $table->index(['device_id', 'action_at']);
$table->index(['export_status', 'action_at']); $table->index(['export_status', 'action_at']);
$table->index(['export_format', 'action_at']);
$table->index('platform'); $table->index('platform');
}); });
} }

View File

@@ -28,15 +28,8 @@ public function up(): void
*/ */
public function down(): void public function down(): void
{ {
Schema::table('export_tokens', function (Blueprint $table) { // This migration made user_id nullable to support anonymous users
// Drop foreign key constraint // Reversing this would break existing anonymous tokens, so we'll leave it nullable
$table->dropForeign(['user_id']); // and just update the foreign key constraint if needed
// Make user_id NOT nullable again
$table->unsignedBigInteger('user_id')->nullable(false)->change();
// Add foreign key back without nullable
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
} }
}; };

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('export_tokens', function (Blueprint $table) {
$table->json('metadata')->nullable()->after('credits_reserved');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('export_tokens', function (Blueprint $table) {
$table->dropColumn('metadata');
});
}
};

View File

@@ -0,0 +1,51 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('track_saves', function (Blueprint $table) {
$table->id();
// Common fields
$table->string('device_id');
$table->text('user_agent')->nullable();
$table->string('ip_address')->nullable();
$table->enum('platform', ['web', 'ios', 'android'])->default('web');
// Save action fields
$table->unsignedBigInteger('meme_id')->nullable();
$table->unsignedBigInteger('meme_media_id')->nullable();
$table->unsignedBigInteger('background_media_id')->nullable();
$table->json('caption_texts');
$table->boolean('is_premium_export')->default(false);
// Timestamps
$table->timestamp('action_at');
$table->timestamps();
// Indexes
$table->index(['device_id', 'action_at']);
$table->index(['meme_id', 'action_at']);
$table->index(['meme_media_id', 'action_at']);
$table->index(['background_media_id', 'action_at']);
$table->index(['is_premium_export', 'action_at']);
$table->index('platform');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('track_saves');
}
};

View File

@@ -3,6 +3,7 @@ import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } f
import { Progress } from '@/components/ui/progress'; import { Progress } from '@/components/ui/progress';
import { Spinner } from '@/components/ui/spinner.js'; import { Spinner } from '@/components/ui/spinner.js';
import { Textarea } from '@/components/ui/textarea'; import { Textarea } from '@/components/ui/textarea';
import useMediaStore from '@/stores/MediaStore';
import useUserStore from '@/stores/UserStore'; import useUserStore from '@/stores/UserStore';
import { Clock10Icon, Download } from 'lucide-react'; import { Clock10Icon, Download } from 'lucide-react';
import { useEffect, useRef, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
@@ -18,6 +19,9 @@ const VideoDownloadModal = ({
exportStatus, exportStatus,
videoBlob, videoBlob,
videoBlobFilename, videoBlobFilename,
selectedMeme,
selectedBackground,
currentCaption,
}) => { }) => {
const [showDebug, setShowDebug] = useState(false); const [showDebug, setShowDebug] = useState(false);
const [isPremiumExport, setIsPremiumExport] = useState(false); const [isPremiumExport, setIsPremiumExport] = useState(false);
@@ -31,6 +35,7 @@ const VideoDownloadModal = ({
const lastProgress = useRef(0); const lastProgress = useRef(0);
const { premiumExportRequest, premiumExportComplete, basicExportRequest, basicExportComplete } = useUserStore(); const { premiumExportRequest, premiumExportComplete, basicExportRequest, basicExportComplete } = useUserStore();
const { saveMeme } = useMediaStore();
const handleShareOrDownload = async () => { const handleShareOrDownload = async () => {
if (!videoBlob || !videoBlobFilename) { if (!videoBlob || !videoBlobFilename) {
@@ -40,6 +45,10 @@ const VideoDownloadModal = ({
setDownloadState('downloading'); setDownloadState('downloading');
// Track the save action before actual download/share
const captionTexts = currentCaption ? [currentCaption] : ['Default caption'];
await saveMeme(captionTexts, isPremiumExport);
try { try {
// Check if mobile and supports navigator.share // Check if mobile and supports navigator.share
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent); const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
@@ -96,8 +105,13 @@ const VideoDownloadModal = ({
setIsPremiumExport(true); setIsPremiumExport(true);
setEstimatedTimeRemaining(null); setEstimatedTimeRemaining(null);
// Prepare export data
const memeMediaIds = selectedMeme?.ids || null;
const backgroundMediaIds = selectedBackground?.ids || null;
const captionTexts = currentCaption ? [currentCaption] : ['Default caption'];
// Call premiumExportRequest and check response // Call premiumExportRequest and check response
const response = await premiumExportRequest(); const response = await premiumExportRequest(memeMediaIds, backgroundMediaIds, captionTexts);
if (response?.error) { if (response?.error) {
// Halt the process if there's an error // Halt the process if there's an error
@@ -120,8 +134,23 @@ const VideoDownloadModal = ({
setIsPremiumExport(false); setIsPremiumExport(false);
setEstimatedTimeRemaining(null); setEstimatedTimeRemaining(null);
// Prepare export data
const memeMediaIds = selectedMeme?.ids || null;
const backgroundMediaIds = selectedBackground?.ids || null;
const captionTexts = currentCaption ? [currentCaption] : ['Default caption'];
// Debug logging
console.log('Export data:', {
selectedMeme,
selectedBackground,
currentCaption,
memeMediaIds,
backgroundMediaIds,
captionTexts
});
// Call basicExportRequest and check response // Call basicExportRequest and check response
const response = await basicExportRequest(); const response = await basicExportRequest(memeMediaIds, backgroundMediaIds, captionTexts);
if (response?.error) { if (response?.error) {
// Halt the process if there's an error // Halt the process if there's an error

View File

@@ -622,6 +622,9 @@ const VideoEditor = ({ width, height, onOpenTextSidebar }) => {
exportStatus={exportStatus} exportStatus={exportStatus}
videoBlob={videoBlob} videoBlob={videoBlob}
videoBlobFilename={videoBlobFilename} videoBlobFilename={videoBlobFilename}
selectedMeme={selectedMeme}
selectedBackground={selectedBackground}
currentCaption={currentCaption}
/> />
</> </>
); );

View File

@@ -30,12 +30,26 @@ const useMediaStore = create(
}, },
// Selection actions // Selection actions
selectMeme: (meme) => { selectMeme: async (meme) => {
set({ selectedMeme: meme }); set({ selectedMeme: meme });
try {
await axiosInstance.post(route('api.app.select.meme'), {
meme_ids: meme.ids,
});
} catch (error) {
console.error('Error meme selection:', error);
}
}, },
selectBackground: (background) => { selectBackground: async (background) => {
set({ selectedBackground: background }); set({ selectedBackground: background });
try {
await axiosInstance.post(route('api.app.select.background'), {
background_ids: background.ids,
});
} catch (error) {
console.error('Error background selection:', error);
}
}, },
// Update current caption when user edits text // Update current caption when user edits text
@@ -52,6 +66,23 @@ const useMediaStore = create(
set({ selectedBackground: null }); set({ selectedBackground: null });
}, },
// Save meme action
saveMeme: async (captionTexts, isPremiumExport = false) => {
try {
const { selectedMeme, selectedBackground } = get();
await axiosInstance.post(route('api.app.save.meme'), {
meme_media_ids: selectedMeme?.ids || null,
background_media_ids: selectedBackground?.ids || null,
caption_texts: captionTexts,
is_premium_export: isPremiumExport,
});
} catch (error) {
console.error('Error saving meme:', error);
// Silently ignore save tracking errors
}
},
// Fetch AI hints // Fetch AI hints
fetchAIHints: async () => { fetchAIHints: async () => {
set({ isLoadingAIHints: true }); set({ isLoadingAIHints: true });

View File

@@ -58,9 +58,13 @@ const useUserStore = create(
} }
}, },
premiumExportRequest: async () => { premiumExportRequest: async (memeMediaIds, backgroundMediaIds, captionTexts) => {
try { try {
const response = await axiosInstance.post(route('api.user.premium_export.request')); const response = await axiosInstance.post(route('api.user.premium_export.request'), {
meme_media_ids: memeMediaIds,
background_media_ids: backgroundMediaIds,
caption_texts: captionTexts,
});
if (response?.data?.success?.data?.user_usage) { if (response?.data?.success?.data?.user_usage) {
set({ set({
@@ -109,9 +113,13 @@ const useUserStore = create(
} }
}, },
basicExportRequest: async () => { basicExportRequest: async (memeMediaIds, backgroundMediaIds, captionTexts) => {
try { try {
const response = await axiosInstance.post(route('api.basic_export.request')); const response = await axiosInstance.post(route('api.basic_export.request'), {
meme_media_ids: memeMediaIds,
background_media_ids: backgroundMediaIds,
caption_texts: captionTexts,
});
if (response?.data?.success?.message) { if (response?.data?.success?.message) {
toast.success(response.data.success.message); toast.success(response.data.success.message);

File diff suppressed because one or more lines are too long

View File

@@ -60,4 +60,9 @@
Route::post('background', [FrontMediaController::class, 'background'])->name('api.app.background'); Route::post('background', [FrontMediaController::class, 'background'])->name('api.app.background');
Route::post('search/background', [FrontMediaController::class, 'searchBackgrounds'])->name('api.app.search.background'); Route::post('search/background', [FrontMediaController::class, 'searchBackgrounds'])->name('api.app.search.background');
Route::post('select/meme', [FrontMediaController::class, 'selectMeme'])->name('api.app.select.meme');
Route::post('select/background', [FrontMediaController::class, 'selectBackground'])->name('api.app.select.background');
Route::post('save/meme', [FrontMediaController::class, 'saveMeme'])->name('api.app.save.meme');
}); });