diff --git a/app/jobs/scheduled/poll_mailbox.rb b/app/jobs/scheduled/poll_mailbox.rb index d0f17b2bcf3..85490681d81 100644 --- a/app/jobs/scheduled/poll_mailbox.rb +++ b/app/jobs/scheduled/poll_mailbox.rb @@ -45,6 +45,8 @@ module Jobs message_template = :email_reject_reply_key when Email::Receiver::BadDestinationAddress message_template = :email_reject_destination + when Email::Receiver::TopicNotFoundError + message_template = :email_reject_topic_not_found when Email::Receiver::TopicClosedError message_template = :email_reject_topic_closed when ActiveRecord::Rollback diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 7e1e67d841c..74e83380727 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1523,6 +1523,13 @@ en: None of the destination addresses are recognized. Please make sure that the site address is in the To: line (not Cc: or Bcc:), and that you are sending to the correct email address provided by staff. + email_reject_topic_not_found: + subject_template: "Email issue -- Topic Not Found" + text_body_template: | + We're sorry, but your email message to %{destination} (titled %{former_title}) didn't work. + + The topic is not found, it may have been deleted. If you believe this is in error, contact a staff member. + email_reject_topic_closed: subject_template: "Email issue -- Topic Closed" text_body_template: | diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 10268310655..f1a79906ed8 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -15,6 +15,7 @@ module Email class UserNotFoundError < ProcessingError; end class UserNotSufficientTrustLevelError < ProcessingError; end class BadDestinationAddress < ProcessingError; end + class TopicNotFoundError < ProcessingError; end class TopicClosedError < ProcessingError; end class EmailLogNotFound < ProcessingError; end class InvalidPost < ProcessingError; end @@ -69,6 +70,7 @@ module Email @email_log = dest_info[:obj] raise EmailLogNotFound if @email_log.blank? + raise TopicNotFoundError if Topic.find_by_id(@email_log.topic_id).nil? raise TopicClosedError if Topic.find_by_id(@email_log.topic_id).closed? create_reply diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index 93fb469e92b..55a7df20276 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -253,6 +253,37 @@ Pleasure to have you here! end end + describe "posting reply to a deleted topic" do + let(:reply_key) { raise "Override this in a lower describe block" } + let(:email_raw) { raise "Override this in a lower describe block" } + let(:receiver) { Email::Receiver.new(email_raw) } + let(:deleted_topic) { Fabricate(:deleted_topic) } + let(:post) { Fabricate(:post, topic: deleted_topic, post_number: 1) } + let(:replying_user_email) { 'jake@adventuretime.ooo' } + let(:replying_user) { Fabricate(:user, email: replying_user_email, trust_level: 2) } + let(:email_log) { EmailLog.new(reply_key: reply_key, + post: post, + post_id: post.id, + topic_id: deleted_topic.id, + email_type: 'user_posted', + user: replying_user, + user_id: replying_user.id, + to_address: replying_user_email + ) } + + before do + email_log.save + end + + describe "should not create post" do + let!(:reply_key) { '59d8df8370b7e95c5a49fbf86aeb2c93' } + let!(:email_raw) { fixture_file("emails/valid_reply.eml") } + it "raises a TopicNotFoundError" do + expect { receiver.process }.to raise_error(Email::Receiver::TopicNotFoundError) + end + end + end + describe "posting a new topic" do let(:category_destination) { raise "Override this in a lower describe block" } let(:email_raw) { raise "Override this in a lower describe block" } diff --git a/spec/jobs/poll_mailbox_spec.rb b/spec/jobs/poll_mailbox_spec.rb index 50a017f5eeb..3b7d883aeb8 100644 --- a/spec/jobs/poll_mailbox_spec.rb +++ b/spec/jobs/poll_mailbox_spec.rb @@ -232,6 +232,31 @@ describe Jobs::PollMailbox do end end + describe "when topic is deleted" do + let(:email) { MockPop3EmailObject.new fixture_file('emails/valid_reply.eml')} + let(:deleted_topic) { Fabricate(:deleted_topic) } + let(:first_post) { Fabricate(:post, topic: deleted_topic, post_number: 1)} + + before do + first_post.save + EmailLog.create(to_address: 'jake@email.example.com', + email_type: 'user_posted', + reply_key: '59d8df8370b7e95c5a49fbf86aeb2c93', + user: user, + post: first_post, + topic: deleted_topic) + end + + describe "should not create post" do + it "raises a TopicNotFoundError" do + expect_exception Email::Receiver::TopicNotFoundError + + poller.handle_mail(email) + email.should be_deleted + end + end + end + describe "in failure conditions" do it "a valid reply without an email log raises an EmailLogNotFound error" do