A platform for community discussion. Free, open, simple.
FIX: identify slug-less topic urls everywhere
Régis Hanol860deeb07285
diff --git a/app/controllers/user_badges_controller.rb b/app/controllers/user_badges_controller.rbindex 0abbfecdd38..2771e443f1b 100644--- a/app/controllers/user_badges_controller.rb+++ b/app/controllers/user_badges_controller.rb@@ -65,10 +65,11 @@ class UserBadgesController < ApplicationControllerendif route = Discourse.route_for(params[:reason])- topic_id = route[:topic_id].to_i- post_number = route[:post_number] || 1-- post_id = Post.find_by(topic_id: topic_id, post_number: post_number).try(:id) if topic_id > 0+ if route[:controller] == "topics" && route[:action] == "show"+ topic_id = (route[:id] || route[:topic_id]).to_i+ post_number = route[:post_number] || 1+ post_id = Post.find_by(topic_id: topic_id, post_number: post_number)&.id if topic_id > 0+ endendenddiff --git a/app/models/topic_link.rb b/app/models/topic_link.rbindex 3f992d9f8ff..134bde3b697 100644--- a/app/models/topic_link.rb+++ b/app/models/topic_link.rb@@ -276,20 +276,25 @@ class TopicLink < ActiveRecord::Baseinternal = true# We aren't interested in tracking internal links to users- return nil if route[:controller] == 'users'+ return nil if route[:controller] == "users"- topic_id = route[:topic_id].to_i+ topic_id = route[:topic_id]+ topic_slug = route[:slug]post_number = route[:post_number] || 1- topic_slug = route[:id]- # Store the canonical URL- topic = Topic.find_by(id: topic_id)- topic ||= Topic.find_by(slug: topic_slug) if topic_slug- topic_id = nil unless topic+ if route[:controller] == "topics" && route[:action] == "show"+ topic_id ||= route[:id]+ topic_slug ||= route[:id]+ end++ topic = Topic.find_by(id: topic_id) if topic_id+ topic ||= Topic.find_by(slug: topic_slug) if topic_slug.present?if topic.present?url = +"#{Discourse.base_url_no_prefix}#{topic.relative_url}"url << "/#{post_number}" if post_number.to_i > 1+ else+ topic_id = nilendend@@ -314,7 +319,7 @@ class TopicLink < ActiveRecord::Basedomain: parsed.host,internal: internal,link_topic_id: topic&.id,- link_post_id: reflected_post.try(:id),+ link_post_id: reflected_post&.id,quote: link.is_quote,extension: file_extension,)diff --git a/lib/search.rb b/lib/search.rbindex d95ec378c93..9b593d8d155 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -247,8 +247,12 @@ class Searchsingle_topic(@term.to_i)elsebegin- route = Rails.application.routes.recognize_path(@term)- single_topic(route[:topic_id]) if route[:topic_id].present?+ if route = Discourse.route_for(@term)+ if route[:controller] == "topics" && route[:action] == "show"+ topic_id = (route[:id] || route[:topic_id]).to_i+ single_topic(topic_id) if topic_id > 0+ end+ endrescue ActionController::RoutingErrorendend