FEATURE: mailjet webhook
This commit is contained in:
parent
1e2bad135f
commit
fe595f1653
|
@ -38,7 +38,8 @@ class WebhooksController < ActionController::Base
|
|||
end
|
||||
|
||||
def sendgrid
|
||||
params["_json"].each do |event|
|
||||
events = params["_json"] || [params]
|
||||
events.each do |event|
|
||||
if event["event"] == "bounce".freeze
|
||||
if event["status"]["4."]
|
||||
sendgrid_process(event, Email::Receiver::SOFT_BOUNCE_SCORE)
|
||||
|
@ -53,6 +54,21 @@ class WebhooksController < ActionController::Base
|
|||
render nothing: true, status: 200
|
||||
end
|
||||
|
||||
def mailjet
|
||||
events = params["_json"] || [params]
|
||||
events.each do |event|
|
||||
if event["event"] == "bounce".freeze
|
||||
if event["hard_bounce"]
|
||||
mailjet_process(event, Email::Receiver::HARD_BOUNCE_SCORE)
|
||||
else
|
||||
mailjet_process(event, Email::Receiver::SOFT_BOUNCE_SCORE)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
render nothing: true, status: 200
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mailgun_failure
|
||||
|
@ -101,4 +117,15 @@ class WebhooksController < ActionController::Base
|
|||
Email::Receiver.update_bounce_score(email_log.user.email, bounce_score)
|
||||
end
|
||||
|
||||
def mailjet_process(event, bounce_score)
|
||||
message_id = event["CustomID"]
|
||||
return if message_id.blank?
|
||||
|
||||
email_log = EmailLog.find_by(message_id: message_id)
|
||||
return if email_log.nil?
|
||||
|
||||
email_log.update_columns(bounced: true)
|
||||
Email::Receiver.update_bounce_score(email_log.user.email, bounce_score)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -18,6 +18,7 @@ Discourse::Application.routes.draw do
|
|||
|
||||
post "webhooks/mailgun" => "webhooks#mailgun"
|
||||
post "webhooks/sendgrid" => "webhooks#sendgrid"
|
||||
post "webhooks/mailjet" => "webhooks#mailjet"
|
||||
|
||||
if Rails.env.development?
|
||||
mount Sidekiq::Web => "/sidekiq"
|
||||
|
|
|
@ -133,6 +133,11 @@ module Email
|
|||
@message.header['X-Discourse-Post-Id'] = nil if post_id.present?
|
||||
@message.header['X-Discourse-Reply-Key'] = nil if reply_key.present?
|
||||
|
||||
# it's the only way to pass the original message_id when using mailjet
|
||||
if ActionMailer::Base.smtp_settings[:address][".mailjet.com"]
|
||||
@message.header['X-MJ-CustomID'] = @message.message_id
|
||||
end
|
||||
|
||||
# Suppress images from short emails
|
||||
if SiteSetting.strip_images_from_short_emails &&
|
||||
@message.html_part.body.to_s.bytesize <= SiteSetting.short_email_length &&
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
require "rails_helper"
|
||||
|
||||
describe WebhooksController do
|
||||
before { $redis.flushall }
|
||||
|
||||
let(:email) { "em@il.com" }
|
||||
|
||||
before { $redis.del("bounce_score:#{email}:#{Date.today}") }
|
||||
|
||||
context "mailgun" do
|
||||
|
||||
it "works" do
|
||||
|
@ -16,7 +15,6 @@ describe WebhooksController do
|
|||
email_log = Fabricate(:email_log, user: user, bounce_key: SecureRandom.hex)
|
||||
return_path = "foo+verp-#{email_log.bounce_key}@bar.com"
|
||||
|
||||
$redis.del("mailgun_token_#{token}")
|
||||
WebhooksController.any_instance.expects(:mailgun_verify).returns(true)
|
||||
|
||||
post :mailgun, "token" => token,
|
||||
|
@ -42,7 +40,6 @@ describe WebhooksController do
|
|||
post :sendgrid, "_json" => [
|
||||
{
|
||||
"email" => email,
|
||||
"timestamp" => 1249948800,
|
||||
"smtp-id" => "<12345@il.com>",
|
||||
"event" => "bounce",
|
||||
"status" => "5.0.0"
|
||||
|
@ -58,4 +55,26 @@ describe WebhooksController do
|
|||
|
||||
end
|
||||
|
||||
context "mailjet" do
|
||||
|
||||
it "works" do
|
||||
message_id = "12345@il.com"
|
||||
user = Fabricate(:user, email: email)
|
||||
email_log = Fabricate(:email_log, user: user, message_id: message_id)
|
||||
|
||||
post :mailjet, {
|
||||
"event" => "bounce",
|
||||
"hard_bounce" => true,
|
||||
"CustomID" => message_id
|
||||
}
|
||||
|
||||
expect(response).to be_success
|
||||
|
||||
email_log.reload
|
||||
expect(email_log.bounced).to eq(true)
|
||||
expect(email_log.user.user_stat.bounce_score).to eq(2)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue