mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 19:34:09 +08:00
36 lines
992 B
Ruby
36 lines
992 B
Ruby
class ClicksController < ApplicationController
|
|
|
|
skip_before_filter :check_xhr
|
|
|
|
def track
|
|
params = track_params.merge(ip: request.remote_ip)
|
|
|
|
if params[:topic_id].present? || params[:post_id].present?
|
|
params.merge!({ user_id: current_user.id }) if current_user.present?
|
|
@redirect_url = TopicLinkClick.create_from(params)
|
|
|
|
if @redirect_url.blank? && params[:url].index('?')
|
|
# Check the url without query parameters
|
|
params[:url].sub!(/\?.*$/, '')
|
|
@redirect_url = TopicLinkClick.create_from(params)
|
|
end
|
|
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' || @redirect_url.blank?
|
|
render nothing: true
|
|
else
|
|
redirect_to(@redirect_url)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def track_params
|
|
params.require(:url)
|
|
params.permit(:url, :post_id, :topic_id, :redirect)
|
|
end
|
|
|
|
end
|