Revert "DEV: Delete dead Topic#incoming_email_addresses code (#19970)" (#20037)

This reverts commit 88a972c61b.

It's actually used in some plugins.
This commit is contained in:
Martin Brennan 2023-01-27 11:27:15 +10:00 committed by GitHub
parent c8f8d9dbb6
commit 48eb8d5f5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 123 additions and 6 deletions

View File

@ -1892,6 +1892,46 @@ class Topic < ActiveRecord::Base
.first
end
def incoming_email_addresses(group: nil, received_before: Time.zone.now)
email_addresses = Set.new
# TODO(martin) Look at improving this N1, it will just get slower the
# more replies/incoming emails there are for the topic.
self
.incoming_email
.where("created_at <= ?", received_before)
.each do |incoming_email|
to_addresses = incoming_email.to_addresses_split
cc_addresses = incoming_email.cc_addresses_split
combined_addresses = [to_addresses, cc_addresses].flatten
# We only care about the emails addressed to the group or CC'd to the
# group if the group is present. If combined addresses is empty we do
# not need to do this check, and instead can proceed on to adding the
# from address.
#
# Will not include test1@gmail.com if the only IncomingEmail
# is:
#
# from: test1@gmail.com
# to: test+support@discoursemail.com
#
# Because we don't care about the from addresses and also the to address
# is not the email_username, which will be something like test1@gmail.com.
if group.present? && combined_addresses.any?
next if combined_addresses.none? { |address| address =~ group.email_username_regex }
end
email_addresses.add(incoming_email.from_address)
email_addresses.merge(combined_addresses)
end
email_addresses.subtract([nil, ""])
email_addresses.delete(group.email_username) if group.present?
email_addresses.to_a
end
def create_invite_notification!(target_user, notification_type, invited_by, post_number: 1)
if UserCommScreener.new(
acting_user: invited_by,

View File

@ -1550,12 +1550,10 @@ RSpec.describe Email::Receiver do
handler_calls = 0
handler =
proc do |topic|
expect(
[
topic.incoming_email.first.from_address,
topic.incoming_email.first.to_addresses_split,
].flatten,
).to contain_exactly("discourse@bar.com", "category@foo.com")
expect(topic.incoming_email_addresses).to contain_exactly(
"discourse@bar.com",
"category@foo.com",
)
handler_calls += 1
end

View File

@ -3271,6 +3271,85 @@ RSpec.describe Topic do
end
end
describe "#incoming_email_addresses" do
fab!(:group) do
Fabricate(
:group,
smtp_server: "imap.gmail.com",
smtp_port: 587,
email_username: "discourse@example.com",
email_password: "discourse@example.com",
)
end
fab!(:topic) do
Fabricate(
:private_message_topic,
topic_allowed_groups: [Fabricate.build(:topic_allowed_group, group: group)],
)
end
let!(:incoming1) do
Fabricate(
:incoming_email,
to_addresses: "discourse@example.com",
from_address: "johnsmith@user.com",
topic: topic,
post: topic.posts.first,
created_at: 20.minutes.ago,
)
end
let!(:incoming2) do
Fabricate(
:incoming_email,
from_address: "discourse@example.com",
to_addresses: "johnsmith@user.com",
topic: topic,
post: Fabricate(:post, topic: topic),
created_at: 10.minutes.ago,
)
end
let!(:incoming3) do
Fabricate(
:incoming_email,
to_addresses: "discourse@example.com",
from_address: "johnsmith@user.com",
topic: topic,
post: topic.posts.first,
cc_addresses: "otherguy@user.com",
created_at: 2.minutes.ago,
)
end
let!(:incoming4) do
Fabricate(
:incoming_email,
to_addresses: "unrelated@test.com",
from_address: "discourse@example.com",
topic: topic,
post: topic.posts.first,
created_at: 1.minutes.ago,
)
end
it "returns an array of all the incoming email addresses" do
expect(topic.incoming_email_addresses).to match_array(
%w[discourse@example.com johnsmith@user.com otherguy@user.com unrelated@test.com],
)
end
it "returns an array of all the incoming email addresses where incoming was received before X" do
expect(topic.incoming_email_addresses(received_before: 5.minutes.ago)).to match_array(
%w[discourse@example.com johnsmith@user.com],
)
end
context "when the group is present" do
it "excludes incoming emails that are not to or CCd to the group" do
expect(topic.incoming_email_addresses(group: group)).not_to include("unrelated@test.com")
end
end
end
describe "#cannot_permanently_delete_reason" do
fab!(:post) { Fabricate(:post) }
let!(:topic) { post.topic }