From eeedc3901e5049108489668e74b9d1bc8cc5b71d Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Mon, 3 Sep 2018 23:06:25 +0200 Subject: [PATCH] FIX: Replying to deleted post via email should create new reply to topic --- lib/email/receiver.rb | 11 +++++++---- spec/components/email/receiver_spec.rb | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 898838faad1..3419dbda062 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -596,12 +596,14 @@ module Email raise ReplyUserNotMatchingError, "post_reply_key.user_id => #{post_reply_key.user_id.inspect}, user.id => #{user.id.inspect}" end + post = Post.with_deleted.find(post_reply_key.post_id) + create_reply(user: user, raw: body, elided: elided, hidden_reason_id: hidden_reason_id, - post: post_reply_key.post, - topic: post_reply_key.post.topic, + post: post, + topic: post&.topic, skip_validations: user.staged?) end end @@ -834,13 +836,14 @@ module Email def create_reply(options = {}) raise TopicNotFoundError if options[:topic].nil? || options[:topic].trashed? + options[:post] = nil if options[:post]&.trashed? if post_action_type = post_action_for(options[:raw]) create_post_action(options[:user], options[:post], post_action_type) else raise TopicClosedError if options[:topic].closed? - options[:topic_id] = options[:post].try(:topic_id) - options[:reply_to_post_number] = options[:post].try(:post_number) + options[:topic_id] = options[:topic].id + options[:reply_to_post_number] = options[:post]&.post_number options[:is_group_message] = options[:topic].private_message? && options[:topic].allowed_groups.exists? create_post_with_attachments(options) end diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index 6d92858fd15..f7721a0e906 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -351,6 +351,12 @@ describe Email::Receiver do expect { process(:staged_reply_restricted) }.to change { topic.posts.count } end + it "posts a reply to the topic when the post was deleted" do + post.update_columns(deleted_at: 1.day.ago) + expect { process(:reply_user_matching) }.to change { topic.posts.count } + expect(topic.ordered_posts.last.reply_to_post_number).to be_nil + end + describe 'Unsubscribing via email' do let(:last_email) { ActionMailer::Base.deliveries.last }