Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Hashtag trending #619

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/EventActions/UpdateQuestionHashtags.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\Hashtag;
use App\Models\Question;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

Expand All @@ -26,9 +27,12 @@ public function __construct(
public function handle(): array
{
$parsedHashtags = $this->parsedHashtagNames();

$existingHashtags = Hashtag::query()->whereIn('name', $parsedHashtags->all())->get();

// Update udated at that particular hashtag used so it should be updated time
Hashtag::query()->whereIn('name', $parsedHashtags->all())->update(['updated_at' => Carbon::now()]);

$newHashtags = $parsedHashtags->diff($existingHashtags->pluck('name'))
->map(fn (string $name): Hashtag => Hashtag::query()->create(['name' => $name]));

Expand Down
21 changes: 21 additions & 0 deletions app/Livewire/Home/TrendingHashtags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Livewire\Home;

use Livewire\Component;
use App\Queries\Feeds\TrendingHashtags as TrendingHashtagsQuery;
use App\Models\Hashtag;
use Illuminate\Database\Eloquent\Builder;

class TrendingHashtags extends Component
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
{
public function render()
{
$hashtags = Hashtag::query()
->withCount(['questions' => function (Builder $query) {
$query->where('created_at', '>=', now()->subDays(1));
}])
->where('updated_at', '>=', now()->subDays(1))->limit(5)->orderBy('questions_count', 'DESC')->get();
return view('livewire.home.trending-hashtags', ['hashtags' => $hashtags]);
}
}
31 changes: 31 additions & 0 deletions app/Queries/Feeds/TrendingHashtags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Queries\Feeds;

use App\Models\Hashtag;
use Illuminate\Database\Eloquent\Builder;

final readonly class TrendingHashtags
{

/**
* The max days since posted for the trending feed.
*/
private const int MAX_DAYS_SINCE_POSTED = 1;

/**
* Get the query builder for the feed.
*
* @return Builder<Question>
*/
public function builder(): Builder
{
$maxDaysSincePosted = self::MAX_DAYS_SINCE_POSTED;

return Hashtag::query()
->withCount('questions')
->where('created_at', '>=', now()->subDays($maxDaysSincePosted));
amitm13 marked this conversation as resolved.
Show resolved Hide resolved
}
}
1 change: 1 addition & 0 deletions resources/views/home/trending-questions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<div class="w-full max-w-md overflow-hidden rounded-lg px-2 shadow-md sm:px-0">
<x-home-menu></x-home-menu>

<livewire:home.trending-hashtags />
<livewire:home.trending-questions :focus-input="true" />
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/layouts/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<nav>
<div class="{{ $navClasses }}">
<div class="flex h-16 justify-between">
<div class="flex h-16 md:justify-end bg-slate-900/50 backdrop-blur-sm w-full justify-center md:bg-transparent md:backdrop-blur-none">
amitm13 marked this conversation as resolved.
Show resolved Hide resolved
<div
class="flex items-center space-x-2.5"
x-data
Expand Down
5 changes: 5 additions & 0 deletions resources/views/livewire/home/trending-hashtags.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
@foreach ($hashtags as $hashtag)
<a class="bg-slate-600/20 p-4 shadow-2xl backdrop-blur font-medium me-2 py-1 px-2 rounded" href="{{ route('hashtag.show', $hashtag->name) }}">#{{ $hashtag->name }}</a>
amitm13 marked this conversation as resolved.
Show resolved Hide resolved
@endforeach
</div>
18 changes: 18 additions & 0 deletions tests/Unit/Livewire/Home/TrendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use App\Livewire\Home\TrendingQuestions;
use App\Livewire\Home\TrendingHashtags;
use App\Models\Like;
use App\Models\Question;
use App\Models\User;
Expand Down Expand Up @@ -148,3 +149,20 @@
]);
$component->assertDontSee('trending question 8');
});

test('render recent most used hashtag', function () {
$user = User::factory()->create();

$questionContent = 'Is this a #trending question?';

Question::factory()->create([
'content' => $questionContent,
'answer' => 'No',
'from_id' => $user->id,
]);

$component = Livewire::test(TrendingHashtags::class);

$component
->assertSee('trending');
});