From 2766b2edc3fc68803d254d9039ca044e25a56d2e Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 19 Sep 2016 13:31:19 -0400 Subject: [PATCH] FIX: Allow redirection for slugs that start with digits --- app/controllers/topics_controller.rb | 7 +++++++ spec/controllers/topics_controller_spec.rb | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 95f26ce4ab7..d890410469a 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -60,6 +60,13 @@ class TopicsController < ApplicationController opts[:slow_platform] = true if slow_platform? opts[:username_filters] = username_filters.split(',') if username_filters.is_a?(String) + # Special case: a slug with a number in front should look by slug first before looking + # up that particular number + if params[:id] && params[:id] =~ /^\d+[^\d\\]+$/ + topic = Topic.find_by(slug: params[:id].downcase) + return redirect_to_correct_topic(topic, opts[:post_number]) if topic && topic.visible + end + begin @topic_view = TopicView.new(params[:id] || params[:topic_id], current_user, opts) rescue Discourse::NotFound diff --git a/spec/controllers/topics_controller_spec.rb b/spec/controllers/topics_controller_spec.rb index ecfd606346e..eddb73bd0ee 100644 --- a/spec/controllers/topics_controller_spec.rb +++ b/spec/controllers/topics_controller_spec.rb @@ -569,6 +569,15 @@ describe TopicsController do expect(response).to redirect_to(topic.relative_url) end + it 'can find a topic when a slug has a number in front' do + another_topic = Fabricate(:post).topic + + topic.update_column(:slug, "#{another_topic.id}-reasons-discourse-is-awesome") + xhr :get, :show, id: "#{another_topic.id}-reasons-discourse-is-awesome" + + expect(response).to redirect_to(topic.relative_url) + end + it 'keeps the post_number parameter around when redirecting' do xhr :get, :show, id: topic.slug, post_number: 42 expect(response).to redirect_to(topic.relative_url + "/42")