2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class EmailController < ApplicationController
|
2015-01-15 15:56:53 -05:00
|
|
|
layout 'no_ember'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2018-01-31 20:26:45 -05:00
|
|
|
skip_before_action :check_xhr, :preload_json, :redirect_to_login_if_required
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def unsubscribe
|
2017-07-20 06:24:24 -04: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
|
2019-04-17 11:14:40 -04:00
|
|
|
@digest_unsubscribe = !@topic && !SiteSetting.disable_digest_emails
|
2017-07-20 06:24:24 -04:00
|
|
|
@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 21:27:52 -04:00
|
|
|
|
2017-07-20 06:24:24 -04:00
|
|
|
watching = TopicUser.notification_levels[:watching]
|
|
|
|
|
2020-07-23 02:20:10 -04:00
|
|
|
@unsubscribed_from_all = @user.user_option.unsubscribed_from_all?
|
|
|
|
|
2017-07-20 06:24:24 -04:00
|
|
|
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)
|
|
|
|
.where(user: @user, notification_level: watching, "topics.category_id" => @topic.category_id)
|
|
|
|
.count
|
|
|
|
end
|
|
|
|
end
|
2019-04-17 11:14:40 -04:00
|
|
|
else
|
|
|
|
@digest_frequencies = digest_frequencies(@user)
|
2016-06-16 21:27:52 -04:00
|
|
|
end
|
|
|
|
end
|
2014-07-15 17:19:45 -04:00
|
|
|
end
|
2016-06-16 21:27:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_unsubscribe
|
2018-05-21 19:06:46 -04:00
|
|
|
RateLimiter.new(nil, "unsubscribe_#{request.ip}", 10, 1.minute).performed!
|
|
|
|
|
2016-06-16 21:27:52 -04:00
|
|
|
key = UnsubscribeKey.find_by(key: params[:key])
|
2017-07-20 06:24:24 -04:00
|
|
|
raise Discourse::NotFound unless key && key.user
|
2016-06-16 21:27:52 -04:00
|
|
|
|
2017-07-20 06:24:24 -04:00
|
|
|
topic = key&.post&.topic || key.topic
|
2016-06-16 21:27:52 -04:00
|
|
|
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,
|
2016-07-05 15:16:32 -04:00
|
|
|
notification_level: CategoryUser.watching_levels
|
2016-06-16 21:27:52 -04:00
|
|
|
)
|
|
|
|
.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"]
|
2016-06-16 21:57:23 -04:00
|
|
|
user.user_option.update_columns(mailing_list_mode: false)
|
2016-06-16 21:27:52 -04:00
|
|
|
updated = true
|
|
|
|
end
|
|
|
|
|
2019-04-17 11:14:40 -04:00
|
|
|
if params['digest_after_minutes']
|
|
|
|
digest_frequency = params['digest_after_minutes'].to_i
|
|
|
|
|
|
|
|
user.user_option.update_columns(
|
|
|
|
digest_after_minutes: digest_frequency,
|
|
|
|
email_digests: digest_frequency.positive?
|
|
|
|
)
|
2016-06-16 21:27:52 -04:00
|
|
|
updated = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if params["unsubscribe_all"]
|
2019-03-15 10:55:11 -04:00
|
|
|
user.user_option.update_columns(email_digests: false,
|
|
|
|
email_level: UserOption.email_level_types[:never],
|
2020-08-03 02:59:54 -04:00
|
|
|
email_messages_level: UserOption.email_level_types[:never],
|
|
|
|
mailing_list_mode: false)
|
2016-06-16 21:27:52 -04:00
|
|
|
updated = true
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-07-15 17:19:45 -04:00
|
|
|
|
2016-06-16 21:27:52 -04:00
|
|
|
unless updated
|
2017-12-11 01:47:41 -05:00
|
|
|
redirect_back fallback_location: path("/")
|
2016-01-20 04:25:25 -05:00
|
|
|
else
|
2018-05-21 19:06:46 -04:00
|
|
|
|
|
|
|
key = "unsub_#{SecureRandom.hex}"
|
2019-11-27 00:11:49 -05:00
|
|
|
Discourse.cache.write key, user.email, expires_in: 1.hour
|
2018-05-21 19:06:46 -04:00
|
|
|
|
|
|
|
url = path("/email/unsubscribed?key=#{key}")
|
2016-06-16 21:27:52 -04:00
|
|
|
if topic
|
2018-05-21 19:06:46 -04:00
|
|
|
url += "&topic_id=#{topic.id}"
|
2016-06-16 21:27:52 -04:00
|
|
|
end
|
2018-05-21 19:06:46 -04:00
|
|
|
|
|
|
|
redirect_to url
|
2016-01-20 04:25:25 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2016-06-16 21:27:52 -04:00
|
|
|
def unsubscribed
|
2019-11-27 00:11:49 -05:00
|
|
|
@email = Discourse.cache.read(params[:key])
|
2018-04-16 00:44:43 -04:00
|
|
|
@topic_id = params[:topic_id]
|
2018-05-21 19:06:46 -04:00
|
|
|
user = User.find_by_email(@email)
|
2018-04-15 08:29:58 -04:00
|
|
|
raise Discourse::NotFound unless user
|
2018-04-16 00:44:43 -04:00
|
|
|
topic = Topic.find_by(id: params[:topic_id].to_i) if @topic_id
|
|
|
|
@topic = topic if topic && Guardian.new(nil).can_see?(topic)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2019-04-17 11:14:40 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def digest_frequencies(user)
|
|
|
|
frequency_in_minutes = user.user_option.digest_after_minutes
|
2020-07-23 02:20:10 -04:00
|
|
|
email_digests = user.user_option.email_digests
|
2019-04-17 11:14:40 -04:00
|
|
|
frequencies = DigestEmailSiteSetting.values.dup
|
|
|
|
never = frequencies.delete_at(0)
|
|
|
|
allowed_frequencies = %w[never weekly every_month every_six_months]
|
|
|
|
|
|
|
|
result = frequencies.reduce(frequencies: [], current: nil, selected: nil, take_next: false) do |memo, v|
|
2020-07-23 02:20:10 -04:00
|
|
|
memo[:current] = v[:name] if v[:value] == frequency_in_minutes && email_digests
|
2019-04-17 11:14:40 -04:00
|
|
|
next(memo) unless allowed_frequencies.include?(v[:name])
|
|
|
|
|
|
|
|
memo.tap do |m|
|
2020-07-23 02:20:10 -04:00
|
|
|
m[:selected] = v[:value] if m[:take_next] && email_digests
|
2019-04-17 11:14:40 -04:00
|
|
|
m[:frequencies] << [I18n.t("unsubscribe.digest_frequency.#{v[:name]}"), v[:value]]
|
|
|
|
m[:take_next] = !m[:take_next] && m[:current]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result.slice(:frequencies, :current, :selected).tap do |r|
|
|
|
|
r[:frequencies] << [I18n.t("unsubscribe.digest_frequency.#{never[:name]}"), never[:value]]
|
|
|
|
r[:selected] ||= never[:value]
|
|
|
|
r[:current] ||= never[:name]
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|