Skip to content

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

FIX: Tag search 500 when content_localization is enabled (#37769)

Natalie Tayf939c4ebac85
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index 42f6860c8b7..e98ec7e69f0 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -390,12 +390,7 @@ class TagsController < ::ApplicationController
tags_with_counts, filter_result_context =
DiscourseTagging.filter_allowed_tags(guardian, **filter_params, with_context: true)
- if SiteSetting.content_localization_enabled && tags_with_counts.present?
- ActiveRecord::Associations::Preloader.new(
- records: tags_with_counts,
- associations: :localizations,
- ).call
- end
+ tags_with_counts = Tag.with_localizations(tags_with_counts)
tags = self.class.tag_counts_json(tags_with_counts, guardian)
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 74f073f5077..9621d54a603 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -196,6 +196,13 @@ class Tag < ActiveRecord::Base
end
end
+ def self.with_localizations(tags)
+ return tags unless SiteSetting.content_localization_enabled && tags.present?
+ tag_ids = tags.map(&:id)
+ tags_by_id = where(id: tag_ids).includes(:localizations).index_by(&:id)
+ tag_ids.filter_map { |id| tags_by_id[id] }
+ end
+
def self.pm_tags(limit: 1000, guardian: nil, allowed_user: nil)
return [] if allowed_user.blank? || !(guardian || Guardian.new).can_tag_pms?
user_id = allowed_user.id
diff --git a/app/services/tag_hashtag_data_source.rb b/app/services/tag_hashtag_data_source.rb
index d713273dc78..f63a2719fc0 100644
--- a/app/services/tag_hashtag_data_source.rb
+++ b/app/services/tag_hashtag_data_source.rb
@@ -70,14 +70,12 @@ class TagHashtagDataSource
order_search_results: true,
)
+ tags_with_counts = Tag.with_localizations(tags_with_counts)
+
TagsController
.tag_counts_json(tags_with_counts, guardian)
.take(limit)
- .map do |tag|
- # We want the actual ID here not the `name` as tag_counts_json gives us.
- tag[:id] = tags_with_counts.find { |t| t.name == tag[:name] }.id
- tag_to_hashtag_item(tag, guardian)
- end
+ .map { |tag| tag_to_hashtag_item(tag, guardian) }
end
def self.search_sort(search_results, _)
@@ -94,6 +92,8 @@ class TagHashtagDataSource
excluded_tag_names: DiscourseTagging.muted_tags(guardian.user),
)
+ tags_with_counts = Tag.with_localizations(tags_with_counts)
+
TagsController
.tag_counts_json(tags_with_counts, guardian)
.take(limit)
diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb
index 82157ede056..398898a7fe3 100644
--- a/spec/models/tag_spec.rb
+++ b/spec/models/tag_spec.rb
@@ -478,6 +478,42 @@ RSpec.describe Tag do
end
end
+ describe ".with_localizations" do
+ fab!(:tag1) { Fabricate(:tag, name: "strategy", locale: "en") }
+ fab!(:tag2) { Fabricate(:tag, name: "design", locale: "en") }
+
+ it "returns tags unchanged when content_localization is disabled" do
+ SiteSetting.content_localization_enabled = false
+ tags = [tag1, tag2]
+ expect(Tag.with_localizations(tags)).to eq(tags)
+ end
+
+ it "returns empty input unchanged" do
+ SiteSetting.content_localization_enabled = true
+ expect(Tag.with_localizations([])).to eq([])
+ end
+
+ it "returns Tag AR instances with localizations preloaded" do
+ SiteSetting.content_localization_enabled = true
+ Fabricate(:tag_localization, tag: tag1, locale: "ja", name: "戦略")
+
+ result = Tag.with_localizations([tag1, tag2])
+
+ expect(result.map(&:id)).to eq([tag1.id, tag2.id])
+ expect(result.all? { |t| t.is_a?(Tag) }).to eq(true)
+ expect(result.first.localizations).to be_loaded
+ expect(result.first.localizations.first.name).to eq("戦略")
+ end
+
+ it "preserves order of input tags" do
+ SiteSetting.content_localization_enabled = true
+
+ result = Tag.with_localizations([tag2, tag1])
+
+ expect(result.map(&:id)).to eq([tag2.id, tag1.id])
+ end
+ end
+
describe "slug" do
it "generates slug from name on create" do
tag = Fabricate(:tag, name: "Hello World")
diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb
index bcdfd07f385..bfddb390869 100644
--- a/spec/requests/tags_controller_spec.rb
+++ b/spec/requests/tags_controller_spec.rb
@@ -1272,6 +1272,39 @@ RSpec.describe TagsController do
expect(response.status).to eq(200)
expect(response.parsed_body["results"].map { |t| t["name"] }).to contain_exactly(tag2.name)
end
+
+ context "with content_localization_enabled" do
+ fab!(:japanese_user) { Fabricate(:user, locale: "ja") }
+
+ before do
+ SiteSetting.content_localization_enabled = true
+ sign_in(japanese_user)
+ end
+
+ it "returns localized tag names when localizations exist for the user's locale" do
+ tag = Fabricate(:tag, name: "strategy", locale: "en", public_topic_count: 1)
+ Fabricate(:tag_localization, tag: tag, locale: "ja", name: "戦略")
+
+ get "/tags/filter/search.json", params: { q: "strat" }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["results"]).to include(
+ include("name" => "戦略", "id" => tag.id),
+ )
+ end
+
+ it "returns original tag names when no localization matches the user's locale" do
+ tag = Fabricate(:tag, name: "strategy", locale: "en", public_topic_count: 1)
+ Fabricate(:tag_localization, tag: tag, locale: "de", name: "stratégie")
+
+ get "/tags/filter/search.json", params: { q: "strat" }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["results"]).to include(
+ include("name" => "strategy", "id" => tag.id),
+ )
+ end
+ end
end
end
diff --git a/spec/services/tag_hashtag_data_source_spec.rb b/spec/services/tag_hashtag_data_source_spec.rb
index b84553ca999..032afdeebe8 100644
--- a/spec/services/tag_hashtag_data_source_spec.rb
+++ b/spec/services/tag_hashtag_data_source_spec.rb
@@ -75,4 +75,41 @@ RSpec.describe TagHashtagDataSource do
expect(described_class.search_without_term(guardian, 5).map(&:slug)).not_to include("factor")
end
end
+
+ context "with content_localization_enabled" do
+ before { SiteSetting.content_localization_enabled = true }
+
+ fab!(:ja_user) { Fabricate(:user, locale: "ja") }
+ let(:ja_guardian) { Guardian.new(ja_user) }
+
+ describe "#search" do
+ it "returns localized tag names" do
+ tag2.update!(locale: "en")
+ Fabricate(:tag_localization, tag: tag2, locale: "ja", name: "要因")
+
+ I18n.with_locale(:ja) do
+ results = described_class.search(ja_guardian, "fact", 5)
+ factor_result = results.find { |r| r.id == tag2.id }
+
+ expect(factor_result).to be_present
+ expect(factor_result.text).to eq("要因")
+ end
+ end
+ end
+
+ describe "#search_without_term" do
+ it "returns localized tag names" do
+ tag2.update!(locale: "en")
+ Fabricate(:tag_localization, tag: tag2, locale: "ja", name: "要因")
+
+ I18n.with_locale(:ja) do
+ results = described_class.search_without_term(ja_guardian, 5)
+ factor_result = results.find { |r| r.id == tag2.id }
+
+ expect(factor_result).to be_present
+ expect(factor_result.text).to eq("要因")
+ end
+ end
+ end
+ end
end
diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rb
index 373440771eb..709ca703d1f 100644
--- a/spec/system/content_localization_spec.rb
+++ b/spec/system/content_localization_spec.rb
@@ -534,6 +534,19 @@ describe "Content Localization" do
topic_list.visit_topic_with_title("Life strategies from The Art of War")
expect(page).to have_css(".title-wrapper .discourse-tag", text: "strategy")
end
+
+ it "displays localized tag names in the composer tag chooser" do
+ SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:everyone]
+ SiteSetting.create_topic_allowed_groups = Group::AUTO_GROUPS[:everyone]
+ mini_tag_chooser = PageObjects::Components::SelectKit.new(".mini-tag-chooser")
+
+ sign_in(japanese_user)
+ visit("/new-topic")
+ expect(composer).to be_opened
+ mini_tag_chooser.expand
+ mini_tag_chooser.search("戦")
+ expect(mini_tag_chooser).to have_option_name("戦略")
+ end
end
end