A platform for community discussion. Free, open, simple.
Revert "DEV: Delete dead Topic#incoming_email_addresses code (#19970)" (#20037)
Martin Brennan48eb8d5f5ad9
diff --git a/app/models/topic.rb b/app/models/topic.rbindex 2fe7f114463..e373f4f4d4a 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -1892,6 +1892,46 @@ class Topic < ActiveRecord::Base.firstend+ 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 [email protected] if the only IncomingEmail+ # is:+ #+ # from: [email protected]+ # to: [email protected]+ #+ # Because we don't care about the from addresses and also the to address+ # is not the email_username, which will be something like [email protected].+ 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,diff --git a/spec/lib/email/receiver_spec.rb b/spec/lib/email/receiver_spec.rbindex baf9e0dae06..99035646be8 100644--- a/spec/lib/email/receiver_spec.rb+++ b/spec/lib/email/receiver_spec.rb@@ -1550,12 +1550,10 @@ RSpec.describe Email::Receiver dohandler_calls = 0handler =proc do |topic|- expect(- [- topic.incoming_email.first.from_address,- topic.incoming_email.first.to_addresses_split,- ].flatten,- ).to contain_exactly("[email protected]", "[email protected]")+ expect(topic.incoming_email_addresses).to contain_exactly(+ "[email protected]",+ "[email protected]",+ )handler_calls += 1enddiff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 4dd28ebff33..4db4f3a5546 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -3271,6 +3271,85 @@ RSpec.describe Topic doendend+ describe "#incoming_email_addresses" do+ fab!(:group) do+ Fabricate(+ :group,+ smtp_server: "imap.gmail.com",+ smtp_port: 587,+ email_username: "[email protected]",+ email_password: "[email protected]",+ )+ 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: "[email protected]",+ from_address: "[email protected]",+ topic: topic,+ post: topic.posts.first,+ created_at: 20.minutes.ago,+ )+ end+ let!(:incoming2) do+ Fabricate(+ :incoming_email,+ from_address: "[email protected]",+ to_addresses: "[email protected]",+ topic: topic,+ post: Fabricate(:post, topic: topic),+ created_at: 10.minutes.ago,+ )+ end+ let!(:incoming3) do+ Fabricate(+ :incoming_email,+ to_addresses: "[email protected]",+ from_address: "[email protected]",+ topic: topic,+ post: topic.posts.first,+ cc_addresses: "[email protected]",+ created_at: 2.minutes.ago,+ )+ end+ let!(:incoming4) do+ Fabricate(+ :incoming_email,+ to_addresses: "[email protected]",+ from_address: "[email protected]",+ 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(+ )+ 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(+ )+ 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("[email protected]")+ end+ end+ end+describe "#cannot_permanently_delete_reason" dofab!(:post) { Fabricate(:post) }let!(:topic) { post.topic }