Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: implement RFC 8058 for email unsubscribe (#33392)

Sam8294b933d2c4
diff --git a/app/controllers/email_controller.rb b/app/controllers/email_controller.rb
index e1da5643f96..bec2e897711 100644
--- a/app/controllers/email_controller.rb
+++ b/app/controllers/email_controller.rb
@@ -8,6 +8,8 @@ class EmailController < ApplicationController
:redirect_to_login_if_required,
:redirect_to_profile_if_required
+ skip_before_action :verify_authenticity_token, only: [:unsubscribe], if: :one_click_unsubscribe?
+
def unsubscribe
key = UnsubscribeKey.includes(:user).find_by(key: params[:key])
@found = key.present?
@@ -29,7 +31,22 @@ class EmailController < ApplicationController
key = UnsubscribeKey.includes(:user).find_by(key: params[:key])
raise Discourse::NotFound if key.nil? || key.user.nil?
user = key.user
- updated = UnsubscribeKey.get_unsubscribe_strategy_for(key)&.unsubscribe(params)
+
+ resolved_params = params.dup
+
+ if params["List-Unsubscribe"] == "One-Click"
+ # digests
+ resolved_params["digest_after_minutes"] = 0
+
+ # specific topics
+ resolved_params["unwatch_topic"] = true
+ resolved_params["unwatch_category"] = true
+
+ # everything else (mailing list mode etc...)
+ resolved_params["unsubscribe_all"] = true
+ end
+
+ updated = UnsubscribeKey.get_unsubscribe_strategy_for(key)&.unsubscribe(resolved_params)
if updated
cache_key = "unsub_#{SecureRandom.hex}"
@@ -54,4 +71,10 @@ class EmailController < ApplicationController
@topic = topic if topic && Guardian.new.can_see?(topic)
end
end
+
+ private
+
+ def one_click_unsubscribe?
+ request.post? && params["List-Unsubscribe"] == "One-Click"
+ end
end
diff --git a/spec/requests/email_controller_spec.rb b/spec/requests/email_controller_spec.rb
index b9ade85f4c7..cff3c29a19d 100644
--- a/spec/requests/email_controller_spec.rb
+++ b/spec/requests/email_controller_spec.rb
@@ -47,6 +47,67 @@ RSpec.describe EmailController do
end
end
+ describe "unsubscribe using rfc 8058" do
+ fab!(:topic_post) { Fabricate(:post) }
+ before { ActionController::Base.allow_forgery_protection = true }
+ after { ActionController::Base.allow_forgery_protection = false }
+
+ it "supports one-click unsubscribe from list" do
+ key = UnsubscribeKey.create_key_for(user, UnsubscribeKey::DIGEST_TYPE)
+ user.user_option.update_columns(
+ email_digests: true,
+ email_level: UserOption.email_level_types[:never],
+ email_messages_level: UserOption.email_level_types[:never],
+ mailing_list_mode: true,
+ )
+
+ # ensure we don't have a CSRF token
+ post "/email/unsubscribe/#{key}", params: { "List-Unsubscribe": "One-Click" }
+ # we have no CSRF token, but this is fine, we should still get a 302
+ expect(response.status).to eq(302)
+
+ user.user_option.reload
+ expect(user.user_option.email_digests).to eq(false)
+ expect(user.user_option.mailing_list_mode).to eq(false)
+ end
+
+ it "supports one-click unsubscribe from topic" do
+ topic_key =
+ UnsubscribeKey.create_key_for(user, UnsubscribeKey::TOPIC_TYPE, post: topic_post)
+
+ TopicUser.change(
+ user.id,
+ topic_post.topic_id,
+ notification_level: TopicUser.notification_levels[:watching],
+ )
+
+ post "/email/unsubscribe/#{topic_key}", params: { "List-Unsubscribe": "One-Click" }
+ expect(response.status).to eq(302)
+
+ expect(TopicUser.get(topic_post.topic, user).notification_level).to eq(
+ TopicUser.notification_levels[:tracking],
+ )
+ end
+
+ it "supports one-click unsubscribe from mailing list" do
+ mailing_list_key = UnsubscribeKey.create_key_for(user, UnsubscribeKey::ALL_TYPE)
+
+ user.user_option.update_columns(
+ mailing_list_mode: true,
+ email_digests: true,
+ email_level: UserOption.email_level_types[:always],
+ )
+
+ post "/email/unsubscribe/#{mailing_list_key}", params: { "List-Unsubscribe": "One-Click" }
+ expect(response.status).to eq(302)
+
+ user.user_option.reload
+ expect(user.user_option.mailing_list_mode).to eq(false)
+ expect(user.user_option.email_digests).to eq(false)
+ expect(user.user_option.email_level).to eq(UserOption.email_level_types[:never])
+ end
+ end
+
describe "unsubscribe from digest" do
let(:key) { UnsubscribeKey.create_key_for(user, UnsubscribeKey::DIGEST_TYPE) }