Skip to content

A platform for community discussion. Free, open, simple.

FIX: Only allow translation of posts that the user can see (#37939)

Natalie Taya2bcf8630f54
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb
index d9e48a68fb7..2abbd1349f0 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/translation/translation_controller.rb
@@ -47,9 +47,9 @@ module DiscourseAi
guardian.ensure_can_see!(topic)
- untranslated_posts = find_untranslated_posts(topic)
+ untranslated_posts = find_untranslated_posts(topic, guardian)
- if untranslated_posts.empty?
+ if !untranslated_posts.exists?
return(
render_json_error(
I18n.t("discourse_ai.translation.errors.all_posts_translated"),
@@ -81,7 +81,7 @@ module DiscourseAi
end
end
- def find_untranslated_posts(topic)
+ def find_untranslated_posts(topic, guardian)
supported_locales = DiscourseAi::Translation.locales
base_locales = supported_locales.map { |locale| locale.split("_").first }
@@ -92,6 +92,7 @@ module DiscourseAi
# 4. Are not deleted and have content
topic
.posts
+ .secured(guardian)
.where("user_id > 0")
.where.not(raw: "")
.where(deleted_at: nil)
diff --git a/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb b/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb
index a13af3ae26f..5aa1df1e8dd 100644
--- a/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/translation/translation_controller_spec.rb
@@ -200,6 +200,76 @@ describe DiscourseAi::Translation::TranslationController do
expect(response.parsed_body["scheduled_posts"]).to eq(1)
end
+ context "with post-type visibility scoping" do
+ before do
+ post1.update!(locale: "en")
+ post2.update!(locale: "en")
+ SiteSetting.content_localization_supported_locales = "en|es"
+ end
+
+ it "only schedules posts the user can see" do
+ Fabricate(:post, topic: topic, user: admin, post_type: Post.types[:whisper], locale: "en")
+
+ expect_enqueued_with(
+ job: Jobs::DetectTranslatePost,
+ args: {
+ post_id: post1.id,
+ force: true,
+ },
+ ) do
+ expect_enqueued_with(
+ job: Jobs::DetectTranslatePost,
+ args: {
+ post_id: post2.id,
+ force: true,
+ },
+ ) { post "/discourse-ai/translate/topics/#{topic.id}" }
+ end
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["scheduled_posts"]).to eq(2)
+ end
+
+ it "schedules posts of all types visible to the user" do
+ whisper =
+ Fabricate(
+ :post,
+ topic: topic,
+ user: admin,
+ post_type: Post.types[:whisper],
+ locale: "en",
+ )
+ SiteSetting.whispers_allowed_groups = group.id.to_s
+
+ expect_enqueued_with(
+ job: Jobs::DetectTranslatePost,
+ args: {
+ post_id: post1.id,
+ force: true,
+ },
+ ) do
+ expect_enqueued_with(
+ job: Jobs::DetectTranslatePost,
+ args: {
+ post_id: post2.id,
+ force: true,
+ },
+ ) do
+ expect_enqueued_with(
+ job: Jobs::DetectTranslatePost,
+ args: {
+ post_id: whisper.id,
+ force: true,
+ },
+ ) { post "/discourse-ai/translate/topics/#{topic.id}" }
+ end
+ end
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["scheduled_posts"]).to eq(3)
+ end
+ end
+
it "ignores posts that already have translations" do
post1.update!(locale: "en")
post2.update!(locale: "en")