Skip to content

Commit cb12b3d

Browse files
committed
FIX: Gate translation to public posts or posts within a certain age
1 parent 2631ce6 commit cb12b3d

14 files changed

+367
-14
lines changed

app/jobs/regular/detect_translate_post.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ def execute(args)
1010
post = Post.find_by(id: args[:post_id])
1111
return if post.blank? || post.raw.blank? || post.deleted_at.present? || post.user_id <= 0
1212

13+
if SiteSetting.automatic_translation_backfill_limit_to_public_content
14+
topic = post.topic
15+
return if topic.blank? || topic.category&.read_restricted?
16+
end
17+
1318
detected_locale = DiscourseTranslator::PostLocaleDetector.detect_locale(post)
1419

1520
locales = SiteSetting.automatic_translation_target_languages.split("|")

app/jobs/regular/detect_translate_topic.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def execute(args)
1212
return
1313
end
1414

15+
if SiteSetting.automatic_translation_backfill_limit_to_public_content
16+
return if topic.category&.read_restricted?
17+
end
18+
1519
detected_locale = DiscourseTranslator::TopicLocaleDetector.detect_locale(topic)
1620

1721
locales = SiteSetting.automatic_translation_target_languages.split("|")

app/jobs/regular/translate_categories.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def execute(args)
2121
return if categories.empty?
2222

2323
categories.each do |category|
24+
if SiteSetting.automatic_translation_backfill_limit_to_public_content &&
25+
category.read_restricted?
26+
last_id = category.id
27+
next
28+
end
29+
2430
CategoryLocalization.transaction do
2531
locales.each do |locale|
2632
next if CategoryLocalization.exists?(category_id: category.id, locale: locale)

app/jobs/regular/translate_posts.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,25 @@ def execute(args)
2828
.where.not(locale: nil)
2929
.where.not(locale: locale)
3030
.where("pl.id IS NULL")
31-
.limit(limit)
31+
32+
if SiteSetting.automatic_translation_backfill_limit_to_public_content
33+
public_categories = Category.where(read_restricted: false).pluck(:id)
34+
posts =
35+
posts
36+
.joins(:topic)
37+
.where(topics: { category_id: public_categories })
38+
.where(topics: { archetype: "regular" })
39+
end
40+
41+
if SiteSetting.automatic_translation_backfill_max_age_days > 0
42+
posts =
43+
posts.where(
44+
"posts.created_at > ?",
45+
SiteSetting.automatic_translation_backfill_max_age_days.days.ago,
46+
)
47+
end
48+
49+
posts = posts.order(updated_at: :desc).limit(limit)
3250

3351
next if posts.empty?
3452

app/jobs/regular/translate_topics.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,21 @@ def execute(args)
2727
.where.not(locale: nil)
2828
.where.not(locale: locale)
2929
.where("tl.id IS NULL")
30-
.limit(limit)
30+
31+
if SiteSetting.automatic_translation_backfill_limit_to_public_content
32+
public_categories = Category.where(read_restricted: false).pluck(:id)
33+
topics = topics.where(category_id: public_categories)
34+
end
35+
36+
if SiteSetting.automatic_translation_backfill_max_age_days > 0
37+
topics =
38+
topics.where(
39+
"topics.created_at > ?",
40+
SiteSetting.automatic_translation_backfill_max_age_days.days.ago,
41+
)
42+
end
43+
44+
topics = topics.order(updated_at: :desc).limit(limit)
3145

3246
next if topics.empty?
3347

app/jobs/scheduled/posts_locale_detection_backfill.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,31 @@ def execute(args)
1010
return unless SiteSetting.experimental_content_translation
1111
return if SiteSetting.automatic_translation_backfill_rate == 0
1212

13-
limit = SiteSetting.automatic_translation_backfill_rate
1413
posts =
1514
Post
1615
.where(locale: nil)
1716
.where(deleted_at: nil)
1817
.where("posts.user_id > 0")
1918
.where.not(raw: [nil, ""])
20-
.order(updated_at: :desc)
21-
.limit(limit)
19+
20+
if SiteSetting.automatic_translation_backfill_limit_to_public_content
21+
public_categories = Category.where(read_restricted: false).pluck(:id)
22+
posts =
23+
posts
24+
.joins(:topic)
25+
.where(topics: { category_id: public_categories })
26+
.where(topics: { archetype: "regular" })
27+
end
28+
29+
if SiteSetting.automatic_translation_backfill_max_age_days > 0
30+
posts =
31+
posts.where(
32+
"posts.created_at > ?",
33+
SiteSetting.automatic_translation_backfill_max_age_days.days.ago,
34+
)
35+
end
36+
37+
posts = posts.order(updated_at: :desc).limit(SiteSetting.automatic_translation_backfill_rate)
2238
return if posts.empty?
2339

2440
posts.each do |post|

app/jobs/scheduled/topics_locale_detection_backfill.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,25 @@ class TopicsLocaleDetectionBackfill < ::Jobs::Scheduled
88
def execute(args)
99
return unless SiteSetting.translator_enabled
1010
return unless SiteSetting.experimental_content_translation
11-
return if SiteSetting.automatic_translation_backfill_rate == 0
12-
1311
limit = SiteSetting.automatic_translation_backfill_rate
14-
topics =
15-
Topic
16-
.where(locale: nil)
17-
.where(deleted_at: nil)
18-
.where("topics.user_id > 0")
19-
.order(updated_at: :desc)
20-
.limit(limit)
12+
return if limit == 0
13+
14+
topics = Topic.where(locale: nil).where(deleted_at: nil).where("topics.user_id > 0")
15+
16+
if SiteSetting.automatic_translation_backfill_limit_to_public_content
17+
public_categories = Category.where(read_restricted: false).pluck(:id)
18+
topics = topics.where(category_id: public_categories)
19+
end
20+
21+
if SiteSetting.automatic_translation_backfill_max_age_days > 0
22+
topics =
23+
topics.where(
24+
"topics.created_at > ?",
25+
SiteSetting.automatic_translation_backfill_max_age_days.days.ago,
26+
)
27+
end
28+
29+
topics = topics.order(updated_at: :desc).limit(limit)
2130
return if topics.empty?
2231

2332
topics.each do |topic|

spec/jobs/detect_translate_post_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,14 @@
6868

6969
expect { job.execute({ post_id: post.id }) }.not_to raise_error
7070
end
71+
72+
it "skips public content when configured" do
73+
SiteSetting.automatic_translation_backfill_limit_to_public_content = true
74+
post.topic.category.update!(read_restricted: true)
75+
76+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post).never
77+
DiscourseTranslator::PostTranslator.expects(:translate).never
78+
79+
job.execute({ post_id: post.id })
80+
end
7181
end

spec/jobs/detect_translate_topic_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,14 @@
6868

6969
expect { job.execute({ topic_id: topic.id }) }.not_to raise_error
7070
end
71+
72+
it "skips public content when configured" do
73+
SiteSetting.automatic_translation_backfill_limit_to_public_content = true
74+
topic.category.update!(read_restricted: true)
75+
76+
DiscourseTranslator::TopicLocaleDetector.expects(:detect_locale).never
77+
DiscourseTranslator::TopicTranslator.expects(:translate).never
78+
79+
job.execute({ topic_id: topic.id })
80+
end
7181
end

spec/jobs/posts_locale_detection_backfill_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,57 @@
7676

7777
job.execute({})
7878
end
79+
80+
describe "with public content limitation" do
81+
fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
82+
fab!(:private_topic) { Fabricate(:topic, category: private_category) }
83+
fab!(:private_post) { Fabricate(:post, topic: private_topic, locale: nil) }
84+
85+
before { SiteSetting.automatic_translation_backfill_limit_to_public_content = true }
86+
87+
it "only processes posts from public categories" do
88+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post).once
89+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(private_post).never
90+
91+
job.execute({})
92+
end
93+
94+
it "processes all posts when setting is disabled" do
95+
SiteSetting.automatic_translation_backfill_limit_to_public_content = false
96+
97+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(post).once
98+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(private_post).once
99+
100+
job.execute({})
101+
end
102+
end
103+
104+
describe "with max age limit" do
105+
fab!(:old_post) { Fabricate(:post, locale: nil, created_at: 10.days.ago) }
106+
fab!(:new_post) { Fabricate(:post, locale: nil, created_at: 2.days.ago) }
107+
108+
before { SiteSetting.automatic_translation_backfill_max_age_days = 5 }
109+
110+
it "only processes posts within the age limit" do
111+
# other posts
112+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).at_least_once
113+
114+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(new_post).once
115+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(old_post).never
116+
117+
job.execute({})
118+
end
119+
120+
it "processes all posts when setting is disabled" do
121+
SiteSetting.automatic_translation_backfill_max_age_days = 0
122+
123+
# other posts
124+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).at_least_once
125+
126+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(new_post).once
127+
DiscourseTranslator::PostLocaleDetector.expects(:detect_locale).with(old_post).once
128+
129+
job.execute({})
130+
end
131+
end
79132
end

0 commit comments

Comments
 (0)