2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
def unsubscribe
|
2022-06-21 13:49:47 -05:00
|
|
|
key = UnsubscribeKey.find_by(key: params[:key])
|
|
|
|
@found = key.present?
|
2017-07-20 05:24:24 -05:00
|
|
|
|
2022-06-21 13:49:47 -05:00
|
|
|
if @found
|
|
|
|
UnsubscribeKey
|
|
|
|
.get_unsubscribe_strategy_for(key)
|
|
|
|
&.prepare_unsubscribe_options(self)
|
2020-07-23 01:20:10 -05:00
|
|
|
|
2022-06-21 13:49:47 -05:00
|
|
|
if current_user.present? && (@user != current_user)
|
|
|
|
@different_user = @user.name
|
|
|
|
@return_url = request.original_url
|
2016-06-16 20:27:52 -05:00
|
|
|
end
|
2014-07-15 16:19:45 -05:00
|
|
|
end
|
2016-06-16 20:27:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_unsubscribe
|
2018-05-21 18:06:46 -05:00
|
|
|
RateLimiter.new(nil, "unsubscribe_#{request.ip}", 10, 1.minute).performed!
|
|
|
|
|
2016-06-16 20:27:52 -05:00
|
|
|
key = UnsubscribeKey.find_by(key: params[:key])
|
2022-06-21 13:49:47 -05:00
|
|
|
raise Discourse::NotFound if key.nil? || key.user.nil?
|
2016-06-16 20:27:52 -05:00
|
|
|
user = key.user
|
2022-06-21 13:49:47 -05:00
|
|
|
updated = UnsubscribeKey.get_unsubscribe_strategy_for(key)
|
|
|
|
&.unsubscribe(params)
|
2016-06-16 20:27:52 -05:00
|
|
|
|
2022-06-21 13:49:47 -05:00
|
|
|
if updated
|
|
|
|
cache_key = "unsub_#{SecureRandom.hex}"
|
|
|
|
Discourse.cache.write cache_key, user.email, expires_in: 1.hour
|
2018-05-21 18:06:46 -05:00
|
|
|
|
2022-06-21 13:49:47 -05:00
|
|
|
url = path("/email/unsubscribed?key=#{cache_key}")
|
|
|
|
if key.associated_topic
|
|
|
|
url += "&topic_id=#{key.associated_topic.id}"
|
2016-06-16 20:27:52 -05:00
|
|
|
end
|
2018-05-21 18:06:46 -05:00
|
|
|
|
|
|
|
redirect_to url
|
2022-06-21 13:49:47 -05:00
|
|
|
else
|
|
|
|
redirect_back fallback_location: path("/")
|
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
|
2019-11-26 23:11:49 -06:00
|
|
|
@email = Discourse.cache.read(params[:key])
|
2018-04-15 23:44:43 -05:00
|
|
|
@topic_id = params[:topic_id]
|
2018-05-21 18:06:46 -05:00
|
|
|
user = User.find_by_email(@email)
|
2018-04-15 07:29:58 -05:00
|
|
|
raise Discourse::NotFound unless user
|
2018-04-15 23:44:43 -05: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 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|