FIX: Prevent resurrecting old topics via email reply for group inboxes with SMTP enabled (#13382)

We already reject email replies to public topics via `SiteSetting.disallow_reply_by_email_after_days` and raising the `OldDestinationError`. This PR introduces similar behaviour for group inboxes, but without the rejection, and **only when SMTP is enabled for the group**.

If a reply is sent via email and the post is older than `SiteSetting.disallow_reply_by_email_after_days` days ago, then we create a new topic instead of making a reply in the old one and link back to the original topic. This is done to prevent long running group inbox discussions.
This commit is contained in:
Martin Brennan
2021-06-21 11:45:00 +10:00
committed by GitHub
parent f0c10edd28
commit 22b96c9ce1
3 changed files with 86 additions and 31 deletions

View File

@ -986,8 +986,14 @@ describe Email::Receiver do
end
context "emailing a group by email_username and following reply flow" do
let!(:topic) do
group.update!(email_username: "team@somesmtpaddress.com")
let!(:original_inbound_email_topic) do
group.update!(
email_username: "team@somesmtpaddress.com",
smtp_server: "smtp.test.com",
smtp_port: 587,
smtp_ssl: true,
smtp_enabled: true
)
process(:email_to_group_email_username_1)
Topic.last
end
@ -999,6 +1005,7 @@ describe Email::Receiver do
before do
NotificationEmailer.enable
SiteSetting.disallow_reply_by_email_after_days = 10000
Jobs.run_immediately!
end
@ -1006,17 +1013,17 @@ describe Email::Receiver do
group_post = PostCreator.create(
user_in_group,
raw: "Thanks for your request. Please try to restart.",
topic_id: topic.id
topic_id: original_inbound_email_topic.id
)
email_log = EmailLog.last
[email_log, group_post]
end
it "the inbound processed email creates an incoming email and topic record correctly, and adds the group to the topic" do
incoming = IncomingEmail.find_by(topic: topic)
incoming = IncomingEmail.find_by(topic: original_inbound_email_topic)
user = User.find_by_email("two@foo.com")
expect(topic.topic_allowed_users.first.user_id).to eq(user.id)
expect(topic.topic_allowed_groups.first.group_id).to eq(group.id)
expect(original_inbound_email_topic.topic_allowed_users.first.user_id).to eq(user.id)
expect(original_inbound_email_topic.topic_allowed_groups.first.group_id).to eq(group.id)
expect(incoming.to_addresses).to eq("team@somesmtpaddress.com")
expect(incoming.from_address).to eq("two@foo.com")
expect(incoming.message_id).to eq("u4w8c9r4y984yh98r3h69873@foo.bar.mail")
@ -1024,7 +1031,7 @@ describe Email::Receiver do
it "creates an EmailLog when someone from the group replies, and does not create an IncomingEmail record for the reply" do
email_log, group_post = reply_as_group_user
expect(email_log.message_id).to eq("topic/#{topic.id}/#{group_post.id}@test.localhost")
expect(email_log.message_id).to eq("topic/#{original_inbound_email_topic.id}/#{group_post.id}@test.localhost")
expect(email_log.to_address).to eq("two@foo.com")
expect(email_log.email_type).to eq("user_private_message")
expect(email_log.post_id).to eq(group_post.id)
@ -1056,7 +1063,7 @@ describe Email::Receiver do
end.to change { Topic.count }.by(1).and change { Post.count }.by(1)
reply_post = Post.last
expect(reply_post.topic_id).not_to eq(topic.id)
expect(reply_post.topic_id).not_to eq(original_inbound_email_topic.id)
end
it "processes the reply from the user as a reply if they have replied from a different address (e.g. auto forward) and allow_unknown_sender_topic_replies is enabled" do
@ -1070,7 +1077,32 @@ describe Email::Receiver do
end.to change { Topic.count }.by(0).and change { Post.count }.by(1)
reply_post = Post.last
expect(reply_post.topic_id).to eq(topic.id)
expect(reply_post.topic_id).to eq(original_inbound_email_topic.id)
end
it "creates a new topic with a reference back to the original if replying to a too old topic" do
SiteSetting.disallow_reply_by_email_after_days = 2
email_log, group_post = reply_as_group_user
group_post.update(created_at: 10.days.ago)
group_post.topic.update(created_at: 10.days.ago)
reply_email = email(:email_to_group_email_username_2)
reply_email.gsub!("MESSAGE_ID_REPLY_TO", email_log.message_id)
expect do
Email::Receiver.new(reply_email).process!
end.to change { Topic.count }.by(1).and change { Post.count }.by(1)
reply_post = Post.last
new_topic = Topic.last
expect(reply_post.topic).to eq(new_topic)
expect(reply_post.raw).to include(
I18n.t(
"emails.incoming.continuing_old_discussion",
url: group_post.topic.url, title: group_post.topic.title, count: SiteSetting.disallow_reply_by_email_after_days
)
)
end
end