Skip to content

Instantly share code, notes, and snippets.

@CamKem
Created July 9, 2024 06:19
Show Gist options
  • Save CamKem/da8d3913263c70fdce672f9ed8e93d73 to your computer and use it in GitHub Desktop.
Save CamKem/da8d3913263c70fdce672f9ed8e93d73 to your computer and use it in GitHub Desktop.
Subject: [PATCH] feat(hashtags): add hashtag controller & parseable provider
---
Index: app/Services/ParsableContent.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/app/Services/ParsableContent.php b/app/Services/ParsableContent.php
--- a/app/Services/ParsableContent.php (revision bbb50cf7f4d0053a4c1ae2d1daf3cef7fbd71497)
+++ b/app/Services/ParsableContent.php (date 1720505326881)
@@ -7,6 +7,7 @@
use App\Contracts\Services\ParsableContentProvider;
use App\Services\ParsableContentProviders\BrProviderParsable;
use App\Services\ParsableContentProviders\CodeProviderParsable;
+use App\Services\ParsableContentProviders\HashtagProviderParsable;
use App\Services\ParsableContentProviders\LinkProviderParsable;
use App\Services\ParsableContentProviders\MentionProviderParsable;
use App\Services\ParsableContentProviders\StripProviderParsable;
@@ -24,6 +25,7 @@
BrProviderParsable::class,
LinkProviderParsable::class,
MentionProviderParsable::class,
+ HashtagProviderParsable::class,
])
{
//
Index: app/Livewire/Home/Feed.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/app/Livewire/Home/Feed.php b/app/Livewire/Home/Feed.php
--- a/app/Livewire/Home/Feed.php (revision bbb50cf7f4d0053a4c1ae2d1daf3cef7fbd71497)
+++ b/app/Livewire/Home/Feed.php (date 1720505479854)
@@ -16,6 +16,11 @@
{
use HasLoadMore;
+ /**
+ * Hashtag to filter the feed.
+ */
+ public ?string $hashtag = null;
+
/**
* Ignore the given question.
*/
@@ -39,7 +44,13 @@
*/
public function render(): View
{
- $questions = (new RecentQuestionsFeed())->builder()->simplePaginate($this->perPage);
+ $questions = (new RecentQuestionsFeed())
+ ->builder()
+ ->when($this->hashtag, fn ($query) => $query
+ ->where('content', 'like', "%{$this->hashtag}%")
+ ->orWhere('answer', 'like', "%{$this->hashtag}%")
+ )
+ ->simplePaginate($this->perPage);
IncrementViews::dispatchUsingSession($questions->getCollection());
Index: routes/web.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/routes/web.php b/routes/web.php
--- a/routes/web.php (revision bbb50cf7f4d0053a4c1ae2d1daf3cef7fbd71497)
+++ b/routes/web.php (date 1720505019434)
@@ -3,6 +3,7 @@
declare(strict_types=1);
use App\Http\Controllers\ChangelogController;
+use App\Http\Controllers\HashtagController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\QrCodeController;
use App\Http\Controllers\QuestionController;
@@ -21,6 +22,8 @@
Route::view('/trending', 'home/trending-questions')->name('home.trending');
Route::view('/users', 'home/users')->name('home.users');
+Route::get('/tag/{hashtag}', HashtagController::class)->name('home.hashtag');
+
Route::view('/terms', 'terms')->name('terms');
Route::view('/privacy', 'privacy')->name('privacy');
Route::view('/support', 'support')->name('support');
Index: app/Services/ParsableContentProviders/HashtagProviderParsable.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/app/Services/ParsableContentProviders/HashtagProviderParsable.php b/app/Services/ParsableContentProviders/HashtagProviderParsable.php
new file mode 100644
--- /dev/null (date 1720505326885)
+++ b/app/Services/ParsableContentProviders/HashtagProviderParsable.php (date 1720505326885)
@@ -0,0 +1,21 @@
+<?php
+
+namespace App\Services\ParsableContentProviders;
+
+use App\Contracts\Services\ParsableContentProvider;
+
+class HashtagProviderParsable implements ParsableContentProvider
+{
+
+ /**
+ * {@inheritDoc}
+ */
+ public function parse(string $content): string
+ {
+ return preg_replace(
+ '/#(\w+)/',
+ '<a href="/tag/$1" class="text-blue-500 hover:underline hover:text-blue-700 cursor-pointer" wire-navigate>#$1</a>',
+ $content
+ );
+ }
+}
Index: app/Http/Controllers/HashtagController.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/app/Http/Controllers/HashtagController.php b/app/Http/Controllers/HashtagController.php
new file mode 100644
--- /dev/null (date 1720505326873)
+++ b/app/Http/Controllers/HashtagController.php (date 1720505326873)
@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Http\Controllers;
+
+class HashtagController
+{
+
+ public function __invoke(string $hashtag)
+ {
+ return view('hashtag.show', [
+ 'hashtag' => $hashtag,
+ ]);
+ }
+
+}
Index: resources/views/hashtag/show.blade.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/resources/views/hashtag/show.blade.php b/resources/views/hashtag/show.blade.php
new file mode 100644
--- /dev/null (date 1720505510031)
+++ b/resources/views/hashtag/show.blade.php (date 1720505510031)
@@ -0,0 +1,16 @@
+<x-app-layout>
+ <x-slot name="title">Posts with #{{ $hashtag }}</x-slot>
+
+ <div class="flex flex-col items-center justify-center">
+ <div class="w-full max-w-md overflow-hidden rounded-lg px-2 shadow-md sm:px-0">
+ <x-home-menu></x-home-menu>
+
+ @auth
+ <livewire:questions.create :toId="auth()->id()" />
+ @endauth
+
+ <livewire:home.feed :hashtag="$hashtag" />
+ </div>
+ </div>
+</x-app-layout>
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment