8000 FIX: When allowing private content translation, only translate group PMs and not personal PMs by nattsw · Pull Request #1432 · discourse/discourse-ai · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

FIX: When allowing private content translation, only translate group PMs and not personal PMs #1432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
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
10 changes: 7 additions & 3 deletions app/jobs/regular/detect_translate_post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ def execute(args)
post = Post.find_by(id: args[:post_id])
return if post.blank? || post.raw.blank? || post.deleted_at.present? || post.user_id <= 0

topic = post.topic
return if topic.blank?

if SiteSetting.ai_translation_backfill_limit_to_public_content
topic = post.topic
if topic.blank? || topic.category&.read_restricted? ||
topic.archetype == Archetype.private_message
return if topic.category&.read_restricted? || topic.archetype == Archetype.private_message
else
if topic.archetype == Archetype.private_message &&
!TopicAllowedGroup.exists?(topic_id: topic.id)
return
end
end
Expand Down
7 changes: 6 additions & 1 deletion app/jobs/regular/detect_translate_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ def execute(args)
end

if SiteSetting.ai_translation_backfill_limit_to_public_content
return if topic.category&.read_restricted?
return if topic.category&.read_restricted? || topic.archetype == Archetype.private_message
else
if topic.archetype == Archetype.private_message &&
!TopicAllowedGroup.exists?(topic_id: topic.id)
return
end
end

if (detected_locale = topic.locale).blank?
Expand Down
14 changes: 12 additions & 2 deletions app/jobs/regular/localize_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ def execute(args)
.where.not(locale: locale)
.where("pl.id IS NULL")

posts = posts.joins(:topic)

if SiteSetting.ai_translation_backfill_limit_to_public_content
# exclude all PMs
# and only include posts from public categories
posts =
posts
.joins(:topic)
.where(topics: { category_id: Category.where(read_restricted: false).select(:id) })
.where.not(topics: { archetype: Archetype.private_message })
.where(topics: { category_id: Category.where(read_restricted: false).select(:id) })
else
# all regular topics, and group PMs
posts =
posts.where(
"topics.archetype != ? OR topics.id IN (SELECT topic_id FROM topic_allowed_groups)",
Archetype.private_message,
)
end

if SiteSetting.ai_translation_backfill_max_age_days > 0
Expand Down
14 changes: 13 additions & 1 deletion app/jobs/regular/localize_topics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ def execute(args)
.where("tl.id IS NULL")

if SiteSetting.ai_translation_backfill_limit_to_public_content
topics = topics.where(category_id: Category.where(read_restricted: false).select(:id))
# exclude all PMs
# and only include posts from public categories
topics =
topics
.where.not(archetype: Archetype.private_message)
.where(category_id: Category.where(read_restricted: false).select(:id))
else
# all regular topics, and group PMs
topics =
topics.where(
"topics.archetype != ? OR topics.id IN (SELECT topic_id FROM topic_allowed_groups)",
Archetype.private_message,
)
end

if SiteSetting.ai_translation_backfill_max_age_days > 0
Expand Down
78 changes: 70 additions & 8 deletions spec/jobs/regular/detect_translate_post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,78 @@
expect { job.execute({ post_id: post.id }) }.not_to raise_error
end

it "skips public content when `ai_translation_backfill_limit_to_public_content ` site setting is enabled" do
SiteSetting.ai_translation_backfill_limit_to_public_content = true
post.topic.category.update!(read_restricted: true)
describe "with public content and PM limitations" 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) }

DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(post).never
DiscourseAi::Translation::PostLocalizer.expects(:localize).never
fab!(:personal_pm_topic) { Fabricate(:private_message_topic) }
fab!(:personal_pm_post) { Fabricate(:post, topic: personal_pm_topic) }

job.execute({ post_id: post.id })
fab!(:group_pm_topic) do
Fabricate(:group_private_message_topic, recipient_group: Fabricate(:group))
end
fab!(:group_pm_post) { Fabricate(:post, topic: group_pm_topic) }

context "when ai_translation_backfill_limit_to_public_content is true" do
before { SiteSetting.ai_translation_backfill_limit_to_public_content = true }

it "skips posts from restricted categories and PMs" do
DiscourseAi::Translation::PostLocaleDetector
.expects(:detect_locale)
.with(private_post)
.never
DiscourseAi::Translation::PostLocalizer
.expects(:localize)
.with(private_post, any_parameters)
.never
job.execute({ post_id: private_post.id })

DiscourseAi::Translation::PostLocaleDetector
.expects(:detect_locale)
.with(personal_pm_post)
.never
DiscourseAi::Translation::PostLocalizer
.expects(:localize)
.with(personal_pm_post, any_parameters)
.never
job.execute({ post_id: personal_pm_post.id })

DiscourseAi::Translation::PostLocaleDetector
.expects(:detect_locale)
.with(group_pm_post)
.never
DiscourseAi::Translation::PostLocalizer
.expects(:localize)
.with(group_pm_post, any_parameters)
.never
job.execute({ post_id: group_pm_post.id })
end
end

pm_post = Fabricate(:post, topic: Fabricate(:private_message_topic))
job.execute({ post_id: pm_post.id })
context "when ai_translation_backfill_limit_to_public_content is false" do
before { SiteSetting.ai_translation_backfill_limit_to_public_content = false }

it "processes posts from private categories and group PMs but skips personal PMs" do
DiscourseAi::Translation::PostLocaleDetector.expects(:detect_locale).with(private_post).once
job.execute({ post_id: private_post.id })

DiscourseAi::Translation::PostLocaleDetector
.expects(:detect_locale)
.with(group_pm_post)
.once
job.execute({ post_id: group_pm_post.id })

DiscourseAi::Translation::PostLocaleDetector
.expects(:detect_locale)
.with(personal_pm_post)
.never
DiscourseAi::Translation::PostLocalizer
.expects(:localize)
.with(personal_pm_post, any_parameters)
.never
job.execute({ post_id: personal_pm_post.id })
end
end
end
end
79 changes: 73 additions & 6 deletions spec/jobs/regular/detect_translate_topic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,80 @@
expect { job.execute({ topic_id: topic.id }) }.not_to raise_error
end

it "skips public content when `ai_translation_backfill_limit_to_public_content ` site setting is enabled" do
SiteSetting.ai_translation_backfill_limit_to_public_content = true
topic.category.update!(read_restricted: true)
describe "with public content and PM limitations" do
fab!(:private_category) { Fabricate(:private_category, group: Group[:staff]) }
fab!(:private_topic) { Fabricate(:topic, category: private_category) }

DiscourseAi::Translation::TopicLocaleDetector.expects(:detect_locale).never
DiscourseAi::Translation::TopicLocalizer.expects(:localize).never
fab!(:personal_pm_topic) { Fabricate(:private_message_topic) }

job.execute({ topic_id: topic.id })
fab!(:group_pm_topic) do
Fabricate(:group_private_message_topic, recipient_group: Fabricate(:group))
end

context "when ai_translation_backfill_limit_to_public_content is true" do
before { SiteSetting.ai_translation_backfill_limit_to_public_content = true }

it "skips topics from restricted categories and PMs" do
DiscourseAi::Translation::TopicLocaleDetector
.expects(:detect_locale)
.with(private_topic)
.never
DiscourseAi::Translation::TopicLocalizer
.expects(:localize)
.with(private_topic, any_parameters)
.never
job.execute({ topic_id: private_topic.id })

# Skip personal PMs
DiscourseAi::Translation::TopicLocaleDetector
.expects(:detect_locale)
.with(personal_pm_topic)
.never
DiscourseAi::Translation::TopicLocalizer
.expects(:localize)
.with(personal_pm_topic, any_parameters)
.never
job.execute({ topic_id: personal_pm_topic.id })

DiscourseAi::Translation::TopicLocaleDetector
.expects(:detect_locale)
.with(group_pm_topic)
.never
DiscourseAi::Translation::TopicLocalizer
.expects(:localize)
.with(group_pm_topic, any_parameters)
.never

job.execute({ topic_id: group_pm_topic.id })
end
end

context "when ai_translation_backfill_limit_to_public_content is false" do
before { SiteSetting.ai_translation_backfill_limit_to_public_content = false }

it "processes topics from private categories and group PMs but skips personal PMs" do
DiscourseAi::Translation::TopicLocaleDetector
.expects(:detect_locale)
.with(private_topic)
.once
job.execute({ topic_id: private_topic.id })

DiscourseAi::Translation::TopicLocaleDetector
.expects(:detect_locale)
.with(group_pm_topic)
.once
job.execute({ topic_id: group_pm_topic.id })

DiscourseAi::Translation::TopicLocaleDetector
.expects(:detect_locale)
.with(personal_pm_topic)
.never
DiscourseAi::Translation::TopicLocalizer
.expects(:localize)
.with(personal_pm_topic, any_parameters)
.never
job.execute({ topic_id: personal_pm_topic.id })
end
end
end
end
61 changes: 41 additions & 20 deletions spec/jobs/regular/localize_posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,36 +127,57 @@
fab!(:private_topic) { Fabricate(:topic, category: pri 106B7 vate_category) }
fab!(:private_post) { Fabricate(:post, topic: private_topic, locale: "es") }

fab!(:pm_post) { Fabricate(:post, topic: Fabricate(:private_message_topic), locale: "es") }

fab!(:public_post) { Fabricate(:post, locale: "es") }

before do
SiteSetting.ai_translation_backfill_limit_to_public_content = true
SiteSetting.experimental_content_localization_supported_locales = "ja"
end
fab!(:personal_pm_topic) { Fabricate(:private_message_topic) }
fab!(:personal_pm_post) { Fabricate(:post, topic: personal_pm_topic, locale: "es") }

it "only processes posts from public categories" do
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(public_post, "ja").once
fab!(:group)
fab!(:group_pm_topic) { Fabricate(:group_private_message_topic, recipient_group: group) }
fab!(:group_pm_post) { Fabricate(:post, topic: group_pm_topic, locale: "es") }

DiscourseAi::Translation::PostLocalizer
.expects(:localize)
.with(private_post, any_parameters)
.never
before { SiteSetting.experimental_content_localization_supported_locales = "ja" }

DiscourseAi::Translation::PostLocalizer.expects(:localize).with(pm_post, any_parameters).never
context "when ai_translation_backfill_limit_to_public_content is true" do
before { SiteSetting.ai_translation_backfill_limit_to_public_content = true }

job.execute({})
it "only processes posts from public categories" do
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(public_post, "ja").once

DiscourseAi::Translation::PostLocalizer
.expects(:localize)
.with(private_post, any_parameters)
.never

DiscourseAi::Translation::PostLocalizer
.expects(:localize)
.with(personal_pm_post, any_parameters)
.never
DiscourseAi::Translation::PostLocalizer
.expects(:localize)
.with(group_pm_post, any_parameters)
.never

job.execute({})
end
end

it "processes all posts when setting is disabled" do
SiteSetting.ai_translation_backfill_limit_to_public_content = false
context "when ai_translation_backfill_limit_to_public_content is false" do
before { SiteSetting.ai_translation_backfill_limit_to_public_content = false }

DiscourseAi::Translation::PostLocalizer.expects(:localize).with(public_post, "ja").once
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(pm_post, "ja").once
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(private_post, "ja").once
it "processes public posts and group PMs but not personal PMs" do
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(public_post, "ja").once
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(private_post, "ja").once

job.execute({})
DiscourseAi::Translation::PostLocalizer.expects(:localize).with(group_pm_post, "ja").once

DiscourseAi::Translation::PostLocalizer
.expects(:localize)
.with(personal_pm_post, any_parameters)
.never

job.execute({})
end
end
end

Expand Down
Loading
Loading
0