Skip to content

A platform for community discussion. Free, open, simple.

FIX: preserve poll data when moving posts to another topic (#37791)

Alan Guo Xiang Tan9b8c97c9faae
diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb
index 8c11179f021..197b576aa09 100644
--- a/plugins/poll/plugin.rb
+++ b/plugins/poll/plugin.rb
@@ -136,6 +136,26 @@ after_initialize do
post.publish_message!("/polls/#{post.topic_id}", post_id: post.id, polls: polls)
end
+ on(:post_moved) do |new_post, _original_topic_id, old_post|
+ next if old_post.blank? || new_post.id == old_post.id
+
+ ActiveRecord::Base.transaction do
+ # Remove empty polls auto-created by PostCreator on the new post
+ # so the originals (with votes) can be moved without a unique constraint violation.
+ new_polls = Poll.where(post_id: new_post.id)
+ if new_polls.exists?
+ PollVote.where(poll_id: new_polls.select(:id)).delete_all
+ PollOption.where(poll_id: new_polls.select(:id)).delete_all
+ new_polls.delete_all
+ end
+
+ Poll.where(post_id: old_post.id).update_all(post_id: new_post.id)
+ end
+
+ DiscoursePoll::PollsUpdater.update_post_custom_fields(new_post)
+ DiscoursePoll::PollsUpdater.update_post_custom_fields(old_post)
+ end
+
on(:merging_users) do |source_user, target_user|
DB.exec(<<-SQL, source_user_id: source_user.id, target_user_id: target_user.id)
DELETE FROM poll_votes
diff --git a/plugins/poll/spec/integration/post_mover_spec.rb b/plugins/poll/spec/integration/post_mover_spec.rb
new file mode 100644
index 00000000000..da9a32ec01a
--- /dev/null
+++ b/plugins/poll/spec/integration/post_mover_spec.rb
@@ -0,0 +1,97 @@
+# frozen_string_literal: true
+
+RSpec.describe "Poll post_moved handler" do
+ fab!(:admin)
+ fab!(:user1, :user)
+ fab!(:user2, :user)
+
+ fab!(:first_post) do
+ Fabricate(
+ :post,
+ user: admin,
+ raw: "[poll name=first_post_poll type=regular]\n- Option A\n- Option B\n[/poll]",
+ )
+ end
+
+ fab!(:source_topic) { first_post.topic }
+
+ fab!(:reply) do
+ Fabricate(
+ :post,
+ topic: source_topic,
+ user: admin,
+ raw: "[poll name=reply_poll type=regular]\n- Option A\n- Option B\n[/poll]",
+ )
+ end
+
+ fab!(:first_post_poll) { first_post.polls.find_by(name: "first_post_poll") }
+ fab!(:reply_poll) { reply.polls.find_by(name: "reply_poll") }
+
+ fab!(:user1_first_post_poll_votes) do
+ first_post_poll.poll_options.map do |option|
+ Fabricate(:poll_vote, poll: first_post_poll, poll_option: option, user: user1)
+ end
+ end
+
+ fab!(:user2_reply_poll_votes) do
+ reply_poll.poll_options.map do |option|
+ Fabricate(:poll_vote, poll: reply_poll, poll_option: option, user: user2)
+ end
+ end
+
+ fab!(:destination_topic) { Fabricate(:post, user: admin).topic }
+
+ before { Jobs.run_immediately! }
+
+ it "moves polls, options, and votes when the first post is moved" do
+ original_options = first_post_poll.poll_options.to_a
+
+ source_topic.move_posts(admin, [first_post.id], destination_topic_id: destination_topic.id)
+
+ new_post = destination_topic.posts.last
+ moved_poll = new_post.polls.find_by(name: "first_post_poll")
+
+ expect(moved_poll).to eq(first_post_poll)
+ expect(moved_poll.poll_options).to contain_exactly(*original_options)
+ expect(moved_poll.poll_votes.map { |vote| [vote.user, vote.poll_option] }).to contain_exactly(
+ *original_options.map { |option| [user1, option] },
+ )
+
+ expect(new_post.custom_fields[DiscoursePoll::HAS_POLLS]).to eq(true)
+ expect(Poll.exists?(post_id: first_post.id)).to eq(false)
+ end
+
+ it "moves polls when a copy is kept in the original topic" do
+ original_options = reply_poll.poll_options.to_a
+
+ PostMover.new(source_topic, admin, [reply.id], options: { freeze_original: true }).to_topic(
+ destination_topic.id,
+ )
+
+ moved_reply = destination_topic.posts.last
+ moved_poll = moved_reply.polls.find_by(name: "reply_poll")
+
+ expect(moved_poll).to eq(reply_poll)
+ expect(moved_poll.poll_options).to contain_exactly(*original_options)
+ expect(moved_poll.poll_votes.map { |vote| [vote.user, vote.poll_option] }).to contain_exactly(
+ *original_options.map { |option| [user2, option] },
+ )
+
+ expect(moved_reply.custom_fields[DiscoursePoll::HAS_POLLS]).to eq(true)
+ expect(Poll.exists?(post_id: reply.id)).to eq(false)
+ expect(reply.reload.custom_fields[DiscoursePoll::HAS_POLLS]).to eq(nil)
+ end
+
+ it "preserves polls when a reply is moved without creating a new post" do
+ source_topic.move_posts(admin, [reply.id], destination_topic_id: destination_topic.id)
+
+ expect(reply.reload.topic_id).to eq(destination_topic.id)
+
+ poll = reply.polls.find_by(name: "reply_poll")
+ expect(poll).to eq(reply_poll)
+ expect(poll.poll_options).to contain_exactly(*reply_poll.poll_options)
+ expect(poll.poll_votes.map { |vote| [vote.user, vote.poll_option] }).to contain_exactly(
+ *poll.poll_options.map { |option| [user2, option] },
+ )
+ end
+end