discourse/app/controllers/clicks_controller.rb

40 lines
1.1 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
class ClicksController < ApplicationController
layout "no_ember"
2013-02-06 03:16:51 +08:00
skip_before_action :check_xhr, :preload_json
2013-02-07 23:45:24 +08:00
2013-02-06 03:16:51 +08:00
def track
raise Discourse::NotFound unless params[:url]
params = track_params.merge(ip: request.remote_ip)
2013-02-06 03:16:51 +08:00
if params[:topic_id].present? || params[:post_id].present?
2017-07-28 09:20:09 +08:00
params.merge!(user_id: current_user.id) if current_user.present?
@redirect_url = TopicLinkClick.create_from(params)
2013-02-06 03:16:51 +08:00
end
# links in whispers aren't extracted, just allow the redirection to staff
if @redirect_url.blank? && current_user&.staff? && params[:post_id].present?
@redirect_url = params[:url] if Post.exists?(id: params[:post_id], post_type: Post.types[:whisper])
end
# Sometimes we want to record a link without a 302.
# Since XHR has to load the redirected URL we want it to not return a 302 in those cases.
if params[:redirect] == "false"
render body: nil
elsif @redirect_url.present?
redirect_to(@redirect_url)
else
render
2013-02-06 03:16:51 +08:00
end
end
private
2018-06-07 13:28:18 +08:00
def track_params
params.require(:url)
params.permit(:url, :post_id, :topic_id, :redirect)
end
2013-02-07 23:45:24 +08:00
end