This commit is contained in:
ct
2025-07-17 03:28:02 +08:00
parent b195a9e7c3
commit 62cefe271e
9 changed files with 267 additions and 90 deletions

View File

@@ -2,7 +2,7 @@
namespace App\Http\Controllers;
use App\Models\MemeMedia;
use App\Services\MemeMediaService;
use Artesaos\SEOTools\Facades\SEOMeta;
use Artesaos\SEOTools\Facades\OpenGraph;
use Artesaos\SEOTools\Facades\TwitterCard;
@@ -15,6 +15,10 @@
class FrontMemeController extends Controller
{
public function __construct(
private MemeMediaService $memeMediaService
) {}
public function index(Request $request): Response
{
return $this->getMemes($request->input('search'));
@@ -29,23 +33,7 @@ public function search(string $search): Response
private function getMemes(?string $search = null): Response
{
$query = MemeMedia::query()
->where('is_enabled', true)
->orderBy('id', 'desc');
// Search functionality
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);
});
}
$memes = $query->cursorPaginate(24);
$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';
@@ -89,21 +77,10 @@ private function getMemes(?string $search = null): Response
TwitterCard::setType('summary_large_image');
// Get available types for filter
$types = MemeMedia::where('is_enabled', true)
->distinct()
->pluck('type')
->filter();
$types = $this->memeMediaService->getAvailableTypes();
// Get popular keywords for filter
$popularKeywords = MemeMedia::where('is_enabled', true)
->get()
->pluck('keywords')
->flatten()
->countBy()
->sort()
->reverse()
->take(20)
->keys();
$popularKeywords = $this->memeMediaService->getPopularKeywords(20);
return Inertia::render('memes/index', [
'memes' => $memes,
@@ -118,39 +95,10 @@ private function getMemes(?string $search = null): Response
public function show(string $slug): Response
{
$meme = MemeMedia::where('slug', $slug)
->where('is_enabled', true)
->firstOrFail();
$meme = $this->memeMediaService->findBySlug($slug);
// Get related memes based on similar keywords
$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(6)
->get();
// If we have less than 6 related memes, fill up with random ones
if ($relatedMemes->count() < 6) {
$excludeIds = $relatedMemes->pluck('id')->push($meme->id)->toArray();
$needed = 6 - $relatedMemes->count();
$randomMemes = MemeMedia::where('is_enabled', true)
->whereNotIn('id', $excludeIds)
->inRandomOrder()
->limit($needed)
->get();
$relatedMemes = $relatedMemes->merge($randomMemes);
}
$relatedMemes = $this->memeMediaService->getRelatedMemes($meme, 6);
// Set up SEO meta tags for individual meme page
$title = "{$meme->name} - Make Video Memes with MEMEFA.ST";
@@ -184,21 +132,8 @@ public function show(string $slug): Response
public function generateOG(string $ids): HttpResponse
{
// Decode the hashids to get the meme media ID
$memeId = hashids_decode($ids);
if (!$memeId) {
abort(404, 'Meme not found');
}
// Get the meme media
$meme = MemeMedia::where('id', $memeId)
->where('is_enabled', true)
->first();
if (!$meme) {
abort(404, 'Meme not found');
}
// Get the meme media using the service
$meme = $this->memeMediaService->findByHashIds($ids);
// Load the template image
$templatePath = public_path('memefast-og-template.jpg');