Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
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
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def user_voted
sort_dir = (options[:ascending] == "true") ? "ASC" : "DESC"
result = result
.joins("LEFT JOIN discourse_voting_topic_vote_count ON discourse_voting_topic_vote_count.topic_id = topics.id")
.reorder("COALESCE(discourse_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}")
.reorder("COALESCE(discourse_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}, topics.bumped_at DESC")
end

result
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/topic_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@
it "returns topics voted by user" do
expect(TopicQuery.new(user0, { state: 'my_votes' }).list_latest.topics.map(&:id)).to eq([topic1.id])
end

it "orders topic by bumped_at if votes are equal" do
topic2 = Fabricate(:topic, category: category1, bumped_at: 2.hours.ago)
DiscourseVoting::TopicVoteCount.create!(topic_id: topic2.id, votes_count: 2)
topic3 = Fabricate(:topic, category: category1, bumped_at: 3.hours.ago)
topic4 = Fabricate(:topic, category: category1, bumped_at: 1.hour.ago)

opts = { order: 'votes', per_page: 2, category: category1.slug }
expect(TopicQuery.new(user0, opts).list_latest.topics.map(&:id)).to eq([topic2.id, topic1.id])
expect(TopicQuery.new(user0, opts.merge(page: 1)).list_latest.topics.map(&:id)).to eq([topic0.id, topic4.id])
expect(TopicQuery.new(user0, opts.merge(page: 2)).list_latest.topics.map(&:id)).to eq([topic3.id])
end
end