2013-02-05 14:16:51 -05:00
|
|
|
class ClicksController < ApplicationController
|
2018-03-16 19:33:11 -04:00
|
|
|
layout "no_ember"
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
skip_before_action :check_xhr, :preload_json
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def track
|
2015-05-05 21:27:41 -04:00
|
|
|
raise Discourse::NotFound unless params[:url]
|
|
|
|
|
2013-06-06 03:14:32 -04:00
|
|
|
params = track_params.merge(ip: request.remote_ip)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-06 03:14:32 -04:00
|
|
|
if params[:topic_id].present? || params[:post_id].present?
|
2017-07-27 21:20:09 -04:00
|
|
|
params.merge!(user_id: current_user.id) if current_user.present?
|
2013-07-26 12:14:11 -04:00
|
|
|
@redirect_url = TopicLinkClick.create_from(params)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-12-20 11:55:15 -05:00
|
|
|
# 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.
|
2018-03-16 19:33:11 -04:00
|
|
|
if params[:redirect] == "false"
|
2017-08-31 00:06:56 -04:00
|
|
|
render body: nil
|
2018-03-16 19:33:11 -04:00
|
|
|
elsif @redirect_url.present?
|
2013-07-26 12:14:11 -04:00
|
|
|
redirect_to(@redirect_url)
|
2018-03-16 19:33:11 -04:00
|
|
|
else
|
|
|
|
render
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-06 03:14:32 -04:00
|
|
|
private
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def track_params
|
|
|
|
params.require(:url)
|
|
|
|
params.permit(:url, :post_id, :topic_id, :redirect)
|
|
|
|
end
|
2013-06-06 03:14:32 -04:00
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|