Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 740be26

Browse files
authored
DEV: Also make sure locale detection skips PMs that are not group PMs when public content only (#1457)
In the earlier PR #1432, when `SiteSetting.ai_translation_backfill_limit_to_public_content = false`, we **translate** PMs but **skip translating** PMs that do not involve groups. This commit covers the missing case on **locale detection**.
1 parent e2d7ca0 commit 740be26

File tree

4 files changed

+65
-13
lines changed

4 files changed

+65
-13
lines changed

app/jobs/scheduled/posts_locale_detection_backfill.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ def execute(args)
2020
.where.not(raw: [nil, ""])
2121

2222
if SiteSetting.ai_translation_backfill_limit_to_public_content
23-
public_categories = Category.where(read_restricted: false).pluck(:id)
2423
posts =
2524
posts
2625
.joins(:topic)
27-
.where(topics: { category_id: public_categories })
28-
.where(topics: { archetype: "regular" })
26+
.where(topics: { category_id: Category.where(read_restricted: false).select(:id) })
27+
.where("archetype != ?", Archetype.private_message)
28+
else
29+
posts =
30+
posts.joins(:topic).where(
31+
"topics.archetype != ? OR EXISTS (SELECT 1 FROM topic_allowed_groups WHERE topic_id = topics.id)",
32+
Archetype.private_message,
33+
)
2934
end
3035

3136
if SiteSetting.ai_translation_backfill_max_age_days > 0

app/jobs/scheduled/topics_locale_detection_backfill.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ def execute(args)
1515
topics = Topic.where(locale: nil, deleted_at: nil).where("topics.user_id > 0")
1616

1717
if SiteSetting.ai_translation_backfill_limit_to_public_content
18-
topics = topics.where(category_id: Category.where(read_restricted: false).select(:id))
18+
topics =
19+
topics.where(category_id: Category.where(read_restricted: false).select(:id)).where(
20+
"archetype != ?",
21+
Archetype.private_message,
22+
)
23+
else
24+
topics =
25+
topics.where(
26+
"archetype != ? OR EXISTS (SELECT 1 FROM topic_allowed_groups WHERE topic_id = topics.id)",
27+
Archetype.private_message,
28+
)
1929
end
2030

2131
if SiteSetting.ai_translation_backfill_max_age_days > 0

spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,40 @@
8282

8383
describe "with public content limitation" do
8484
fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
85-
fab!(:private_topic) { Fabricate(:topic, category: private_category) }
86-
fab!(:private_post) { Fabricate(:post, topic: private_topic, locale: nil) }
85+
fab!(:private_cat_topic) { Fabricate(:topic, category: private_category) }
86+
fab!(:private_cat_post) { Fabricate(:post, topic: private_cat_topic, locale: nil) }
87+
88+
fab!(:group)
89+
fab!(:group_pm_topic) { Fabricate(:private_message_topic, allowed_groups: [group]) }
90+
fab!(:group_pm_post) { Fabricate(:post, topic: group_pm_topic, locale: nil) }
91+
92+
fab!(:pm_topic) { Fabricate(:private_message_topic) }
93+
fab!(:pm_post) { Fabricate(:post, topic: pm_topic, locale: nil) }
8794

8895
before { SiteSetting.ai_translation_backfill_limit_to_public_content = true }
8996

9097
it "only processes posts from public categories" do
9198
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).once
92-
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(private_post).never
99+
DiscourseAi::Translation::PostLocaleDetector
100+
.expects(:detect_locale)
101+
.with(private_cat_post)
102+
.never
103+
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(group_pm_post).never
104+
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(pm_post).never
93105

94106
job.execute({})
95107
end
96108

97-
it "processes all posts when setting is disabled" do
109+
it "processes all public content and group PMs and private categories when setting is disabled" do
98110
SiteSetting.ai_translation_backfill_limit_to_public_content = false
99111

100112
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).once
101-
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(private_post).once
113+
DiscourseAi::Translation::PostLocaleDetector
114+
.expects(:detect_locale)
115+
.with(private_cat_post)
116+
.once
117+
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(group_pm_post).once
118+
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(pm_post).never
102119

103120
job.execute({})
104121
end

spec/jobs/scheduled/topics_locale_detection_backfill_spec.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@
8282

8383
describe "with public content limitation" do
8484
fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
85+
fab!(:private_cat_topic) { Fabricate(:topic, category: private_category, locale: nil) }
86+
87+
fab!(:group)
88+
fab!(:group_pm_topic) { Fabricate(:private_message_topic, allowed_groups: [group]) }
89+
90+
fab!(:pm_topic) { Fabricate(:private_message_topic) }
91+
8592
fab!(:public_topic) { Fabricate(:topic, locale: nil) }
86-
fab!(:private_topic) { Fabricate(:topic, category: private_category, locale: nil) }
8793

8894
before do
8995
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).at_least_once
@@ -93,19 +99,33 @@
9399

94100
it "only processes topics from public categories" do
95101
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(public_topic).once
102+
103+
DiscourseAi::Translation::TopicLocaleDetector
104+
.expects(:detect_locale)
105+
.with(private_cat_topic)
106+
.never
96107
DiscourseAi::Translation::TopicLocaleDetector
97108
.expects(:detect_locale)
98-
.with(private_topic)
109+
.with(group_pm_topic)
99110
.never
111+
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(pm_topic).never
100112

101113
job.execute({ limit: 10 })
102114
end
103115

104-
it "processes all topics when setting is disabled" do
116+
it "processes public category topics, group PMs, and private category topics when setting is disabled" do
105117
SiteSetting.ai_translation_backfill_limit_to_public_content = false
106118

107119
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(public_topic).once
108-
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(private_topic).once
120+
DiscourseAi::Translation::TopicLocaleDetector
121+
.expects(:detect_locale)
122+
.with(group_pm_topic)
123+
.once
124+
DiscourseAi::Translation::TopicLocaleDetector
125+
.expects(:detect_locale)
126+
.with(private_cat_topic)
127+
.once
128+
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(pm_topic).never
109129

110130
job.execute({ limit: 10 })
111131
end

0 commit comments

Comments
 (0)