A platform for community discussion. Free, open, simple.
FEATURE: Switch from opt-in to opt-out for categories in AI translations (#40169)
Penar Musaraj90baea1ea789
diff --git a/db/structure.sql b/db/structure.sqlindex 0f6d2118587..0ed4c3e99ad 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -21054,6 +21054,7 @@ INSERT INTO "schema_migrations" (version) VALUES('20260518054805'),('20260514055648'),('20260514043815'),+('20260513162531'),('20260513105516'),('20260513101242'),('20260513055222'),diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-translations.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-translations.gjsindex 8dfd121e167..7933c16d676 100644--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-translations.gjs+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-translations.gjs@@ -48,23 +48,24 @@ export default class AiTranslations extends Component {@tracked isTogglingTranslation = false;@tracked creditStatus = null;@tracked creditCheckComplete = false;- @tracked selectedCategories = [];- @tracked originalCategoryIds = this.args.model?.target_category_ids || [];+ @tracked excludedCategories = [];+ @tracked+ originalExcludedCategoryIds = this.args.model?.excluded_category_ids || [];hourlyRate = this.args.model?.hourly_rate || 0;constructor() {super(...arguments);this._checkCredits();- this._loadTargetCategories();+ this._loadExcludedCategories();if (this.enabled) {this._loadProgress();}}- async _loadTargetCategories() {- const ids = this.args.model?.target_category_ids || [];+ async _loadExcludedCategories() {+ const ids = this.args.model?.excluded_category_ids || [];if (ids.length) {- this.selectedCategories = await Category.asyncFindByIds(ids);+ this.excludedCategories = await Category.asyncFindByIds(ids);}}@@ -128,10 +129,10 @@ export default class AiTranslations extends Component {}get categoriesChanged() {- const current = [...this.selectedCategories.map((c) => c.id)]+ const current = [...this.excludedCategories.map((c) => c.id)].sort().join("|");- const original = [...this.originalCategoryIds].sort().join("|");+ const original = [...this.originalExcludedCategoryIds].sort().join("|");return current !== original;}@@ -220,22 +221,22 @@ export default class AiTranslations extends Component {}@action- updateSelectedCategories(categories) {- this.selectedCategories = categories;+ updateExcludedCategories(categories) {+ this.excludedCategories = categories;}@actionasync saveCategories() {this.isSavingCategories = true;try {- const ids = this.selectedCategories.map((c) => c.id);- await ajax("/admin/site_settings/ai_translation_target_categories", {+ const ids = this.excludedCategories.map((c) => c.id);+ await ajax("/admin/site_settings/ai_translation_excluded_categories", {type: "PUT",data: {- ai_translation_target_categories: ids.join("|"),+ ai_translation_excluded_categories: ids.join("|"),},});- this.originalCategoryIds = ids;+ this.originalExcludedCategoryIds = ids;} catch (e) {popupAjaxError(e);} finally {@@ -245,14 +246,14 @@ export default class AiTranslations extends Component {@actionasync cancelCategories() {- this.selectedCategories = await Category.asyncFindByIds(- this.originalCategoryIds+ this.excludedCategories = await Category.asyncFindByIds(+ this.originalExcludedCategoryIds);}@actionresetCategories() {- this.selectedCategories = [];+ this.excludedCategories = [];}@action@@ -576,14 +577,14 @@ export default class AiTranslations extends Component {<div class="setting"><div class="setting-label"><label>{{i18n- "discourse_ai.translations.translatable_categories"+ "discourse_ai.translations.excluded_categories"}}</label></div><div class="setting-value"><div class="ai-translations__category-input-row"><CategorySelector- @categories={{this.selectedCategories}}- @onChange={{this.updateSelectedCategories}}+ @categories={{this.excludedCategories}}+ @onChange={{this.updateExcludedCategories}}/>{{#if this.categoriesChanged}}<div class="setting-controls">@@ -602,7 +603,7 @@ export default class AiTranslations extends Component {class="cancel setting-controls__cancel"/></div>- {{else if this.selectedCategories.length}}+ {{else if this.excludedCategories.length}}<DButton@action={{this.resetCategories}}@icon="arrow-rotate-left"@@ -612,7 +613,7 @@ export default class AiTranslations extends Component {{{/if}}</div><div class="desc">{{i18n- "discourse_ai.translations.translatable_categories_description"+ "discourse_ai.translations.excluded_categories_description"}}</div></div></div>diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rbindex ded1a13a4a8..140393132b4 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_translations_controller.rb@@ -39,8 +39,7 @@ module DiscourseAitranslation_enabled: SiteSetting.ai_translation_enabled,hourly_rate: SiteSetting.ai_translation_backfill_hourly_rate,backfill_max_age_days: SiteSetting.ai_translation_backfill_max_age_days,- target_category_ids:- SiteSetting.ai_translation_target_categories.to_s.split("|").map(&:to_i),+ excluded_category_ids: DiscourseAi::Translation.excluded_category_ids,}endenddiff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rbindex e7a95948766..3d018bbbbdd 100644--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb@@ -37,9 +37,7 @@ module Jobsreturnendelse- target_category_ids = SiteSetting.ai_translation_target_categories- return if target_category_ids.blank?- return if target_category_ids.split("|").map(&:to_i).exclude?(topic.category_id)+ return if DiscourseAi::Translation.category_excluded?(topic.category_id)end# the user may fill locale in manuallydiff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rbindex 47db78227c6..eb906cb0d0a 100644--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb@@ -34,9 +34,7 @@ module Jobsreturnendelse- target_category_ids = SiteSetting.ai_translation_target_categories- return if target_category_ids.blank?- return if target_category_ids.split("|").map(&:to_i).exclude?(topic.category_id)+ return if DiscourseAi::Translation.category_excluded?(topic.category_id)endif (detected_locale = topic.locale).blank?diff --git a/plugins/discourse-ai/app/jobs/scheduled/categories_locale_detection_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/categories_locale_detection_backfill.rbindex 04021d5e954..a7f659c6079 100644--- a/plugins/discourse-ai/app/jobs/scheduled/categories_locale_detection_backfill.rb+++ b/plugins/discourse-ai/app/jobs/scheduled/categories_locale_detection_backfill.rb@@ -19,13 +19,9 @@ module Jobsreturnend- target_category_ids = SiteSetting.ai_translation_target_categories- if target_category_ids.present?- categories =- Category.where(locale: nil).where(id: target_category_ids.split("|").map(&:to_i))- else- return- end+ categories = Category.where(locale: nil)+ excluded_category_ids = DiscourseAi::Translation.excluded_category_ids+ categories = categories.where.not(id: excluded_category_ids) if excluded_category_ids.present?limit = SiteSetting.ai_translation_backfill_hourly_ratecategories = categories.limit(limit)diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.jsindex 5729d4c15c1..7ef25edf4e5 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js@@ -166,7 +166,7 @@ export const AI_FEATURE_SETTING_GROUPS = {"discourse_ai.features.translation.setting_groups.backfill_and_limits",settings: ["ai_translation_backfill_hourly_rate",- "ai_translation_target_categories",+ "ai_translation_excluded_categories","ai_translation_personal_messages","ai_translation_include_bot_content","ai_translation_max_post_length",diff --git a/plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scss b/plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scssindex 88ddad453f4..82768bfa556 100644--- a/plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scss+++ b/plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scss@@ -32,6 +32,16 @@float: none;width: auto;margin-right: 0;+ padding-right: 0;+ }++ .setting-value {+ max-width: 100%;++ .select-kit {+ // Not ideal, but upstream .select-kit gets a width: 100% !important that is too eager+ max-width: calc(100% - 100px);+ }}}}diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 562c635bd39..d332547f3f5 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -384,8 +384,8 @@ en:description: "Automatically translate all content on your forum to the user's preferred language"supported_locales: "Supported languages"supported_locales_description: "This site setting also allows visitors to contribute post translations."- translatable_categories: "Translatable categories"- translatable_categories_description: "Only content in the selected categories will be translated. Subcategories must be added separately."+ excluded_categories: "Excluded categories"+ excluded_categories_description: "Content in these categories will not be translated. Leave empty to translate content in all categories, including private categories."admin_actions:translation_settings: "Translation settings"localization_settings: "Localization settings"diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex 36ca3d7c933..8c691a2e488 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -148,7 +148,7 @@ en:ai_discord_allowed_guilds: "Discord guilds (servers) where the bot is allowed to search"ai_translation_enabled: "Enable the AI translation feature"- ai_translation_target_categories: "Only content in the selected categories will be translated. Subcategories must be added separately. Leave empty to disable translation of topics in categories."+ ai_translation_excluded_categories: "Content in these categories will not be translated. Leave empty to translate content in all categories, including private categories."ai_translation_personal_messages: "Controls which personal messages are translated. 'none' disables PM translation. 'group' translates only group PMs. 'all' translates all PMs."ai_translation_include_bot_content: "When enabled, content written by bots (user IDs below 0) will also be sent for translation."ai_translation_max_post_length: "The maximum number of characters for a post to be translated. Posts longer than this will not be translated."diff --git a/plugins/discourse-ai/config/settings.yml b/plugins/discourse-ai/config/settings.ymlindex 94269e30ada..a7136b75ff3 100644--- a/plugins/discourse-ai/config/settings.yml+++ b/plugins/discourse-ai/config/settings.yml@@ -491,7 +491,7 @@ discourse_ai:max: 100000client: falsearea: "ai-features/translation"- ai_translation_target_categories:+ ai_translation_excluded_categories:type: category_listdefault: ""client: falsediff --git a/plugins/discourse-ai/db/migrate/20260513162531_convert_ai_translation_target_categories_to_excluded_categories.rb b/plugins/discourse-ai/db/migrate/20260513162531_convert_ai_translation_target_categories_to_excluded_categories.rbnew file mode 100644index 00000000000..aaa83a12090--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20260513162531_convert_ai_translation_target_categories_to_excluded_categories.rb@@ -0,0 +1,37 @@+# frozen_string_literal: true++class ConvertAiTranslationTargetCategoriesToExcludedCategories < ActiveRecord::Migration[8.0]+ OLD_SETTING = "ai_translation_target_categories"+ NEW_SETTING = "ai_translation_excluded_categories"+ CATEGORY_LIST_DATA_TYPE = 20++ def up+ old_value =+ DB.query_single("SELECT value FROM site_settings WHERE name = '#{OLD_SETTING}' LIMIT 1").first+ ai_translation_enabled =+ DB.query_single(+ "SELECT value FROM site_settings WHERE name = 'ai_translation_enabled' LIMIT 1",+ ).first == "t"++ execute "DELETE FROM site_settings WHERE name = '#{NEW_SETTING}'"++ if Migration::Helpers.existing_site?+ all_category_ids = DB.query_single("SELECT id FROM categories").map(&:to_i)+ target_category_ids = old_value.to_s.split("|").filter_map { |id| id.presence&.to_i }+ excluded_category_ids = all_category_ids - target_category_ids++ if excluded_category_ids.present? && (target_category_ids.present? || ai_translation_enabled)+ execute <<~SQL+ INSERT INTO site_settings (name, data_type, value, created_at, updated_at)+ VALUES ('#{NEW_SETTING}', #{CATEGORY_LIST_DATA_TYPE}, '#{excluded_category_ids.join("|")}', NOW(), NOW())+ SQL+ end+ end++ execute "DELETE FROM site_settings WHERE name = '#{OLD_SETTING}'"+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/plugins/discourse-ai/lib/translation.rb b/plugins/discourse-ai/lib/translation.rbindex 6d6d1fba544..df155fda6ca 100644--- a/plugins/discourse-ai/lib/translation.rb+++ b/plugins/discourse-ai/lib/translation.rb@@ -38,6 +38,18 @@ module DiscourseAiSiteSetting.ai_translation_backfill_max_age_days > 0end+ # AI translation intentionally treats every category, including read-restricted+ # categories, as eligible unless admins add it to the exclusion setting.+ def self.excluded_category_ids+ SiteSetting.ai_translation_excluded_categories.to_s.split("|").map(&:to_i)+ end++ def self.category_excluded?(category_id)+ return true if category_id.blank?++ excluded_category_ids.include?(category_id)+ end+def self.llm_model_for_agent(agent_id)return nil if agent_id.blank?diff --git a/plugins/discourse-ai/lib/translation/category_candidates.rb b/plugins/discourse-ai/lib/translation/category_candidates.rbindex 0980197f804..ba017ade325 100644--- a/plugins/discourse-ai/lib/translation/category_candidates.rb+++ b/plugins/discourse-ai/lib/translation/category_candidates.rb@@ -8,12 +8,11 @@ module DiscourseAi# all categories that are eligible for translation based on site settings,# including those without locale detected yet.def self.get- target_category_ids = SiteSetting.ai_translation_target_categories- if target_category_ids.present?- Category.where(id: target_category_ids.split("|").map(&:to_i))- else- Category.none- end+ categories = Category.all+ excluded_category_ids = DiscourseAi::Translation.excluded_category_ids+ categories =+ categories.where.not(id: excluded_category_ids) if excluded_category_ids.present?+ categoriesenddef self.calculate_completion_per_locale(locale)diff --git a/plugins/discourse-ai/lib/translation/post_candidates.rb b/plugins/discourse-ai/lib/translation/post_candidates.rbindex fcd2cce4f01..a9510b61d2d 100644--- a/plugins/discourse-ai/lib/translation/post_candidates.rb+++ b/plugins/discourse-ai/lib/translation/post_candidates.rb@@ -8,8 +8,7 @@ module DiscourseAi# Also returns aggregate counts for total eligible posts and posts with detected locale.# @return [Hash] a hash with keys :translation_progress (array), :total (integer), and :posts_with_detected_locale (integer)def self.get_completion_all_locales- cache_key = "ai-translations-progress-#{SiteSetting.content_localization_supported_locales}"- Discourse.cache.fetch(cache_key, expires_in: 30.minutes) { completion_all_locales }+ Discourse.cache.fetch(progress_cache_key, expires_in: 30.minutes) { completion_all_locales }enddef self.needs_localization(limit:)@@ -60,20 +59,24 @@ module DiscourseAiposts = posts.joins(:topic)- target_category_ids = SiteSetting.ai_translation_target_categories+ # if no categories are excluded, posts from all categories will be sent for translation+ # private categories need to be explicitly excluded+ excluded_category_ids = DiscourseAi::Translation.excluded_category_idspm_scope = SiteSetting.ai_translation_personal_messages- # Category filter: include target categories + PMs (PMs filtered in next step)- if target_category_ids.present?- category_ids = target_category_ids.split("|").map(&:to_i)+ if excluded_category_ids.present?posts =posts.where(- "topics.category_id IN (:cats) OR topics.archetype = :pm",- cats: category_ids,+ "topics.category_id NOT IN (:cats) OR topics.archetype = :pm",+ cats: excluded_category_ids,pm: Archetype.private_message,)else- posts = posts.where(topics: { archetype: Archetype.private_message })+ posts =+ posts.where(+ "topics.category_id IS NOT NULL OR topics.archetype = :pm",+ pm: Archetype.private_message,+ )end# PM scope filter@@ -105,6 +108,18 @@ module DiscourseAipostsend+ def self.progress_cache_key+ [+ "ai-translations-progress",+ SiteSetting.content_localization_supported_locales,+ SiteSetting.ai_translation_backfill_max_age_days,+ SiteSetting.ai_translation_include_bot_content,+ SiteSetting.ai_translation_max_post_length,+ SiteSetting.ai_translation_personal_messages,+ DiscourseAi::Translation.excluded_category_ids.sort.join(","),+ ].join(":")+ end+def self.completion_all_localessupported = SiteSetting.content_localization_supported_locales.split("|")values_rows = supported.map { |loc| "('#{loc}')" }.join(", ")diff --git a/plugins/discourse-ai/lib/translation/topic_candidates.rb b/plugins/discourse-ai/lib/translation/topic_candidates.rbindex 0bb056239f1..430e87cd3ed 100644--- a/plugins/discourse-ai/lib/translation/topic_candidates.rb+++ b/plugins/discourse-ai/lib/translation/topic_candidates.rb@@ -45,20 +45,22 @@ module DiscourseAitopics =topics.where("topics.user_id > 0") unless SiteSetting.ai_translation_include_bot_content- target_category_ids = SiteSetting.ai_translation_target_categories+ excluded_category_ids = DiscourseAi::Translation.excluded_category_idspm_scope = SiteSetting.ai_translation_personal_messages- # Category filter: include target categories + PMs (PMs filtered in next step)- if target_category_ids.present?- category_ids = target_category_ids.split("|").map(&:to_i)+ if excluded_category_ids.present?topics =topics.where(- "topics.category_id IN (:cats) OR topics.archetype = :pm",- cats: category_ids,+ "topics.category_id NOT IN (:cats) OR topics.archetype = :pm",+ cats: excluded_category_ids,pm: Archetype.private_message,)else- topics = topics.where(archetype: Archetype.private_message)+ topics =+ topics.where(+ "topics.category_id IS NOT NULL OR topics.archetype = :pm",+ pm: Archetype.private_message,+ )end# PM scope filterdiff --git a/plugins/discourse-ai/spec/db/migrate/20260513162531_convert_ai_translation_target_categories_to_excluded_categories_spec.rb b/plugins/discourse-ai/spec/db/migrate/20260513162531_convert_ai_translation_target_categories_to_excluded_categories_spec.rbnew file mode 100644index 00000000000..6f9ecc0fede--- /dev/null+++ b/plugins/discourse-ai/spec/db/migrate/20260513162531_convert_ai_translation_target_categories_to_excluded_categories_spec.rb@@ -0,0 +1,137 @@+# frozen_string_literal: true++require Rails.root.join(+ "plugins/discourse-ai/db/migrate/20260513162531_convert_ai_translation_target_categories_to_excluded_categories",+ )++RSpec.describe ConvertAiTranslationTargetCategoriesToExcludedCategories do+ subject(:migration) { described_class.new }++ let(:connection) { ActiveRecord::Base.connection }++ fab!(:category_1, :category)+ fab!(:category_2, :category)+ fab!(:category_3, :category)++ around { |example| ActiveRecord::Migration.suppress_messages { example.run } }++ before do+ enable_current_plugin+ delete_settings+ end++ after { delete_settings }++ def delete_settings+ connection.execute(+ "DELETE FROM site_settings WHERE name IN ('ai_translation_target_categories', 'ai_translation_excluded_categories', 'ai_translation_enabled')",+ )+ end++ def store_setting(name, val, data_type: 20)+ connection.execute <<~SQL+ INSERT INTO site_settings(name, data_type, value, created_at, updated_at)+ VALUES ('#{name}', #{data_type}, '#{val}', NOW(), NOW())+ SQL+ end++ def setting_value(name)+ DB.query_single("SELECT value FROM site_settings WHERE name = '#{name}'").first+ end++ def setting_exists?(name)+ DB.query_single("SELECT COUNT(*) FROM site_settings WHERE name = '#{name}'").first > 0+ end++ def all_category_ids+ DB.query_single("SELECT id FROM categories").map(&:to_i)+ end++ describe "#up" do+ before { Migration::Helpers.stubs(:existing_site?).returns(true) }++ it "converts target categories to excluded categories for existing sites" do+ store_setting("ai_translation_target_categories", "#{category_1.id}|#{category_3.id}")++ migration.up++ excluded_category_ids =+ setting_value("ai_translation_excluded_categories").split("|").map(&:to_i)+ expect(excluded_category_ids).to contain_exactly(+ *(all_category_ids - [category_1.id, category_3.id]),+ )+ expect(setting_exists?("ai_translation_target_categories")).to eq(false)+ end++ it "converts saved target categories when translation is disabled" do+ store_setting("ai_translation_enabled", "f", data_type: 5)+ store_setting("ai_translation_target_categories", category_1.id.to_s)++ migration.up++ excluded_category_ids =+ setting_value("ai_translation_excluded_categories").split("|").map(&:to_i)+ expect(excluded_category_ids).to contain_exactly(*(all_category_ids - [category_1.id]))+ expect(setting_exists?("ai_translation_target_categories")).to eq(false)+ end++ it "excludes all categories for existing sites with translation enabled and an empty target list" do+ store_setting("ai_translation_enabled", "t", data_type: 5)+ store_setting("ai_translation_target_categories", "")++ migration.up++ excluded_category_ids =+ setting_value("ai_translation_excluded_categories").split("|").map(&:to_i)+ expect(excluded_category_ids).to contain_exactly(*all_category_ids)+ expect(setting_exists?("ai_translation_target_categories")).to eq(false)+ end++ it "does not store an excluded list for disabled sites with an empty target list" do+ store_setting("ai_translation_enabled", "f", data_type: 5)+ store_setting("ai_translation_target_categories", "")++ migration.up++ expect(setting_value("ai_translation_excluded_categories")).to be_nil+ expect(setting_exists?("ai_translation_target_categories")).to eq(false)+ end++ it "excludes all categories for existing sites with translation enabled and without a stored target list" do+ store_setting("ai_translation_enabled", "t", data_type: 5)++ migration.up++ excluded_category_ids =+ setting_value("ai_translation_excluded_categories").split("|").map(&:to_i)+ expect(excluded_category_ids).to contain_exactly(*all_category_ids)+ end++ it "does not store an excluded list for disabled sites without a stored target list" do+ store_setting("ai_translation_enabled", "f", data_type: 5)++ migration.up++ expect(setting_value("ai_translation_excluded_categories")).to be_nil+ end++ it "does not store an excluded list when all existing categories were targets" do+ store_setting("ai_translation_target_categories", all_category_ids.join("|"))++ migration.up++ expect(setting_value("ai_translation_excluded_categories")).to be_nil+ expect(setting_exists?("ai_translation_target_categories")).to eq(false)+ end++ it "does not migrate target categories on new sites" do+ Migration::Helpers.stubs(:existing_site?).returns(false)+ store_setting("ai_translation_target_categories", category_1.id.to_s)++ migration.up++ expect(setting_value("ai_translation_excluded_categories")).to be_nil+ expect(setting_exists?("ai_translation_target_categories")).to eq(false)+ end+ end+enddiff --git a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rbindex 145d1f38641..023f87430f7 100644--- a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb@@ -18,7 +18,7 @@ describe Jobs::DetectTranslatePost doenable_current_pluginSiteSetting.ai_translation_enabled = trueSiteSetting.content_localization_supported_locales = locales.join("|")- SiteSetting.ai_translation_target_categories = post.topic.category_id.to_s+ SiteSetting.ai_translation_excluded_categories = ""endit "does nothing when translator is disabled" do@@ -144,13 +144,13 @@ describe Jobs::DetectTranslatePost doexpect { job.execute({ post_id: post.id }) }.not_to raise_errorend- describe "with target categories and PM scope" do- fab!(:target_category, :category)- fab!(:non_target_category, :category)- fab!(:target_topic) { Fabricate(:topic, category: target_category) }- fab!(:non_target_topic) { Fabricate(:topic, category: non_target_category) }- fab!(:target_post) { Fabricate(:post, topic: target_topic) }- fab!(:non_target_post) { Fabricate(:post, topic: non_target_topic) }+ describe "with excluded categories and PM scope" do+ fab!(:included_category, :category)+ fab!(:excluded_category, :category)+ fab!(:included_topic) { Fabricate(:topic, category: included_category) }+ fab!(:excluded_topic) { Fabricate(:topic, category: excluded_category) }+ fab!(:included_post) { Fabricate(:post, topic: included_topic) }+ fab!(:excluded_post) { Fabricate(:post, topic: excluded_topic) }fab!(:personal_pm_topic, :private_message_topic)fab!(:personal_pm_post) { Fabricate(:post, topic: personal_pm_topic) }@@ -160,19 +160,16 @@ describe Jobs::DetectTranslatePost doendfab!(:group_pm_post) { Fabricate(:post, topic: group_pm_topic) }- before { SiteSetting.ai_translation_target_categories = target_category.id.to_s }+ before { SiteSetting.ai_translation_excluded_categories = excluded_category.id.to_s }- it "skips posts not in target categories" do- DiscourseAi::Translation::PostLocaleDetector- .expects(:detect_locale)- .with(non_target_post)- .never- job.execute({ post_id: non_target_post.id })+ it "skips posts in excluded categories" do+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(excluded_post).never+ job.execute({ post_id: excluded_post.id })end- it "processes posts in target categories" do- DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(target_post).once- job.execute({ post_id: target_post.id })+ it "processes posts in included categories" do+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(included_post).once+ job.execute({ post_id: included_post.id })endcontext "when pm_translation_scope is none" dodiff --git a/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb b/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rbindex 06a2c8da6cb..6ffad76d8c1 100644--- a/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb@@ -12,7 +12,7 @@ describe Jobs::DetectTranslateTopic doenable_current_pluginSiteSetting.ai_translation_enabled = trueSiteSetting.content_localization_supported_locales = locales.join("|")- SiteSetting.ai_translation_target_categories = topic.category_id.to_s+ SiteSetting.ai_translation_excluded_categories = ""endit "does nothing when translator is disabled" do@@ -139,11 +139,11 @@ describe Jobs::DetectTranslateTopic doexpect { job.execute({ topic_id: topic.id }) }.not_to raise_errorend- describe "with target categories and PM scope" do- fab!(:target_category, :category)- fab!(:non_target_category, :category)- fab!(:target_topic) { Fabricate(:topic, category: target_category) }- fab!(:non_target_topic) { Fabricate(:topic, category: non_target_category) }+ describe "with excluded categories and PM scope" do+ fab!(:included_category, :category)+ fab!(:excluded_category, :category)+ fab!(:included_topic) { Fabricate(:topic, category: included_category) }+ fab!(:excluded_topic) { Fabricate(:topic, category: excluded_category) }fab!(:personal_pm_topic, :private_message_topic)@@ -151,29 +151,35 @@ describe Jobs::DetectTranslateTopic doFabricate(:group_private_message_topic, recipient_group: Fabricate(:group))end- before { SiteSetting.ai_translation_target_categories = target_category.id.to_s }+ before { SiteSetting.ai_translation_excluded_categories = excluded_category.id.to_s }- it "skips topics not in target categories" do+ it "skips topics in excluded categories" doDiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale)- .with(non_target_topic)+ .with(excluded_topic).never- job.execute({ topic_id: non_target_topic.id })+ job.execute({ topic_id: excluded_topic.id })end- it "processes topics in target categories" do- DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(target_topic).once+ it "processes topics in included categories" do+ DiscourseAi::Translation::TopicLocaleDetector+ .expects(:detect_locale)+ .with(included_topic)+ .once- job.execute({ topic_id: target_topic.id })+ job.execute({ topic_id: included_topic.id })end- it "skips topics when target_categories is empty" do- SiteSetting.ai_translation_target_categories = ""+ it "processes regular topics when excluded categories is empty" do+ SiteSetting.ai_translation_excluded_categories = ""- DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(target_topic).never+ DiscourseAi::Translation::TopicLocaleDetector+ .expects(:detect_locale)+ .with(included_topic)+ .once- job.execute({ topic_id: target_topic.id })+ job.execute({ topic_id: included_topic.id })endcontext "when pm_translation_scope is none" dodiff --git a/plugins/discourse-ai/spec/jobs/regular/localize_categories_spec.rb b/plugins/discourse-ai/spec/jobs/regular/localize_categories_spec.rbindex f7769e9b9a6..86e3de655e7 100644--- a/plugins/discourse-ai/spec/jobs/regular/localize_categories_spec.rb+++ b/plugins/discourse-ai/spec/jobs/regular/localize_categories_spec.rb@@ -66,7 +66,7 @@ describe Jobs::LocalizeCategories doendit "translates categories to the configured locales" do- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")+ SiteSetting.ai_translation_excluded_categories = ""Category.update_all(locale: "en")number_of_categories = Category.count@@ -95,7 +95,7 @@ describe Jobs::LocalizeCategories doSiteSetting.content_localization_supported_locales = "pt"6.times { Fabricate(:category) }- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")+ SiteSetting.ai_translation_excluded_categories = ""Category.update_all(locale: "en")DiscourseAi::Translation::CategoryLocalizer@@ -111,7 +111,7 @@ describe Jobs::LocalizeCategories doendit "skips categories that already have localizations" do- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")+ SiteSetting.ai_translation_excluded_categories = ""localize_all_categories("pt", "zh_CN")DiscourseAi::Translation::CategoryLocalizer@@ -135,12 +135,10 @@ describe Jobs::LocalizeCategories doendit "handles translation errors gracefully" do- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")+ SiteSetting.ai_translation_excluded_categories = ""localize_all_categories("pt", "zh_CN")category1 = Fabricate(:category, name: "First", description: "First description", locale: "en")- SiteSetting.ai_translation_target_categories =- (SiteSetting.ai_translation_target_categories.split("|") + [category1.id.to_s]).join("|")DiscourseAi::Translation::CategoryLocalizer.expects(:localize)@@ -163,25 +161,25 @@ describe Jobs::LocalizeCategories doexpect { job.execute({ limit: 10 }) }.not_to raise_errorend- it "only translates categories in target_categories" do- target = Fabricate(:category, locale: "en")- non_target = Fabricate(:category, locale: "en")- SiteSetting.ai_translation_target_categories = target.id.to_s+ it "does not translate excluded categories" do+ included = Fabricate(:category, locale: "en")+ excluded = Fabricate(:category, locale: "en")+ SiteSetting.ai_translation_excluded_categories = excluded.id.to_sDiscourseAi::Translation::CategoryLocalizer.expects(:localize)- .with(target, any_parameters)+ .with(included, any_parameters).twiceDiscourseAi::Translation::CategoryLocalizer.expects(:localize)- .with(non_target, any_parameters)+ .with(excluded, any_parameters).neverjob.execute({ limit: 10 })endit "skips creating localizations in the same language as the category's locale" do- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")+ SiteSetting.ai_translation_excluded_categories = ""Category.update_all(locale: "pt")DiscourseAi::Translation::CategoryLocalizer@@ -205,7 +203,7 @@ describe Jobs::LocalizeCategories doendit "deletes existing localizations that match the category's locale" do- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")+ SiteSetting.ai_translation_excluded_categories = ""# update all categories to portugueseCategory.update_all(locale: "pt")@@ -219,7 +217,7 @@ describe Jobs::LocalizeCategories doit "doesn't process categories with nil locale" do# Add a category with nil localenil_locale_category = Fabricate(:category, name: "No Locale", locale: nil)- SiteSetting.ai_translation_target_categories = nil_locale_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = ""# Make sure our query for categories with non-null locales excludes itDiscourseAi::Translation::CategoryLocalizerdiff --git a/plugins/discourse-ai/spec/jobs/scheduled/categories_locale_detection_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/categories_locale_detection_backfill_spec.rbindex e6cee4aab0b..9e343e1b48c 100644--- a/plugins/discourse-ai/spec/jobs/scheduled/categories_locale_detection_backfill_spec.rb+++ b/plugins/discourse-ai/spec/jobs/scheduled/categories_locale_detection_backfill_spec.rb@@ -11,7 +11,8 @@ describe Jobs::CategoriesLocaleDetectionBackfill doSiteSetting.ai_translation_enabled = trueSiteSetting.ai_translation_backfill_hourly_rate = 100SiteSetting.content_localization_supported_locales = "en"- SiteSetting.ai_translation_target_categories = category.id.to_s+ SiteSetting.ai_translation_excluded_categories =+ Category.where.not(id: category.id).pluck(:id).join("|")endit "does nothing when AI is disabled" do@@ -42,19 +43,22 @@ describe Jobs::CategoriesLocaleDetectionBackfill dojob.execute({})end- it "does nothing when target_categories is empty" do- SiteSetting.ai_translation_target_categories = ""+ it "does nothing when all categories are excluded" do+ SiteSetting.ai_translation_excluded_categories = Category.pluck(:id).join("|")DiscourseAi::Translation::CategoryLocaleDetector.expects(:detect_locale).neverjob.execute({})end- it "detects locale for categories with nil locale in target categories" do- non_target = Fabricate(:category, locale: nil)+ it "detects locale for categories with nil locale when they are not excluded" do+ excluded = Fabricate(:category, locale: nil)+ SiteSetting.ai_translation_excluded_categories = excluded.id.to_s+ SiteSetting.ai_translation_excluded_categories =+ Category.where.not(id: category.id).pluck(:id).join("|")DiscourseAi::Translation::CategoryLocaleDetector.expects(:detect_locale).with(category).once- DiscourseAi::Translation::CategoryLocaleDetector.expects(:detect_locale).with(non_target).never+ DiscourseAi::Translation::CategoryLocaleDetector.expects(:detect_locale).with(excluded).neverjob.execute({})end@@ -81,7 +85,8 @@ describe Jobs::CategoriesLocaleDetectionBackfill doit "limits processing to the backfill rate" doSiteSetting.ai_translation_backfill_hourly_rate = 1extra = Fabricate(:category, locale: nil)- SiteSetting.ai_translation_target_categories = "#{category.id}|#{extra.id}"+ SiteSetting.ai_translation_excluded_categories =+ Category.where.not(id: [category.id, extra.id]).pluck(:id).join("|")DiscourseAi::Translation::CategoryLocaleDetector.expects(:detect_locale).oncediff --git a/plugins/discourse-ai/spec/jobs/scheduled/post_localization_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/post_localization_backfill_spec.rbindex 41096ba3cc2..fa76f73c9d3 100644--- a/plugins/discourse-ai/spec/jobs/scheduled/post_localization_backfill_spec.rb+++ b/plugins/discourse-ai/spec/jobs/scheduled/post_localization_backfill_spec.rb@@ -10,7 +10,7 @@ describe Jobs::PostLocalizationBackfill doSiteSetting.ai_translation_backfill_max_age_days = 100SiteSetting.content_localization_supported_locales = "en"SiteSetting.ai_translation_enabled = true- SiteSetting.ai_translation_target_categories = post.topic.category_id.to_s+ SiteSetting.ai_translation_excluded_categories = ""endit "does not enqueue post translation when translator disabled" dodiff --git a/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rbindex 2fe52a69bfb..7ee9b93d2b4 100644--- a/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb+++ b/plugins/discourse-ai/spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb@@ -12,7 +12,7 @@ describe Jobs::PostsLocaleDetectionBackfill doSiteSetting.ai_translation_enabled = trueSiteSetting.ai_translation_backfill_hourly_rate = 100SiteSetting.content_localization_supported_locales = "en"- SiteSetting.ai_translation_target_categories = post.topic.category_id.to_s+ SiteSetting.ai_translation_excluded_categories = ""endit "does nothing when translator is disabled" do@@ -97,13 +97,13 @@ describe Jobs::PostsLocaleDetectionBackfill dojob.execute({})end- describe "with target categories" do- fab!(:target_category, :category)- fab!(:non_target_category, :category)- fab!(:target_topic) { Fabricate(:topic, category: target_category) }- fab!(:non_target_topic) { Fabricate(:topic, category: non_target_category) }- fab!(:target_post) { Fabricate(:post, topic: target_topic, locale: nil) }- fab!(:non_target_post) { Fabricate(:post, topic: non_target_topic, locale: nil) }+ describe "with excluded categories" do+ fab!(:included_category, :category)+ fab!(:excluded_category, :category)+ fab!(:included_topic) { Fabricate(:topic, category: included_category) }+ fab!(:excluded_topic) { Fabricate(:topic, category: excluded_category) }+ fab!(:included_post) { Fabricate(:post, topic: included_topic, locale: nil) }+ fab!(:excluded_post) { Fabricate(:post, topic: excluded_topic, locale: nil) }fab!(:group)fab!(:group_pm_topic) { Fabricate(:private_message_topic, allowed_groups: [group]) }@@ -113,30 +113,25 @@ describe Jobs::PostsLocaleDetectionBackfill dofab!(:pm_post) { Fabricate(:post, topic: pm_topic, locale: nil) }before do- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories =+ Category.where.not(id: included_category.id).pluck(:id).join("|")SiteSetting.ai_translation_personal_messages = "none"end- it "only processes posts from target categories" do- DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(target_post).once- DiscourseAi::Translation::PostLocaleDetector- .expects(:detect_locale)- .with(non_target_post)- .never+ it "does not process posts from excluded categories" do+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(included_post).once+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(excluded_post).neverDiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(group_pm_post).neverDiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(pm_post).neverjob.execute({})end- it "processes target posts and group PMs when pm_translation_scope is group" do+ it "processes included posts and group PMs when pm_translation_scope is group" doSiteSetting.ai_translation_personal_messages = "group"- DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(target_post).once- DiscourseAi::Translation::PostLocaleDetector- .expects(:detect_locale)- .with(non_target_post)- .never+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(included_post).once+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(excluded_post).neverDiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(group_pm_post).onceDiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(pm_post).neverdiff --git a/plugins/discourse-ai/spec/jobs/scheduled/topic_localization_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/topic_localization_backfill_spec.rbindex a59267aa986..097b51d6398 100644--- a/plugins/discourse-ai/spec/jobs/scheduled/topic_localization_backfill_spec.rb+++ b/plugins/discourse-ai/spec/jobs/scheduled/topic_localization_backfill_spec.rb@@ -10,7 +10,7 @@ describe Jobs::TopicLocalizationBackfill doSiteSetting.ai_translation_backfill_max_age_days = 100SiteSetting.content_localization_supported_locales = "en"SiteSetting.ai_translation_enabled = true- SiteSetting.ai_translation_target_categories = topic.category_id.to_s+ SiteSetting.ai_translation_excluded_categories = ""endit "does not enqueue topic translation when translator disabled" dodiff --git a/plugins/discourse-ai/spec/jobs/scheduled/topics_locale_detection_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/topics_locale_detection_backfill_spec.rbindex 10d4c7c1fe7..4f17b3fa477 100644--- a/plugins/discourse-ai/spec/jobs/scheduled/topics_locale_detection_backfill_spec.rb+++ b/plugins/discourse-ai/spec/jobs/scheduled/topics_locale_detection_backfill_spec.rb@@ -12,7 +12,7 @@ describe Jobs::TopicsLocaleDetectionBackfill doSiteSetting.ai_translation_enabled = trueSiteSetting.ai_translation_backfill_hourly_rate = 100SiteSetting.content_localization_supported_locales = "en"- SiteSetting.ai_translation_target_categories = topic.category_id.to_s+ SiteSetting.ai_translation_excluded_categories = ""endit "does nothing when translator is disabled" do@@ -82,11 +82,11 @@ describe Jobs::TopicsLocaleDetectionBackfill dojob.execute({ limit: 10 })end- describe "with target categories" do- fab!(:target_category, :category)- fab!(:non_target_category, :category)- fab!(:target_topic) { Fabricate(:topic, category: target_category, locale: nil) }- fab!(:non_target_topic) { Fabricate(:topic, category: non_target_category, locale: nil) }+ describe "with excluded categories" do+ fab!(:included_category, :category)+ fab!(:excluded_category, :category)+ fab!(:included_topic) { Fabricate(:topic, category: included_category, locale: nil) }+ fab!(:excluded_topic) { Fabricate(:topic, category: excluded_category, locale: nil) }fab!(:group)fab!(:group_pm_topic) { Fabricate(:private_message_topic, allowed_groups: [group]) }@@ -94,15 +94,19 @@ describe Jobs::TopicsLocaleDetectionBackfill dofab!(:pm_topic, :private_message_topic)before do- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories =+ Category.where.not(id: included_category.id).pluck(:id).join("|")SiteSetting.ai_translation_personal_messages = "none"end- it "only processes topics from target categories" do- DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(target_topic).once+ it "does not process topics from excluded categories" doDiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale)- .with(non_target_topic)+ .with(included_topic)+ .once+ DiscourseAi::Translation::TopicLocaleDetector+ .expects(:detect_locale)+ .with(excluded_topic).neverDiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale)@@ -113,17 +117,20 @@ describe Jobs::TopicsLocaleDetectionBackfill dojob.execute({ limit: 10 })end- it "processes target category topics and group PMs when pm_translation_scope is group" do+ it "processes included category topics and group PMs when pm_translation_scope is group" doSiteSetting.ai_translation_personal_messages = "group"- DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(target_topic).once+ DiscourseAi::Translation::TopicLocaleDetector+ .expects(:detect_locale)+ .with(included_topic)+ .onceDiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(group_pm_topic).onceDiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale)- .with(non_target_topic)+ .with(excluded_topic).neverDiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(pm_topic).neverdiff --git a/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb b/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rbindex b747348319b..07421dbec20 100644--- a/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/category_candidates_spec.rb@@ -2,28 +2,37 @@describe DiscourseAi::Translation::CategoryCandidates dodescribe ".get" do- it "returns only target categories when target_categories is set" do- target = Fabricate(:category)- non_target = Fabricate(:category)- SiteSetting.ai_translation_target_categories = target.id.to_s+ it "returns all categories when no excluded categories are set" do+ category_1 = Fabricate(:category)+ category_2 = Fabricate(:category)+ SiteSetting.ai_translation_excluded_categories = ""categories = DiscourseAi::Translation::CategoryCandidates.get- expect(categories).to include(target)- expect(categories).not_to include(non_target)+ expect(categories).to include(category_1, category_2)end- it "returns no categories when target_categories is empty" do- Fabricate(:category)+ it "returns private categories by default" do+ private_category = Fabricate(:private_category, group: Fabricate(:group))+ SiteSetting.ai_translation_excluded_categories = ""- SiteSetting.ai_translation_target_categories = ""- expect(DiscourseAi::Translation::CategoryCandidates.get.count).to eq(0)+ expect(DiscourseAi::Translation::CategoryCandidates.get).to include(private_category)+ end++ it "does not return excluded categories" do+ included = Fabricate(:category)+ excluded = Fabricate(:category)+ SiteSetting.ai_translation_excluded_categories = excluded.id.to_s++ categories = DiscourseAi::Translation::CategoryCandidates.get+ expect(categories).to include(included)+ expect(categories).not_to include(excluded)endenddescribe ".calculate_completion_per_locale" dofab!(:target_category, :category)- before { SiteSetting.ai_translation_target_categories = target_category.id.to_s }+ before { SiteSetting.ai_translation_excluded_categories = "" }context "when (scenario A) completion determined by category's locale" doit "returns done = total if all categories are in the locale" do@@ -38,7 +47,6 @@ describe DiscourseAi::Translation::CategoryCandidates doit "returns correct done and total if some categories are in the locale" dolocale = "pt_BR"target2 = Fabricate(:category, locale: "ar")- SiteSetting.ai_translation_target_categories = "#{target_category.id}|#{target2.id}"target_category.update!(locale: locale)completion =@@ -61,7 +69,6 @@ describe DiscourseAi::Translation::CategoryCandidates doit "returns correct done and total if some categories have a localization in the locale" dolocale = "es"target2 = Fabricate(:category, locale: "fr")- SiteSetting.ai_translation_target_categories = "#{target_category.id}|#{target2.id}"target_category.update!(locale: "en")Fabricate(:category_localization, category: target_category, locale:)Fabricate(:category_localization, category: target2, locale: "ar")@@ -83,7 +90,7 @@ describe DiscourseAi::Translation::CategoryCandidates doendit "returns 0 for done and total when no categories match" do- SiteSetting.ai_translation_target_categories = ""+ SiteSetting.ai_translation_excluded_categories = Category.pluck(:id).join("|")completion =DiscourseAi::Translation::CategoryCandidates.calculate_completion_per_locale("pt")diff --git a/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb b/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rbindex 1423764ff88..aef736675fd 100644--- a/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/post_candidates_spec.rb@@ -1,12 +1,11 @@# frozen_string_literal: truedescribe DiscourseAi::Translation::PostCandidates do- before { SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|") }+ before { SiteSetting.ai_translation_excluded_categories = "" }describe ".get" doit "does not return bot posts" dopost = Fabricate(:post, user: Discourse.system_user)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")expect(DiscourseAi::Translation::PostCandidates.get).not_to include(post)end@@ -16,7 +15,6 @@ describe DiscourseAi::Translation::PostCandidates doSiteSetting.ai_translation_include_bot_content = truebot_post = Fabricate(:post, user: Discourse.system_user)regular_post = Fabricate(:post)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")posts = DiscourseAi::Translation::PostCandidates.getexpect(posts).to include(bot_post)@@ -30,14 +28,12 @@ describe DiscourseAi::Translation::PostCandidates do:post,created_at: SiteSetting.ai_translation_backfill_max_age_days.days.ago - 1.day,)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")expect(DiscourseAi::Translation::PostCandidates.get).not_to include(post)endit "does not return deleted posts" dopost = Fabricate(:post, deleted_at: Time.now)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")expect(DiscourseAi::Translation::PostCandidates.get).not_to include(post)end@@ -46,7 +42,6 @@ describe DiscourseAi::Translation::PostCandidates doSiteSetting.ai_translation_max_post_length = 100short_post = Fabricate(:post, raw: "This is a short post that fits within the limit.")long_post = Fabricate(:post, raw: "a" * 50 + " This is a long post. " + "b" * 50)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")posts = DiscourseAi::Translation::PostCandidates.getexpect(posts).to include(short_post)@@ -56,20 +51,26 @@ describe DiscourseAi::Translation::PostCandidates dodescribe "category and PM filtering" dofab!(:target_category, :category)fab!(:non_target_category, :category)+ fab!(:group)fab!(:pm_post) { Fabricate(:post, topic: Fabricate(:private_message_topic)) }fab!(:group_pm_post) do- Fabricate(- :post,- topic: Fabricate(:private_message_topic, allowed_groups: [Fabricate(:group)]),- )+ Fabricate(:post, topic: Fabricate(:private_message_topic, allowed_groups: [group]))endfab!(:target_post) { Fabricate(:post, topic: Fabricate(:topic, category: target_category)) }fab!(:non_target_post) doFabricate(:post, topic: Fabricate(:topic, category: non_target_category))end- it "only includes posts from target categories" do- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ it "includes posts from private categories by default" do+ private_category = Fabricate(:private_category, group:)+ private_post = Fabricate(:post, topic: Fabricate(:topic, category: private_category))+ SiteSetting.ai_translation_personal_messages = "none"++ expect(DiscourseAi::Translation::PostCandidates.get).to include(private_post)+ end++ it "does not include posts from excluded categories" do+ SiteSetting.ai_translation_excluded_categories = non_target_category.id.to_sSiteSetting.ai_translation_personal_messages = "none"posts = DiscourseAi::Translation::PostCandidates.get@@ -80,7 +81,7 @@ describe DiscourseAi::Translation::PostCandidates doendit "includes group PMs but not personal PMs when pm_translation_scope is group" do- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = non_target_category.id.to_sSiteSetting.ai_translation_personal_messages = "group"posts = DiscourseAi::Translation::PostCandidates.get@@ -90,7 +91,7 @@ describe DiscourseAi::Translation::PostCandidates doendit "includes all PMs when pm_translation_scope is all" do- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = non_target_category.id.to_sSiteSetting.ai_translation_personal_messages = "all"posts = DiscourseAi::Translation::PostCandidates.get@@ -107,7 +108,7 @@ describe DiscourseAi::Translation::PostCandidates dobefore doSiteSetting.ai_translation_backfill_max_age_days = 100SiteSetting.content_localization_supported_locales = "en|ja|de"- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = ""SiteSetting.ai_translation_personal_messages = "none"end@@ -192,7 +193,7 @@ describe DiscourseAi::Translation::PostCandidates doDiscourse.cache.clearSiteSetting.content_localization_supported_locales = "en_GB|pt|es"SiteSetting.ai_translation_backfill_max_age_days = 30- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = ""SiteSetting.ai_translation_personal_messages = "group"end@@ -207,6 +208,19 @@ describe DiscourseAi::Translation::PostCandidates doexpect(result[:posts_with_detected_locale]).to eq(0)end+ it "uses excluded categories in the cache key" do+ Post.delete_all+ excluded_category = Fabricate(:category)+ Fabricate(:post, locale: "en_GB", topic: Fabricate(:topic, category: target_category))+ Fabricate(:post, locale: "fr", topic: Fabricate(:topic, category: excluded_category))++ SiteSetting.ai_translation_excluded_categories = ""+ expect(described_class.get_completion_all_locales[:total]).to eq(2)++ SiteSetting.ai_translation_excluded_categories = excluded_category.id.to_s+ expect(described_class.get_completion_all_locales[:total]).to eq(1)+ end+it "returns progress grouped by base locale (of en_GB) and correct totals" dopost1 = Fabricate(:post, locale: "en_GB", topic: Fabricate(:topic, category: target_category))post2 = Fabricate(:post, locale: "fr", topic: Fabricate(:topic, category: target_category))diff --git a/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb b/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rbindex 670e566ca4d..1d89fcc9766 100644--- a/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/topic_candidates_spec.rb@@ -1,12 +1,11 @@# frozen_string_literal: truedescribe DiscourseAi::Translation::TopicCandidates do- before { SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|") }+ before { SiteSetting.ai_translation_excluded_categories = "" }describe ".get" doit "does not return bot topics" dotopic = Fabricate(:topic, user: Discourse.system_user)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")expect(DiscourseAi::Translation::TopicCandidates.get).not_to include(topic)end@@ -16,7 +15,6 @@ describe DiscourseAi::Translation::TopicCandidates doSiteSetting.ai_translation_include_bot_content = truebot_topic = Fabricate(:topic, user: Discourse.system_user)regular_topic = Fabricate(:topic)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")topics = DiscourseAi::Translation::TopicCandidates.getexpect(topics).to include(bot_topic)@@ -30,7 +28,6 @@ describe DiscourseAi::Translation::TopicCandidates do:topic,created_at: SiteSetting.ai_translation_backfill_max_age_days.days.ago - 1.day,)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")expect(DiscourseAi::Translation::TopicCandidates.get).not_to include(topic)end@@ -46,8 +43,8 @@ describe DiscourseAi::Translation::TopicCandidates doexpect(DiscourseAi::Translation::TopicCandidates.get).to include(banner_topic)end- it "returns banner topics even when no target categories are set" do- SiteSetting.ai_translation_target_categories = ""+ it "returns banner topics even when all categories are excluded" do+ SiteSetting.ai_translation_excluded_categories = Category.pluck(:id).join("|")SiteSetting.ai_translation_personal_messages = "none"banner_topic = Fabricate(:topic, archetype: Archetype.banner)@@ -63,7 +60,6 @@ describe DiscourseAi::Translation::TopicCandidates doit "does not return deleted topics" dotopic = Fabricate(:topic, deleted_at: Time.now)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")expect(DiscourseAi::Translation::TopicCandidates.get).not_to include(topic)end@@ -71,21 +67,29 @@ describe DiscourseAi::Translation::TopicCandidates dodescribe "category and PM filtering" dofab!(:target_category, :category)fab!(:non_target_category, :category)+ fab!(:group)fab!(:pm, :private_message_topic)- fab!(:group_pm) { Fabricate(:private_message_topic, allowed_groups: [Fabricate(:group)]) }+ fab!(:group_pm) { Fabricate(:private_message_topic, allowed_groups: [group]) }fab!(:target_topic) { Fabricate(:topic, category: target_category) }fab!(:non_target_topic) { Fabricate(:topic, category: non_target_category) }- it "only includes topics from target categories when target_categories is set" do- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ it "includes topics from private categories by default" do+ private_category = Fabricate(:private_category, group:)+ private_topic = Fabricate(:topic, category: private_category)++ expect(DiscourseAi::Translation::TopicCandidates.get).to include(private_topic)+ end++ it "does not include excluded categories" do+ SiteSetting.ai_translation_excluded_categories = non_target_category.id.to_stopics = DiscourseAi::Translation::TopicCandidates.getexpect(topics).to include(target_topic)expect(topics).not_to include(non_target_topic)end- it "returns no regular topics when target_categories is empty" do- SiteSetting.ai_translation_target_categories = ""+ it "returns no regular topics when all categories are excluded" do+ SiteSetting.ai_translation_excluded_categories = Category.pluck(:id).join("|")SiteSetting.ai_translation_personal_messages = "none"topics = DiscourseAi::Translation::TopicCandidates.get@@ -96,7 +100,7 @@ describe DiscourseAi::Translation::TopicCandidates doendit "excludes all PMs when pm_translation_scope is none" do- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = non_target_category.id.to_sSiteSetting.ai_translation_personal_messages = "none"topics = DiscourseAi::Translation::TopicCandidates.get@@ -106,7 +110,7 @@ describe DiscourseAi::Translation::TopicCandidates doendit "includes group PMs but not personal PMs when pm_translation_scope is group" do- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = non_target_category.id.to_sSiteSetting.ai_translation_personal_messages = "group"topics = DiscourseAi::Translation::TopicCandidates.get@@ -116,7 +120,7 @@ describe DiscourseAi::Translation::TopicCandidates doendit "includes all PMs when pm_translation_scope is all" do- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = non_target_category.id.to_sSiteSetting.ai_translation_personal_messages = "all"topics = DiscourseAi::Translation::TopicCandidates.get@@ -133,7 +137,7 @@ describe DiscourseAi::Translation::TopicCandidates dobefore doSiteSetting.ai_translation_backfill_max_age_days = 100SiteSetting.content_localization_supported_locales = "en|ja|de"- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = ""SiteSetting.ai_translation_personal_messages = "none"end@@ -201,7 +205,7 @@ describe DiscourseAi::Translation::TopicCandidates doenddescribe ".calculate_completion_per_locale" do- before { SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|") }+ before { SiteSetting.ai_translation_excluded_categories = "" }context "when (scenario A) 'done' determined by topic's locale" doit "returns total = done if all topics are in the locale" do@@ -209,7 +213,6 @@ describe DiscourseAi::Translation::TopicCandidates doFabricate(:topic, locale:)Topic.update_all(locale: locale)Fabricate(:topic, locale: "pt")- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")completion =DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)@@ -220,7 +223,6 @@ describe DiscourseAi::Translation::TopicCandidates dolocale = "es"Fabricate(:topic, locale:)Fabricate(:topic, locale: "not_es")- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")completion =DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)@@ -237,7 +239,6 @@ describe DiscourseAi::Translation::TopicCandidates doFabricate(:topic_localization, topic:, locale:)endTopicLocalization.order("RANDOM()").first.update(locale: "pt")- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")completion =DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)@@ -250,7 +251,6 @@ describe DiscourseAi::Translation::TopicCandidates dotopic2 = Fabricate(:topic, locale: "fr")Fabricate(:topic_localization, topic: topic1, locale:)Fabricate(:topic_localization, topic: topic2, locale: "not_es")- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")completion =DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)@@ -274,7 +274,6 @@ describe DiscourseAi::Translation::TopicCandidates do# not a candidate as it is a bot topictopic3 = Fabricate(:topic, user: Discourse.system_user, locale: "de")Fabricate(:topic_localization, topic: topic3, locale:)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")completion = DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)translated_candidates = 2 # topic1 + topic2@@ -286,7 +285,6 @@ describe DiscourseAi::Translation::TopicCandidates dolocale = "es"topic = Fabricate(:topic, locale:)Fabricate(:topic_localization, topic:, locale:)- SiteSetting.ai_translation_target_categories = Category.pluck(:id).join("|")completion = DiscourseAi::Translation::TopicCandidates.calculate_completion_per_locale(locale)expect(completion).to eq({ done: 1, total: 1 })diff --git a/plugins/discourse-ai/spec/requests/admin/ai_translations_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_translations_controller_spec.rbindex 79d1f6151d0..0a90e5ff744 100644--- a/plugins/discourse-ai/spec/requests/admin/ai_translations_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/admin/ai_translations_controller_spec.rb@@ -11,14 +11,14 @@ describe DiscourseAi::Admin::AiTranslationsController dodescribe "#show" docontext "when logged in as admin" do- fab!(:target_category, :category)+ fab!(:excluded_category, :category)before dosign_in(admin)SiteSetting.discourse_ai_enabled = trueSiteSetting.ai_translation_enabled = trueSiteSetting.content_localization_supported_locales = "en|fr|es"- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = excluded_category.id.to_sendit "returns base configuration data without progress" do@@ -97,26 +97,27 @@ describe DiscourseAi::Admin::AiTranslationsController doexpect(response.parsed_body["backfill_enabled"]).to eq(false)end- it "returns target_category_ids from ai_translation_target_categories" do+ it "returns excluded_category_ids from ai_translation_excluded_categories" doother_category = Fabricate(:category)- SiteSetting.ai_translation_target_categories = "#{target_category.id}|#{other_category.id}"+ SiteSetting.ai_translation_excluded_categories =+ "#{excluded_category.id}|#{other_category.id}"get "/admin/plugins/discourse-ai/ai-translations.json"expect(response.status).to eq(200)- expect(response.parsed_body["target_category_ids"]).to contain_exactly(- target_category.id,+ expect(response.parsed_body["excluded_category_ids"]).to contain_exactly(+ excluded_category.id,other_category.id,)end- it "returns an empty array when no target categories are configured" do- SiteSetting.ai_translation_target_categories = ""+ it "returns an empty array when no excluded categories are configured" do+ SiteSetting.ai_translation_excluded_categories = ""get "/admin/plugins/discourse-ai/ai-translations.json"expect(response.status).to eq(200)- expect(response.parsed_body["target_category_ids"]).to eq([])+ expect(response.parsed_body["excluded_category_ids"]).to eq([])endit "correctly indicates if feature is enabled" do@@ -187,7 +188,7 @@ describe DiscourseAi::Admin::AiTranslationsController doSiteSetting.discourse_ai_enabled = trueSiteSetting.ai_translation_enabled = trueSiteSetting.content_localization_supported_locales = "en|fr|es"- SiteSetting.ai_translation_target_categories = target_category.id.to_s+ SiteSetting.ai_translation_excluded_categories = ""endit "returns translation progress data" dodiff --git a/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb b/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rbindex 51381a63d75..135f5617669 100644--- a/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb+++ b/plugins/discourse-ai/spec/system/admin_ai_translations_spec.rb@@ -74,7 +74,7 @@ RSpec.describe "Admin AI translations" doSiteSetting.discourse_ai_enabled = trueSiteSetting.ai_translation_enabled = falseSiteSetting.content_localization_supported_locales = "en|fr|es"- SiteSetting.ai_translation_target_categories = category.id.to_s+ SiteSetting.ai_translation_excluded_categories = category.id.to_sSiteSetting.ai_translation_backfill_max_age_days = 30translations_page.visit@@ -113,7 +113,7 @@ RSpec.describe "Admin AI translations" doit "displays the category selector alongside the locale selector" doexpect(page).to have_css(".alert.alert-info")- expect(page).to have_content(I18n.t("js.discourse_ai.translations.translatable_categories"))+ expect(page).to have_content(I18n.t("js.discourse_ai.translations.excluded_categories"))expect(page).to have_css(".category-selector")end@@ -130,14 +130,14 @@ RSpec.describe "Admin AI translations" doendend- describe "when categories are not configured" do+ describe "when categories are not excluded" dofab!(:category)before doSiteSetting.discourse_ai_enabled = trueSiteSetting.ai_translation_enabled = falseSiteSetting.content_localization_supported_locales = "en|fr"- SiteSetting.ai_translation_target_categories = ""+ SiteSetting.ai_translation_excluded_categories = ""SiteSetting.ai_translation_backfill_max_age_days = 30visit "/admin/plugins/discourse-ai/ai-translations"@@ -145,18 +145,18 @@ RSpec.describe "Admin AI translations" doit "displays the setup alert with the category selector" doexpect(page).to have_css(".alert.alert-info")- expect(page).to have_content(I18n.t("js.discourse_ai.translations.translatable_categories"))+ expect(page).to have_content(I18n.t("js.discourse_ai.translations.excluded_categories"))expect(page).to have_css(".category-selector")end- it "allows adding and saving translatable categories" do+ it "allows adding and saving excluded categories" dofind(".category-selector").clickfind(".category-row[data-value='#{category.id}']").clickwithin(".ai-translations__category-input-row") { find(".setting-controls__ok").click }expect(page).to have_no_css(".ai-translations__category-input-row .setting-controls__ok")- expect(SiteSetting.ai_translation_target_categories).to eq(category.id.to_s)+ expect(SiteSetting.ai_translation_excluded_categories).to eq(category.id.to_s)endend