2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-09 16:02:06 +01:00
|
|
|
class EmailEvent < ApplicationRecord
|
2023-02-03 17:09:58 +01:00
|
|
|
RETENTION_DURATION = 1.month
|
|
|
|
|
2023-01-09 16:02:06 +01:00
|
|
|
enum status: {
|
2023-06-19 15:24:10 +02:00
|
|
|
pending: 'pending',
|
2023-01-09 23:16:14 +01:00
|
|
|
dispatched: 'dispatched',
|
|
|
|
dispatch_error: 'dispatch_error'
|
2023-01-09 16:02:06 +01:00
|
|
|
}
|
2023-02-03 17:09:58 +01:00
|
|
|
|
2023-02-03 15:42:46 +01:00
|
|
|
scope :dolist, -> { dolist_smtp.or(dolist_api) }
|
2023-04-27 09:50:12 +02:00
|
|
|
scope :dolist_smtp, -> { where(method: 'dolist_smtp') } # legacy method: removable after 2023-06
|
2023-02-03 15:42:46 +01:00
|
|
|
scope :dolist_api, -> { where(method: 'dolist_api') }
|
2023-01-16 23:42:26 +01:00
|
|
|
scope :sendinblue, -> { where(method: 'sendinblue') }
|
2023-02-03 17:09:58 +01:00
|
|
|
scope :outdated, -> { where("created_at < ?", RETENTION_DURATION.ago) }
|
2023-01-16 23:42:26 +01:00
|
|
|
|
2023-01-09 17:05:38 +01:00
|
|
|
class << self
|
|
|
|
def create_from_message!(message, status:)
|
2024-03-11 13:11:25 +01:00
|
|
|
to = message.to_addrs || ["unset"] # no recipients when error occurs *before* setting to: in the mailer
|
2023-01-10 13:58:30 +01:00
|
|
|
|
|
|
|
to.each do |recipient|
|
2023-01-09 17:05:38 +01:00
|
|
|
EmailEvent.create!(
|
2023-01-10 15:47:00 +01:00
|
|
|
to: recipient,
|
2023-01-17 16:46:54 +01:00
|
|
|
subject: message.subject || "",
|
2023-01-09 17:05:38 +01:00
|
|
|
processed_at: message.date,
|
|
|
|
method: ActionMailer::Base.delivery_methods.key(message.delivery_method.class),
|
2023-02-03 15:40:16 +01:00
|
|
|
message_id: message.message_id,
|
2023-01-09 17:05:38 +01:00
|
|
|
status:
|
|
|
|
)
|
|
|
|
rescue StandardError => error
|
|
|
|
Sentry.capture_exception(error, extra: { subject: message.subject, status: })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-01-16 23:42:26 +01:00
|
|
|
|
|
|
|
def match_dolist_email
|
|
|
|
return if to == "unset"
|
|
|
|
|
|
|
|
# subjects does not match, so compare to event time with tolerance
|
|
|
|
Dolist::API.new.sent_mails(to).sort_by(&:delivered_at).find { (processed_at..processed_at + 1.hour).cover?(_1.delivered_at) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def domain
|
|
|
|
to.split("@").last
|
|
|
|
end
|
2023-01-09 16:02:06 +01:00
|
|
|
end
|