FIX: identify slug-less topic urls everywhere

In 91c89df6, I fixed the onebox to support local topics with a slug-less URL.
This commit fixes all the other spots (search, topic links and user badges) where we look up for a local topic.

Follow-up-to: 91c89df6
This commit is contained in:
Régis Hanol 2020-06-29 12:31:20 +02:00
parent 0edffcc47d
commit 860deeb072
3 changed files with 24 additions and 14 deletions

View File

@ -65,10 +65,11 @@ class UserBadgesController < ApplicationController
end
if 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
end
end
end

View File

@ -276,20 +276,25 @@ class TopicLink < ActiveRecord::Base
internal = 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 = nil
end
end
@ -314,7 +319,7 @@ class TopicLink < ActiveRecord::Base
domain: 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,
)

View File

@ -247,8 +247,12 @@ class Search
single_topic(@term.to_i)
else
begin
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
end
rescue ActionController::RoutingError
end
end