Skip to content

Commit 7e0a517

Browse files
authored
FIX: Gate translation to public posts or posts within a certain age (#307)
We have two existing site settings: - automatic_translation_backfill_limit_to_public_content - automatic_translation_backfill_max_age_days We need to use them for PostLocalization (& topic, cat) as well. They are used in the old feature for DiscourseTranslate::PostTranslation (& topic). Also added sidekiq_options retry: false for the jobs as they are non-critical.
1 parent 2631ce6 commit 7e0a517

14 files changed

+388
-16
lines changed

app/jobs/regular/detect_translate_post.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module Jobs
44
class DetectTranslatePost < ::Jobs::Base
5+
sidekiq_options retry: false
6+
57
def execute(args)
68
return unless SiteSetting.translator_enabled
79
return unless SiteSetting.experimental_content_translation
@@ -10,7 +12,18 @@ def execute(args)
1012
post = Post.find_by(id: args[:post_id])
1113
return if post.blank? || post.raw.blank? || post.deleted_at.present? || post.user_id <= 0
1214

13-
detected_locale = DiscourseTranslator::PostLocaleDetector.detect_locale(post)
15+
if SiteSetting.automatic_translation_backfill_limit_to_public_content
16+
topic = post.topic
17+
return if topic.blank? || topic.category&.read_restricted?
18+
end
19+
20+
begin
21+
detected_locale = DiscourseTranslator::PostLocaleDetector.detect_locale(post)
22+
rescue FinalDestination::SSRFDetector::LookupFailedError
23+
# this job is non-critical
24+
# the backfill job will handle failures
25+
return
26+
end
1427

1528
locales = SiteSetting.automatic_translation_target_languages.split("|")
1629
return if locales.blank?

app/jobs/regular/detect_translate_topic.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module Jobs
44
class DetectTranslateTopic < ::Jobs::Base
5+
sidekiq_options retry: false
6+
57
def execute(args)
68
return unless SiteSetting.translator_enabled
79
return unless SiteSetting.experimental_content_translation
@@ -12,7 +14,17 @@ def execute(args)
1214
return
1315
end
1416

15-
detected_locale = DiscourseTranslator::TopicLocaleDetector.detect_locale(topic)
17+
if SiteSetting.automatic_translation_backfill_limit_to_public_content
18+
return if topic.category&.read_restricted?
19+
end
20+
21+
begin
22+
detected_locale = DiscourseTranslator::TopicLocaleDetector.detect_locale(topic)
23+
rescue FinalDestination::SSRFDetector::LookupFailedError
24+
# this job is non-critical
25+
# the backfill job will handle failures
26+
return
27+
end
1628

1729
locales = SiteSetting.automatic_translation_target_languages.split("|")
1830
return if locales.blank?

app/jobs/regular/translate_categories.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module Jobs
44
class TranslateCategories < ::Jobs::Base
55
cluster_concurrency 1
6+
sidekiq_options retry: false
7+
68
BATCH_SIZE = 50
79

810
def execute(args)
@@ -21,6 +23,12 @@ def execute(args)
2123
return if categories.empty?
2224

2325
categories.each do |category|
26+
if SiteSetting.automatic_translation_backfill_limit_to_public_content &&
27+
category.read_restricted?
28+
last_id = category.id
29+
next
30+
end
31+
2432
CategoryLocalization.transaction do
2533
locales.each do |locale|
2634
next if CategoryLocalization.exists?(category_id: category.id, locale: locale)

app/jobs/regular/translate_posts.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,26 @@ 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+
posts =
34+
posts.joins(:topic).where(
35+
topics: {
36+
category_id: Category.where(read_restricted: false).select(:id),
37+
archetype: "regular",
38+
},
39+
)
40+
end
41+
42+
if SiteSetting.automatic_translation_backfill_max_age_days > 0
43+
posts =
44+
posts.where(
45+
"posts.created_at > ?",
46+
SiteSetting.automatic_translation_backfill_max_age_days.days.ago,
47+
)
48+
end
49+
50+
posts = posts.order(updated_at: :desc).limit(limit)
3251

3352
next if posts.empty?
3453

app/jobs/regular/translate_topics.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,20 @@ 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+
topics = topics.where(category_id: Category.where(read_restricted: false).select(:id))
33+
end
34+
35+
if SiteSetting.automatic_translation_backfill_max_age_days > 0
36+
topics =
37+
topics.where(
38+
"topics.created_at > ?",
39+
SiteSetting.automatic_translation_backfill_max_age_days.days.ago,
40+
)
41+
end
42+
43+
topics = topics.order(updated_at: :desc).limit(limit)
3144

3245
next if topics.empty?
3346

app/jobs/scheduled/posts_locale_detection_backfill.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,39 @@
33
module Jobs
44
class PostsLocaleDetectionBackfill < ::Jobs::Scheduled
55
every 5.minutes
6+
sidekiq_options retry: false
67
cluster_concurrency 1
78

89
def execute(args)
910
return unless SiteSetting.translator_enabled
1011
return unless SiteSetting.experimental_content_translation
1112
return if SiteSetting.automatic_translation_backfill_rate == 0
1213

13-
limit = SiteSetting.automatic_translation_backfill_rate
1414
posts =
1515
Post
1616
.where(locale: nil)
1717
.where(deleted_at: nil)
1818
.where("posts.user_id > 0")
1919
.where.not(raw: [nil, ""])
20-
.order(updated_at: :desc)
21-
.limit(limit)
20+
21+
if SiteSetting.automatic_translation_backfill_limit_to_public_content
22+
public_categories = Category.where(read_restricted: false).pluck(:id)
23+
posts =
24+
posts
25+
.joins(:topic)
26+
.where(topics: { category_id: public_categories })
27+
.where(topics: { archetype: "regular" })
28+
end
29+
30+
if SiteSetting.automatic_translation_backfill_max_age_days > 0
31+
posts =
32+
posts.where(
33+
"posts.created_at > ?",
34+
SiteSetting.automatic_translation_backfill_max_age_days.days.ago,
35+
)
36+
end
37+
38+
posts = posts.order(updated_at: :desc).limit(SiteSetting.automatic_translation_backfill_rate)
2239
return if posts.empty?
2340

2441
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
@@ -3,21 +3,30 @@
33
module Jobs
44
class TopicsLocaleDetectionBackfill < ::Jobs::Scheduled
55
every 5.minutes
6+
sidekiq_options retry: false
67
cluster_concurrency 1
78

89
def execute(args)
910
return unless SiteSetting.translator_enabled
1011
return unless SiteSetting.experimental_content_translation
11-
return if SiteSetting.automatic_translation_backfill_rate == 0
12-
1312
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)
13+
return if limit == 0
14+
15+
topics = Topic.where(locale: nil, deleted_at: nil).where("topics.user_id > 0")
16+
17+
if SiteSetting.automatic_translation_backfill_limit_to_public_content
18+
topics = topics.where(category_id: Category.where(read_restricted: false).select(:id))
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 `automatic_translation_backfill_limit_to_public_content ` site setting is enabled" 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 `automatic_translation_backfill_limit_to_public_content ` site setting is enabled" 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)