2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-13 15:26:40 -05:00
|
|
|
module Jobs
|
|
|
|
|
2019-10-02 00:01:53 -04:00
|
|
|
class ProcessSnsNotification < ::Jobs::Base
|
2019-02-13 15:26:40 -05:00
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
def execute(args)
|
2019-03-19 06:39:41 -04:00
|
|
|
return unless raw = args[:raw].presence
|
2019-02-13 15:26:40 -05:00
|
|
|
return unless json = args[:json].presence
|
|
|
|
return unless message = json["Message"].presence
|
2019-03-19 06:39:41 -04:00
|
|
|
|
|
|
|
message = begin
|
|
|
|
JSON.parse(message)
|
|
|
|
rescue JSON::ParserError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless message && message["notificationType"] == "Bounce"
|
2019-02-13 15:26:40 -05:00
|
|
|
return unless message_id = message.dig("mail", "messageId").presence
|
|
|
|
return unless bounce_type = message.dig("bounce", "bounceType").presence
|
|
|
|
|
2019-02-14 05:08:21 -05:00
|
|
|
require "aws-sdk-sns"
|
2019-02-13 15:26:40 -05:00
|
|
|
return unless Aws::SNS::MessageVerifier.new.authentic?(raw)
|
|
|
|
|
|
|
|
message.dig("bounce", "bouncedRecipients").each do |r|
|
2019-04-22 23:03:31 -04:00
|
|
|
if email_log = EmailLog.order("created_at DESC").where(to_address: r["emailAddress"]).first
|
2022-02-14 23:17:26 -05:00
|
|
|
email_log.update_columns(bounced: true, bounce_error_code: r["status"])
|
2019-02-13 15:26:40 -05:00
|
|
|
|
|
|
|
if email_log.user&.email.present?
|
2019-04-22 23:03:31 -04:00
|
|
|
if email_log.user.user_stat.bounce_score.to_s.start_with?("4.") || bounce_type == "Transient"
|
2019-02-13 15:26:40 -05:00
|
|
|
Email::Receiver.update_bounce_score(email_log.user.email, SiteSetting.soft_bounce_score)
|
|
|
|
else
|
|
|
|
Email::Receiver.update_bounce_score(email_log.user.email, SiteSetting.hard_bounce_score)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|