2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
class EmailController < ApplicationController
|
2015-01-16 04:56:53 +08:00
|
|
|
layout "no_ember"
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2018-02-01 09:26:45 +08:00
|
|
|
skip_before_action :check_xhr, :preload_json, :redirect_to_login_if_required
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def unsubscribe
|
2022-12-01 01:29:07 +08:00
|
|
|
key = UnsubscribeKey.includes(:user).find_by(key: params[:key])
|
2022-06-22 02:49:47 +08:00
|
|
|
@found = key.present?
|
2022-12-01 01:29:07 +08:00
|
|
|
@key_owner_found = key&.user.present?
|
2017-07-20 18:24:24 +08:00
|
|
|
|
2022-12-01 01:29:07 +08:00
|
|
|
if @found && @key_owner_found
|
2022-06-22 02:49:47 +08:00
|
|
|
UnsubscribeKey.get_unsubscribe_strategy_for(key)&.prepare_unsubscribe_options(self)
|
2020-07-23 14:20:10 +08:00
|
|
|
|
2022-06-22 02:49:47 +08:00
|
|
|
if current_user.present? && (@user != current_user)
|
|
|
|
@different_user = @user.name
|
|
|
|
@return_url = request.original_url
|
2016-06-17 09:27:52 +08:00
|
|
|
end
|
2014-07-16 05:19:45 +08:00
|
|
|
end
|
2016-06-17 09:27:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_unsubscribe
|
2018-05-22 07:06:46 +08:00
|
|
|
RateLimiter.new(nil, "unsubscribe_#{request.ip}", 10, 1.minute).performed!
|
|
|
|
|
2016-06-17 09:27:52 +08:00
|
|
|
key = UnsubscribeKey.find_by(key: params[:key])
|
2022-06-22 02:49:47 +08:00
|
|
|
raise Discourse::NotFound if key.nil? || key.user.nil?
|
2016-06-17 09:27:52 +08:00
|
|
|
user = key.user
|
2022-06-22 02:49:47 +08:00
|
|
|
updated = UnsubscribeKey.get_unsubscribe_strategy_for(key)&.unsubscribe(params)
|
2016-06-17 09:27:52 +08:00
|
|
|
|
2022-06-22 02:49:47 +08:00
|
|
|
if updated
|
|
|
|
cache_key = "unsub_#{SecureRandom.hex}"
|
|
|
|
Discourse.cache.write cache_key, user.email, expires_in: 1.hour
|
2018-05-22 07:06:46 +08:00
|
|
|
|
2022-06-22 02:49:47 +08:00
|
|
|
url = path("/email/unsubscribed?key=#{cache_key}")
|
|
|
|
url += "&topic_id=#{key.associated_topic.id}" if key.associated_topic
|
2018-05-22 07:06:46 +08:00
|
|
|
|
|
|
|
redirect_to url
|
2022-06-22 02:49:47 +08:00
|
|
|
else
|
|
|
|
redirect_back fallback_location: path("/")
|
2016-01-20 17:25:25 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-06-17 09:27:52 +08:00
|
|
|
def unsubscribed
|
2019-11-27 13:11:49 +08:00
|
|
|
@email = Discourse.cache.read(params[:key])
|
2018-04-16 12:44:43 +08:00
|
|
|
@topic_id = params[:topic_id]
|
2018-05-22 07:06:46 +08:00
|
|
|
user = User.find_by_email(@email)
|
2018-04-15 20:29:58 +08:00
|
|
|
raise Discourse::NotFound unless user
|
2018-04-16 12:44:43 +08:00
|
|
|
topic = Topic.find_by(id: params[:topic_id].to_i) if @topic_id
|
2023-12-06 14:37:32 +08:00
|
|
|
@topic = topic if topic && Guardian.new(nil).can_see?(topic)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|