Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/jobs/scheduled/posts_locale_detection_backfill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ def execute(args)
.where.not(raw: [nil, ""])

if SiteSetting.ai_translation_backfill_limit_to_public_content
public_categories = Category.where(read_restricted: false).pluck(:id)
posts =
posts
.joins(:topic)
.where(topics: { category_id: public_categories })
.where(topics: { archetype: "regular" })
.where(topics: { category_id: Category.where(read_restricted: false).select(:id) })
.where("archetype != ?", Archetype.private_message)
else
posts =
posts.joins(:topic).where(
"topics.archetype != ? OR EXISTS (SELECT 1 FROM topic_allowed_groups WHERE topic_id = topics.id)",
Archetype.private_message,
)
end

if SiteSetting.ai_translation_backfill_max_age_days > 0
Expand Down
12 changes: 11 additions & 1 deletion app/jobs/scheduled/topics_locale_detection_backfill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ def execute(args)
topics = Topic.where(locale: nil, deleted_at: nil).where("topics.user_id > 0")

if SiteSetting.ai_translation_backfill_limit_to_public_content
topics = topics.where(category_id: Category.where(read_restricted: false).select(:id))
topics =
topics.where(category_id: Category.where(read_restricted: false).select(:id)).where(
"archetype != ?",
Archetype.private_message,
)
else
topics =
topics.where(
"archetype != ? OR EXISTS (SELECT 1 FROM topic_allowed_groups WHERE topic_id = topics.id)",
Archetype.private_message,
)
end

if SiteSetting.ai_translation_backfill_max_age_days > 0
Expand Down
27 changes: 22 additions & 5 deletions spec/jobs/scheduled/posts_locale_detection_backfill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,40 @@

describe "with public content limitation" do
fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
fab!(:private_topic) { Fabricate(:topic, category: private_category) }
fab!(:private_post) { Fabricate(:post, topic: private_topic, locale: nil) }
fab!(:private_cat_topic) { Fabricate(:topic, category: private_category) }
fab!(:private_cat_post) { Fabricate(:post, topic: private_cat_topic, locale: nil) }

fab!(:group)
fab!(:group_pm_topic) { Fabricate(:private_message_topic, allowed_groups: [group]) }
fab!(:group_pm_post) { Fabricate(:post, topic: group_pm_topic, locale: nil) }

fab!(:pm_topic) { Fabricate(:private_message_topic) }
fab!(:pm_post) { Fabricate(:post, topic: pm_topic, locale: nil) }

before { SiteSetting.ai_translation_backfill_limit_to_public_content = true }

it "only processes posts from public categories" do
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(private_post).never
DiscourseAi::Translation::PostLocaleDetector
.expects(:detect_locale)
.with(private_cat_post)
.never
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(group_pm_post).never
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(pm_post).never

job.execute({})
end

it "processes all posts when setting is disabled" do
it "processes all public content and group PMs and private categories when setting is disabled" do
SiteSetting.ai_translation_backfill_limit_to_public_content = false

DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(private_post).once
DiscourseAi::Translation::PostLocaleDetector
.expects(:detect_locale)
.with(private_cat_post)
.once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(group_pm_post).once
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(pm_post).never

job.execute({})
end
Expand Down
28 changes: 24 additions & 4 deletions spec/jobs/scheduled/topics_locale_detection_backfill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@

describe "with public content limitation" do
fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
fab!(:private_cat_topic) { Fabricate(:topic, category: private_category, locale: nil) }

fab!(:group)
fab!(:group_pm_topic) { Fabricate(:private_message_topic, allowed_groups: [group]) }

fab!(:pm_topic) { Fabricate(:private_message_topic) }

fab!(:public_topic) { Fabricate(:topic, locale: nil) }
fab!(:private_topic) { Fabricate(:topic, category: private_category, locale: nil) }

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

it "only processes topics from public categories" do
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(public_topic).once

DiscourseAi::Translation::TopicLocaleDetector
.expects(:detect_locale)
.with(private_cat_topic)
.never
DiscourseAi::Translation::TopicLocaleDetector
.expects(:detect_locale)
.with(private_topic)
.with(group_pm_topic)
.never
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(pm_topic).never

job.execute({ limit: 10 })
end

it "processes all topics when setting is disabled" do
it "processes public category topics, group PMs, and private category topics when setting is disabled" do
SiteSetting.ai_translation_backfill_limit_to_public_content = false

DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(public_topic).once
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(private_topic).once
DiscourseAi::Translation::TopicLocaleDetector
.expects(:detect_locale)
.with(group_pm_topic)
.once
DiscourseAi::Translation::TopicLocaleDetector
.expects(:detect_locale)
.with(private_cat_topic)
.once
DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).with(pm_topic).never

job.execute({ limit: 10 })
end
Expand Down