A platform for community discussion. Free, open, simple.
FEATURE: Also process manually updated translations (#35276)
Natalie Tay38e6420016fe
diff --git a/app/jobs/regular/process_localized_cooked.rb b/app/jobs/regular/process_localized_cooked.rbnew file mode 100644index 00000000000..3ff93a5d72e--- /dev/null+++ b/app/jobs/regular/process_localized_cooked.rb@@ -0,0 +1,28 @@+# frozen_string_literal: true++module Jobs+ class ProcessLocalizedCooked < ::Jobs::Base+ def execute(args)+ DistributedMutex.synchronize(+ "process_localized_cook_#{args[:post_localization_id]}",+ validity: 10.minutes,+ ) do+ post_localization = PostLocalization.find_by(id: args[:post_localization_id])+ return if post_localization.blank?++ post = post_localization.post+ return if post.blank? || post.topic.blank?+ original_cooked = post_localization.cooked++ processor = LocalizedCookedPostProcessor.new(post_localization, post, {})+ processor.post_process+ cooked = processor.html++ if cooked != original_cooked && cooked.present?+ post_localization.update_column(:cooked, cooked)+ MessageBus.publish("/topic/#{post.topic_id}", type: :localized, id: post.id)+ end+ end+ end+ end+enddiff --git a/lib/localized_cooked_post_processor.rb b/lib/localized_cooked_post_processor.rbnew file mode 100644index 00000000000..b1ff77d6c49--- /dev/null+++ b/lib/localized_cooked_post_processor.rb@@ -0,0 +1,28 @@+# frozen_string_literal: true++class LocalizedCookedPostProcessor+ include CookedProcessorMixin++ def initialize(post_localization, post, opts = {})+ @post_localization = post_localization+ @post = post+ @opts = opts+ @doc = Loofah.html5_fragment(@post_localization.cooked)+ @cooking_options = @post.cooking_options || {}+ @cooking_options[:topic_id] = @post.topic_id+ @cooking_options = @cooking_options.symbolize_keys+ @model = @post+ @category_id = @post&.topic&.category_id+ @omit_nofollow = @post.omit_nofollow?+ @size_cache = {}+ end++ def post_process+ post_process_oneboxes+ post_process_images+ end++ def html+ @doc.try(:to_html)+ end+enddiff --git a/lib/post_localization_creator.rb b/lib/post_localization_creator.rbindex 7c6a7fbb03d..db5005d2106 100644--- a/lib/post_localization_creator.rb+++ b/lib/post_localization_creator.rb@@ -7,13 +7,18 @@ class PostLocalizationCreatorpost = Post.find_by(id: post_id)raise Discourse::NotFound unless post- PostLocalization.create!(- post_id: post.id,- post_version: post.version,- locale: locale,- raw: raw,- cooked: PrettyText.cook(raw),- localizer_user_id: user.id,- )+ localization =+ PostLocalization.create!(+ post: post,+ locale: locale,+ raw: raw,+ cooked: post.post_analyzer.cook(raw, post.cooking_options || {}),+ post_version: post.version,+ localizer_user_id: user.id,+ )++ Jobs.enqueue(:process_localized_cooked, post_localization_id: localization.id)++ localizationendenddiff --git a/lib/post_localization_destroyer.rb b/lib/post_localization_destroyer.rbindex c1352f7378e..aaef3663f6d 100644--- a/lib/post_localization_destroyer.rb+++ b/lib/post_localization_destroyer.rb@@ -6,7 +6,10 @@ class PostLocalizationDestroyerlocalization = PostLocalization.find_by(post_id: post_id, locale: locale)raise Discourse::NotFound unless localization+ post = localization.post- localization.destroy+ localization.destroy!++ post.publish_change_to_clients! :revisedendenddiff --git a/lib/post_localization_updater.rb b/lib/post_localization_updater.rbindex f4d7cf550b4..383a42592ec 100644--- a/lib/post_localization_updater.rb+++ b/lib/post_localization_updater.rb@@ -15,6 +15,9 @@ class PostLocalizationUpdaterlocalization.localizer_user_id = user.idlocalization.post_version = post.versionlocalization.save!++ Jobs.enqueue(:process_localized_cooked, post_localization_id: localization.id)+localizationendenddiff --git a/plugins/discourse-ai/lib/translation/localized_cooked_post_processor.rb b/plugins/discourse-ai/lib/translation/localized_cooked_post_processor.rbdeleted file mode 100644index ae2c07d986b..00000000000--- a/plugins/discourse-ai/lib/translation/localized_cooked_post_processor.rb+++ /dev/null@@ -1,32 +0,0 @@-# frozen_string_literal: true--module DiscourseAi- module Translation- class LocalizedCookedPostProcessor- include CookedProcessorMixin-- def initialize(post_localization, post, opts = {})- @post_localization = post_localization- @post = post- @opts = opts- @doc = Loofah.html5_fragment(@post_localization.cooked)- @cooking_options = @post.cooking_options || {}- @cooking_options[:topic_id] = @post.topic_id- @cooking_options = @cooking_options.symbolize_keys- @model = @post- @category_id = @post&.topic&.category_id- @omit_nofollow = @post.omit_nofollow?- @size_cache = {}- end-- def post_process- post_process_oneboxes- post_process_images- end-- def html- @doc.try(:to_html)- end- end- end-enddiff --git a/plugins/discourse-ai/lib/translation/post_localizer.rb b/plugins/discourse-ai/lib/translation/post_localizer.rbindex 544fb8c7492..a99c21be09b 100644--- a/plugins/discourse-ai/lib/translation/post_localizer.rb+++ b/plugins/discourse-ai/lib/translation/post_localizer.rb@@ -1,7 +1,5 @@# frozen_string_literal: true-require_relative "localized_cooked_post_processor"-module DiscourseAimodule Translationclass PostLocalizerdiff --git a/spec/jobs/process_localized_cooked_spec.rb b/spec/jobs/process_localized_cooked_spec.rbnew file mode 100644index 00000000000..102b157151e--- /dev/null+++ b/spec/jobs/process_localized_cooked_spec.rb@@ -0,0 +1,105 @@+# frozen_string_literal: true++describe Jobs::ProcessLocalizedCooked do+ subject(:job) { described_class.new }++ fab!(:post)+ fab!(:post_localization) do+ Fabricate(+ :post_localization,+ post: post,+ locale: "ja",+ raw: "これはテスト投稿です。",+ cooked: "<p>これはテスト投稿です。</p>",+ )+ end++ it "returns when the post_localization cannot be found" do+ expect { job.execute(post_localization_id: 999_999) }.not_to raise_error+ end++ it "returns when the post_localization's post is deleted" do+ post_localization.post.destroy!+ expect { job.execute(post_localization_id: post_localization.id) }.not_to raise_error+ end++ it "returns when the post_localization's topic is deleted" do+ post_localization.post.topic.destroy!+ expect { job.execute(post_localization_id: post_localization.id) }.not_to raise_error+ end++ it "does not replace cooked when LocalizedCookedPostProcessor returns blank" do+ LocalizedCookedPostProcessor.any_instance.expects(:html).returns(" ")+ original_cooked = post_localization.cooked++ job.execute(post_localization_id: post_localization.id)++ post_localization.reload+ expect(post_localization.cooked).to eq(original_cooked)+ end++ it "updates cooked when processor makes changes" do+ processed_html = "<p>これはテスト投稿です。</p><div class='onebox'>Processed</div>"+ LocalizedCookedPostProcessor.any_instance.expects(:html).returns(processed_html)++ job.execute(post_localization_id: post_localization.id)++ post_localization.reload+ expect(post_localization.cooked).to eq(processed_html)+ end++ it "does not update cooked when processor returns same content" do+ LocalizedCookedPostProcessor.any_instance.expects(:html).returns(post_localization.cooked)++ expect { job.execute(post_localization_id: post_localization.id) }.not_to change {+ post_localization.reload.cooked+ }+ end++ it "publishes MessageBus notification when cooked changes" do+ processed_html = "<p>これはテスト投稿です。</p><div class='onebox'>Processed</div>"+ LocalizedCookedPostProcessor.any_instance.expects(:html).returns(processed_html)++ messages =+ MessageBus.track_publish("/topic/#{post.topic_id}") do+ job.execute(post_localization_id: post_localization.id)+ end++ expect(messages.length).to eq(1)+ expect(messages.first.data[:type]).to eq(:localized)+ expect(messages.first.data[:id]).to eq(post.id)+ end++ it "does not publish MessageBus notification when cooked unchanged" do+ LocalizedCookedPostProcessor.any_instance.expects(:html).returns(post_localization.cooked)++ messages =+ MessageBus.track_publish("/topic/#{post.topic_id}") do+ job.execute(post_localization_id: post_localization.id)+ end++ expect(messages.length).to eq(0)+ end++ it "processes oneboxes and images" do+ stub_image_size+ onebox_html = <<~HTML+ <aside class="onebox">+ <article class="onebox-body">+ <h3><a href="https://www.discourse.org">Discourse</a></h3>+ <p>A platform for community discussion</p>+ </article>+ </aside>+ HTML++ post_localization.update!(+ raw: "Check out https://www.discourse.org",+ cooked: "<p>Check out https://www.discourse.org</p>\n#{onebox_html}",+ )++ job.execute(post_localization_id: post_localization.id)++ post_localization.reload+ expect(post_localization.cooked).to include("onebox")+ end+enddiff --git a/spec/lib/post_localization_creator_spec.rb b/spec/lib/post_localization_creator_spec.rbindex 38919551b15..35d9a7964a9 100644--- a/spec/lib/post_localization_creator_spec.rb+++ b/spec/lib/post_localization_creator_spec.rb@@ -26,6 +26,12 @@ describe PostLocalizationCreator do)end+ it "enqueues ProcessLocalizedCook job" do+ loc = described_class.create(post_id: post.id, locale:, raw:, user:)++ expect_job_enqueued(job: :process_localized_cooked, args: { post_localization_id: loc.id })+ end+it "raises not found if the post is missing" doexpect { described_class.create(post_id: -1, locale:, raw:, user:) }.to raise_error(Discourse::NotFound,diff --git a/spec/lib/post_localization_destroyer_spec.rb b/spec/lib/post_localization_destroyer_spec.rbindex 2b1dec777d2..d0d45ab157e 100644--- a/spec/lib/post_localization_destroyer_spec.rb+++ b/spec/lib/post_localization_destroyer_spec.rb@@ -26,4 +26,15 @@ describe PostLocalizationDestroyer dodescribed_class.destroy(post_id: post.id, locale: "nope", acting_user: user)}.to raise_error(Discourse::NotFound)end++ it "publishes MessageBus notification" do+ messages =+ MessageBus.track_publish("/topic/#{post.topic_id}") do+ described_class.destroy(post_id: post.id, locale: locale, acting_user: user)+ end++ expect(messages.length).to eq(1)+ expect(messages.first.data[:type]).to eq(:revised)+ expect(messages.first.data[:id]).to eq(post.id)+ endenddiff --git a/spec/lib/post_localization_updater_spec.rb b/spec/lib/post_localization_updater_spec.rbindex f71dc12c422..6f24aee85d7 100644--- a/spec/lib/post_localization_updater_spec.rb+++ b/spec/lib/post_localization_updater_spec.rb@@ -29,6 +29,12 @@ describe PostLocalizationUpdater do)end+ it "enqueues ProcessLocalizedCook job" do+ loc = described_class.update(post_id: post.id, locale: locale, raw: new_raw, user: user)++ expect_job_enqueued(job: :process_localized_cooked, args: { post_localization_id: loc.id })+ end+it "raises not found if the localization is missing" doexpect {described_class.update(post_id: post.id, locale: "nope", raw: new_raw, user: user)