Skip to content

Commit

Permalink
Merge pull request #658 from pinkary-project/feat/optimize-feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Sep 22, 2024
2 parents 296f576 + a02b745 commit 9d8484f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
3 changes: 1 addition & 2 deletions app/Queries/Feeds/QuestionsFollowingFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public function builder(): Builder
'root as showRoot' => $followQueryClosure,
'parent as showParent' => $followQueryClosure,
])
->withAggregate('to as username', 'username')
->withAggregate('parent as grand_parent_id', 'parent_id')
->with('root.to:username,id', 'root:id,to_id', 'parent:id,parent_id')
->whereNotNull('answer')
->where('is_reported', false)
->where('is_ignored', false)
Expand Down
3 changes: 1 addition & 2 deletions app/Queries/Feeds/RecentQuestionsFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function builder(): Builder
})->orderByDesc('updated_at');
}, function (Builder $query): void {
$query->addSelect('root_id', 'parent_id')
->withAggregate('to as username', 'username')
->withAggregate('parent as grand_parent_id', 'parent_id')
->with('root.to:username,id', 'root:id,to_id', 'parent:id,parent_id')
->where(function (Builder $query): void {
$query->whereNull('root_id')
->orHas('root');
Expand Down
22 changes: 13 additions & 9 deletions app/Queries/Feeds/TrendingQuestionsFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ public function builder(): Builder
$maxDaysSincePosted = self::MAX_DAYS_SINCE_POSTED;

return Question::query()
->withCount('likes', 'children')
->orderByRaw(<<<SQL
(((likes_count * {$likesBias} + 1.0) * (children_count * {$commentsBias} + 1.0))
/ (strftime('%s') - strftime('%s', answer_created_at) + {$timeBias} + 1.0)) desc
SQL,
)
->where('is_reported', false)
->where('is_ignored', false)
->where('answer_created_at', '>=', now()->subDays($maxDaysSincePosted));
->select('id')
->from(
Question::query()
->withCount('likes', 'children')
->orderByRaw(<<<SQL
(((likes_count * {$likesBias} + 1.0) * (children_count * {$commentsBias} + 1.0))
/ (strftime('%s') - strftime('%s', answer_created_at) + {$timeBias} + 1.0)) desc
SQL)
->where('is_reported', false)
->where('is_ignored', false)
->where('answer_created_at', '>=', now()->subDays($maxDaysSincePosted)),
'trending_questions'
);
}
}
4 changes: 2 additions & 2 deletions resources/views/components/thread.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'username' => null,
])

<div wire:key="thread-{{ $questionId.'-'.$rootId.'-'.$parentId }}">
<div wire:key="thread-inner-{{ $questionId.'-'.$rootId.'-'.$parentId }}">
@if ($rootId !== null)
<livewire:questions.show
:questionId="$rootId"
Expand All @@ -20,7 +20,7 @@
<x-post-divider wire:key="divider-{{ $parentId }}" />
@else
<x-post-divider
:link="route('questions.show', ['username' => $username, 'question' => $questionId])"
:link="route('questions.show', ['username' => $username, 'question' => $rootId])"
:text="'View more comments...'"
wire:key="divider-{{ $parentId }}"
/>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/livewire/feed.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
@else
<x-thread
:rootId="$question->root_id"
:grandParentId="$question->grand_parent_id"
:grandParentId="$question->parent?->parent_id"
:parentId="$question->parent_id"
:questionId="$question->id"
:username="$question->username"
:username="$question->root?->to->username"
/>
@endif
</div>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/livewire/home/questions-following.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
@foreach ($followingQuestions as $question)
<x-thread
:rootId="$question->showRoot ? $question->root_id : null"
:grandParentId="$question->grand_parent_id"
:grandParentId="$question->parent?->parent_id"
:parentId="$question->showParent ? $question->parent_id : null"
:questionId="$question->id"
:username="$question->username"
:username="$question->root?->to->username"
/>
@endforeach

Expand Down

0 comments on commit 9d8484f

Please sign in to comment.