From b3094e9954e168f5bcd7634ef85ecae5897f01c9 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Fri, 24 Nov 2017 11:13:19 +0100 Subject: [PATCH] FIX: incoming and outgoing emails got lost when post was moved --- app/models/post_mover.rb | 23 +++++++++++++++ spec/models/post_mover_spec.rb | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb index 2682045dad3..42e8ad42b5f 100644 --- a/app/models/post_mover.rb +++ b/app/models/post_mover.rb @@ -92,10 +92,16 @@ class PostMover raw: post.raw, topic_id: destination_topic.id, acting_user: user, + cook_method: post.cook_method, + via_email: post.via_email, + raw_email: post.raw_email, skip_validations: true, guardian: Guardian.new(user) ) + move_incoming_emails(post, new_post) + move_email_logs(post, new_post) + PostAction.copy(post, new_post) new_post.update_column(:reply_count, @reply_count[1] || 0) new_post.custom_fields = post.custom_fields @@ -123,12 +129,29 @@ class PostMover post.update(update) + move_incoming_emails(post, post) + move_email_logs(post, post) + DiscourseEvent.trigger(:post_moved, post, original_topic.id) # Move any links from the post to the new topic post.topic_links.update_all(topic_id: destination_topic.id) end + def move_incoming_emails(old_post, new_post) + return if old_post.incoming_email.nil? + + email = old_post.incoming_email + email.update_columns(topic_id: new_post.topic_id, post_id: new_post.id) + new_post.incoming_email = email + end + + def move_email_logs(old_post, new_post) + EmailLog + .where(post_id: old_post.id) + .update_all(topic_id: new_post.topic_id, post_id: new_post.id) + end + def update_statistics destination_topic.update_statistics original_topic.update_statistics diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb index 3e44fa33489..3ef6bd21542 100644 --- a/spec/models/post_mover_spec.rb +++ b/spec/models/post_mover_spec.rb @@ -334,6 +334,49 @@ describe PostMover do end end + shared_examples "moves email related stuff" do + it "moves incoming email" do + Fabricate(:incoming_email, user: old_post.user, topic: old_post.topic, post: old_post) + + new_topic = topic.move_posts(user, [old_post.id], title: "new testing topic name") + new_post = new_topic.first_post + email = new_post.incoming_email + + expect(email).to be_present + expect(email.topic_id).to eq(new_topic.id) + expect(email.post_id).to eq(new_post.id) + + expect(old_post.reload.incoming_email).to_not be_present unless old_post.id == new_post.id + end + + it "moves email log entries" do + old_topic = old_post.topic + Fabricate(:email_log, user: old_post.user, topic: old_topic, post: old_post, email_type: :mailing_list) + Fabricate(:email_log, user: old_post.user, topic: old_topic, post: old_post, email_type: :mailing_list) + Fabricate(:email_log, user: old_post.user, post: old_post, email_type: :mailing_list) + + expect(EmailLog.where(topic_id: old_topic.id, post_id: old_post.id).count).to eq(2) + expect(EmailLog.where(topic_id: nil, post_id: old_post.id).count).to eq(1) + + new_topic = old_topic.move_posts(user, [old_post.id], title: "new testing topic name") + new_post = new_topic.first_post + + expect(EmailLog.where(topic_id: old_topic.id, post_id: old_post.id).count).to eq(0) + expect(EmailLog.where(topic_id: new_topic.id, post_id: new_post.id).count).to eq(3) + end + + it "preserves post attributes" do + old_post.update_columns(cook_method: Post.cook_methods[:email], via_email: true, raw_email: "raw email content") + + new_topic = old_post.topic.move_posts(user, [old_post.id], title: "new testing topic name") + new_post = new_topic.first_post + + expect(new_post.cook_method).to eq(Post.cook_methods[:email]) + expect(new_post.via_email).to eq(true) + expect(new_post.raw_email).to eq("raw email content") + end + end + context "moving the first post" do it "copies the OP, doesn't delete it" do @@ -390,6 +433,15 @@ describe PostMover do expect(new_topic.first_post.custom_fields).to eq(custom_fields) end + include_examples "moves email related stuff" do + let!(:old_post) { p1 } + end + end + + context "moving replies" do + include_examples "moves email related stuff" do + let!(:old_post) { p3 } + end end context "to an existing topic with a deleted post" do