Class: WebhooksController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/webhooks_controller.rb

Instance Method Summary collapse

Instance Method Details

#awsObject



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/webhooks_controller.rb', line 176

def aws
  raw = request.raw_post
  json = JSON.parse(raw)

  case json["Type"]
  when "SubscriptionConfirmation"
    Jobs.enqueue(:confirm_sns_subscription, raw: raw, json: json)
  when "Notification"
    Jobs.enqueue(:process_sns_notification, raw: raw, json: json)
  end

  success
end

#mailgunObject



8
9
10
11
12
# File 'app/controllers/webhooks_controller.rb', line 8

def mailgun
  return signature_failure if SiteSetting.mailgun_api_key.blank?

  params["event-data"] ? handle_mailgun_new(params) : handle_mailgun_legacy(params)
end

#mailjetObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/webhooks_controller.rb', line 42

def mailjet
  if SiteSetting.mailjet_webhook_token.present?
    return signature_failure if !valid_mailjet_token?
  else
    Rails.logger.warn(
      "Received a Mailjet webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",
    )
  end

  events = params["_json"] || [params]
  events.each do |event|
    message_id = event["CustomID"]
    to_address = event["email"]
    if event["event"] == "bounce"
      if event["hard_bounce"]
        process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
      else
        process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
      end
    end
  end

  success
end

#mailpaceObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/webhooks_controller.rb', line 67

def mailpace
  # see https://docs.mailpace.com/guide/webhooks#email-events

  message_id = Email::MessageIdService.message_id_clean(params["payload"]["message_id"])
  to_address = params["payload"]["to"]
  status = params["payload"]["status"]

  case status
  when "bounced"
    process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
  when "deferred"
    process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
  end

  success
end

#mandrillObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/webhooks_controller.rb', line 84

def mandrill
  if SiteSetting.mandrill_authentication_key.present?
    return signature_failure if !valid_mandrill_signature?
  else
    Rails.logger.warn(
      "Received a Mandrill webhook, but no authentication key has been configured. This is unsafe behaviour and will be disallowed in the future.",
    )
  end

  JSON
    .parse(params["mandrill_events"])
    .each do |event|
      message_id = event.dig("msg", "metadata", "message_id")
      to_address = event.dig("msg", "email")
      error_code = event.dig("msg", "diag")

      case event["event"]
      when "hard_bounce"
        process_bounce(message_id, to_address, SiteSetting.hard_bounce_score, error_code)
      when "soft_bounce"
        process_bounce(message_id, to_address, SiteSetting.soft_bounce_score, error_code)
      end
    end

  success
end

#mandrill_headObject



111
112
113
114
115
# File 'app/controllers/webhooks_controller.rb', line 111

def mandrill_head
  # Mandrill sends a HEAD request to validate the webhook before saving
  # Rails interprets it as a GET request
  success
end

#postmarkObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/webhooks_controller.rb', line 117

def postmark
  if SiteSetting.postmark_webhook_token.present?
    return signature_failure if !valid_postmark_token?
  else
    Rails.logger.warn(
      "Received a Postmark webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",
    )
  end

  # see https://postmarkapp.com/developer/webhooks/bounce-webhook#bounce-webhook-data
  # and https://postmarkapp.com/developer/api/bounce-api#bounce-types

  message_id = params["MessageID"]
  to_address = params["Email"]
  type = params["Type"]
  case type
  when "HardBounce", "SpamNotification", "SpamComplaint"
    process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
  when "SoftBounce"
    process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
  end

  success
end

#sendgridObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/webhooks_controller.rb', line 14

def sendgrid
  if SiteSetting.sendgrid_verification_key.present?
    return signature_failure if !valid_sendgrid_signature?
  else
    Rails.logger.warn(
      "Received a Sendgrid webhook, but no verification key has been configured. This is unsafe behaviour and will be disallowed in the future.",
    )
  end

  events = params["_json"] || [params]
  events.each do |event|
    message_id = Email::MessageIdService.message_id_clean((event["smtp-id"] || ""))
    to_address = event["email"]
    error_code = event["status"]
    if event["event"] == "bounce"
      if error_code[Email::SMTP_STATUS_TRANSIENT_FAILURE]
        process_bounce(message_id, to_address, SiteSetting.soft_bounce_score, error_code)
      else
        process_bounce(message_id, to_address, SiteSetting.hard_bounce_score, error_code)
      end
    elsif event["event"] == "dropped"
      process_bounce(message_id, to_address, SiteSetting.hard_bounce_score, error_code)
    end
  end

  success
end

#sparkpostObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/webhooks_controller.rb', line 142

def sparkpost
  if SiteSetting.sparkpost_webhook_token.present?
    return signature_failure if !valid_sparkpost_token?
  else
    Rails.logger.warn(
      "Received a Sparkpost webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",
    )
  end

  events = params["_json"] || [params]
  events.each do |event|
    message_event = event.dig("msys", "message_event")
    next unless message_event

    message_id = message_event.dig("rcpt_meta", "message_id")
    to_address = message_event["rcpt_to"]
    bounce_class = message_event["bounce_class"]
    next unless bounce_class

    bounce_class = bounce_class.to_i

    # bounce class definitions: https://support.sparkpost.com/customer/portal/articles/1929896
    if bounce_class < 80
      if bounce_class == 10 || bounce_class == 25 || bounce_class == 30
        process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
      else
        process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
      end
    end
  end

  success
end