A platform for community discussion. Free, open, simple.
DEV: Add specs and general refactors for topic voting (#39397)
Martin Brennan5524444dda3e
diff --git a/plugins/discourse-topic-voting/app/jobs/regular/discourse_topic_voting/vote_reclaim.rb b/plugins/discourse-topic-voting/app/jobs/regular/discourse_topic_voting/vote_reclaim.rbindex c08a7836410..e9fd58ad03f 100644--- a/plugins/discourse-topic-voting/app/jobs/regular/discourse_topic_voting/vote_reclaim.rb+++ b/plugins/discourse-topic-voting/app/jobs/regular/discourse_topic_voting/vote_reclaim.rb@@ -2,13 +2,17 @@module Jobsmodule DiscourseTopicVoting+ # Restores archived votes and refreshes the topic vote count when voting+ # should apply again (e.g. topic reopened/unarchived/recovered, or moved+ # into a voting category).class VoteReclaim < ::Jobs::Basedef execute(args)- if topic = Topic.with_deleted.find_by(id: args[:topic_id])- ActiveRecord::Base.transaction do- ::DiscourseTopicVoting::Vote.where(topic_id: args[:topic_id]).update_all(archive: false)- topic.update_vote_count- end+ topic = Topic.with_deleted.find_by(id: args[:topic_id])+ return if !topic++ ActiveRecord::Base.transaction do+ ::DiscourseTopicVoting::Vote.where(topic_id: topic.id).update_all(archive: false)+ topic.update_vote_countendendenddiff --git a/plugins/discourse-topic-voting/app/jobs/regular/discourse_topic_voting/vote_release.rb b/plugins/discourse-topic-voting/app/jobs/regular/discourse_topic_voting/vote_release.rbindex 5fbcda5cc32..bbd58473b15 100644--- a/plugins/discourse-topic-voting/app/jobs/regular/discourse_topic_voting/vote_release.rb+++ b/plugins/discourse-topic-voting/app/jobs/regular/discourse_topic_voting/vote_release.rb@@ -2,26 +2,32 @@module Jobsmodule DiscourseTopicVoting+ # Archives votes and refreshes the topic vote count when voting no longer+ # applies (topic closed/archived/trashed, or moved out of a voting+ # category). Notifies voters unless trashed.class VoteRelease < ::Jobs::Basedef execute(args)- if topic = Topic.with_deleted.find_by(id: args[:topic_id])- votes = ::DiscourseTopicVoting::Vote.where(topic_id: args[:topic_id])- votes.update_all(archive: true)+ topic = Topic.with_deleted.find_by(id: args[:topic_id])+ return if topic.blank?- topic.update_vote_count+ votes = ::DiscourseTopicVoting::Vote.where(topic_id: topic.id)+ votes.update_all(archive: true)- return if args[:trashed]+ topic.update_vote_count- votes.find_each do |vote|- Notification.create!(- user_id: vote.user_id,- notification_type: Notification.types[:votes_released],- topic_id: vote.topic_id,- data: { message: "votes_released", title: "votes_released" }.to_json,- )- rescue StandardError- # If one notification crashes, inform others- end+ # The user can't reach a trashed topic from the notification,+ # so no need to send one.+ return if args[:trashed]++ votes.find_each do |vote|+ Notification.create!(+ user_id: vote.user_id,+ notification_type: Notification.types[:votes_released],+ topic_id: vote.topic_id,+ data: { message: "votes_released", title: "votes_released" }.to_json,+ )+ rescue StandardError+ # If one notification crashes, inform othersendendenddiff --git a/plugins/discourse-topic-voting/app/models/discourse_topic_voting/vote.rb b/plugins/discourse-topic-voting/app/models/discourse_topic_voting/vote.rbindex aa7da874f4d..b9d254e211d 100644--- a/plugins/discourse-topic-voting/app/models/discourse_topic_voting/vote.rb+++ b/plugins/discourse-topic-voting/app/models/discourse_topic_voting/vote.rb@@ -9,6 +9,8 @@ module DiscourseTopicVotingscope :active, -> { where(archive: false) }scope :archived, -> { where(archive: true) }++ alias_attribute :archived, :archiveendenddiff --git a/plugins/discourse-topic-voting/lib/discourse_topic_voting/topic_merger.rb b/plugins/discourse-topic-voting/lib/discourse_topic_voting/topic_merger.rbnew file mode 100644index 00000000000..8b718f3c044--- /dev/null+++ b/plugins/discourse-topic-voting/lib/discourse_topic_voting/topic_merger.rb@@ -0,0 +1,58 @@+# frozen_string_literal: true++module DiscourseTopicVoting+ class TopicMerger+ def self.merge(source_topic, target_topic)+ new(source_topic, target_topic).merge+ end++ def initialize(source_topic, target_topic)+ @source_topic = source_topic+ @target_topic = target_topic+ end++ def merge+ moved_votes = 0+ duplicated_votes = 0++ who_voted = @source_topic.votes.map(&:user)+ if who_voted.present? && @source_topic.closed+ who_voted.each do |user|+ next if user.blank?++ user_votes = user.topics_with_vote.pluck(:topic_id)+ user_archived_votes = user.topics_with_archived_vote.pluck(:topic_id)++ if user_votes.include?(@source_topic.id) || user_archived_votes.include?(@source_topic.id)+ if user_votes.include?(@target_topic.id) ||+ user_archived_votes.include?(@target_topic.id)+ duplicated_votes += 1+ user.votes.destroy_by(topic_id: @source_topic.id)+ else+ user+ .votes+ .find_by(topic_id: @source_topic.id, user_id: user.id)+ .update!(topic_id: @target_topic.id, archive: @target_topic.closed)+ moved_votes += 1+ end+ else+ next+ end+ end+ end++ if moved_votes > 0+ @source_topic.update_vote_count+ @target_topic.update_vote_count++ if moderator_post = @source_topic.ordered_posts.where(action_code: "split_topic").last+ moderator_post.raw << "\n\n#{I18n.t("topic_voting.votes_moved", count: moved_votes)}"+ if duplicated_votes > 0+ moderator_post.raw << " #{I18n.t("topic_voting.duplicated_votes", count: duplicated_votes)}"+ end+ moderator_post.save!+ end+ end+ end+ end+enddiff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rbindex 03604cc4adc..f46abbd9fb4 100755--- a/plugins/discourse-topic-voting/plugin.rb+++ b/plugins/discourse-topic-voting/plugin.rb@@ -212,48 +212,7 @@ after_initialize doendend- on(:topic_merged) do |orig, dest|- moved_votes = 0- duplicated_votes = 0-- who_voted = orig.votes.map(&:user)- if who_voted.present? && orig.closed- who_voted.each do |user|- next if user.blank?-- user_votes = user.topics_with_vote.pluck(:topic_id)- user_archived_votes = user.topics_with_archived_vote.pluck(:topic_id)-- if user_votes.include?(orig.id) || user_archived_votes.include?(orig.id)- if user_votes.include?(dest.id) || user_archived_votes.include?(dest.id)- duplicated_votes += 1- user.votes.destroy_by(topic_id: orig.id)- else- user- .votes- .find_by(topic_id: orig.id, user_id: user.id)- .update!(topic_id: dest.id, archive: dest.closed)- moved_votes += 1- end- else- next- end- end- end-- if moved_votes > 0- orig.update_vote_count- dest.update_vote_count-- if moderator_post = orig.ordered_posts.where(action_code: "split_topic").last- moderator_post.raw << "\n\n#{I18n.t("topic_voting.votes_moved", count: moved_votes)}"- if duplicated_votes > 0- moderator_post.raw << " #{I18n.t("topic_voting.duplicated_votes", count: duplicated_votes)}"- end- moderator_post.save!- end- end- end+ on(:topic_merged) { |orig, dest| DiscourseTopicVoting::TopicMerger.merge(orig, dest) }on(:merging_users) do |source_user, target_user|DiscourseTopicVoting::UserMerger.merge(source_user, target_user)diff --git a/plugins/discourse-topic-voting/spec/jobs/vote_reclaim_spec.rb b/plugins/discourse-topic-voting/spec/jobs/vote_reclaim_spec.rbnew file mode 100644index 00000000000..ed652326819--- /dev/null+++ b/plugins/discourse-topic-voting/spec/jobs/vote_reclaim_spec.rb@@ -0,0 +1,42 @@+# frozen_string_literal: true++RSpec.describe Jobs::DiscourseTopicVoting::VoteReclaim do+ context "when the topic does not exist" do+ fab!(:topic)++ it "does nothing" do+ topic.destroy!+ Topic.any_instance.expects(:update_vote_count).never+ Jobs::DiscourseTopicVoting::VoteReclaim.new.execute(topic_id: topic.id)+ end+ end++ context "when the topic exists" do+ fab!(:topic)++ it "updates the vote count" do+ Fabricate(:topic_voting_votes, topic: topic, archive: true)+ expect { Jobs::DiscourseTopicVoting::VoteReclaim.new.execute(topic_id: topic.id) }.to change {+ topic.reload.vote_count+ }.by(1)+ end++ it "reclaims the votes" do+ vote = Fabricate(:topic_voting_votes, topic: topic, archive: true)+ expect { Jobs::DiscourseTopicVoting::VoteReclaim.new.execute(topic_id: topic.id) }.to change {+ vote.reload.archived+ }.from(true).to(false)+ end++ context "when the topic is deleted" do+ before { topic.trash! }++ it "reclaims the votes" do+ vote = Fabricate(:topic_voting_votes, topic: topic, archive: true)+ expect {+ Jobs::DiscourseTopicVoting::VoteReclaim.new.execute(topic_id: topic.id)+ }.to change { vote.reload.archived }.from(true).to(false)+ end+ end+ end+enddiff --git a/plugins/discourse-topic-voting/spec/jobs/vote_release_spec.rb b/plugins/discourse-topic-voting/spec/jobs/vote_release_spec.rbnew file mode 100644index 00000000000..1a34f293bbb--- /dev/null+++ b/plugins/discourse-topic-voting/spec/jobs/vote_release_spec.rb@@ -0,0 +1,50 @@+# frozen_string_literal: true++RSpec.describe Jobs::DiscourseTopicVoting::VoteRelease do+ context "when the topic does not exist" do+ fab!(:topic)++ it "does nothing" do+ topic.destroy!+ Topic.any_instance.expects(:update_vote_count).never+ Jobs::DiscourseTopicVoting::VoteRelease.new.execute(topic_id: topic.id)+ end+ end++ context "when the topic exists" do+ fab!(:topic)++ it "releases the votes" do+ vote = Fabricate(:topic_voting_votes, topic: topic, archive: false)+ expect { Jobs::DiscourseTopicVoting::VoteRelease.new.execute(topic_id: topic.id) }.to change {+ vote.reload.archived+ }.from(false).to(true)+ end++ it "creates a notification for the user about the votes being released" do+ vote = Fabricate(:topic_voting_votes, topic: topic, archive: false)+ expect { Jobs::DiscourseTopicVoting::VoteRelease.new.execute(topic_id: topic.id) }.to change {+ Notification.where(+ user_id: vote.user_id,+ notification_type: Notification.types[:votes_released],+ ).count+ }.by(1)+ end++ context "when the topic is trashed" do+ before { topic.trash! }++ it "does not create a notification for the user" do+ vote = Fabricate(:topic_voting_votes, topic: topic, archive: false)+ expect {+ Jobs::DiscourseTopicVoting::VoteRelease.new.execute(topic_id: topic.id, trashed: true)+ }.not_to change {+ Notification.where(+ user_id: vote.user_id,+ notification_type: Notification.types[:votes_released],+ ).count+ }+ end+ end+ end+enddiff --git a/plugins/discourse-topic-voting/spec/lib/topic_merger_spec.rb b/plugins/discourse-topic-voting/spec/lib/topic_merger_spec.rbnew file mode 100644index 00000000000..0a03e881337--- /dev/null+++ b/plugins/discourse-topic-voting/spec/lib/topic_merger_spec.rb@@ -0,0 +1,125 @@+# frozen_string_literal: true++describe DiscourseTopicVoting::TopicMerger do+ fab!(:user_0, :user)+ fab!(:user_1, :user)+ fab!(:user_2, :user)+ fab!(:user_3, :user)+ fab!(:user_4, :user)+ fab!(:user_5, :user)++ fab!(:category_1, :category)+ fab!(:category_2, :category)++ fab!(:topic_0) { Fabricate(:topic, category: category_1) }+ fab!(:topic_1) { Fabricate(:topic, category: category_2) }++ before { SiteSetting.topic_voting_enabled = true }++ describe ".merge" do+ it "does nothing when the source topic is still open" do+ DiscourseTopicVoting::Vote.create!(user: user_0, topic: topic_0)+ topic_0.update_vote_count++ described_class.merge(topic_0, topic_1)++ user_0.reload+ expect(user_0.topics_with_vote.pluck(:topic_id)).to contain_exactly(topic_0.id)+ expect(user_0.topics_with_archived_vote.pluck(:topic_id)).to be_blank+ end++ it "archives moved votes when the destination topic is closed" do+ topic_0.update_status("closed", true, Discourse.system_user)+ topic_1.update_status("closed", true, Discourse.system_user)++ DiscourseTopicVoting::Vote.create!(user: user_0, topic: topic_0)+ topic_0.update_vote_count++ described_class.merge(topic_0, topic_1)++ user_0.reload+ expect(user_0.topics_with_vote.pluck(:topic_id)).to be_blank+ expect(user_0.topics_with_archived_vote.pluck(:topic_id)).to contain_exactly(topic_1.id)+ expect(topic_0.reload.vote_count).to eq(0)+ expect(topic_1.reload.vote_count).to eq(1)+ end+ end++ context "when merging topics via move_posts (topic_merged)" do+ let(:users) { [user_0, user_1, user_2, user_3, user_4, user_5] }++ before do+ SiteSetting.topic_voting_show_who_voted = false++ Fabricate(:post, topic: topic_0, user: user_0)+ Fabricate(:post, topic: topic_0, user: user_0)++ DiscourseTopicVoting::Vote.create!(user: users[0], topic: topic_0)+ DiscourseTopicVoting::Vote.create!(user: users[1], topic: topic_1)+ DiscourseTopicVoting::Vote.create!(user: users[2], topic: topic_0)+ DiscourseTopicVoting::Vote.create!(user: users[2], topic: topic_1)+ DiscourseTopicVoting::Vote.create!(user: users[4], topic: topic_0, archive: true)+ DiscourseTopicVoting::Vote.create!(user: users[5], topic: topic_0, archive: true)+ DiscourseTopicVoting::Vote.create!(user: users[5], topic: topic_1)++ [topic_0, topic_1].each { |t| t.update_vote_count }+ end++ it "moves votes when entire topic is merged" do+ topic_0.move_posts(+ Discourse.system_user,+ topic_0.posts.pluck(:id),+ destination_topic_id: topic_1.id,+ )++ users.each { |user| user.reload }+ expect(users[0].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic_1.id)+ expect(users[0].topics_with_archived_vote.pluck(:topic_id)).to be_blank++ expect(users[1].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic_1.id)+ expect(users[1].topics_with_archived_vote.pluck(:topic_id)).to be_blank++ expect(users[2].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic_1.id)+ expect(users[2].topics_with_archived_vote.pluck(:topic_id)).to be_blank++ expect(users[3].topics_with_vote.pluck(:topic_id)).to be_blank+ expect(users[3].topics_with_archived_vote.pluck(:topic_id)).to be_blank++ expect(users[4].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic_1.id)+ expect(users[4].topics_with_archived_vote.pluck(:topic_id)).to be_blank++ expect(users[5].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic_1.id)+ expect(users[5].topics_with_archived_vote.pluck(:topic_id)).to be_blank++ expect(topic_0.reload.vote_count).to eq(0)+ expect(topic_1.reload.vote_count).to eq(5)++ merged_post = topic_0.posts.find_by(action_code: "split_topic")+ expect(merged_post.raw).to include(I18n.t("topic_voting.votes_moved", count: 2))+ expect(merged_post.raw).to include(I18n.t("topic_voting.duplicated_votes", count: 2))+ end++ it "does not move votes when not all posts are moved and the original topic does not get closed" do+ topic_0.move_posts(+ Discourse.system_user,+ [topic_0.posts.order(:post_number).first.id],+ destination_topic_id: topic_1.id,+ )++ users.each { |user| user.reload }+ expect(users[0].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic_0.id)+ expect(users[0].topics_with_archived_vote.pluck(:topic_id)).to be_blank+ expect(users[1].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic_1.id)+ expect(users[1].topics_with_archived_vote.pluck(:topic_id)).to be_blank+ expect(users[2].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic_0.id, topic_1.id)+ expect(users[2].topics_with_archived_vote.pluck(:topic_id)).to be_blank+ expect(users[3].topics_with_vote.pluck(:topic_id)).to be_blank+ expect(users[3].topics_with_archived_vote.pluck(:topic_id)).to be_blank+ expect(users[4].topics_with_vote.pluck(:topic_id)).to be_blank+ expect(users[4].topics_with_archived_vote.pluck(:topic_id)).to contain_exactly(topic_0.id)++ expect(topic_0.reload.vote_count).to eq(4)+ expect(topic_1.reload.vote_count).to eq(3)+ end+ end+enddiff --git a/plugins/discourse-topic-voting/spec/voting_spec.rb b/plugins/discourse-topic-voting/spec/voting_spec.rbindex ec8d53e211d..7a64d178f19 100644--- a/plugins/discourse-topic-voting/spec/voting_spec.rb+++ b/plugins/discourse-topic-voting/spec/voting_spec.rb@@ -3,10 +3,6 @@describe DiscourseTopicVoting dolet!(:user0) { Fabricate(:user) }let!(:user1) { Fabricate(:user) }- let!(:user2) { Fabricate(:user) }- let!(:user3) { Fabricate(:user) }- let!(:user4) { Fabricate(:user) }- let!(:user5) { Fabricate(:user) }let(:category1) { Fabricate(:category) }let(:category2) { Fabricate(:category) }@@ -53,87 +49,6 @@ describe DiscourseTopicVoting doexpect(user0.reached_voting_limit?).to eq(true)end- context "with two topics" do- let(:users) { [user0, user1, user2, user3, user4, user5] }-- before do- # ensure merging votes works regardless of voters' visibility- SiteSetting.topic_voting_show_who_voted = false-- Fabricate(:post, topic: topic0, user: user0)- Fabricate(:post, topic: topic0, user: user0)-- # +user0+ votes +topic0+, +user1+ votes +topic1+ and +user2+ votes both- # topics.- DiscourseTopicVoting::Vote.create!(user: users[0], topic: topic0)- DiscourseTopicVoting::Vote.create!(user: users[1], topic: topic1)- DiscourseTopicVoting::Vote.create!(user: users[2], topic: topic0)- DiscourseTopicVoting::Vote.create!(user: users[2], topic: topic1)- DiscourseTopicVoting::Vote.create!(user: users[4], topic: topic0, archive: true)- DiscourseTopicVoting::Vote.create!(user: users[5], topic: topic0, archive: true)- DiscourseTopicVoting::Vote.create!(user: users[5], topic: topic1)-- [topic0, topic1].each { |t| t.update_vote_count }- end-- it "moves votes when entire topic is merged" do- topic0.move_posts(- Discourse.system_user,- topic0.posts.pluck(:id),- destination_topic_id: topic1.id,- )-- users.each { |user| user.reload }- expect(users[0].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)- expect(users[0].topics_with_archived_vote.pluck(:topic_id)).to be_blank-- expect(users[1].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)- expect(users[1].topics_with_archived_vote.pluck(:topic_id)).to be_blank-- expect(users[2].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)- expect(users[2].topics_with_archived_vote.pluck(:topic_id)).to be_blank-- expect(users[3].topics_with_vote.pluck(:topic_id)).to be_blank- expect(users[3].topics_with_archived_vote.pluck(:topic_id)).to be_blank-- expect(users[4].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)- expect(users[4].topics_with_archived_vote.pluck(:topic_id)).to be_blank-- expect(users[5].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)- expect(users[5].topics_with_archived_vote.pluck(:topic_id)).to be_blank-- expect(topic0.reload.vote_count).to eq(0)- expect(topic1.reload.vote_count).to eq(5)-- merged_post = topic0.posts.find_by(action_code: "split_topic")- expect(merged_post.raw).to include(I18n.t("topic_voting.votes_moved", count: 2))- expect(merged_post.raw).to include(I18n.t("topic_voting.duplicated_votes", count: 2))- end-- it "does not move votes when not all posts are moved and the original topic does not get closed" do- topic0.move_posts(- Discourse.system_user,- [topic0.posts.order(:post_number).first.id],- destination_topic_id: topic1.id,- )-- users.each { |user| user.reload }- expect(users[0].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic0.id)- expect(users[0].topics_with_archived_vote.pluck(:topic_id)).to be_blank- expect(users[1].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)- expect(users[1].topics_with_archived_vote.pluck(:topic_id)).to be_blank- expect(users[2].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic0.id, topic1.id)- expect(users[2].topics_with_archived_vote.pluck(:topic_id)).to be_blank- expect(users[3].topics_with_vote.pluck(:topic_id)).to be_blank- expect(users[3].topics_with_archived_vote.pluck(:topic_id)).to be_blank- expect(users[4].topics_with_vote.pluck(:topic_id)).to be_blank- expect(users[4].topics_with_archived_vote.pluck(:topic_id)).to contain_exactly(topic0.id)-- expect(topic0.reload.vote_count).to eq(4)- expect(topic1.reload.vote_count).to eq(3)- end- end-context "when a user has an empty string as the votes custom field" dobefore { user0.votes.delete_all }