WIP: Add trending vote sort to topic voting plugin#38839
Draft
WIP: Add trending vote sort to topic voting plugin#38839
Conversation
98c8b9f to
8bc459b
Compare
The existing "Votes" sort orders topics by all-time vote count, which causes old, resolved topics to permanently dominate the list. Communities using the voting plugin have been asking for a way to surface topics gaining traction *recently* since 2016. This adds a "Trending" sort that uses a time-decay formula where each vote contributes `1 / (age_in_hours + 2)` to the topic's trending score. Recent votes naturally weigh more — a vote cast 1 hour ago scores ~0.33 while a vote cast 30 days ago scores ~0.001. No windows, thresholds, or scheduled jobs needed. The sort is available via `?order=votes-trending` on topic lists and through the new "Trending" navigation tab in voting-enabled categories. It also works with the TopicsFilter query string (`order:votes-trending`). All-time vote count is used as a tiebreaker, then bumped_at. A composite index on `(topic_id, created_at)` is added to `topic_voting_votes` to keep the correlated subquery fast. The JS initializer is also refactored to reduce duplication across the three voting navigation bar items. Ref - t/180396
Discourse enforces a safety check that prevents creating indexes concurrently without first dropping any existing (potentially invalid) index. This follows the Postgres recommendation that if a concurrent index build fails, it leaves behind an invalid index that should be dropped before retrying. Replace the single `add_index` with `if_not_exists` by an explicit `remove_index` with `if_exists` followed by `add_index` with `algorithm: :concurrently`, which satisfies the safety check.
8bc459b to
0206c28
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The existing "Votes" sort orders topics by all-time vote count, which causes old, resolved topics to permanently dominate the list. Communities using the voting plugin have been asking for a way to surface topics gaining traction recently since 2016.
This adds a "Trending" sort that uses a time-decay formula where each vote contributes
1 / (age_in_hours + 2)to the topic's trending score. Recent votes naturally weigh more — a vote cast 1 hour ago scores ~0.33 while a vote cast 30 days ago scores ~0.001. No windows, thresholds, or scheduled jobs needed.The sort is available via
?order=votes-trendingon topic lists and through the new "Trending" navigation tab in voting-enabled categories. It also works with the TopicsFilter query string (order:votes-trending). All-time vote count is used as a tiebreaker, then bumped_at.A composite index on
(topic_id, created_at)is added totopic_voting_votesto keep the correlated subquery fast.The JS initializer is also refactored to reduce duplication across the three voting navigation bar items.
Ref - t/180396