Skip to content

A platform for community discussion. Free, open, simple.

FIX: Filter orphaned TopicLinks and normalize http URLs to https (#37098)

Alan Guo Xiang Tan63ca5673df2e
diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb
index 702ce7f5342..7675576d8e6 100644
--- a/app/models/topic_link.rb
+++ b/app/models/topic_link.rb
@@ -48,6 +48,7 @@ class TopicLink < ActiveRecord::Base
FROM topic_links AS ftl
LEFT JOIN topics AS ft ON ftl.link_topic_id = ft.id
LEFT JOIN categories AS c ON c.id = ft.category_id
+ LEFT JOIN posts AS target_posts ON ftl.link_post_id = target_posts.id
/*where*/
GROUP BY ftl.url, ft.title, ftl.title, ftl.link_topic_id, ftl.reflection, ftl.internal, ftl.domain
ORDER BY clicks DESC, count(*) DESC
@@ -55,7 +56,12 @@ class TopicLink < ActiveRecord::Base
SQL
builder.where("ftl.topic_id = :topic_id", topic_id: topic_id)
- builder.where("ft.deleted_at IS NULL")
+ apply_link_visibility_filters(
+ builder,
+ link: "ftl",
+ target_topic: "ft",
+ target_posts: "target_posts",
+ )
builder.where("ftl.extension IS NULL OR ftl.extension NOT IN ('png','jpg','gif')")
builder.where(
"COALESCE(ft.archetype, 'regular') <> :archetype",
@@ -86,12 +92,18 @@ class TopicLink < ActiveRecord::Base
FROM topic_links l
LEFT JOIN topics t ON t.id = l.link_topic_id
LEFT JOIN categories AS c ON c.id = t.category_id
+ LEFT JOIN posts AS target_posts ON l.link_post_id = target_posts.id
/*left_join*/
/*where*/
ORDER BY reflection ASC, clicks DESC",
)
- builder.where("t.deleted_at IS NULL")
+ apply_link_visibility_filters(
+ builder,
+ link: "l",
+ target_topic: "t",
+ target_posts: "target_posts",
+ )
builder.where(
"COALESCE(t.archetype, 'regular') <> :archetype",
archetype: Archetype.private_message,
@@ -189,6 +201,14 @@ class TopicLink < ActiveRecord::Base
private
+ def self.apply_link_visibility_filters(builder, link:, target_topic:, target_posts:)
+ builder.where(<<~SQL)
+ #{target_topic}.deleted_at IS NULL
+ AND (#{link}.internal = false OR #{target_topic}.id IS NOT NULL)
+ AND (#{link}.link_post_id IS NULL OR (#{target_posts}.id IS NOT NULL AND #{target_posts}.deleted_at IS NULL))
+ SQL
+ end
+
# This pattern is used to create topic links very efficiently with minimal
# errors under heavy concurrent use
#
diff --git a/app/serializers/topic_link_serializer.rb b/app/serializers/topic_link_serializer.rb
index a31feaa2a84..b8f0e45e916 100644
--- a/app/serializers/topic_link_serializer.rb
+++ b/app/serializers/topic_link_serializer.rb
@@ -12,6 +12,14 @@ class TopicLinkSerializer < ApplicationSerializer
:domain,
:root_domain
+ def url
+ if object.internal && SiteSetting.force_https && object.url.start_with?("http://")
+ object.url.sub("http://", "https://")
+ else
+ object.url
+ end
+ end
+
def attachment
Discourse.store.has_been_uploaded?(object.url)
end
diff --git a/spec/fabricators/topic_link_click_fabricator.rb b/spec/fabricators/topic_link_click_fabricator.rb
new file mode 100644
index 00000000000..d02f1b37a81
--- /dev/null
+++ b/spec/fabricators/topic_link_click_fabricator.rb
@@ -0,0 +1,6 @@
+# frozen_string_literal: true
+
+Fabricator(:topic_link_click) do
+ topic_link
+ ip_address "192.168.1.1"
+end
diff --git a/spec/fabricators/topic_link_fabricator.rb b/spec/fabricators/topic_link_fabricator.rb
new file mode 100644
index 00000000000..38310a8dce7
--- /dev/null
+++ b/spec/fabricators/topic_link_fabricator.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+Fabricator(:topic_link) do
+ post
+ transient :link_topic
+ transient :link_post
+ topic_id { |attrs| attrs[:post].topic_id }
+ user_id { |attrs| attrs[:post].user_id }
+ url { |attrs| attrs[:link_topic]&.url || "https://example.com/page" }
+ domain "example.com"
+ internal { |attrs| attrs[:link_topic].present? || attrs[:link_post].present? }
+ link_topic_id { |attrs| attrs[:link_topic]&.id || attrs[:link_post]&.topic_id }
+ link_post_id { |attrs| attrs[:link_post]&.id }
+end
diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb
index 4c1dfcfe1f3..a20a8384c71 100644
--- a/spec/models/topic_link_spec.rb
+++ b/spec/models/topic_link_spec.rb
@@ -453,6 +453,80 @@ RSpec.describe TopicLink do
expect(TopicLink.counts_for(Guardian.new, nil, nil)).to be_blank
end
+ describe ".topic_map" do
+ fab!(:source_post, :post)
+ fab!(:target_topic, :topic)
+ fab!(:topic_link) { Fabricate(:topic_link, post: source_post, link_topic: target_topic) }
+ fab!(:topic_link_click) { Fabricate(:topic_link_click, topic_link: topic_link) }
+
+ it "returns topic links with clicks" do
+ result = TopicLink.topic_map(Guardian.new, source_post.topic_id).first
+ expect(result.url).to eq(topic_link.url)
+ expect(result.title).to eq(target_topic.title)
+ expect(result.link_topic_id).to eq(target_topic.id)
+ expect(result.internal).to eq(true)
+ expect(result.reflection).to eq(false)
+ expect(result.clicks).to eq(1)
+ end
+
+ it "excludes deleted target topics" do
+ target_topic.trash!
+ expect(TopicLink.topic_map(Guardian.new, source_post.topic_id)).to be_empty
+ end
+
+ it "excludes hard-deleted target topics" do
+ target_topic.destroy!
+ expect(TopicLink.topic_map(Guardian.new, source_post.topic_id)).to be_empty
+ end
+
+ it "excludes deleted target posts" do
+ target_post = Fabricate(:post)
+ topic_link.update!(link_post: target_post)
+ target_post.trash!
+ expect(TopicLink.topic_map(Guardian.new, source_post.topic_id)).to be_empty
+ end
+ end
+
+ describe ".counts_for" do
+ fab!(:source_post, :post)
+ fab!(:target_topic, :topic)
+ fab!(:topic_link) { Fabricate(:topic_link, post: source_post, link_topic: target_topic) }
+ fab!(:topic_link_click) { Fabricate(:topic_link_click, topic_link: topic_link) }
+
+ it "returns topic link data for posts" do
+ result =
+ TopicLink.counts_for(Guardian.new, source_post.topic, [source_post])[source_post.id].first
+ expect(result[:url]).to eq(topic_link.url)
+ expect(result[:title]).to eq(target_topic.title)
+ expect(result[:internal]).to eq(true)
+ expect(result[:reflection]).to eq(false)
+ expect(result[:clicks]).to eq(1)
+ end
+
+ it "excludes deleted target topics" do
+ target_topic.trash!
+ expect(
+ TopicLink.counts_for(Guardian.new, source_post.topic, [source_post])[source_post.id],
+ ).to be_blank
+ end
+
+ it "excludes hard-deleted target topics" do
+ target_topic.destroy!
+ expect(
+ TopicLink.counts_for(Guardian.new, source_post.topic, [source_post])[source_post.id],
+ ).to be_blank
+ end
+
+ it "excludes deleted target posts" do
+ target_post = Fabricate(:post)
+ topic_link.update!(link_post: target_post)
+ target_post.trash!
+ expect(
+ TopicLink.counts_for(Guardian.new, source_post.topic, [source_post])[source_post.id],
+ ).to be_blank
+ end
+ end
+
context "with data" do
let(:post) do
topic = Fabricate(:topic, user: Fabricate(:user, refresh_auto_groups: true))
diff --git a/spec/serializers/topic_link_serializer_spec.rb b/spec/serializers/topic_link_serializer_spec.rb
index 3e893ef4510..719d5dd4c45 100644
--- a/spec/serializers/topic_link_serializer_spec.rb
+++ b/spec/serializers/topic_link_serializer_spec.rb
@@ -9,4 +9,42 @@ RSpec.describe TopicLinkSerializer do
expect(serialized[:domain]).to eq("meta.discourse.org")
expect(serialized[:root_domain]).to eq("discourse.org")
end
+
+ describe "#url" do
+ it "normalizes internal http:// URLs to https:// when force_https is enabled" do
+ SiteSetting.force_https = true
+
+ topic_link =
+ Fabricate(:topic_link, url: "http://example.com/t/test-topic/123", internal: true)
+ serialized = described_class.new(topic_link, root: false).as_json
+
+ expect(serialized[:url]).to eq("https://example.com/t/test-topic/123")
+ end
+
+ it "does not modify external http:// URLs" do
+ SiteSetting.force_https = true
+
+ topic_link =
+ Fabricate(
+ :topic_link,
+ url: "http://external-site.com/page",
+ domain: "external-site.com",
+ internal: false,
+ )
+
+ serialized = described_class.new(topic_link, root: false).as_json
+
+ expect(serialized[:url]).to eq("http://external-site.com/page")
+ end
+
+ it "does not modify internal URLs when force_https is disabled" do
+ SiteSetting.force_https = false
+
+ topic_link =
+ Fabricate(:topic_link, url: "http://example.com/t/test-topic/123", internal: true)
+ serialized = described_class.new(topic_link, root: false).as_json
+
+ expect(serialized[:url]).to eq("http://example.com/t/test-topic/123")
+ end
+ end
end
diff --git a/spec/serializers/topic_view_serializer_spec.rb b/spec/serializers/topic_view_serializer_spec.rb
index f4a1a082c7c..aa621f5adce 100644
--- a/spec/serializers/topic_view_serializer_spec.rb
+++ b/spec/serializers/topic_view_serializer_spec.rb
@@ -375,8 +375,9 @@ RSpec.describe TopicViewSerializer do
context "with details" do
it "returns the details object" do
- PostCreator.create!(user, topic_id: topic.id, raw: "this is my post content")
+ post = PostCreator.create!(user, topic_id: topic.id, raw: "this is my post content")
topic.topic_links.create!(
+ post: post,
user: user,
url: "https://discourse.org",
domain: "discourse.org",