Merge pull request #8978 from colinux/refactor-dolist-module

Tech: ne mélange pas class & module Dolist, supprime adapter SMTP
This commit is contained in:
Colin Darie 2023-04-27 09:52:42 +00:00 committed by GitHub
commit b0f4c578a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 298 additions and 320 deletions

View file

@ -1,6 +1,7 @@
require "support/jsv"
class Dolist::API
module Dolist
class API
CONTACT_URL = "https://apiv9.dolist.net/v1/contacts/read?AccountID=%{account_id}"
EMAIL_LOGS_URL = "https://apiv9.dolist.net/v1/statistics/email/sendings/transactional/search?AccountID=%{account_id}"
EMAIL_KEY = 7
@ -312,3 +313,4 @@ class Dolist::API
end
end
end
end

View file

@ -0,0 +1,21 @@
module Dolist
class APISender
def initialize(mail); end
def deliver!(mail)
client = Dolist::API.new
response = client.send_email(mail)
if response&.dig("Result")
mail.message_id = response.dig("Result")
else
_, invalid_contact_status = client.ignorable_error?(response, mail)
if invalid_contact_status
raise Dolist::IgnorableError.new("DoList delivery error. contact unreachable: #{invalid_contact_status}")
else
fail "DoList delivery error. Body: #{response}"
end
end
end
end
end

View file

@ -0,0 +1,3 @@
module Dolist
class IgnorableError < StandardError; end
end

View file

@ -21,7 +21,7 @@ class EmailEvent < ApplicationRecord
}
scope :dolist, -> { dolist_smtp.or(dolist_api) }
scope :dolist_smtp, -> { where(method: 'dolist_smtp') }
scope :dolist_smtp, -> { where(method: 'dolist_smtp') } # legacy method: removable after 2023-06
scope :dolist_api, -> { where(method: 'dolist_api') }
scope :sendinblue, -> { where(method: 'sendinblue') }
scope :outdated, -> { where("created_at < ?", RETENTION_DURATION.ago) }

View file

@ -124,7 +124,7 @@ SENDINBLUE_BALANCING_VALUE="50"
# Ratio of emails sent using DoList
# When present, N % of emails will be sent using DoList
# (and the others using the default SMTP provider)
DOLIST_BALANCING_VALUE="50"
DOLIST_API_BALANCING_VALUE="50"
# Used only by a migration to choose your default regarding procedure archive dossiers after duree_conservation_dossiers_dans_ds
# DEFAULT_PROCEDURE_EXPIRES_WHEN_TERMINE_ENABLED=true

View file

@ -96,14 +96,12 @@ Rails.application.configure do
}
else
sendinblue_weigth = ENV.fetch('SENDINBLUE_BALANCING_VALUE') { 0 }.to_i
dolist_weigth = ENV.fetch('DOLIST_BALANCING_VALUE') { 0 }.to_i
dolist_api_weight = ENV.fetch('DOLIST_API_BALANCING_VALUE') { 0 }.to_i
ActionMailer::Base.add_delivery_method :balancer, BalancerDeliveryMethod
config.action_mailer.balancer_settings = {
sendinblue: sendinblue_weigth,
dolist_smtp: dolist_weigth,
dolist_api: dolist_api_weight,
mailjet: 100 - (sendinblue_weigth + dolist_weigth + dolist_api_weight)
mailjet: 100 - (sendinblue_weigth + dolist_api_weight)
}
config.action_mailer.delivery_method = :balancer
end

View file

@ -1,49 +1,3 @@
ActiveSupport.on_load(:action_mailer) do
module Dolist
class IgnorableError < StandardError
end
class SMTP < ::Mail::SMTP
def deliver!(mail)
mail.from(ENV['DOLIST_NO_REPLY_EMAIL'])
mail.sender(ENV['DOLIST_NO_REPLY_EMAIL'])
mail['X-ACCOUNT-ID'] = Rails.application.secrets.dolist[:account_id]
mail['X-Dolist-Sending-Type'] = 'TransactionalService' # send even if the target is not active
super(mail)
end
end
class ApiSender
def initialize(mail); end
def deliver!(mail)
client = Dolist::API.new
response = client.send_email(mail)
if response&.dig("Result")
mail.message_id = response.dig("Result")
else
_, invalid_contact_status = client.ignorable_error?(response, mail)
if invalid_contact_status
raise Dolist::IgnorableError.new("DoList delivery error. contact unreachable: #{invalid_contact_status}")
else
fail "DoList delivery error. Body: #{response}"
end
end
end
end
end
ActionMailer::Base.add_delivery_method :dolist_smtp, Dolist::SMTP
ActionMailer::Base.dolist_smtp_settings = {
user_name: Rails.application.secrets.dolist[:username],
password: Rails.application.secrets.dolist[:password],
address: 'smtp.dolist.net',
port: 587,
authentication: 'plain',
enable_starttls_auto: true
}
ActionMailer::Base.add_delivery_method :dolist_api, Dolist::ApiSender
ActionMailer::Base.add_delivery_method :dolist_api, Dolist::APISender
end