From 6b481166feafdae23927f7137282b5c44f52dd63 Mon Sep 17 00:00:00 2001 From: Mac Siri Date: Wed, 12 Feb 2020 10:48:51 -0500 Subject: [PATCH] Add unsubscribe option to badge and connect emails (#6027) --- .../email_subscriptions_controller.rb | 25 ++++++++----------- app/mailers/notify_mailer.rb | 2 ++ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/app/controllers/email_subscriptions_controller.rb b/app/controllers/email_subscriptions_controller.rb index 5378d5675..a0355e064 100644 --- a/app/controllers/email_subscriptions_controller.rb +++ b/app/controllers/email_subscriptions_controller.rb @@ -1,28 +1,25 @@ class EmailSubscriptionsController < ApplicationController - # No need to authorize this because its implicit when unsubbing + PREFERRED_EMAIL_NAME = { + email_digest_periodic: "DEV digest emails", + email_comment_notifications: "comment notifications", + email_follower_notifications: "follower notifications", + email_mention_notifications: "mention notifications", + email_connect_messages: "DEV connect messages", + email_unread_notifications: "unread notifications", + email_badge_notifications: "badge notifications" + }.freeze + def unsubscribe verified_params = Rails.application.message_verifier(:unsubscribe).verify(params[:ut]) if verified_params[:expires_at] > Time.current user = User.find(verified_params[:user_id]) user.update(verified_params[:email_type] => false) - @email_type = preferred_email_name(verified_params[:email_type]) + @email_type = PREFERRED_EMAIL_NAME[verified_params[:email_type]] else render "invalid_token" end rescue ActiveSupport::MessageVerifier::InvalidSignature not_found end - - def preferred_email_name(given_email_type) - emails_type = { - email_digest_periodic: "DEV digest emails", - email_comment_notifications: "comment notifications", - email_follower_notifications: "follower notifications", - email_mention_notifications: "mention notifications", - email_connect_messages: "connect messages", - email_unread_notifications: "unread notifications" - } - emails_type[given_email_type] - end end diff --git a/app/mailers/notify_mailer.rb b/app/mailers/notify_mailer.rb index 272e25f10..5f31b74be 100644 --- a/app/mailers/notify_mailer.rb +++ b/app/mailers/notify_mailer.rb @@ -54,6 +54,7 @@ class NotifyMailer < ApplicationMailer @badge_achievement = badge_achievement @user = @badge_achievement.user @badge = @badge_achievement.badge + @unsubscribe = generate_unsubscribe_token(@user.id, :email_badge_notifications) mail(to: @user.email, subject: "You just got a badge") end @@ -69,6 +70,7 @@ class NotifyMailer < ApplicationMailer @message = direct_message @user = @message.direct_receiver subject = "#{@message.user.name} just messaged you" + @unsubscribe = generate_unsubscribe_token(@user.id, :email_connect_messages) mail(to: @user.email, subject: subject) end