discourse/spec/fabricators/topic_link_fabricator.rb
Alan Guo Xiang Tan 63ca5673df
FIX: Filter orphaned TopicLinks and normalize http URLs to https (#37098)
What is the problem?

`TopicLink` records can reference deleted topics or posts. When a topic
or the target post being linked to is deleted, these orphaned links
continue to appear in topic maps and post link counts.

Note that `TopicLink` records are already deleted when the source post
containing the link is trashed (see `Post#trash!`), so we only need to
filter for deleted target topics/posts.

Additionally, internal links stored with http:// are not normalized to
https:// when `force_https` site setting is enabled.

---

What is the solution?

Extend filtering in `TopicLink.topic_map` and `TopicLink.counts_for` to
exclude links where the target topic or target post is deleted. Extract
common visibility filters into `TopicLink.apply_link_visibility_filters`
helper method.

Normalize internal http:// URLs to https:// in `TopicLinkSerializer#url`
when `force_https` is enabled.
2026-01-15 12:12:34 +08:00

14 lines
511 B
Ruby

# 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