Skip to content

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

FIX: Ensure "Translate Post" button does not silently skip bots (#38168)

Natalie Tay77bab9e64562
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb
index 2abbd1349f0..99294241bc2 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb
@@ -90,10 +90,10 @@ module DiscourseAi
# 2. Need translation to other supported locales (excluding their own locale)
# 3. Don't have PostLocalization records for those target locales yet
# 4. Are not deleted and have content
- topic
- .posts
- .secured(guardian)
- .where("user_id > 0")
+ posts = topic.posts.secured(guardian)
+ posts =
+ posts.where("posts.user_id > 0") unless SiteSetting.ai_translation_include_bot_content
+ posts
.where.not(raw: "")
.where(deleted_at: nil)
.where.not(locale: nil)
diff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb
index bfd34b200bf..b6d6942a244 100644
--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb
+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb
@@ -17,13 +17,14 @@ module Jobs
end
post = Post.find_by(id: args[:post_id])
- return if post.blank? || post.raw.blank? || post.deleted_at.present? || post.user_id <= 0
+ return if post.blank? || post.raw.blank? || post.deleted_at.present?
+
+ force = args[:force] || false
+ return if post.user_id <= 0 && !force && !SiteSetting.ai_translation_include_bot_content
topic = post.topic
return if topic.blank?
- force = args[:force] || false
-
if force
# no restrictions
elsif SiteSetting.ai_translation_backfill_limit_to_public_content
diff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb
index 5e661ebc929..29d246adf15 100644
--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb
+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb
@@ -17,11 +17,10 @@ module Jobs
end
topic = Topic.find_by(id: args[:topic_id])
- if topic.blank? || topic.title.blank? || topic.deleted_at.present? || topic.user_id <= 0
- return
- end
+ return if topic.blank? || topic.title.blank? || topic.deleted_at.present?
force = args[:force] || false
+ return if topic.user_id <= 0 && !force && !SiteSetting.ai_translation_include_bot_content
if force
# no restrictions
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.js
index 32317019524..e1fa4073a5b 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
@@ -167,6 +167,7 @@ export const AI_FEATURE_SETTING_GROUPS = {
settings: [
"ai_translation_backfill_hourly_rate",
"ai_translation_backfill_limit_to_public_content",
+ "ai_translation_include_bot_content",
"ai_translation_max_post_length",
"ai_translation_backfill_max_age_days",
"ai_translation_max_tokens_multiplier",
diff --git a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb
index 69505813050..cfca233dfc6 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
@@ -59,7 +59,7 @@ describe Jobs::DetectTranslatePost do
job.execute({ post_id: post.id })
end
- it "skips bot posts" do
+ it "skips bot posts by default" do
post.update!(user: Discourse.system_user)
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).never
DiscourseAi::Translation::PostLocalizer.expects(:localize).never
@@ -67,6 +67,21 @@ describe Jobs::DetectTranslatePost do
job.execute({ post_id: post.id })
end
+ it "translates bot posts when force is true" do
+ post.update!(user: Discourse.system_user)
+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).once
+
+ job.execute({ post_id: post.id, force: true })
+ end
+
+ it "translates bot posts when ai_translation_include_bot_content is true" do
+ SiteSetting.ai_translation_include_bot_content = true
+ post.update!(user: Discourse.system_user)
+ DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).once
+
+ job.execute({ post_id: post.id })
+ end
+
it "skips locale detection when no target languages are configured" do
SiteSetting.content_localization_supported_locales = ""
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).never
diff --git a/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb b/plugins/discourse-ai/spec/jobs/regular/detect_translate_topic_spec.rb
index 54fd2745204..55995ecd956 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
@@ -55,13 +55,28 @@ describe Jobs::DetectTranslateTopic do
job.execute({ topic_id: topic.id })
end
- it "skips bot topics" do
+ it "skips bot topics by default" do
topic.update!(user: Discourse.system_user)
DiscourseAi::Translation::TopicLocalizer.expects(:localize).never
job.execute({ topic_id: topic.id })
end
+ it "translates bot topics when force is true" do
+ topic.update!(user: Discourse.system_user)
+ DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).once
+
+ job.execute({ topic_id: topic.id, force: true })
+ end
+
+ it "translates bot topics when ai_translation_include_bot_content is true" do
+ SiteSetting.ai_translation_include_bot_content = true
+ topic.update!(user: Discourse.system_user)
+ DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).once
+
+ job.execute({ topic_id: topic.id })
+ end
+
it "does not get locale or translate when no target languages are configured" do
SiteSetting.content_localization_supported_locales = ""
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).never
diff --git a/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb b/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb
index 5aa1df1e8dd..0c94bd98d7c 100644
--- a/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb
@@ -270,6 +270,49 @@ describe DiscourseAi::Translation::TranslationController do
end
end
+ it "excludes bot posts by default" do
+ bot_post = Fabricate(:post, topic: topic, user: Discourse.system_user, locale: "en")
+ post1.update!(locale: "en")
+ SiteSetting.content_localization_supported_locales = "en|es"
+
+ expect_enqueued_with(
+ job: Jobs::DetectTranslatePost,
+ args: {
+ post_id: post1.id,
+ force: true,
+ },
+ ) { post "/discourse-ai/translate/topics/#{topic.id}" }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["scheduled_posts"]).to eq(1)
+ end
+
+ it "includes bot posts when ai_translation_include_bot_content is true" do
+ SiteSetting.ai_translation_include_bot_content = true
+ bot_post = Fabricate(:post, topic: topic, user: Discourse.system_user, locale: "en")
+ post1.update!(locale: "en")
+ SiteSetting.content_localization_supported_locales = "en|es"
+
+ expect_enqueued_with(
+ job: Jobs::DetectTranslatePost,
+ args: {
+ post_id: post1.id,
+ force: true,
+ },
+ ) do
+ expect_enqueued_with(
+ job: Jobs::DetectTranslatePost,
+ args: {
+ post_id: bot_post.id,
+ force: true,
+ },
+ ) { post "/discourse-ai/translate/topics/#{topic.id}" }
+ end
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["scheduled_posts"]).to eq(2)
+ end
+
it "ignores posts that already have translations" do
post1.update!(locale: "en")
post2.update!(locale: "en")