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