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 'generation_helpers.php';
include 'user_access_helpers.php';
include 'geo_helper.php';

View File

@@ -6,6 +6,7 @@
use App\Models\BackgroundMedia;
use App\Models\Meme;
use App\Models\MemeMedia;
use App\Services\TrackingAnalyticsService;
use Illuminate\Http\Request;
class FrontMediaController extends Controller
@@ -61,6 +62,16 @@ public function searchMemes(Request $request)
$query = $request->input('query', '');
$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)) {
// Return random memes if no search query
$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', '');
$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)) {
// Return random backgrounds if no search query
$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;
use App\Models\ExportToken;
use App\Services\TrackingAnalyticsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
@@ -11,6 +12,17 @@ class UserExportController extends Controller
{
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->load('user_usage');
@@ -33,9 +45,22 @@ public function premiumExportRequest(Request $request)
'token' => Str::uuid()->toString(),
'is_premium' => true,
'credits_reserved' => 1,
'metadata' => [
'meme_media_id' => $memeMediaId,
'background_media_id' => $backgroundMediaId,
'caption_texts' => $request->caption_texts,
],
'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();
return response()->json([
@@ -92,6 +117,17 @@ public function premiumExportComplete(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
// Create export token (expires in 30 minutes)
$token = ExportToken::create([
@@ -100,8 +136,21 @@ public function basicExportRequest(Request $request)
'is_premium' => false,
'credits_reserved' => 0, // No credits for basic exports
'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([
'success' => [
'data' => [

View File

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

View File

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

View File

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

View File

@@ -19,8 +19,6 @@ class TrackingExport extends Model
'meme_media_id',
'background_media_id',
'caption_texts',
'export_format',
'export_quality',
'export_status',
'error_message',
'action_at',
@@ -37,7 +35,6 @@ class TrackingExport extends Model
protected $attributes = [
'platform' => 'web',
'export_quality' => 'standard',
'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\TrackExportJob;
use App\Jobs\TrackSaveJob;
use App\Jobs\TrackSearchJob;
use App\Jobs\UpdateExportStatusJob;
use Carbon\Carbon;
@@ -11,9 +12,9 @@
class TrackingAnalyticsService
{
/**
* Track a search action
* Track a search action with full context
*/
public function trackSearch(
public function trackSearchWithContext(
string $deviceId,
string $searchType,
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 $contentType,
int $contentId,
string $contentName,
string $selectionMethod,
?string $searchQuery = null,
?Carbon $actionAt = null,
?string $userAgent = null,
?string $ipAddress = null,
@@ -56,7 +56,6 @@ public function trackContentSelection(
$contentId,
$contentName,
$selectionMethod,
$searchQuery,
$actionAt ?? now(),
$userAgent,
$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,
?int $memeId,
?int $memeMediaId,
?int $backgroundMediaId,
array $captionTexts,
string $exportFormat,
string $exportQuality = 'standard',
?Carbon $actionAt = null,
?string $userAgent = null,
?string $ipAddress = null,
string $platform = 'web'
): int {
return TrackExportJob::dispatchSync(
): void {
TrackExportJob::dispatch(
$deviceId,
$memeId,
$memeMediaId,
$backgroundMediaId,
$captionTexts,
$exportFormat,
$exportQuality,
$actionAt ?? now(),
$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(),
$userAgent,
$ipAddress,
@@ -121,7 +145,7 @@ public function getDeviceContext(): array
return [
'user_agent' => $request->userAgent(),
'ip_address' => $request->ip(),
'ip_address' => get_current_ip(),
'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();
$deviceId = $this->generateDeviceId();
$this->trackSearch(
$this->trackSearchWithContext(
$deviceId,
$searchType,
$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();
$deviceId = $this->generateDeviceId();
$this->trackContentSelection(
$this->trackContentSelectionWithContext(
$deviceId,
$contentType,
$contentId,
$contentName,
$selectionMethod,
$searchQuery,
null,
$context['user_agent'],
$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();
$deviceId = $this->generateDeviceId();
return $this->trackExport(
$this->trackExportWithContext(
$deviceId,
$memeId,
$memeMediaId,
$backgroundMediaId,
$captionTexts,
$exportFormat,
$exportQuality,
null,
$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,
$context['user_agent'],
$context['ip_address'],