From 09f2a42f5fba26e994b8c4c6974a2d428d3ad618 Mon Sep 17 00:00:00 2001 From: David Battersby Date: Fri, 26 Apr 2024 14:29:35 +0800 Subject: [PATCH] FIX: build chat message excerpt for thread preview (#26765) Follow up to #26712 to account for older threads that don't have a persisted excerpt, as this was previously generated on every page load. This change allows us to build the excerpt on the fly when none exists, fixing the issue of missing message excerpts for thread previews (within channel) and thread lists (on mobile/desktop). --- plugins/chat/app/models/chat/null_message.rb | 2 +- .../chat/thread_original_message_serializer.rb | 4 ++++ .../app/serializers/chat/thread_preview_serializer.rb | 2 +- plugins/chat/spec/models/chat/null_message_spec.rb | 4 ++-- .../chat/spec/system/message_thread_indicator_spec.rb | 9 +++++++++ plugins/chat/spec/system/thread_list/full_page_spec.rb | 9 +++++++++ 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/plugins/chat/app/models/chat/null_message.rb b/plugins/chat/app/models/chat/null_message.rb index 44a92fad60c..2815d970c3d 100644 --- a/plugins/chat/app/models/chat/null_message.rb +++ b/plugins/chat/app/models/chat/null_message.rb @@ -6,7 +6,7 @@ module Chat nil end - def excerpt(max_length: nil) + def build_excerpt nil end diff --git a/plugins/chat/app/serializers/chat/thread_original_message_serializer.rb b/plugins/chat/app/serializers/chat/thread_original_message_serializer.rb index 9fda1a0946f..1e1bbab3653 100644 --- a/plugins/chat/app/serializers/chat/thread_original_message_serializer.rb +++ b/plugins/chat/app/serializers/chat/thread_original_message_serializer.rb @@ -12,6 +12,10 @@ module Chat :mentioned_users, :user + def excerpt + object.excerpt || object.build_excerpt + end + def mentioned_users object .user_mentions diff --git a/plugins/chat/app/serializers/chat/thread_preview_serializer.rb b/plugins/chat/app/serializers/chat/thread_preview_serializer.rb index 65ba953c4e3..3a5c5db7f52 100644 --- a/plugins/chat/app/serializers/chat/thread_preview_serializer.rb +++ b/plugins/chat/app/serializers/chat/thread_preview_serializer.rb @@ -28,7 +28,7 @@ module Chat end def last_reply_excerpt - object.last_message.excerpt + object.last_message.excerpt || object.last_message.build_excerpt end def last_reply_user diff --git a/plugins/chat/spec/models/chat/null_message_spec.rb b/plugins/chat/spec/models/chat/null_message_spec.rb index 0a3b92425e2..65efc5cecc8 100644 --- a/plugins/chat/spec/models/chat/null_message_spec.rb +++ b/plugins/chat/spec/models/chat/null_message_spec.rb @@ -9,9 +9,9 @@ describe Chat::NullMessage do end end - describe "#excerpt" do + describe "#build_excerpt" do it "returns nil" do - expect(null_message.excerpt(max_length: 1)).to be_nil + expect(null_message.build_excerpt).to be_nil end end diff --git a/plugins/chat/spec/system/message_thread_indicator_spec.rb b/plugins/chat/spec/system/message_thread_indicator_spec.rb index 60a62473b96..2d8a4ea06a9 100644 --- a/plugins/chat/spec/system/message_thread_indicator_spec.rb +++ b/plugins/chat/spec/system/message_thread_indicator_spec.rb @@ -137,6 +137,15 @@ describe "Thread indicator for chat messages", type: :system do ).to have_content(thread_excerpt(thread_1.last_message.reload)) end + it "builds an excerpt for the last reply if it doesn’t have one" do + thread_1.last_message.update!(excerpt: nil) + chat_page.visit_channel(channel) + + expect( + channel_page.message_thread_indicator(thread_1.original_message).excerpt, + ).to have_content(thread_1.last_message.build_excerpt) + end + it "updates the last reply excerpt and participants when a new message is added to the thread" do new_user = Fabricate(:user) chat_system_user_bootstrap(user: new_user, channel: channel) diff --git a/plugins/chat/spec/system/thread_list/full_page_spec.rb b/plugins/chat/spec/system/thread_list/full_page_spec.rb index 4a719666bd7..03072d857a4 100644 --- a/plugins/chat/spec/system/thread_list/full_page_spec.rb +++ b/plugins/chat/spec/system/thread_list/full_page_spec.rb @@ -111,6 +111,15 @@ describe "Thread list in side panel | full page", type: :system do ) end + it "builds an excerpt for the original message if it doesn’t have one" do + thread_1.original_message.update!(excerpt: nil) + chat_page.visit_threads_list(channel) + + expect(thread_list_page.item_by_id(thread_1.id)).to have_content( + thread_1.original_message.build_excerpt, + ) + end + it "doesn’t show the thread original message user avatar" do chat_page.visit_threads_list(channel)