0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/plugins/discourse-topic-voting/lib/discourse_topic_voting/topic_query_extension.rb
Martin Brennan d798c84585
DEV: Fix const redefine warning in topic voting (#42290)
Fixes this console error:

```
 [rails] /Users/mb/repos/discourse/plugins/discourse-topic-voting/lib/discourse_topic_voting/topic_query_extension.rb:4: warning: already initialized constant DiscourseTopicVoting::TRENDING_SCORE_SQL
  [rails] /Users/mb/repos/discourse/plugins/discourse-topic-voting/lib/discourse_topic_voting/topic_query_extension.rb:4: warning: previous definition of TRENDING_SCORE_SQL was here
```
2026-08-04 14:13:13 +10:00

45 lines
1.5 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscourseTopicVoting
module TopicQueryExtension
TRENDING_SCORE_SQL = <<~SQL.squish
COALESCE((
SELECT SUM(1.0 / (EXTRACT(EPOCH FROM (NOW() - tv.created_at)) / 3600.0 + 2.0))
FROM topic_voting_votes tv
WHERE tv.topic_id = topics.id
), 0)
SQL
def list_voted_by(user)
create_list(:user_topics) do |topics|
topics.joins(
"INNER JOIN topic_voting_votes ON topic_voting_votes.topic_id = topics.id",
).where("topic_voting_votes.user_id = ? AND topic_voting_votes.archive = FALSE", user.id)
end
end
def list_votes
create_list(:votes, unordered: true) do |topics|
topics.joins(
"LEFT JOIN topic_voting_topic_vote_count dvtvc ON dvtvc.topic_id = topics.id",
).order("COALESCE(dvtvc.votes_count,'0')::integer DESC, topics.bumped_at DESC")
end
end
def list_hot
category_id = get_category_id(@options[:category]) || @options[:category_id]
if category_id && Category.can_vote?(category_id)
create_list(:hot, unordered: true, prioritize_pinned: true) do |topics|
topics = remove_muted(topics, user, options)
topics.joins(
"LEFT JOIN topic_voting_topic_vote_count dvtvc ON dvtvc.topic_id = topics.id",
).order(
"#{TRENDING_SCORE_SQL} DESC, COALESCE(dvtvc.votes_count, 0) DESC, topics.bumped_at DESC",
)
end
else
super
end
end
end
end