From 6637ef7852fb5cb341b971807a76a5b013300d22 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 12 Jun 2023 14:22:46 +0200 Subject: [PATCH] Add unsubscribe link to e-mails (#25378) --- .../mail_subscriptions_controller.rb | 41 +++++++++++++++++++ app/mailers/notification_mailer.rb | 30 +++++++++----- app/views/layouts/mailer.html.haml | 6 ++- app/views/mail_subscriptions/create.html.haml | 9 ++++ app/views/mail_subscriptions/show.html.haml | 12 ++++++ config/i18n-tasks.yml | 1 + config/locales/en.yml | 16 ++++++++ config/routes.rb | 2 + 8 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 app/controllers/mail_subscriptions_controller.rb create mode 100644 app/views/mail_subscriptions/create.html.haml create mode 100644 app/views/mail_subscriptions/show.html.haml diff --git a/app/controllers/mail_subscriptions_controller.rb b/app/controllers/mail_subscriptions_controller.rb new file mode 100644 index 000000000..b071a8060 --- /dev/null +++ b/app/controllers/mail_subscriptions_controller.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +class MailSubscriptionsController < ApplicationController + layout 'auth' + + skip_before_action :require_functional! + + before_action :set_body_classes + before_action :set_user + before_action :set_type + + def show; end + + def create + @user.settings[email_type_from_param] = false + @user.save! + end + + private + + def set_user + @user = GlobalID::Locator.locate_signed(params[:token], for: 'unsubscribe') + end + + def set_body_classes + @body_classes = 'lighter' + end + + def set_type + @type = email_type_from_param + end + + def email_type_from_param + case params[:type] + when 'follow', 'reblog', 'favourite', 'mention', 'follow_request' + "notification_emails.#{params[:type]}" + else + raise ArgumentError + end + end +end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index c428fd30d..7cd3bab1a 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -8,61 +8,71 @@ class NotificationMailer < ApplicationMailer def mention(recipient, notification) @me = recipient + @user = recipient.user + @type = 'mention' @status = notification.target_status - return unless @me.user.functional? && @status.present? + return unless @user.functional? && @status.present? locale_for_account(@me) do thread_by_conversation(@status.conversation) - mail to: email_address_with_name(@me.user.email, @me.user.account.username), subject: I18n.t('notification_mailer.mention.subject', name: @status.account.acct) + mail to: email_address_with_name(@user.email, @me.username), subject: I18n.t('notification_mailer.mention.subject', name: @status.account.acct) end end def follow(recipient, notification) @me = recipient + @user = recipient.user + @type = 'follow' @account = notification.from_account - return unless @me.user.functional? + return unless @user.functional? locale_for_account(@me) do - mail to: email_address_with_name(@me.user.email, @me.user.account.username), subject: I18n.t('notification_mailer.follow.subject', name: @account.acct) + mail to: email_address_with_name(@user.email, @me.username), subject: I18n.t('notification_mailer.follow.subject', name: @account.acct) end end def favourite(recipient, notification) @me = recipient + @user = recipient.user + @type = 'favourite' @account = notification.from_account @status = notification.target_status - return unless @me.user.functional? && @status.present? + return unless @user.functional? && @status.present? locale_for_account(@me) do thread_by_conversation(@status.conversation) - mail to: email_address_with_name(@me.user.email, @me.user.account.username), subject: I18n.t('notification_mailer.favourite.subject', name: @account.acct) + mail to: email_address_with_name(@user.email, @me.username), subject: I18n.t('notification_mailer.favourite.subject', name: @account.acct) end end def reblog(recipient, notification) @me = recipient + @user = recipient.user + @type = 'reblog' @account = notification.from_account @status = notification.target_status - return unless @me.user.functional? && @status.present? + return unless @user.functional? && @status.present? locale_for_account(@me) do thread_by_conversation(@status.conversation) - mail to: email_address_with_name(@me.user.email, @me.user.account.username), subject: I18n.t('notification_mailer.reblog.subject', name: @account.acct) + mail to: email_address_with_name(@user.email, @me.username), subject: I18n.t('notification_mailer.reblog.subject', name: @account.acct) end end def follow_request(recipient, notification) @me = recipient + @user = recipient.user + @type = 'follow_request' @account = notification.from_account - return unless @me.user.functional? + return unless @user.functional? locale_for_account(@me) do - mail to: email_address_with_name(@me.user.email, @me.user.account.username), subject: I18n.t('notification_mailer.follow_request.subject', name: @account.acct) + mail to: email_address_with_name(@user.email, @me.username), subject: I18n.t('notification_mailer.follow_request.subject', name: @account.acct) end end diff --git a/app/views/layouts/mailer.html.haml b/app/views/layouts/mailer.html.haml index 43c855927..e39a09780 100644 --- a/app/views/layouts/mailer.html.haml +++ b/app/views/layouts/mailer.html.haml @@ -44,7 +44,11 @@ %tbody %td.column-cell %p= t 'about.hosted_on', domain: site_hostname - %p= link_to t('application_mailer.notification_preferences'), settings_preferences_notifications_url + %p + = link_to t('application_mailer.notification_preferences'), settings_preferences_notifications_url + - if defined?(@type) + ยท + = link_to t('application_mailer.unsubscribe'), unsubscribe_url(token: @user.to_sgid(for: 'unsubscribe').to_s, type: @type) %td.column-cell.text-right = link_to root_url do = image_tag full_pack_url('media/images/mailer/logo.png'), alt: 'Mastodon', height: 24 diff --git a/app/views/mail_subscriptions/create.html.haml b/app/views/mail_subscriptions/create.html.haml new file mode 100644 index 000000000..16ee486b0 --- /dev/null +++ b/app/views/mail_subscriptions/create.html.haml @@ -0,0 +1,9 @@ +- content_for :page_title do + = t('mail_subscriptions.unsubscribe.title') + +.simple_form + %h1.title= t('mail_subscriptions.unsubscribe.complete') + %p.lead + = t('mail_subscriptions.unsubscribe.success_html', domain: content_tag(:strong, site_hostname), type: content_tag(:strong, I18n.t(@type, scope: 'mail_subscriptions.unsubscribe.emails')), email: content_tag(:strong, @user.email)) + %p.lead + = t('mail_subscriptions.unsubscribe.resubscribe_html', settings_path: settings_preferences_notifications_path) diff --git a/app/views/mail_subscriptions/show.html.haml b/app/views/mail_subscriptions/show.html.haml new file mode 100644 index 000000000..afa2ab6ed --- /dev/null +++ b/app/views/mail_subscriptions/show.html.haml @@ -0,0 +1,12 @@ +- content_for :page_title do + = t('mail_subscriptions.unsubscribe.title') + +.simple_form + %h1.title= t('mail_subscriptions.unsubscribe.title') + %p.lead + = t('mail_subscriptions.unsubscribe.confirmation_html', domain: content_tag(:strong, site_hostname), type: content_tag(:strong, I18n.t(@type, scope: 'mail_subscriptions.unsubscribe.emails')), email: content_tag(:strong, @user.email), settings_path: settings_preferences_notifications_path) + + = form_tag unsubscribe_path, method: :post do + = hidden_field_tag :token, params[:token] + = hidden_field_tag :type, params[:type] + = button_tag t('mail_subscriptions.unsubscribe.action'), type: :submit diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index ddf503197..035a0e999 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -66,6 +66,7 @@ ignore_unused: - 'notification_mailer.*' - 'imports.overwrite_preambles.{following,blocking,muting,domain_blocking,bookmarks}_html' - 'imports.preambles.{following,blocking,muting,domain_blocking,bookmarks}_html' + - 'mail_subscriptions.unsubscribe.emails.*' ignore_inconsistent_interpolations: - '*.one' diff --git a/config/locales/en.yml b/config/locales/en.yml index 2c292c42d..10eac9aea 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -978,6 +978,7 @@ en: notification_preferences: Change e-mail preferences salutation: "%{name}," settings: 'Change e-mail preferences: %{link}' + unsubscribe: Unsubscribe view: 'View:' view_profile: View profile view_status: View post @@ -1342,6 +1343,21 @@ en: failed_sign_in_html: Failed sign-in attempt with %{method} from %{ip} (%{browser}) successful_sign_in_html: Successful sign-in with %{method} from %{ip} (%{browser}) title: Authentication history + mail_subscriptions: + unsubscribe: + action: Yes, unsubscribe + complete: Unsubscribed + confirmation_html: Are you sure you want to unsubscribe from receiving %{type} for Mastodon on %{domain} to your e-mail at %{email}? You can always re-subscribe from your e-mail notification settings. + emails: + notification_emails: + favourite: favorite notification e-mails + follow: follow notification e-mails + follow_request: follow request e-mails + mention: mention notification e-mails + reblog: boost notification e-mails + resubscribe_html: If you've unsubscribed by mistake, you can re-subscribe from your e-mail notification settings. + success_html: You'll no longer receive %{type} for Mastodon on %{domain} to your e-mail at %{email}. + title: Unsubscribe media_attachments: validations: images_and_video: Cannot attach a video to a post that already contains images diff --git a/config/routes.rb b/config/routes.rb index 55e5cf36a..f11fcdc23 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -67,6 +67,8 @@ Rails.application.routes.draw do devise_scope :user do get '/invite/:invite_code', to: 'auth/registrations#new', as: :public_invite + resource :unsubscribe, only: [:show, :create], controller: :mail_subscriptions + namespace :auth do resource :setup, only: [:show, :update], controller: :setup resource :challenge, only: [:create], controller: :challenges