mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
FEATURE: simpler and friendlier unsubscribe workflow
- All unsubscribes go to the exact same page - You may unsubscribe from watching a category on that page - You no longer need to be logged in to unsubscribe from a topic - Simplified footer on emails
This commit is contained in:
@ -1,45 +1,123 @@
|
||||
class EmailController < ApplicationController
|
||||
skip_before_filter :check_xhr, :preload_json
|
||||
skip_before_filter :check_xhr, :preload_json, :redirect_to_login_if_required
|
||||
layout 'no_ember'
|
||||
|
||||
before_filter :ensure_logged_in, only: :preferences_redirect
|
||||
skip_before_filter :redirect_to_login_if_required
|
||||
|
||||
def preferences_redirect
|
||||
redirect_to(email_preferences_path(current_user.username_lower))
|
||||
end
|
||||
|
||||
def unsubscribe
|
||||
@user = DigestUnsubscribeKey.user_for_key(params[:key])
|
||||
RateLimiter.new(@user, "unsubscribe_via_email", 3, 1.day).performed! unless @user && @user.staff?
|
||||
key = UnsubscribeKey.find_by(key: params[:key])
|
||||
|
||||
# Don't allow the use of a key while logged in as a different user
|
||||
if current_user.present? && (@user != current_user)
|
||||
@different_user = true
|
||||
return
|
||||
if key
|
||||
@user = key.user
|
||||
post = key.post
|
||||
@topic = (post && post.topic) || key.topic
|
||||
@type = key.unsubscribe_key_type
|
||||
|
||||
if current_user.present? && (@user != current_user)
|
||||
@different_user = @user.name
|
||||
@return_url = request.original_url
|
||||
end
|
||||
|
||||
@watching_topic = @topic && TopicUser.exists?(user_id: @user.id,
|
||||
notification_level: TopicUser.notification_levels[:watching],
|
||||
topic_id: @topic.id)
|
||||
|
||||
@watched_count = nil
|
||||
if @topic && @topic.category_id
|
||||
if CategoryUser.exists?(user_id: @user.id,
|
||||
notification_level: CategoryUser.notification_levels[:watching],
|
||||
category_id: @topic.category_id)
|
||||
@watched_count = TopicUser.joins(:topic)
|
||||
.where(:user => @user,
|
||||
:notification_level => TopicUser.notification_levels[:watching],
|
||||
"topics.category_id" => @topic.category_id
|
||||
).count
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if @user.blank?
|
||||
@not_found = true
|
||||
return
|
||||
end
|
||||
|
||||
if params[:from_all]
|
||||
@user.user_option.update_columns(email_always: false,
|
||||
email_digests: false,
|
||||
email_direct: false,
|
||||
email_private_messages: false)
|
||||
else
|
||||
@user.user_option.update_column(:email_digests, false)
|
||||
end
|
||||
|
||||
@success = true
|
||||
end
|
||||
|
||||
def resubscribe
|
||||
@user = DigestUnsubscribeKey.user_for_key(params[:key])
|
||||
raise Discourse::NotFound unless @user.present?
|
||||
@user.user_option.update_column(:email_digests, true)
|
||||
def perform_unsubscribe
|
||||
|
||||
key = UnsubscribeKey.find_by(key: params[:key])
|
||||
unless key && key.user
|
||||
raise Discourse::NotFound
|
||||
end
|
||||
|
||||
topic = (key.post && key.post.topic) || key.topic
|
||||
user = key.user
|
||||
|
||||
updated = false
|
||||
|
||||
if topic
|
||||
if params["unwatch_topic"]
|
||||
TopicUser.where(topic_id: topic.id, user_id: user.id)
|
||||
.update_all(notification_level: TopicUser.notification_levels[:tracking])
|
||||
updated = true
|
||||
end
|
||||
|
||||
if params["unwatch_category"] && topic.category_id
|
||||
TopicUser.joins(:topic)
|
||||
.where(:user => user,
|
||||
:notification_level => TopicUser.notification_levels[:watching],
|
||||
"topics.category_id" => topic.category_id)
|
||||
.update_all(notification_level: TopicUser.notification_levels[:tracking])
|
||||
|
||||
CategoryUser.where(user_id: user.id,
|
||||
category_id: topic.category_id,
|
||||
notification_level: CategoryUser.notification_levels[:watching]
|
||||
)
|
||||
.destroy_all
|
||||
updated = true
|
||||
end
|
||||
|
||||
if params["mute_topic"]
|
||||
TopicUser.where(topic_id: topic.id, user_id: user.id)
|
||||
.update_all(notification_level: TopicUser.notification_levels[:muted])
|
||||
updated = true
|
||||
end
|
||||
end
|
||||
|
||||
if params["disable_mailing_list"]
|
||||
user.user_option.update_columns(email_always: false)
|
||||
updated = true
|
||||
end
|
||||
|
||||
if params["disable_digest_emails"]
|
||||
user.user_option.update_columns(email_digests: false)
|
||||
updated = true
|
||||
end
|
||||
|
||||
if params["unsubscribe_all"]
|
||||
user.user_option.update_columns(email_always: false,
|
||||
email_digests: false,
|
||||
email_direct: false,
|
||||
email_private_messages: false)
|
||||
updated = true
|
||||
end
|
||||
|
||||
unless updated
|
||||
redirect_to :back
|
||||
else
|
||||
if topic
|
||||
redirect_to path("/email/unsubscribed?topic_id=#{topic.id}")
|
||||
else
|
||||
redirect_to path("/email/unsubscribed")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def unsubscribed
|
||||
@topic = Topic.find_by(id: params[:topic_id].to_i) if params[:topic_id]
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user