This commit is contained in:
ct
2025-07-17 03:42:55 +08:00
parent 06675de71b
commit 85917e0cdf
46 changed files with 2426 additions and 121 deletions

View File

@@ -0,0 +1,64 @@
<?php
namespace App\Console\Commands;
use App\Services\MemeMediaService;
use Illuminate\Console\Command;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
class GenerateMemesSitemap extends Command
{
protected $signature = 'sitemap:generate:memes';
protected $description = 'Generate sitemap for individual meme pages (memes.show)';
public function __construct(
private MemeMediaService $memeMediaService
) {
parent::__construct();
}
public function handle(): int
{
$this->info('Starting memes sitemap generation...');
$sitemap = Sitemap::create();
// Get the base URL from config
$baseUrl = config('app.url');
// Get all enabled memes
$memes = $this->memeMediaService->getAllEnabledMemes();
$this->info("Found {$memes->count()} enabled memes");
// Add a progress bar for large datasets
$progressBar = $this->output->createProgressBar($memes->count());
$progressBar->start();
// Add each meme to the sitemap
foreach ($memes as $meme) {
$url = Url::create($baseUrl.'/meme/'.$meme->slug)
->setPriority(0.8)
->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY)
->setLastModificationDate($meme->updated_at);
$sitemap->add($url);
$progressBar->advance();
}
$progressBar->finish();
$this->newLine();
// Save the sitemap
$sitemapPath = public_path('sitemap_memes.xml');
$sitemap->writeToFile($sitemapPath);
$this->info('Memes sitemap generated successfully!');
$this->info("Sitemap saved to: {$sitemapPath}");
$this->info("Total URLs: {$memes->count()}");
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Spatie\Sitemap\SitemapIndex;
class GenerateSitemap extends Command
{
protected $signature = 'sitemap:generate';
protected $description = 'Generate main sitemap index that links to all sub-sitemaps';
public function handle(): int
{
$this->info('Starting main sitemap generation...');
$sitemapIndex = SitemapIndex::create();
// Get the base URL from config
$baseUrl = config('app.url');
// List of sub-sitemaps to include in the main sitemap
$subSitemaps = [
[
'url' => $baseUrl.'/sitemap_static.xml',
'lastModified' => $this->getSitemapLastModified('sitemap_static.xml'),
],
[
'url' => $baseUrl.'/sitemap_memes.xml',
'lastModified' => $this->getSitemapLastModified('sitemap_memes.xml'),
],
// Future sitemaps can be added here:
// [
// 'url' => $baseUrl . '/sitemap_pages.xml',
// 'lastModified' => now(),
// ],
];
// Add each sub-sitemap to the index
foreach ($subSitemaps as $sitemap) {
$sitemapIndex->add($sitemap['url'], $sitemap['lastModified']);
$this->info("Added sub-sitemap: {$sitemap['url']}");
}
// Save the main sitemap index
$sitemapPath = public_path('sitemap.xml');
$sitemapIndex->writeToFile($sitemapPath);
$this->info('Main sitemap index generated successfully!');
$this->info("Sitemap saved to: {$sitemapPath}");
$this->info('Total sub-sitemaps: '.count($subSitemaps));
return Command::SUCCESS;
}
/**
* Get the last modification date of a sitemap file
*/
private function getSitemapLastModified(string $filename): \DateTime
{
$filepath = public_path($filename);
if (file_exists($filepath)) {
return new \DateTime('@'.filemtime($filepath));
}
// If file doesn't exist, return current time
return new \DateTime;
}
}

View File

@@ -0,0 +1,80 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
class GenerateStaticSitemap extends Command
{
protected $signature = 'sitemap:generate:static';
protected $description = 'Generate sitemap for static pages (home, privacy, terms, dmca, meme library)';
public function handle(): int
{
$this->info('Starting static sitemap generation...');
$sitemap = Sitemap::create();
// Get the base URL from config
$baseUrl = config('app.url');
// Add static pages
$staticPages = [
[
'url' => $baseUrl,
'priority' => 1.0,
'changeFrequency' => Url::CHANGE_FREQUENCY_WEEKLY,
'lastModificationDate' => now(),
],
[
'url' => $baseUrl.'/meme-library',
'priority' => 0.9,
'changeFrequency' => Url::CHANGE_FREQUENCY_DAILY,
'lastModificationDate' => now(),
],
[
'url' => $baseUrl.'/privacy',
'priority' => 0.3,
'changeFrequency' => Url::CHANGE_FREQUENCY_YEARLY,
'lastModificationDate' => now()->subMonths(6), // Adjust based on when you last updated
],
[
'url' => $baseUrl.'/terms',
'priority' => 0.3,
'changeFrequency' => Url::CHANGE_FREQUENCY_YEARLY,
'lastModificationDate' => now()->subMonths(6), // Adjust based on when you last updated
],
[
'url' => $baseUrl.'/dmca-copyright',
'priority' => 0.2,
'changeFrequency' => Url::CHANGE_FREQUENCY_YEARLY,
'lastModificationDate' => now()->subMonths(6), // Adjust based on when you last updated
],
];
// Add each static page to the sitemap
foreach ($staticPages as $page) {
$url = Url::create($page['url'])
->setPriority($page['priority'])
->setChangeFrequency($page['changeFrequency'])
->setLastModificationDate($page['lastModificationDate']);
$sitemap->add($url);
$this->info("Added: {$page['url']}");
}
// Save the sitemap
$sitemapPath = public_path('sitemap_static.xml');
$sitemap->writeToFile($sitemapPath);
$this->info('Static sitemap generated successfully!');
$this->info("Sitemap saved to: {$sitemapPath}");
$this->info('Total URLs: '.count($staticPages));
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Console\Commands;
use App\Models\MemeMedia;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
class PopulateMemeMediaSlugs extends Command
{
protected $signature = 'memes:populate-slugs';
protected $description = 'Populate slug field for existing MemeMedia records';
public function handle()
{
$this->info('Starting to populate MemeMedia slugs...');
$memes = MemeMedia::whereNull('slug')->get();
if ($memes->isEmpty()) {
$this->info('No memes found without slugs.');
return;
}
$this->info("Found {$memes->count()} memes without slugs.");
$bar = $this->output->createProgressBar($memes->count());
$bar->start();
foreach ($memes as $meme) {
$baseSlug = Str::slug($meme->name);
$slug = $baseSlug;
$counter = 1;
// Ensure slug is unique
while (MemeMedia::where('slug', $slug)->exists()) {
$slug = $baseSlug.'-'.$counter;
$counter++;
}
$meme->update(['slug' => $slug]);
$bar->advance();
}
$bar->finish();
$this->newLine();
$this->info('Successfully populated all MemeMedia slugs!');
}
}

View File

@@ -4,6 +4,7 @@
use App\Models\BackgroundMedia;
use App\Models\MemeMedia;
use App\Services\MemeMediaService;
use Artesaos\SEOTools\Facades\JsonLd;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
@@ -11,6 +12,10 @@
class FrontHomeController extends Controller
{
public function __construct(
private MemeMediaService $memeMediaService
) {}
public function index()
{
if (App::environment('production') && env('COMING_SOON_ENABLED', true)) {
@@ -28,12 +33,16 @@ public function index()
// Get FAQ data
$faqData = $this->getFaqData();
// Get popular keywords for search suggestions
$popularKeywords = $this->memeMediaService->getPopularKeywords(10);
// Add FAQ JSON-LD structured data
$this->addFaqJsonLd($faqData);
return Inertia::render('home/home', [
'stats' => $stats,
'faqData' => $faqData,
'popularKeywords' => $popularKeywords,
]);
}

View File

@@ -0,0 +1,333 @@
<?php
namespace App\Http\Controllers;
use App\Services\MemeMediaService;
use Artesaos\SEOTools\Facades\OpenGraph;
use Artesaos\SEOTools\Facades\SEOMeta;
use Artesaos\SEOTools\Facades\TwitterCard;
use Illuminate\Http\Request;
use Illuminate\Http\Response as HttpResponse;
use Inertia\Inertia;
use Inertia\Response;
use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\ImageManager;
class FrontMemeController extends Controller
{
public function __construct(
private MemeMediaService $memeMediaService
) {}
public function index(Request $request): Response
{
return $this->getMemes($request->input('search'));
}
public function search(string $search): Response
{
// Convert + back to spaces for search
$searchTerm = str_replace('+', ' ', $search);
return $this->getMemes($searchTerm);
}
private function getMemes(?string $search = null): Response
{
$memes = $this->memeMediaService->searchMemes($search, 24);
// Set up SEO meta tags
$title = $search ? ucfirst($search).' Memes in MEMEFA.ST' : 'Meme Library - Thousands of Video Meme Templates';
if ($search) {
// Get SEO descriptions from config
$descriptions = config('platform.seo_descriptions.search_descriptions', []);
// Use deterministic selection based on search term hash
$searchHash = crc32($search);
$descriptionIndex = abs($searchHash) % count($descriptions);
$descriptionTemplate = $descriptions[$descriptionIndex];
// Replace keyword placeholder
$description = str_replace('__KEYWORD__', $search, $descriptionTemplate);
} else {
$description = 'Browse thousands of video meme templates ready for TikTok, Instagram Reels, Threads and YouTube Shorts. Create viral content in minutes with our meme editor.';
}
SEOMeta::setTitle($title, false);
SEOMeta::setDescription($description);
SEOMeta::setCanonical(request()->url());
// Add pagination rel links
if ($memes->previousPageUrl()) {
SEOMeta::addMeta('link:prev', $memes->previousPageUrl(), 'rel');
}
if ($memes->nextPageUrl()) {
SEOMeta::addMeta('link:next', $memes->nextPageUrl(), 'rel');
}
// OpenGraph tags
OpenGraph::setTitle($title);
OpenGraph::setDescription($description);
OpenGraph::setUrl(request()->url());
OpenGraph::addProperty('type', 'website');
// Twitter Card
TwitterCard::setTitle($title);
TwitterCard::setDescription($description);
TwitterCard::setType('summary_large_image');
// Get available types for filter
$types = $this->memeMediaService->getAvailableTypes();
// Get popular keywords for filter
$popularKeywords = $this->memeMediaService->getPopularKeywords(20);
return Inertia::render('memes/index', [
'memes' => $memes,
'types' => $types,
'popularKeywords' => $popularKeywords,
'filters' => [
'search' => $search,
],
'dynamicDescription' => $search ? $description : null,
]);
}
public function show(string $slug): Response
{
$meme = $this->memeMediaService->findBySlug($slug);
// Get related memes based on similar keywords
$relatedMemes = $this->memeMediaService->getRelatedMemes($meme, 6);
// Set up SEO meta tags for individual meme page
$title = "{$meme->name} - Make Video Memes with MEMEFA.ST";
$description = $meme->description
? "This meme is about: {$meme->description}."
: "Create {$meme->name} video memes with our online editor. Perfect for TikTok, Instagram Reels, and YouTube Shorts.";
SEOMeta::setTitle($title, false);
SEOMeta::setDescription($description);
SEOMeta::setCanonical(request()->url());
SEOMeta::addKeyword(collect([$meme->keywords, $meme->action_keywords, $meme->emotion_keywords, $meme->misc_keywords])->flatten()->filter()->implode(', '));
// OpenGraph tags
OpenGraph::setTitle($title);
OpenGraph::setDescription($description);
OpenGraph::setUrl(request()->url());
OpenGraph::addProperty('type', 'video.other');
OpenGraph::addImage(route('memes.og', $meme->ids));
// Twitter Card
TwitterCard::setTitle($title);
TwitterCard::setDescription($description);
TwitterCard::setType('summary_large_image');
TwitterCard::setImage(route('memes.og', $meme->ids));
return Inertia::render('memes/show', [
'meme' => $meme,
'relatedMemes' => $relatedMemes,
]);
}
public function generateOG(string $ids): HttpResponse
{
// Get the meme media using the service
$meme = $this->memeMediaService->findByHashIds($ids);
// Load the template image
$templatePath = public_path('memefast-og-template.jpg');
if (! file_exists($templatePath)) {
abort(404, 'Template image not found');
}
// Create ImageManager with Imagick driver
$manager = new ImageManager(new Driver);
// Load the template image
$image = $manager->read($templatePath);
// Ensure RGB colorspace for proper color rendering
$imagick = $image->core()->native();
$imagick->setColorspace(\Imagick::COLORSPACE_SRGB);
// Load the meme image from URL
if ($meme->webp_url) {
try {
// Use cURL to get the image content with proper headers
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $meme->webp_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MemeFast OG Generator)');
$imageContent = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($imageContent !== false && $httpCode === 200) {
$memeImage = $manager->read($imageContent);
// Image positioning variables
$imageX = 0; // Horizontal offset
$imageY = 100; // Vertical offset
// Resize meme image to 1.5x the template height while maintaining aspect ratio
$memeImage = $memeImage->scale(height: 1350);
// Place the meme image vertically centered, horizontally right-justified
$image->place($memeImage, 'center-right', $imageX, $imageY);
} else {
$image->text('HTTP Error: '.$httpCode, 200, 200, function ($font) {
$font->size(20);
$font->color('#ff0000');
$font->align('center');
});
}
} catch (\Exception $e) {
// If meme image fails to load, add debug text
$image->text('Image failed to load: '.$e->getMessage(), 200, 200, function ($font) {
$font->size(20);
$font->color('#ff0000');
$font->align('center');
});
}
}
// Add the meme name as text with proper kerning and line wrapping
if ($meme->name) {
$fontPath = resource_path('fonts/Geist/Geist-Bold.ttf');
// Fallback to built-in font if TTF not found
if (file_exists($fontPath)) {
// Use native Imagick for text kerning with line wrapping
$imagick = $image->core()->native(); // Get the native Imagick object
// First draw the green outline
$drawOutline = new \ImagickDraw;
$drawOutline->setFillColor('#00FF00');
$drawOutline->setFont($fontPath);
$drawOutline->setFontSize(70);
$drawOutline->setTextAlignment(\Imagick::ALIGN_LEFT);
$drawOutline->setStrokeColor('#00FF00');
$drawOutline->setStrokeWidth(20); // Thicker stroke for outline effect
// Then draw the black text on top
$draw = new \ImagickDraw;
$draw->setFillColor('#000000');
$draw->setFont($fontPath);
$draw->setFontSize(70);
// $draw->setTextKerning(-4); // Negative for tighter kerning
$draw->setTextAlignment(\Imagick::ALIGN_LEFT);
// Text wrapping - 70% of canvas width (1600 * 0.7 = 1120px)
$maxWidth = 1120;
$text = 'Make meme videos instantly with '.$meme->name.' meme with';
// Split text into words
$words = explode(' ', $text);
$lines = [];
$currentLine = '';
foreach ($words as $word) {
$testLine = $currentLine.($currentLine ? ' ' : '').$word;
// Get text metrics for the test line
$metrics = $imagick->queryFontMetrics($draw, $testLine);
if ($metrics['textWidth'] > $maxWidth && $currentLine !== '') {
// Line is too long, save current line and start new one
$lines[] = $currentLine;
$currentLine = $word;
} else {
$currentLine = $testLine;
}
}
// Add the last line
if ($currentLine) {
$lines[] = $currentLine;
}
$lines[] .= 'MEMEFAST';
// Text positioning variables
$textX = 100; // Horizontal position
$textY = 320; // Base vertical position
// Calculate line height and starting Y position
$lineHeight = 75; // Tighter line spacing
$totalHeight = count($lines) * $lineHeight;
$startY = $textY - ($totalHeight / 2); // Center vertically
// Draw each line (left-aligned) - first the outline, then the text
foreach ($lines as $index => $line) {
$y = $startY + ($index * $lineHeight);
// Check if this is the MEMEFA.ST line
if ($line === 'MEMEFAST') {
$extraSizing = 40;
// Use Bungee font for MEMEFA.ST
$bungeeFontPath = resource_path('fonts/Bungee/Bungee-Regular.ttf');
if (file_exists($bungeeFontPath)) {
// Create separate draw objects for Bungee font
$bungeeOutline = new \ImagickDraw;
$bungeeOutline->setFillColor('#00FF00');
$bungeeOutline->setFont($bungeeFontPath);
$bungeeOutline->setFontSize(70);
$bungeeOutline->setTextAlignment(\Imagick::ALIGN_LEFT);
$bungeeOutline->setStrokeColor('#00FF00');
$bungeeOutline->setStrokeWidth(20);
$bungeeText = new \ImagickDraw;
$bungeeText->setFillColor('#000000');
$bungeeText->setFont($bungeeFontPath);
$bungeeText->setFontSize(70 + $extraSizing);
$bungeeText->setTextAlignment(\Imagick::ALIGN_LEFT);
// Draw with Bungee font
$bungeeOutline->annotation($textX, $y + $extraSizing, $line);
$bungeeText->annotation($textX, $y + $extraSizing, $line);
$imagick->drawImage($bungeeOutline);
$imagick->drawImage($bungeeText);
} else {
// Fallback to regular font
$drawOutline->annotation($textX, $y + $extraSizing, $line);
$draw->annotation($textX, $y + $extraSizing, $line);
}
} else {
// Use regular font for other lines
$drawOutline->annotation($textX, $y, $line);
$draw->annotation($textX, $y, $line);
}
}
// Apply the drawing to the image (only for non-Bungee lines)
$imagick->drawImage($drawOutline); // Draw outline first
$imagick->drawImage($draw); // Draw text on top
} else {
$image->text($meme->name, 400, 450, function ($font) {
$font->size(80);
$font->color('#000000');
$font->align('center');
$font->valign('middle');
});
}
}
// Generate the image and return as response
$encodedImage = $image->toJpeg(100);
return response($encodedImage, 200, [
'Content-Type' => 'image/jpeg',
'Cache-Control' => 'public, max-age=3600',
]);
}
}

View File

@@ -2,6 +2,9 @@
namespace App\Http\Controllers;
use Artesaos\SEOTools\Facades\OpenGraph;
use Artesaos\SEOTools\Facades\SEOMeta;
use Artesaos\SEOTools\Facades\TwitterCard;
use Illuminate\Support\Str;
use Inertia\Inertia;
@@ -9,6 +12,25 @@ class FrontPagesController extends Controller
{
public function privacy()
{
// Set up SEO meta tags
$title = 'Privacy Policy';
$description = 'Read our privacy policy to understand how Memefast collects, uses, and protects your personal information when using our video meme creation platform.';
SEOMeta::setTitle($title);
SEOMeta::setDescription($description);
SEOMeta::setCanonical(request()->url());
// OpenGraph tags
OpenGraph::setTitle($title);
OpenGraph::setDescription($description);
OpenGraph::setUrl(request()->url());
OpenGraph::addProperty('type', 'website');
// Twitter Card
TwitterCard::setTitle($title);
TwitterCard::setDescription($description);
TwitterCard::setType('summary');
$markdownPath = resource_path('markdown/privacy.md');
$markdownContent = file_get_contents($markdownPath);
@@ -26,6 +48,25 @@ public function privacy()
public function terms()
{
// Set up SEO meta tags
$title = 'Terms & Conditions';
$description = 'Review our terms and conditions to understand the rules and guidelines for using Memefast video meme creation platform and services.';
SEOMeta::setTitle($title);
SEOMeta::setDescription($description);
SEOMeta::setCanonical(request()->url());
// OpenGraph tags
OpenGraph::setTitle($title);
OpenGraph::setDescription($description);
OpenGraph::setUrl(request()->url());
OpenGraph::addProperty('type', 'website');
// Twitter Card
TwitterCard::setTitle($title);
TwitterCard::setDescription($description);
TwitterCard::setType('summary');
$markdownPath = resource_path('markdown/terms.md');
$markdownContent = file_get_contents($markdownPath);
@@ -41,6 +82,42 @@ public function terms()
]);
}
public function dmcaCopyright()
{
// Set up SEO meta tags
$title = 'DMCA Copyright Policy';
$description = 'MEMEFA.ST DMCA copyright policy and procedures for reporting copyright infringement. Learn how to file DMCA notices and counter-notices.';
SEOMeta::setTitle($title);
SEOMeta::setDescription($description);
SEOMeta::setCanonical(request()->url());
// OpenGraph tags
OpenGraph::setTitle($title);
OpenGraph::setDescription($description);
OpenGraph::setUrl(request()->url());
OpenGraph::addProperty('type', 'website');
// Twitter Card
TwitterCard::setTitle($title);
TwitterCard::setDescription($description);
TwitterCard::setType('summary');
$markdownPath = resource_path('markdown/dmca-copyright.md');
$markdownContent = file_get_contents($markdownPath);
// Parse markdown to HTML using Laravel's built-in Str::markdown helper
$htmlContent = Str::markdown($markdownContent);
// Style the HTML with Tailwind classes
$styledContent = $this->styleHtmlContent($htmlContent);
return Inertia::render('FrontPages/DmcaCopyright', [
'content' => $styledContent,
'title' => 'DMCA Copyright Policy',
]);
}
private function styleHtmlContent($html)
{
// Add classes to various HTML elements using string replacement for Tailwind 4

View File

@@ -53,6 +53,7 @@ class MemeMedia extends Model
'sub_type',
'original_id',
'name',
'slug',
'description',
'keywords',
'group',
@@ -79,7 +80,7 @@ class MemeMedia extends Model
'type',
'sub_type',
'original_id',
'description',
// 'description',
'mov_uuid',
'webm_uuid',
'gif_uuid',

View File

@@ -0,0 +1,172 @@
<?php
namespace App\Services;
use App\Models\MemeMedia;
use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Collection as SupportCollection;
use Illuminate\Support\Facades\Cache;
class MemeMediaService
{
/**
* Search memes with optional query and pagination
*/
public function searchMemes(?string $search = null, int $perPage = 24): CursorPaginator
{
$query = $this->buildSearchQuery($search);
return $query->cursorPaginate($perPage);
}
/**
* Get popular keywords for filtering
*/
public function getPopularKeywords(int $limit = 20): SupportCollection
{
$cacheKey = "popular_keywords_limit_{$limit}";
return Cache::remember($cacheKey, 60 * 60 * 24, function () use ($limit) {
return MemeMedia::where('is_enabled', true)
->get()
->pluck('keywords')
->flatten()
->countBy()
->sort()
->reverse()
->take($limit)
->keys();
});
}
/**
* Get available meme types for filtering
*/
public function getAvailableTypes(): SupportCollection
{
return MemeMedia::where('is_enabled', true)
->distinct()
->pluck('type')
->filter();
}
/**
* Find meme by slug
*/
public function findBySlug(string $slug): MemeMedia
{
return MemeMedia::where('slug', $slug)
->where('is_enabled', true)
->firstOrFail();
}
/**
* Find meme by hashids
*/
public function findByHashIds(string $ids): MemeMedia
{
$memeId = hashids_decode($ids);
if (! $memeId) {
throw new ModelNotFoundException('Meme not found');
}
return MemeMedia::where('id', $memeId)
->where('is_enabled', true)
->firstOrFail();
}
/**
* Get related memes based on keywords
*/
public function getRelatedMemes(MemeMedia $meme, int $limit = 6): Collection
{
$relatedMemes = MemeMedia::where('is_enabled', true)
->where('id', '!=', $meme->id)
->where(function ($query) use ($meme) {
if ($meme->keywords) {
foreach ($meme->keywords as $keyword) {
$query->orWhereJsonContains('keywords', $keyword)
->orWhereJsonContains('action_keywords', $keyword)
->orWhereJsonContains('emotion_keywords', $keyword)
->orWhereJsonContains('misc_keywords', $keyword);
}
}
})
->limit($limit)
->get();
// If we have less than the desired limit, fill up with random ones
if ($relatedMemes->count() < $limit) {
$excludeIds = $relatedMemes->pluck('id')->push($meme->id)->toArray();
$needed = $limit - $relatedMemes->count();
$randomMemes = $this->fillWithRandomMemes($relatedMemes, $limit, $excludeIds);
$relatedMemes = $relatedMemes->merge($randomMemes);
}
return $relatedMemes;
}
/**
* Fill collection with random memes up to target count
*/
public function fillWithRandomMemes(Collection $existing, int $targetCount, array $excludeIds): Collection
{
$needed = $targetCount - $existing->count();
if ($needed <= 0) {
return collect();
}
return MemeMedia::where('is_enabled', true)
->whereNotIn('id', $excludeIds)
->inRandomOrder()
->limit($needed)
->get();
}
/**
* Build search query with keyword matching
*/
private function buildSearchQuery(?string $search = null): Builder
{
$query = $this->getEnabledMemesQuery();
if ($search) {
$query->where(function ($q) use ($search) {
$q->where('name', 'ilike', "%{$search}%")
->orWhere('description', 'ilike', "%{$search}%")
->orWhereJsonContains('keywords', $search)
->orWhereJsonContains('action_keywords', $search)
->orWhereJsonContains('emotion_keywords', $search)
->orWhereJsonContains('misc_keywords', $search);
});
}
return $query;
}
/**
* Get all enabled memes for sitemap generation
*/
public function getAllEnabledMemes(): Collection
{
return MemeMedia::where('is_enabled', true)
->orderBy('updated_at', 'desc')
->get();
}
/**
* Get base query for enabled memes
*/
private function getEnabledMemesQuery(): Builder
{
return MemeMedia::query()
->where('is_enabled', true)
->orderBy('id', 'desc');
}
}