A platform for community discussion. Free, open, simple.
FIX: Update topic excerpts when the first post is localized (#36885)
Natalie Tay73a227089af8
diff --git a/app/jobs/regular/process_localized_cooked.rb b/app/jobs/regular/process_localized_cooked.rbindex b8451442333..ba19e0969de 100644--- a/app/jobs/regular/process_localized_cooked.rb+++ b/app/jobs/regular/process_localized_cooked.rb@@ -18,6 +18,21 @@ module Jobscooked = processor.html.strippost_localization.update_column(:cooked, cooked) if cooked.present?++ if 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+ end+MessageBus.publish("/topic/#{post.topic_id}", type: :localized, id: post.id)endenddiff --git a/lib/topic_localization_creator.rb b/lib/topic_localization_creator.rbindex 0ef3b227413..50d872a4a60 100644--- a/lib/topic_localization_creator.rb+++ b/lib/topic_localization_creator.rb@@ -4,12 +4,28 @@ class TopicLocalizationCreatordef self.create(topic:, locale:, title:, user:)Guardian.new(user).ensure_can_localize_topic!(topic)+ excerpt = nil+ first_post = topic.first_post+ if first_post+ post_localization = first_post.localizations.find_by(locale:)+ if post_localization+ excerpt =+ Post.excerpt(+ post_localization.cooked,+ SiteSetting.topic_excerpt_maxlength,+ strip_links: true,+ strip_images: true,+ )+ end+ end+TopicLocalization.create!(topic_id: topic.id,locale: locale,title: title,fancy_title: Topic.fancy_title(title),localizer_user_id: user.id,+ excerpt: excerpt,)endenddiff --git a/spec/jobs/process_localized_cooked_spec.rb b/spec/jobs/process_localized_cooked_spec.rbindex b4f367566ed..9afa10c9f91 100644--- a/spec/jobs/process_localized_cooked_spec.rb+++ b/spec/jobs/process_localized_cooked_spec.rb@@ -91,4 +91,51 @@ describe Jobs::ProcessLocalizedCooked dopost_localization.reloadexpect(post_localization.cooked).to include("onebox")end++ describe "topic localization excerpt" do+ fab!(:topic)+ fab!(:first_post) { Fabricate(:post, topic: topic, post_number: 1) }+ fab!(:first_post_localization) do+ Fabricate(+ :post_localization,+ post: first_post,+ locale: "ja",+ raw: "これは最初の投稿です。",+ cooked: "<p>これは最初の投稿です。</p>",+ )+ end+ fab!(:topic_localization) do+ Fabricate(:topic_localization, topic: topic, locale: "ja", title: "日本語タイトル")+ end++ it "updates topic localization excerpt when processing first post" do+ job.execute(post_localization_id: first_post_localization.id)++ topic_localization.reload+ expect(topic_localization.excerpt).to eq("これは最初の投稿です。")+ end++ it "does not update excerpt when processing non-first post" do+ second_post = Fabricate(:post, topic: topic, post_number: 2)+ second_post_localization =+ Fabricate(+ :post_localization,+ post: second_post,+ locale: "ja",+ raw: "これは2番目の投稿です。",+ cooked: "<p>これは2番目の投稿です。</p>",+ )++ job.execute(post_localization_id: second_post_localization.id)++ topic_localization.reload+ expect(topic_localization.excerpt).to be_nil+ end++ it "does not error when topic localization does not exist" do+ topic_localization.destroy!++ expect { job.execute(post_localization_id: first_post_localization.id) }.not_to raise_error+ end+ endenddiff --git a/spec/lib/topic_localization_creator_spec.rb b/spec/lib/topic_localization_creator_spec.rbindex b3f821ddb6e..c9fd01447f8 100644--- a/spec/lib/topic_localization_creator_spec.rb+++ b/spec/lib/topic_localization_creator_spec.rb@@ -60,4 +60,42 @@ describe TopicLocalizationCreator do}.to raise_error(Discourse::InvalidAccess)endend++ describe "excerpt from existing post localization" do+ fab!(:first_post) { Fabricate(:post, topic: topic, post_number: 1) }++ it "sets excerpt from existing post localization" do+ Fabricate(+ :post_localization,+ post: first_post,+ locale: locale,+ raw: "これは投稿の内容です。",+ cooked: "<p>これは投稿の内容です。</p>",+ )++ localization = described_class.create(topic:, locale:, title:, user:)++ expect(localization.excerpt).to eq("これは投稿の内容です。")+ end++ it "sets excerpt to nil when no post localization exists" do+ localization = described_class.create(topic:, locale:, title:, user:)++ expect(localization.excerpt).to be_nil+ end++ it "sets excerpt to nil when post localization exists for different locale" do+ Fabricate(+ :post_localization,+ post: first_post,+ locale: "fr",+ raw: "Ceci est le contenu du post.",+ cooked: "<p>Ceci est le contenu du post.</p>",+ )++ localization = described_class.create(topic:, locale:, title:, user:)++ expect(localization.excerpt).to be_nil+ end+ endend