FIX: inbound link when the only slug available (#8457)
Problem mentioned in [meta](https://meta.discourse.org/t/inbound-links-dont-show-up-when-topic-id-is-not-included/100551) When there is an internal link without ID, only slug, we should still try to create reflection link.
This commit is contained in:
parent
5e134400e2
commit
46fc45de99
|
@ -172,6 +172,7 @@ class TopicLink < ActiveRecord::Base
|
||||||
internal = false
|
internal = false
|
||||||
topic_id = nil
|
topic_id = nil
|
||||||
post_number = nil
|
post_number = nil
|
||||||
|
topic = nil
|
||||||
|
|
||||||
if upload = Upload.get_from_url(url)
|
if upload = Upload.get_from_url(url)
|
||||||
internal = Discourse.store.internal?
|
internal = Discourse.store.internal?
|
||||||
|
@ -185,9 +186,11 @@ class TopicLink < ActiveRecord::Base
|
||||||
|
|
||||||
topic_id = route[:topic_id].to_i
|
topic_id = route[:topic_id].to_i
|
||||||
post_number = route[:post_number] || 1
|
post_number = route[:post_number] || 1
|
||||||
|
topic_slug = route[:id]
|
||||||
|
|
||||||
# Store the canonical URL
|
# Store the canonical URL
|
||||||
topic = Topic.find_by(id: topic_id)
|
topic = Topic.find_by(id: topic_id)
|
||||||
|
topic ||= Topic.find_by(slug: topic_slug) if topic_slug
|
||||||
topic_id = nil unless topic
|
topic_id = nil unless topic
|
||||||
|
|
||||||
if topic.present?
|
if topic.present?
|
||||||
|
@ -197,11 +200,11 @@ class TopicLink < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
# Skip linking to ourselves
|
# Skip linking to ourselves
|
||||||
return nil if topic_id == post.topic_id
|
return nil if topic&.id == post.topic_id
|
||||||
|
|
||||||
reflected_post = nil
|
reflected_post = nil
|
||||||
if post_number && topic_id
|
if post_number && topic
|
||||||
reflected_post = Post.find_by(topic_id: topic_id, post_number: post_number.to_i)
|
reflected_post = Post.find_by(topic_id: topic.id, post_number: post_number.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
url = url[0...TopicLink.max_url_length]
|
url = url[0...TopicLink.max_url_length]
|
||||||
|
@ -216,7 +219,7 @@ class TopicLink < ActiveRecord::Base
|
||||||
url: url,
|
url: url,
|
||||||
domain: parsed.host || Discourse.current_hostname,
|
domain: parsed.host || Discourse.current_hostname,
|
||||||
internal: internal,
|
internal: internal,
|
||||||
link_topic_id: topic_id,
|
link_topic_id: topic&.id,
|
||||||
link_post_id: reflected_post.try(:id),
|
link_post_id: reflected_post.try(:id),
|
||||||
quote: link.is_quote,
|
quote: link.is_quote,
|
||||||
extension: file_extension)
|
extension: file_extension)
|
||||||
|
@ -228,31 +231,27 @@ class TopicLink < ActiveRecord::Base
|
||||||
reflected_id = nil
|
reflected_id = nil
|
||||||
|
|
||||||
# Create the reflection if we can
|
# Create the reflection if we can
|
||||||
if topic_id.present?
|
if topic && post.topic && topic.archetype != 'private_message' && post.topic.archetype != 'private_message' && post.topic.visible?
|
||||||
topic = Topic.find_by(id: topic_id)
|
prefix = Discourse.base_url_no_prefix
|
||||||
|
reflected_url = "#{prefix}#{post.topic.relative_url(post.post_number)}"
|
||||||
|
tl = TopicLink.find_by(topic_id: topic&.id,
|
||||||
|
post_id: reflected_post&.id,
|
||||||
|
url: reflected_url)
|
||||||
|
|
||||||
if topic && post.topic && topic.archetype != 'private_message' && post.topic.archetype != 'private_message' && post.topic.visible?
|
unless tl
|
||||||
prefix = Discourse.base_url_no_prefix
|
tl = TopicLink.create(user_id: post.user_id,
|
||||||
reflected_url = "#{prefix}#{post.topic.relative_url(post.post_number)}"
|
topic_id: topic&.id,
|
||||||
tl = TopicLink.find_by(topic_id: topic_id,
|
post_id: reflected_post&.id,
|
||||||
post_id: reflected_post.try(:id),
|
url: reflected_url,
|
||||||
url: reflected_url)
|
domain: Discourse.current_hostname,
|
||||||
|
reflection: true,
|
||||||
|
internal: true,
|
||||||
|
link_topic_id: post.topic_id,
|
||||||
|
link_post_id: post.id)
|
||||||
|
|
||||||
unless tl
|
|
||||||
tl = TopicLink.create(user_id: post.user_id,
|
|
||||||
topic_id: topic_id,
|
|
||||||
post_id: reflected_post.try(:id),
|
|
||||||
url: reflected_url,
|
|
||||||
domain: Discourse.current_hostname,
|
|
||||||
reflection: true,
|
|
||||||
internal: true,
|
|
||||||
link_topic_id: post.topic_id,
|
|
||||||
link_post_id: post.id)
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
reflected_id = tl.id if tl.persisted?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
reflected_id = tl.id if tl.persisted?
|
||||||
end
|
end
|
||||||
|
|
||||||
[url, reflected_id]
|
[url, reflected_id]
|
||||||
|
|
|
@ -144,6 +144,27 @@ describe TopicLink do
|
||||||
linked_post.revise(post.user, raw: "no more linkies https://eviltrout.com")
|
linked_post.revise(post.user, raw: "no more linkies https://eviltrout.com")
|
||||||
expect(other_topic.reload.topic_links.where(link_post_id: linked_post.id)).to be_blank
|
expect(other_topic.reload.topic_links.where(link_post_id: linked_post.id)).to be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'works without id' do
|
||||||
|
post
|
||||||
|
url = "http://#{test_uri.host}/t/#{other_topic.slug}"
|
||||||
|
topic.posts.create(user: user, raw: 'initial post')
|
||||||
|
linked_post = topic.posts.create(user: user, raw: "Link to another topic: #{url}")
|
||||||
|
|
||||||
|
TopicLink.extract_from(linked_post)
|
||||||
|
link = topic.topic_links.first
|
||||||
|
|
||||||
|
reflection = other_topic.topic_links.first
|
||||||
|
|
||||||
|
expect(reflection).to be_present
|
||||||
|
expect(reflection).to be_reflection
|
||||||
|
expect(reflection.post_id).to be_present
|
||||||
|
expect(reflection.domain).to eq(test_uri.host)
|
||||||
|
expect(reflection.url).to eq("http://#{test_uri.host}/t/unique-topic-name/#{topic.id}/#{linked_post.post_number}")
|
||||||
|
expect(reflection.link_topic_id).to eq(topic.id)
|
||||||
|
expect(reflection.link_post_id).to eq(linked_post.id)
|
||||||
|
expect(reflection.user_id).to eq(link.user_id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "link to a user on discourse" do
|
context "link to a user on discourse" do
|
||||||
|
|
Loading…
Reference in New Issue