Skip to content

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

FIX: Ensure upload reference is available before cooking (#36887)

Natalie Tayda70ebe7b595
diff --git a/plugins/discourse-ai/lib/translation/post_localizer.rb b/plugins/discourse-ai/lib/translation/post_localizer.rb
index aa97f425992..b28cb909968 100644
--- a/plugins/discourse-ai/lib/translation/post_localizer.rb
+++ b/plugins/discourse-ai/lib/translation/post_localizer.rb
@@ -21,22 +21,20 @@ module DiscourseAi
localization.raw = translated_raw
localization.cooked = post.post_analyzer.cook(translated_raw, post.cooking_options || {})
+ localization.post_version = post.version
+ localization.localizer_user_id = Discourse::SYSTEM_USER_ID
+ localization.save!
cooked_processor = LocalizedCookedPostProcessor.new(localization, post, {})
begin
cooked_processor.post_process
- localization.cooked = cooked_processor.html
+ localization.update!(cooked: cooked_processor.html)
rescue => e
# Log but don't fail translation if post-processing (oneboxes, images) fails
Rails.logger.warn(
"Post-processing failed for localization of post #{post.id} to #{target_locale}: #{e.class} - #{e.message}",
)
- # Keep the cooked content without post-processing
end
-
- localization.post_version = post.version
- localization.localizer_user_id = Discourse.system_user.id
- localization.save!
localization
end
diff --git a/plugins/discourse-ai/spec/lib/translation/post_localizer_spec.rb b/plugins/discourse-ai/spec/lib/translation/post_localizer_spec.rb
index a009a5791bf..2281cfbec7e 100644
--- a/plugins/discourse-ai/spec/lib/translation/post_localizer_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/post_localizer_spec.rb
@@ -163,6 +163,26 @@ describe DiscourseAi::Translation::PostLocalizer do
expect(localization.persisted?).to eq(true)
}.to change { PostLocalization.count }.by(1)
end
+
+ describe "upload references" do
+ fab!(:upload)
+
+ it "creates upload references for images in translated content" do
+ post_with_image = Fabricate(:post, raw: "![image](#{upload.short_url})")
+ post_raw_translator_stub(
+ {
+ text: post_with_image.raw,
+ target_locale: "ja",
+ post: post_with_image,
+ translated: "![画像](#{upload.short_url})",
+ },
+ )
+
+ localization = described_class.localize(post_with_image, "ja")
+
+ expect(UploadReference.exists?(target: localization, upload: upload)).to eq(true)
+ end
+ end
end
end