Skip to content

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

FIX: Redirect to the target tag when synonym is visited (#37672)

Natalie Tay127adedf3fd6
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index acd38334ee0..98d321bc61e 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -569,7 +569,7 @@ class TagsController < ::ApplicationController
def self.tag_counts_json(tags, guardian)
show_pm_tags = guardian.can_tag_pms?
- target_tags = Tag.where(id: tags.map(&:target_tag_id).compact.uniq).select(:id, :name)
+ target_tags = Tag.where(id: tags.map(&:target_tag_id).compact.uniq).select(:id, :name, :slug)
tags
.map do |t|
@@ -586,7 +586,10 @@ class TagsController < ::ApplicationController
count: topic_count,
pm_only: topic_count == 0 && t.pm_topic_count > 0,
target_tag:
- t.target_tag_id ? target_tags.find { |x| x.id == t.target_tag_id }&.name : nil,
+ if t.target_tag_id
+ target = target_tags.find { |x| x.id == t.target_tag_id }
+ target ? { id: target.id, name: target.name, slug: target.slug } : nil
+ end,
}
if show_pm_tags && SiteSetting.display_personal_messages_tag_counts
diff --git a/frontend/discourse/app/routes/tag/show.js b/frontend/discourse/app/routes/tag/show.js
index 6c10983fe7d..ec44a49d0c7 100644
--- a/frontend/discourse/app/routes/tag/show.js
+++ b/frontend/discourse/app/routes/tag/show.js
@@ -159,7 +159,10 @@ export default class TagShowRoute extends DiscourseRoute {
if (list.topic_list.tags && list.topic_list.tags.length >= 1) {
const mainTagData = list.topic_list.tags.find(
- (t) => t.name.toLowerCase() === slug.toLowerCase() || t.slug === slug
+ (t) =>
+ t.id === id ||
+ (!id &&
+ (t.name.toLowerCase() === slug.toLowerCase() || t.slug === slug))
);
if (mainTagData) {
tag.setProperties({
@@ -168,6 +171,24 @@ export default class TagShowRoute extends DiscourseRoute {
slug: mainTagData.slug,
staff: mainTagData.staff,
});
+ } else if (!additionalTags) {
+ // tag was a synonym, redirect to canonical tag URL
+ const canonicalTag = list.topic_list.tags[0];
+ const routeName = transition.to.name;
+ if (params.category_slug_path_with_id) {
+ return this.router.replaceWith(
+ routeName,
+ params.category_slug_path_with_id,
+ canonicalTag.slug,
+ canonicalTag.id
+ );
+ } else {
+ return this.router.replaceWith(
+ routeName,
+ canonicalTag.slug,
+ canonicalTag.id
+ );
+ }
}
if (additionalTags) {
diff --git a/frontend/discourse/select-kit/components/tag-drop.js b/frontend/discourse/select-kit/components/tag-drop.js
index 3cf2cb085c0..7557b2836f1 100644
--- a/frontend/discourse/select-kit/components/tag-drop.js
+++ b/frontend/discourse/select-kit/components/tag-drop.js
@@ -204,6 +204,9 @@ export default class TagDrop extends ComboBoxComponent {
content.count = r.count;
}
content.pmCount = r.pm_count;
+ if (r.target_tag) {
+ content.targetTag = r.target_tag;
+ }
return content;
});
}
@@ -216,6 +219,8 @@ export default class TagDrop extends ComboBoxComponent {
tagArg = NONE_TAG;
} else if (value === ALL_TAGS_ID) {
tagArg = null;
+ } else if (tag?.targetTag) {
+ tagArg = tag.targetTag;
} else if (tag) {
tagArg = tag;
}
diff --git a/frontend/discourse/tests/acceptance/sidebar-user-tags-section-test.js b/frontend/discourse/tests/acceptance/sidebar-user-tags-section-test.js
index fb037b135e6..912b33de1e5 100644
--- a/frontend/discourse/tests/acceptance/sidebar-user-tags-section-test.js
+++ b/frontend/discourse/tests/acceptance/sidebar-user-tags-section-test.js
@@ -55,10 +55,15 @@ acceptance("Sidebar - Logged on user - Tags section", function (needs) {
});
["latest", "top", "new", "unread", "hot"].forEach((type) => {
- server.get(`/tag/:tagId/l/${type}.json`, () => {
- return helper.response(
- cloneJSON(discoveryFixture["/tag/important/l/latest.json"])
+ server.get(`/tag/:tagId/l/${type}.json`, (request) => {
+ const json = cloneJSON(
+ discoveryFixture["/tag/important/l/latest.json"]
);
+ if (json.topic_list.tags?.[0]) {
+ json.topic_list.tags[0].id = parseInt(request.params.tagId, 10);
+ json.topic_list.tags[0].name = `tag${request.params.tagId}`;
+ }
+ return helper.response(json);
});
});
});
diff --git a/spec/system/tag_synonyms_spec.rb b/spec/system/tag_synonyms_spec.rb
index 5661260fbbf..3c01c8afc01 100644
--- a/spec/system/tag_synonyms_spec.rb
+++ b/spec/system/tag_synonyms_spec.rb
@@ -8,6 +8,44 @@ describe "Tag synonyms", type: :system do
before { sign_in(current_user) }
+ describe "when navigating to a synonym tag" do
+ fab!(:target_tag) { Fabricate(:tag, name: "broccoli") }
+ fab!(:synonym_tag) { Fabricate(:tag, name: "cauliflower", target_tag:) }
+ fab!(:target_tagged_topic) { Fabricate(:topic, tags: [target_tag]) }
+
+ let(:topic_list) { PageObjects::Components::TopicList.new }
+
+ it "redirects to the target tag" do
+ tags_page.visit_tag(tag_2)
+
+ tags_page.tags_dropdown.expand
+ tags_page.tags_dropdown.search(synonym_tag.name)
+ tags_page.tags_dropdown.select_row_by_name(synonym_tag.name)
+
+ expect(topic_list).to have_topic(target_tagged_topic)
+ expect(page).to have_current_path("/tag/#{target_tag.name}/#{target_tag.id}")
+ expect(tags_page.tags_dropdown).to have_selected_name(target_tag.name)
+ end
+
+ it "redirects to the target tag when within a category" do
+ category = Fabricate(:category)
+ target_tagged_topic.update!(category: category)
+
+ page.visit("/tags/c/#{category.slug}/#{category.id}/#{tag_2.name}")
+ expect(page).to have_css(".tag-drop")
+
+ tags_page.tags_dropdown.expand
+ tags_page.tags_dropdown.search(synonym_tag.name)
+ tags_page.tags_dropdown.select_row_by_name(synonym_tag.name)
+
+ expect(topic_list).to have_topic(target_tagged_topic)
+ expect(page).to have_current_path(
+ "/tags/c/#{category.slug}/#{category.id}/#{target_tag.name}/#{target_tag.id}",
+ )
+ expect(tags_page.tags_dropdown).to have_selected_name(target_tag.name)
+ end
+ end
+
describe "when visiting edit tag page" do
it "allows an admin to add existing tag as a synonym" do
tags_page.visit_tag(tag_1)