A platform for community discussion. Free, open, simple.
FIX: Do not show cached post after toggling "Show original post" and scrolling past 21st post (#35868)
Natalie Tay6982c053f654
diff --git a/app/helpers/topics_helper.rb b/app/helpers/topics_helper.rbindex c2d43a400b9..32cb3d9b50d 100644--- a/app/helpers/topics_helper.rb+++ b/app/helpers/topics_helper.rb@@ -22,6 +22,8 @@ module TopicsHelperenddef localize_topic_view_content(topic_view)+ return if cookies.key?(ContentLocalization::SHOW_ORIGINAL_COOKIE)+crawl_locale = params[Discourse::LOCALE_PARAM].presence || SiteSetting.default_localeLocalizationAttributesReplacer.replace_topic_attributes(topic_view.topic, crawl_locale)diff --git a/frontend/discourse/app/components/topic-localized-content-toggle.gjs b/frontend/discourse/app/components/topic-localized-content-toggle.gjsindex 8d68685e483..3a154b9b833 100644--- a/frontend/discourse/app/components/topic-localized-content-toggle.gjs+++ b/frontend/discourse/app/components/topic-localized-content-toggle.gjs@@ -30,7 +30,19 @@ export default class TopicLocalizedContentToggle extends Component {});}- this.router.refresh();+ const postStream = this.args.topic?.postStream;+ if (postStream) {+ const currentURL = this.router.currentURL;+ // this is required to clear the post stream cache+ // otherwise the old posts before the toggle will be shown+ postStream.removeAllPosts();+ await this.router.refresh();+ // refreshing clears the post number,+ // and postStream.refresh nearPost does not load to the correct post+ if (this.router.currentURL !== currentURL) {+ this.router.replaceWith(currentURL);+ }+ }}get title() {diff --git a/frontend/discourse/app/models/post-stream.js b/frontend/discourse/app/models/post-stream.jsindex 906d59c8a87..c6467604486 100644--- a/frontend/discourse/app/models/post-stream.js+++ b/frontend/discourse/app/models/post-stream.js@@ -649,6 +649,11 @@ export default class PostStream extends RestModel {postIds.forEach((id) => delete identityMap[id]);}+ removeAllPosts() {+ const posts = Object.values(this._identityMap).filter((p) => p);+ this.removePosts(posts);+ }+// Returns a post from the identity map if it's been inserted.findLoadedPost(id) {return this._identityMap[id];diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 0ab83b2a4c2..bd4ff7a6c5b 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -3637,6 +3637,84 @@ RSpec.describe TopicsController doendendend++ describe "#posts with content localization" do+ fab!(:localized_post) do+ post = Fabricate(:post, user:, locale: "en", cooked: "<p>Original EN</p>")+ Fabricate(:post_localization, post:, locale: "ja", cooked: "<p>Translated JA</p>")+ post+ end+ fab!(:localized_topic) { localized_post.topic }+ fab!(:localized_post2) do+ post =+ Fabricate(+ :post,+ user:,+ topic: localized_topic,+ locale: "ja",+ cooked: "<p>Original 2 JA</p>",+ )+ Fabricate(:post_localization, post:, locale: "en", cooked: "<p>Translated 2 EN</p>")+ post+ end++ before do+ SiteSetting.content_localization_enabled = true+ I18n.locale = "en"+ end++ context "when show_original cookie is not set" do+ it "returns translated posts" do+ get "/t/#{localized_topic.id}/posts.json"++ expect(response.status).to eq(200)++ body = response.parsed_body+ posts = body["post_stream"]["posts"]++ expect(posts.first["cooked"]).to eq("<p>Original EN</p>")+ expect(posts.second["cooked"]).to eq("<p>Translated 2 EN</p>")+ end++ it "returns translated posts when loading specific post_ids" do+ get "/t/#{localized_topic.id}/posts.json", params: { post_ids: [localized_post2.id] }++ expect(response.status).to eq(200)++ body = response.parsed_body+ posts = body["post_stream"]["posts"]++ expect(posts.first["cooked"]).to eq("<p>Translated 2 EN</p>")+ end+ end++ context "when show_original cookie is set" do+ before { cookies[ContentLocalization::SHOW_ORIGINAL_COOKIE] = "true" }++ it "returns original posts" do+ get "/t/#{localized_topic.id}/posts.json"++ expect(response.status).to eq(200)++ body = response.parsed_body+ posts = body["post_stream"]["posts"]++ expect(posts.first["cooked"]).to eq("<p>Original EN</p>")+ expect(posts.second["cooked"]).to eq("<p>Original 2 JA</p>")+ end++ it "returns original posts when loading specific post_ids" do+ get "/t/#{localized_topic.id}/posts.json", params: { post_ids: [localized_post2.id] }++ expect(response.status).to eq(200)++ body = response.parsed_body+ posts = body["post_stream"]["posts"]++ expect(posts.first["cooked"]).to eq("<p>Original 2 JA</p>")+ end+ end+ endenddescribe "#feed" dodiff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rbindex 65927939f88..44aa96067a6 100644--- a/spec/system/content_localization_spec.rb+++ b/spec/system/content_localization_spec.rb@@ -192,6 +192,63 @@ describe "Content Localization" doexpect(post_history_modal.previous_locale).to have_content("English (US)")endend++ context "when loading 20+ posts in stream", trace: true do+ before do+ highest = topic.highest_post_number+ 22.times do |i|+ post_number = i + highest + 1+ post =+ Fabricate(+ :post,+ topic: topic,+ locale: "ja",+ raw: "Japanese content for post #{post_number}",+ cooked: "<p>日本語コンテンツ #{post_number}</p>",+ )++ Fabricate(+ :post_localization,+ post:,+ locale: "en",+ cooked: "<p>English translation #{post_number}</p>",+ )+ end+ end++ let(:post_21_obj) { PageObjects::Components::Post.new(21) }++ it "respects the show_original toggle for posts loaded dynamically when scrolling (20+ posts)" do+ sign_in(site_local_user)+ visit("/")++ topic_page.visit_topic(topic)++ expect(post_3_obj.post).to have_content("A general is one who ..")+ expect(topic_page).to have_post_content(post_number: 3, content: "A general is one who ..")++ 5.times do+ break if page.has_css?("#post_21 .cooked", visible: :all, wait: 0)+ page.execute_script("window.scrollTo(0, document.body.scrollHeight)")+ end++ expect(page).to have_css("#post_21")+ expect(topic_page).to have_post_content(post_number: 21, content: "English translation 21")++ page.find(TOGGLE_LOCALIZE_BUTTON_SELECTOR).click+ expect(post_3_obj.post).to have_content("将とは、智・信・仁・勇・厳なり。")++ 5.times do+ break if page.has_css?("#post_21 .cooked", visible: :all, wait: 0)+ page.execute_script("window.scrollTo(0, document.body.scrollHeight)")+ end++ expect(post_21_obj.post).to have_content("日本語コンテンツ 21")++ page.refresh+ expect(post_3_obj.post).to have_content("将とは、智・信・仁・勇・厳なり。")+ end+ endendcontext "for site settings" do