FEATURE: sendgrid webhooks

This commit is contained in:
Régis Hanol 2016-06-01 21:48:06 +02:00
parent 0f8b4dcc86
commit 9704603fab
7 changed files with 87 additions and 7 deletions

View File

@ -37,6 +37,22 @@ class WebhooksController < ActionController::Base
handled ? mailgun_success : mailgun_failure
end
def sendgrid
params["_json"].each do |event|
if event["event"] == "bounce".freeze
if event["status"]["4."]
sendgrid_process(event, Email::Receiver::SOFT_BOUNCE_SCORE)
else
sendgrid_process(event, Email::Receiver::HARD_BOUNCE_SCORE)
end
elsif event["event"] == "dropped".freeze
sendgrid_process(event, Email::Receiver::HARD_BOUNCE_SCORE)
end
end
render nothing: true, status: 200
end
private
def mailgun_failure
@ -74,4 +90,15 @@ class WebhooksController < ActionController::Base
true
end
def sendgrid_process(event, bounce_score)
message_id = event["smtp-id"]
return if message_id.blank?
email_log = EmailLog.find_by(message_id: message_id.tr("<>", ""))
return if email_log.nil?
email_log.update_columns(bounced: true)
Email::Receiver.update_bounce_score(email_log.user.email, bounce_score)
end
end

View File

@ -51,4 +51,22 @@ Discourse::Application.configure do
if emails = GlobalSetting.developer_emails
config.developer_emails = emails.split(",").map(&:downcase).map(&:strip)
end
if GlobalSetting.smtp_address
settings = {
address: GlobalSetting.smtp_address,
port: GlobalSetting.smtp_port,
domain: GlobalSetting.smtp_domain,
user_name: GlobalSetting.smtp_user_name,
password: GlobalSetting.smtp_password,
authentication: GlobalSetting.smtp_authentication,
enable_starttls_auto: GlobalSetting.smtp_enable_start_tls
}
settings[:openssl_verify_mode] = GlobalSetting.smtp_openssl_verify_mode if GlobalSetting.smtp_openssl_verify_mode
config.action_mailer.smtp_settings = settings.reject{|_, y| y.nil?}
end
end

View File

@ -17,6 +17,7 @@ Discourse::Application.routes.draw do
get "/404-body" => "exceptions#not_found_body"
post "webhooks/mailgun" => "webhooks#mailgun"
post "webhooks/sendgrid" => "webhooks#sendgrid"
if Rails.env.development?
mount Sidekiq::Web => "/sidekiq"

View File

@ -0,0 +1,6 @@
class AddMessageIdToEmailLogs < ActiveRecord::Migration
def change
add_column :email_logs, :message_id, :string
add_index :email_logs, :message_id
end
end

View File

@ -120,12 +120,11 @@ class Auth::DefaultCurrentUserProvider
protected
def lookup_api_user(api_key_value, request)
api_key = ApiKey.where(key: api_key_value).includes(:user).first
if api_key
if api_key = ApiKey.where(key: api_key_value).includes(:user).first
api_username = request["api_username"]
if api_key.allowed_ips.present? && !api_key.allowed_ips.any?{|ip| ip.include?(request.ip)}
Rails.logger.warn("Unauthorized API access: #{api_username} ip address: #{request.ip}")
if api_key.allowed_ips.present? && !api_key.allowed_ips.any? { |ip| ip.include?(request.ip) }
Rails.logger.warn("[Unauthorized API Access] username: #{api_username}, IP address: #{request.ip}")
return nil
end

View File

@ -141,6 +141,8 @@ module Email
@message.html_part.body = style.strip_avatars_and_emojis
end
email_log.message_id = @message.message_id
begin
@message.deliver_now
rescue *SMTP_CLIENT_ERRORS => e

View File

@ -2,19 +2,21 @@ require "rails_helper"
describe WebhooksController do
context "mailgun" do
let(:email) { "em@il.com" }
before { $redis.del("bounce_score:#{email}:#{Date.today}") }
context "mailgun" do
it "works" do
SiteSetting.mailgun_api_key = "pubkey-8221462f0c915af3f6f2e2df7aa5a493"
token = "705a8ccd2ce932be8e98c221fe701c1b4a0afcb8bbd57726de"
user = Fabricate(:user, email: "em@il.com")
user = Fabricate(:user, email: email)
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}")
$redis.del("bounce_score:#{user.email}:#{Date.today}")
WebhooksController.any_instance.expects(:mailgun_verify).returns(true)
post :mailgun, "token" => token,
@ -31,4 +33,29 @@ describe WebhooksController do
end
context "sendgrid" do
it "works" do
user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: "12345@il.com")
post :sendgrid, "_json" => [
{
"email" => email,
"timestamp" => 1249948800,
"smtp-id" => "<12345@il.com>",
"event" => "bounce",
"status" => "5.0.0"
}
]
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