A platform for community discussion. Free, open, simple.
DEV: Add rake task to backfill localization excerpts which were empty (#36901)
Natalie Tay675a5e5be3b5
diff --git a/app/jobs/regular/process_localized_cooked.rb b/app/jobs/regular/process_localized_cooked.rbindex ba19e0969de..fdced54e520 100644--- a/app/jobs/regular/process_localized_cooked.rb+++ b/app/jobs/regular/process_localized_cooked.rb@@ -21,16 +21,7 @@ module Jobsif post.is_first_post?topic_localization = post.topic.localizations.find_by(locale: post_localization.locale)- if topic_localization- excerpt =- Post.excerpt(- cooked,- SiteSetting.topic_excerpt_maxlength,- strip_links: true,- strip_images: true,- )- topic_localization.update_column(:excerpt, excerpt)- end+ topic_localization.update_excerpt(cooked:) if topic_localizationendMessageBus.publish("/topic/#{post.topic_id}", type: :localized, id: post.id)diff --git a/app/models/topic_localization.rb b/app/models/topic_localization.rbindex 65412531c48..b5516481f0b 100644--- a/app/models/topic_localization.rb+++ b/app/models/topic_localization.rb@@ -10,6 +10,19 @@ class TopicLocalization < ActiveRecord::Basevalidates :fancy_title, presence: truevalidates :localizer_user_id, presence: truevalidates :locale, uniqueness: { scope: :topic_id }++ def update_excerpt(cooked: nil)+ return if cooked.blank?++ excerpt =+ Post.excerpt(+ cooked,+ SiteSetting.topic_excerpt_maxlength,+ strip_links: true,+ strip_images: true,+ )+ update_column(:excerpt, excerpt)+ endend# == Schema Informationdiff --git a/lib/tasks/topic_localizations.rake b/lib/tasks/topic_localizations.rakenew file mode 100644index 00000000000..37a99788308--- /dev/null+++ b/lib/tasks/topic_localizations.rake@@ -0,0 +1,29 @@+# frozen_string_literal: true++desc "Backfill empty excerpts for topic localizations from post localizations"+task "topic_localizations:backfill_excerpts" => :environment do+ updated = 0+ scope =+ TopicLocalization+ .where(excerpt: [nil, ""])+ .joins(:topic)+ .where.not(topics: { excerpt: "" })+ .includes(topic: { first_post: :localizations })++ total = scope.count+ puts "Found #{total} topic localizations to process"+ next if total == 0++ scope.find_each do |topic_localization|+ post_localization =+ topic_localization.topic.first_post&.localizations&.find_by(locale: topic_localization.locale)++ if post_localization+ topic_localization.update_excerpt(cooked: post_localization.cooked)+ updated += 1+ end++ print "\r#{updated}/#{total} processed"+ end+ puts "\nDone! Updated #{updated} topic localizations."+enddiff --git a/plugins/discourse-ai/lib/translation/base_translator.rb b/plugins/discourse-ai/lib/translation/base_translator.rbindex cad5fbbcb97..eb708c86f54 100644--- a/plugins/discourse-ai/lib/translation/base_translator.rb+++ b/plugins/discourse-ai/lib/translation/base_translator.rb@@ -12,6 +12,7 @@ module DiscourseAienddef translate+ return nil if @text.blank?return nil if !SiteSetting.ai_translation_enabledif (ai_persona = AiPersona.find_by_id_from_cache(persona_setting)).blank?return nildiff --git a/plugins/discourse-ai/lib/translation/topic_localizer.rb b/plugins/discourse-ai/lib/translation/topic_localizer.rbindex b5b609a3ea1..6bf437d2765 100644--- a/plugins/discourse-ai/lib/translation/topic_localizer.rb+++ b/plugins/discourse-ai/lib/translation/topic_localizer.rb@@ -22,13 +22,15 @@ module DiscourseAitopic:,llm_model: topic_title_llm_model,).translate+ translated_excerpt = nil+translated_excerpt =PostRawTranslator.new(text: topic.excerpt,target_locale:,topic:,llm_model: post_raw_llm_model,- ).translate+ ).translate if topic.excerpt.present?localization =TopicLocalization.find_or_initialize_by(topic_id: topic.id, locale: target_locale)diff --git a/spec/models/topic_localization_spec.rb b/spec/models/topic_localization_spec.rbnew file mode 100644index 00000000000..21052f43573--- /dev/null+++ b/spec/models/topic_localization_spec.rb@@ -0,0 +1,34 @@+# frozen_string_literal: true++describe TopicLocalization do+ describe "#update_excerpt" do+ fab!(:topic)+ fab!(:topic_localization) do+ Fabricate(:topic_localization, topic: topic, locale: "ja", title: "日本語タイトル")+ end++ it "updates excerpt from cooked content" do+ topic_localization.update_excerpt(cooked: "<p>これは投稿の内容です。</p>")++ expect(topic_localization.excerpt).to eq("これは投稿の内容です。")+ end++ it "does nothing when cooked is empty" do+ topic_localization.update_excerpt(cooked: "")+ expect(topic_localization.excerpt).to be_nil++ topic_localization.update_excerpt(cooked: nil)+ expect(topic_localization.excerpt).to be_nil+ end++ it "strips links and images from excerpt" do+ cooked = <<~HTML+ <p>Check out <a href="https://example.com">this link</a> and <img src="test.jpg"> this image.</p>+ HTML++ topic_localization.update_excerpt(cooked:)++ expect(topic_localization.excerpt).to eq("Check out this link and this image.")+ end+ end+enddiff --git a/spec/tasks/topic_localizations_spec.rb b/spec/tasks/topic_localizations_spec.rbnew file mode 100644index 00000000000..655ea5661ab--- /dev/null+++ b/spec/tasks/topic_localizations_spec.rb@@ -0,0 +1,82 @@+# frozen_string_literal: true++describe "topic_localizations rake tasks" do+ around { |example| capture_stdout { example.run } }++ describe "topic_localizations:backfill_excerpts" do+ fab!(:topic)+ fab!(:first_post) { Fabricate(:post, topic:, post_number: 1) }++ it "backfills excerpt from post localization" do+ topic.update_column(:excerpt, "original excerpt")+ Fabricate(+ :post_localization,+ post: first_post,+ locale: "ja",+ raw: "これは投稿の内容です。",+ cooked: "<p>これは投稿の内容です。</p>",+ )+ topic_localization =+ Fabricate(:topic_localization, topic: topic, locale: "ja", title: "日本語タイトル")++ expect(topic_localization.excerpt).to be_nil++ invoke_rake_task("topic_localizations:backfill_excerpts")++ topic_localization.reload+ expect(topic_localization.excerpt).to eq("これは投稿の内容です。")+ end++ it "skips topic localizations that already have an excerpt" do+ Fabricate(+ :post_localization,+ post: first_post,+ locale: "ja",+ raw: "新しい内容",+ cooked: "<p>新しい内容</p>",+ )+ topic_localization =+ Fabricate(+ :topic_localization,+ topic: topic,+ locale: "ja",+ title: "日本語タイトル",+ excerpt: "既存の抜粋",+ )++ invoke_rake_task("topic_localizations:backfill_excerpts")++ topic_localization.reload+ expect(topic_localization.excerpt).to eq("既存の抜粋")+ end++ it "skips topic localizations without matching post localization" do+ topic.update_column(:excerpt, "original excerpt")+ topic_localization =+ Fabricate(:topic_localization, topic: topic, locale: "ja", title: "日本語タイトル")++ invoke_rake_task("topic_localizations:backfill_excerpts")++ topic_localization.reload+ expect(topic_localization.excerpt).to be_nil+ end++ it "skips topic localizations when topic has empty excerpt" do+ topic.update_column(:excerpt, "")+ Fabricate(+ :post_localization,+ post: first_post,+ locale: "ja",+ raw: "これは投稿の内容です。",+ cooked: "<p>これは投稿の内容です。</p>",+ )+ topic_localization =+ Fabricate(:topic_localization, topic: topic, locale: "ja", title: "日本語タイトル")++ invoke_rake_task("topic_localizations:backfill_excerpts")++ topic_localization.reload+ expect(topic_localization.excerpt).to be_nil+ end+ end+end