diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 648c9b0912f..156bde81420 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 1241d5d1ba7..64252c45220 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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 diff --git a/app/models/incoming_link.rb b/app/models/incoming_link.rb index 99823b976e9..45d6513c60a 100644 --- a/app/models/incoming_link.rb +++ b/app/models/incoming_link.rb @@ -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, diff --git a/config/routes.rb b/config/routes.rb index f4b11bb3797..c7a5acddf7f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index fec4e21834d..c3aff3ab629 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -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