Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/services/email_forwarding_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def self.from_address(address, destination_addresses: [])
def initialize(inbound_domain:, inbound_local:, destination_addresses:)
@inbound_domain = EmailRoute.normalize_domain(inbound_domain)
@inbound_local = EmailRoute.normalize_local(inbound_local)
@destination_addresses = destination_addresses.map { |address| EmailRoute.normalize_address(address) }.uniq
@destination_addresses = EmailForwardingRouter.deduplicate_destinations(destination_addresses)
end

def inbound_email
Expand Down Expand Up @@ -166,6 +166,11 @@ def self.all_destination_addresses
all_mappings.values.flatten.uniq
end

def self.deduplicate_destinations(destinations)
destinations_by_normalized_address = destinations.index_by { |dest| EmailRoute.normalize_address(dest) }
destinations_by_normalized_address.values
end

attr_reader :address

def initialize(address)
Expand All @@ -174,12 +179,12 @@ def initialize(address)

def convention_by_domain
return @convention_by_domain if defined?(@convention_by_domain)
@convention_by_domain ||= Convention.find_by(domain: Mail::Address.new(address).domain)
@convention_by_domain = Convention.find_by(domain: Mail::Address.new(address).domain)
end

def convention_by_events_domain
return @convention_by_events_domain if defined?(@convention_by_events_domain)
@convention_by_events_domain ||= Convention.find_by(event_mailing_list_domain: Mail::Address.new(address).domain)
@convention_by_events_domain = Convention.find_by(event_mailing_list_domain: Mail::Address.new(address).domain)
end

def forward_addresses
Expand Down
48 changes: 25 additions & 23 deletions test/services/email_forwarding_router_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ class EmailForwardingRouterTest < ActiveSupport::TestCase
help_mapping = mappings.values.find { |m| m.inbound_email == "help@example.com" }
support_mapping = mappings.values.find { |m| m.inbound_email == "support@example.com" }

expected_destinations = [user1.email, user2.email, "cc@example.com"].map do |addr|
EmailRoute.normalize_address(addr)
end
expected_destinations =
[user1.email, user2.email, "cc@example.com"].map { |addr| EmailRoute.normalize_address(addr) }

assert_equal expected_destinations.sort, primary_mapping.destination_addresses.sort
assert_equal expected_destinations.sort, help_mapping.destination_addresses.sort
Expand All @@ -50,14 +49,7 @@ class EmailForwardingRouterTest < ActiveSupport::TestCase
end

describe "without email aliases" do
let(:staff_position) do
create(
:staff_position,
convention:,
email: "staff@example.com",
email_aliases: []
)
end
let(:staff_position) { create(:staff_position, convention:, email: "staff@example.com", email_aliases: []) }

setup { staff_position.user_con_profiles << user_con_profile1 }

Expand All @@ -72,21 +64,11 @@ class EmailForwardingRouterTest < ActiveSupport::TestCase

describe "with multiple staff positions with aliases" do
let(:staff_position1) do
create(
:staff_position,
convention:,
email: "staff1@example.com",
email_aliases: %w[help support]
)
create(:staff_position, convention:, email: "staff1@example.com", email_aliases: %w[help support])
end

let(:staff_position2) do
create(
:staff_position,
convention:,
email: "staff2@example.com",
email_aliases: ["info"]
)
create(:staff_position, convention:, email: "staff2@example.com", email_aliases: ["info"])
end

setup do
Expand All @@ -111,4 +93,24 @@ class EmailForwardingRouterTest < ActiveSupport::TestCase
end
end
end

describe "deduplication" do
let(:staff_position) do
create(
:staff_position,
convention:,
email: "staff@example.com",
cc_addresses: %w[email.address1@example.com Email.Address1@example.com]
)
end

setup { staff_position }

it "deduplicates email addresses but does not modify them" do
mappings = EmailForwardingRouter.all_staff_position_mappings

assert_equal 1, mappings.values.size
assert_equal ["email.address1@example.com"], mappings.values.first.destination_addresses.map(&:downcase)
end
end
end
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"skipLibCheck": true,
"maxNodeModuleJsDepth": 10,
"baseUrl": "./app/javascript",
"types": ["vitest/globals", "vite/client"]
"types": ["vitest/globals", "vite/client"],
"incremental": true
},
"include": [
"./app/javascript/",
Expand Down
Loading