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
|
2022-11-30 12:29:07 -05:00
|
|
|
key = UnsubscribeKey.includes(:user).find_by(key: params[:key])
|
2022-06-21 14:49:47 -04:00
|
|
|
@found = key.present?
|
2022-11-30 12:29:07 -05:00
|
|
|
@key_owner_found = key&.user.present?
|
2017-07-20 06:24:24 -04:00
|
|
|
|
2022-11-30 12:29:07 -05:00
|
|
|
if @found && @key_owner_found
|
2022-06-21 14:49:47 -04:00
|
|
|
UnsubscribeKey.get_unsubscribe_strategy_for(key)&.prepare_unsubscribe_options(self)
|
2020-07-23 02:20:10 -04:00
|
|
|
|
2022-06-21 14:49:47 -04:00
|
|
|
if current_user.present? && (@user != current_user)
|
|
|
|
@different_user = @user.name
|
|
|
|
@return_url = request.original_url
|
2016-06-16 21:27:52 -04:00
|
|
|
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])
|
2022-06-21 14:49:47 -04:00
|
|
|
raise Discourse::NotFound if key.nil? || key.user.nil?
|
2016-06-16 21:27:52 -04:00
|
|
|
user = key.user
|
2022-06-21 14:49:47 -04:00
|
|
|
updated = UnsubscribeKey.get_unsubscribe_strategy_for(key)&.unsubscribe(params)
|
2016-06-16 21:27:52 -04:00
|
|
|
|
2022-06-21 14:49:47 -04:00
|
|
|
if updated
|
|
|
|
cache_key = "unsub_#{SecureRandom.hex}"
|
|
|
|
Discourse.cache.write cache_key, user.email, expires_in: 1.hour
|
2018-05-21 19:06:46 -04:00
|
|
|
|
2022-06-21 14:49:47 -04:00
|
|
|
url = path("/email/unsubscribed?key=#{cache_key}")
|
|
|
|
url += "&topic_id=#{key.associated_topic.id}" if key.associated_topic
|
2018-05-21 19:06:46 -04:00
|
|
|
|
|
|
|
redirect_to url
|
2022-06-21 14:49:47 -04:00
|
|
|
else
|
|
|
|
redirect_back fallback_location: path("/")
|
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
|
|
|
|
end
|