0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-08 17:53:55 +08:00
discourse/plugins/discourse-topic-voting/lib/discourse_topic_voting/topic_extension.rb
Régis Hanol 73eb2a0b66
FIX: Show voters for closed topics in topic-voting (#40314)
Previously, opening the "who voted" popup on a closed topic returned an
empty list even though the vote count still showed the correct total.
The same was true for any topic whose votes had been archived (e.g.
moved out of a voting category, or trashed).

This was a regression from #39394, which added `votes.active` to
`Topic#who_voted` alongside the same filter applied to "my votes" and
`/topics/voted-by/:username`. That filter is correct for those per-user
listings — you don't want closed-topic votes polluting "your votes" —
but the `who_voted` popup is about historical participation on a
specific topic, not per-user vote accounting.

This change drops `.active` from `Topic#who_voted` so archived votes are
included, restoring the pre-regression behaviour. The accounting filters
in `Votes::Remove`, `User#topics_with_active_vote`, the voted-topics
query, and the `current_user_voted` flag are unchanged.

https://meta.discourse.org/t/403286

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 19:18:25 +02:00

55 lines
1.5 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscourseTopicVoting
module TopicExtension
extend ActiveSupport::Concern
prepended do
has_one :topic_vote_count,
class_name: "DiscourseTopicVoting::TopicVoteCount",
dependent: :destroy
has_many :votes, class_name: "DiscourseTopicVoting::Vote", dependent: :destroy
attribute :current_user_voted
end
def can_vote?
@can_vote ||=
SiteSetting.topic_voting_enabled && regular? && Category.can_vote?(category_id) &&
category && category.topic_id != id
end
def vote_count
topic_vote_count&.votes_count.to_i
end
def user_voted?(user)
if current_user_voted
current_user_voted == 1
else
votes.map(&:user_id).include?(user.id)
end
end
def update_vote_count
count = votes.count
DB.exec(<<~SQL, topic_id: id, votes_count: count)
INSERT INTO topic_voting_topic_vote_count
(topic_id, votes_count, created_at, updated_at)
VALUES
(:topic_id, :votes_count, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON CONFLICT (topic_id) DO UPDATE SET
votes_count = :votes_count,
updated_at = CURRENT_TIMESTAMP
WHERE topic_voting_topic_vote_count.topic_id = :topic_id
SQL
end
def who_voted(limit: DiscourseTopicVoting::VOTER_PREVIEW_LIMIT)
return if !SiteSetting.topic_voting_show_who_voted
votes.includes(:user).order(created_at: :desc).limit(limit).filter_map(&:user)
end
end
end