2013-02-05 13:16:51 -06:00
|
|
|
class EmailController < ApplicationController
|
2015-01-15 14:56:53 -06:00
|
|
|
layout 'no_ember'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2018-01-31 19:26:45 -06:00
|
|
|
skip_before_action :check_xhr, :preload_json, :redirect_to_login_if_required
|
2017-08-30 23:06:56 -05:00
|
|
|
before_action :ensure_logged_in, only: :preferences_redirect
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
def preferences_redirect
|
|
|
|
redirect_to(email_preferences_path(current_user.username_lower))
|
|
|
|
end
|
|
|
|
|
|
|
|
def unsubscribe
|
2017-07-20 05:24:24 -05:00
|
|
|
@not_found = true
|
|
|
|
@watched_count = nil
|
|
|
|
|
|
|
|
if key = UnsubscribeKey.find_by(key: params[:key])
|
|
|
|
if @user = key.user
|
|
|
|
post = key.post
|
|
|
|
@topic = post&.topic || key.topic
|
|
|
|
@type = key.unsubscribe_key_type
|
|
|
|
@not_found = false
|
|
|
|
|
|
|
|
if current_user.present? && (@user != current_user)
|
|
|
|
@different_user = @user.name
|
|
|
|
@return_url = request.original_url
|
|
|
|
end
|
2016-06-16 20:27:52 -05:00
|
|
|
|
2017-07-20 05:24:24 -05:00
|
|
|
watching = TopicUser.notification_levels[:watching]
|
|
|
|
|
|
|
|
if @topic
|
|
|
|
@watching_topic = TopicUser.exists?(user_id: @user.id, notification_level: watching, topic_id: @topic.id)
|
|
|
|
if @topic.category_id
|
|
|
|
if CategoryUser.exists?(user_id: @user.id, notification_level: CategoryUser.watching_levels, category_id: @topic.category_id)
|
|
|
|
@watched_count = TopicUser.joins(:topic)
|
2017-07-27 20:20:09 -05:00
|
|
|
.where(user: @user, notification_level: watching, "topics.category_id" => @topic.category_id)
|
|
|
|
.count
|
2017-07-20 05:24:24 -05:00
|
|
|
end
|
|
|
|
end
|
2016-06-16 20:27:52 -05:00
|
|
|
end
|
|
|
|
end
|
2014-07-15 16:19:45 -05:00
|
|
|
end
|
2016-06-16 20:27:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_unsubscribe
|
|
|
|
key = UnsubscribeKey.find_by(key: params[:key])
|
2017-07-20 05:24:24 -05:00
|
|
|
raise Discourse::NotFound unless key && key.user
|
2016-06-16 20:27:52 -05:00
|
|
|
|
2017-07-20 05:24:24 -05:00
|
|
|
topic = key&.post&.topic || key.topic
|
2016-06-16 20:27:52 -05:00
|
|
|
user = key.user
|
|
|
|
|
|
|
|
updated = false
|
|
|
|
|
|
|
|
if topic
|
|
|
|
if params["unwatch_topic"]
|
|
|
|
TopicUser.where(topic_id: topic.id, user_id: user.id)
|
2017-07-27 20:20:09 -05:00
|
|
|
.update_all(notification_level: TopicUser.notification_levels[:tracking])
|
2016-06-16 20:27:52 -05:00
|
|
|
updated = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if params["unwatch_category"] && topic.category_id
|
|
|
|
TopicUser.joins(:topic)
|
2017-07-27 20:20:09 -05:00
|
|
|
.where(:user => user,
|
|
|
|
:notification_level => TopicUser.notification_levels[:watching],
|
|
|
|
"topics.category_id" => topic.category_id)
|
|
|
|
.update_all(notification_level: TopicUser.notification_levels[:tracking])
|
2016-06-16 20:27:52 -05:00
|
|
|
|
|
|
|
CategoryUser.where(user_id: user.id,
|
2017-07-27 20:20:09 -05:00
|
|
|
category_id: topic.category_id,
|
|
|
|
notification_level: CategoryUser.watching_levels
|
2016-06-16 20:27:52 -05:00
|
|
|
)
|
2017-07-27 20:20:09 -05:00
|
|
|
.destroy_all
|
2016-06-16 20:27:52 -05:00
|
|
|
updated = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if params["mute_topic"]
|
|
|
|
TopicUser.where(topic_id: topic.id, user_id: user.id)
|
2017-07-27 20:20:09 -05:00
|
|
|
.update_all(notification_level: TopicUser.notification_levels[:muted])
|
2016-06-16 20:27:52 -05:00
|
|
|
updated = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if params["disable_mailing_list"]
|
2016-06-16 20:57:23 -05:00
|
|
|
user.user_option.update_columns(mailing_list_mode: false)
|
2016-06-16 20:27:52 -05:00
|
|
|
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,
|
2017-07-27 20:20:09 -05:00
|
|
|
email_digests: false,
|
|
|
|
email_direct: false,
|
|
|
|
email_private_messages: false)
|
2016-06-16 20:27:52 -05:00
|
|
|
updated = true
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-07-15 16:19:45 -05:00
|
|
|
|
2016-06-16 20:27:52 -05:00
|
|
|
unless updated
|
2017-12-11 00:47:41 -06:00
|
|
|
redirect_back fallback_location: path("/")
|
2016-01-20 03:25:25 -06:00
|
|
|
else
|
2016-06-16 20:27:52 -05:00
|
|
|
if topic
|
2017-06-09 23:13:41 -05:00
|
|
|
redirect_to path("/email/unsubscribed?topic_id=#{topic.id}&email=#{user.email}")
|
2016-06-16 20:27:52 -05:00
|
|
|
else
|
2017-06-09 23:13:41 -05:00
|
|
|
redirect_to path("/email/unsubscribed?email=#{user.email}")
|
2016-06-16 20:27:52 -05:00
|
|
|
end
|
2016-01-20 03:25:25 -06:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2016-06-16 20:27:52 -05:00
|
|
|
def unsubscribed
|
2017-06-09 23:13:41 -05:00
|
|
|
@email = params[:email]
|
2017-10-08 20:04:46 -05:00
|
|
|
raise Discourse::NotFound if !User.find_by_email(params[:email])
|
2016-06-16 20:27:52 -05:00
|
|
|
@topic = Topic.find_by(id: params[:topic_id].to_i) if params[:topic_id]
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|