Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mailchimp unsubscribe webhook #5804

Merged
merged 5 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions app/controllers/webhooks/mailchimp_unsubscribes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Webhooks::MailchimpUnsubscribesController < ApplicationController
class InvalidListID < StandardError; end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should all this logic (the mappings and the email_type) be in the controller?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a POC, I opted for "easy to remove again" as my optimization target. If this sticks around, we can always move it out later. But if you prefer I can also move it out now, but since this mapping isn't used elsewhere yet, I don't have a problem with it being in this controller for the time being.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it makes sense, let's leave it as is


LIST_MAPPINGS = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to do this lookup/is this mapping defined somewhere already?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first of it's kind 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good to know. Can you please sanity check the mapping?

mailchimp_newsletter_id: :email_newsletter,
mailchimp_sustaining_members_id: :email_membership_newsletter,
mailchimp_tag_moderators_id: :email_tag_mod_newsletter,
mailchimp_community_moderators_id: :email_community_mod_newsletter
}.freeze

def create
not_authorized unless valid_secret?
user = User.find_by!(email: params.dig(:data, :email))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to raise in case we can't find the user, but I'm open to other approaches (log to DataDog and no-op for example).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fine for the time being. It should be caught by honeybadger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should not raise while still testing this out/debugging, because if my assumption about the webhook payload are wrong we'll raise for no good reason.

user.update(email_type => false)
end

private

def valid_secret?
params[:secret] == SiteConfig.mailchimp_webhook_secret
end

def email_type
list_id = params.dig(:data, :list_id)
key = LIST_MAPPINGS.keys.detect { |k| SiteConfig.public_send(k) == list_id }
raise InvalidListID unless key

LIST_MAPPINGS[key]
end
end
4 changes: 4 additions & 0 deletions app/models/site_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class SiteConfig < RailsSettings::Base
field :mailchimp_tag_moderators_id, type: :string, default: ""
field :mailchimp_community_moderators_id, type: :string, default: ""

# Mailchimp webhook secret. Part of the callback URL in the Mailchimp settings.
# <https://mailchimp.com/developer/guides/about-webhooks/#Webhooks_security>
field :mailchimp_webhook_secret, type: :string, default: ""

# Email digest frequency
field :periodic_email_digest_max, type: :integer, default: 0
field :periodic_email_digest_min, type: :integer, default: 2
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,15 @@
resources :reads, only: [:create]
end

namespace :webhooks do
post "/mailchimp/:secret/unsubscribe", to: "mailchimp_unsubscribes#create", as: :mailchimp_unsubscribe
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably change this at one point and use routing DSL trickery, but for now this seems good enough.

end

resources :messages, only: [:create]
resources :chat_channels, only: %i[index show create update]
resources :chat_channel_memberships, only: %i[create update destroy]
resources :articles, only: %i[update create destroy]
resources :article_mutes, only: %i[update]
resources :article_mutes, only: %i[update]
maestromac marked this conversation as resolved.
Show resolved Hide resolved
resources :comments, only: %i[create update destroy] do
patch "/hide", to: "comments#hide"
patch "/unhide", to: "comments#unhide"
Expand Down
29 changes: 29 additions & 0 deletions spec/requests/webhooks/mailchimp/unsubscribe_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "rails_helper"

RSpec.describe "Webhooks::MailchimpUnsubscribesController", type: :request do
let(:user) { create(:user, email_digest_periodic: true) }

describe "POST /webhooks/mailchimp/:secret/unsubscribe" do
let(:secret) { "secret" }
let(:list_id) { "1234" }
let(:params) { { data: { email: user.email, list_id: list_id } } }

before do
allow(SiteConfig).to receive(:mailchimp_webhook_secret).and_return(secret)
end

it "return not authorized if the secret is incorrect" do
expect do
post "/webhooks/mailchimp/wrong_secret/unsubscribe", params: params
end.to raise_error(Pundit::NotAuthorizedError)
end

it "unsubscribes the user if the secret is correct" do
SiteConfig.mailchimp_newsletter_id = list_id

expect do
post "/webhooks/mailchimp/#{secret}/unsubscribe", params: params
end.to change { user.reload.email_newsletter }.from(true).to(false)
end
end
end