Fix /p/post/user route not saving referrals

Make user id optional for /p/id/uid
Add /posts/id/raw route for debugging failed post processing
This commit is contained in:
riking 2014-07-11 14:34:26 -07:00
parent d45c50f4bb
commit 783454ebe1
5 changed files with 24 additions and 10 deletions

View file

@ -314,7 +314,7 @@ class ApplicationController < ActionController::Base
end
def store_incoming_links
IncomingLink.add(request,current_user) unless request.xhr?
IncomingLink.add(request, current_user) unless request.xhr?
end
def check_xhr

View file

@ -8,10 +8,17 @@ class PostsController < ApplicationController
before_filter :ensure_logged_in, except: [:show, :replies, :by_number, :short_link, :reply_history, :revisions, :expand_embed, :markdown, :raw, :cooked]
skip_before_filter :store_incoming_links, only: [:short_link]
skip_before_filter :check_xhr, only: [:markdown,:short_link]
skip_before_filter :check_xhr, only: [:markdown_id, :markdown_num, :short_link]
def markdown
post = Post.find_by(topic_id: params[:topic_id].to_i, post_number: (params[:post_number] || 1).to_i)
def markdown_id
markdown Post.find(params[:id].to_i)
end
def markdown_num
markdown Post.find_by(topic_id: params[:topic_id].to_i, post_number: (params[:post_number] || 1).to_i)
end
def markdown(post)
if post && guardian.can_see?(post)
render text: post.raw, content_type: 'text/plain'
else
@ -26,7 +33,13 @@ class PostsController < ApplicationController
def short_link
post = Post.find(params[:post_id].to_i)
IncomingLink.add(request,current_user)
# Stuff the user in the request object, because that's what IncomingLink wants
if params[:user_id]
user = User.find(params[:user_id].to_i)
request['u'] = user.username_lower if user
end
IncomingLink.add(request, current_user)
redirect_to post.url
end

View file

@ -27,7 +27,7 @@ class IncomingLink < ActiveRecord::Base
end
if host != request.host && (user_id || referer)
cid = current_user.id if current_user
cid = current_user ? (current_user.id) : (nil)
unless cid && cid == user_id
IncomingLink.create(url: request.url,
referer: referer,

View file

@ -246,8 +246,6 @@ Discourse::Application.routes.draw do
end
end
get "p/:post_id/:user_id" => "posts#short_link"
resources :notifications
match "/auth/:provider/callback", to: "users/omniauth_callbacks#complete", via: [:get, :post]
@ -365,9 +363,11 @@ Discourse::Application.routes.draw do
post "t/:topic_id/notifications" => "topics#set_notifications" , constraints: {topic_id: /\d+/}
get "p/:post_id(/:user_id)" => "posts#short_link"
get "/posts/:id/cooked" => "posts#cooked"
get "/posts/:id/expand-embed" => "posts#expand_embed"
get "raw/:topic_id(/:post_number)" => "posts#markdown"
get "/posts/:id/raw" => "posts#markdown_id"
get "raw/:topic_id(/:post_number)" => "posts#markdown_num"
resources :invites do
collection do

View file

@ -49,10 +49,11 @@ describe PostsController do
describe 'short_link' do
let(:post) { Fabricate(:post) }
let(:user) { Fabricate(:user) }
it 'logs the incoming link once' do
IncomingLink.expects(:add).once.returns(true)
get :short_link, post_id: post.id, user_id: 999
get :short_link, post_id: post.id, user_id: user.id
response.should be_redirect
end
end