Add email subscriptions (#38163)
This commit is contained in:
1 parent
b46d003e20
commit
bcf0718a9a
108 files changed
+839
-516
No files matched your search
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::V1::Accounts::EmailSubscriptionsController < Api::BaseController
|
||||
before_action :set_account
|
||||
before_action :require_feature_enabled!
|
||||
before_action :require_account_permissions!
|
||||
|
||||
def create
|
||||
@account.email_subscriptions.create!(email: params[:email], locale: I18n.locale)
|
||||
render_empty
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_account
|
||||
@account = Account.local.find(params[:account_id])
|
||||
end
|
||||
|
||||
def require_feature_enabled!
|
||||
head 404 unless Mastodon::Feature.email_subscriptions_enabled?
|
||||
end
|
||||
|
||||
def require_account_permissions!
|
||||
head 404 if @account.unavailable? || !@account.user_can?(:manage_email_subscriptions) || !@account.user_email_subscriptions_enabled?
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class EmailSubscriptions::ConfirmationsController < ApplicationController
|
||||
layout 'auth'
|
||||
|
||||
before_action :set_email_subscription
|
||||
|
||||
def show
|
||||
@email_subscription.confirm! unless @email_subscription.confirmed?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_email_subscription
|
||||
@email_subscription = EmailSubscription.find_by!(confirmation_token: params[:confirmation_token])
|
||||
end
|
||||
end
|
||||
@@ -1,39 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class MailSubscriptionsController < ApplicationController
|
||||
layout 'auth'
|
||||
|
||||
skip_before_action :require_functional!
|
||||
|
||||
before_action :set_user
|
||||
before_action :set_type
|
||||
|
||||
protect_from_forgery with: :null_session
|
||||
|
||||
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')
|
||||
not_found unless @user
|
||||
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
|
||||
not_found
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
class Settings::PrivacyController < Settings::BaseController
|
||||
before_action :set_account
|
||||
before_action :set_email_subscriptions_count
|
||||
|
||||
def show; end
|
||||
|
||||
@@ -24,4 +25,8 @@ class Settings::PrivacyController < Settings::BaseController
|
||||
def set_account
|
||||
@account = current_account
|
||||
end
|
||||
|
||||
def set_email_subscriptions_count
|
||||
@email_subscriptions_count = with_read_replica { @account.email_subscriptions.confirmed.count }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,58 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class UnsubscriptionsController < ApplicationController
|
||||
layout 'auth'
|
||||
|
||||
skip_before_action :require_functional!
|
||||
|
||||
before_action :set_recipient
|
||||
before_action :set_type
|
||||
before_action :set_scope
|
||||
before_action :require_type_if_user!
|
||||
|
||||
protect_from_forgery with: :null_session
|
||||
|
||||
def show; end
|
||||
|
||||
def create
|
||||
case @scope
|
||||
when :user
|
||||
@recipient.settings[@type] = false
|
||||
@recipient.save!
|
||||
when :email_subscription
|
||||
@recipient.destroy!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_recipient
|
||||
@recipient = GlobalID::Locator.locate_signed(params[:token], for: 'unsubscribe')
|
||||
not_found unless @recipient
|
||||
end
|
||||
|
||||
def set_scope
|
||||
if @recipient.is_a?(User)
|
||||
@scope = :user
|
||||
elsif @recipient.is_a?(EmailSubscription)
|
||||
@scope = :email_subscription
|
||||
else
|
||||
not_found
|
||||
end
|
||||
end
|
||||
|
||||
def set_type
|
||||
@type = email_type_from_param
|
||||
end
|
||||
|
||||
def require_type_if_user!
|
||||
not_found if @recipient.is_a?(User) && @type.blank?
|
||||
end
|
||||
|
||||
def email_type_from_param
|
||||
case params[:type]
|
||||
when 'follow', 'reblog', 'favourite', 'mention', 'follow_request'
|
||||
"notification_emails.#{params[:type]}"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user