Skip to content

A platform for community discussion. Free, open, simple.

FIX: Disappearing last-visit marker bug in chat (#40506)

Jarek Radosz5dbbc6e04e32
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
index 70edcb41d2b..30e1e85e660 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
@@ -327,7 +327,15 @@ export default class ChatChannel extends Component {
processMessages(channel, result) {
const messages = [];
let foundFirstNew = false;
- channel.newestMessage = null;
+
+ // Only compute the newest message marker on a full load.
+ // In an already loaded list we need to preserve the "last visit" line.
+ const hasNewest = this.messagesManager.messages.some(
+ (m) => m.id === channel.newestMessage?.id
+ );
+ if (!hasNewest) {
+ channel.newestMessage = null;
+ }
result?.messages?.forEach((messageData, index) => {
messageData.firstOfResults = index === 0;
@@ -353,6 +361,7 @@ export default class ChatChannel extends Component {
// newest has to be in after fetch callback as we don't want to make it
// dynamic or it will make the pane jump around, it will disappear on reload
if (
+ !hasNewest &&
!foundFirstNew &&
messageData.id > this.currentUserMembership?.lastReadMessageId
) {
diff --git a/plugins/chat/spec/system/last_visit_spec.rb b/plugins/chat/spec/system/last_visit_spec.rb
index 3727ddfbf9f..9fdf5e23478 100644
--- a/plugins/chat/spec/system/last_visit_spec.rb
+++ b/plugins/chat/spec/system/last_visit_spec.rb
@@ -45,4 +45,24 @@ RSpec.describe "Last visit" do
expect(channel_page).to have_last_visit_line_at_id(message_4.id)
end
+
+ it "keeps the last visit line when loading more messages" do
+ past_limit = Chat::MessagesQuery::PAST_MESSAGE_LIMIT
+
+ # more than a page of older messages, then the last read, then a couple
+ # of unread messages for the last visit line
+ messages = Fabricate.times(past_limit + 4, :chat_message, chat_channel: channel_1, user: user_1)
+ channel_1.membership_for(current_user).update!(last_read_message_id: messages[-3].id)
+ first_unread = messages[-2]
+
+ chat_page.visit_channel(channel_1)
+ expect(channel_page).to have_last_visit_line_at_id(first_unread.id)
+ expect(channel_page.messages).to have_no_message(id: messages.first.id)
+
+ # the older page loads in on scroll, and must not wipe the line
+ channel_page.scroll_to_top
+
+ expect(channel_page.messages).to have_message(id: messages.first.id)
+ expect(channel_page).to have_last_visit_line_at_id(first_unread.id)
+ end
end
diff --git a/plugins/chat/spec/system/page_objects/chat/chat_channel.rb b/plugins/chat/spec/system/page_objects/chat/chat_channel.rb
index 8e6f9f7692d..817ef943d9e 100644
--- a/plugins/chat/spec/system/page_objects/chat/chat_channel.rb
+++ b/plugins/chat/spec/system/page_objects/chat/chat_channel.rb
@@ -70,6 +70,18 @@ module PageObjects
has_no_css?(".chat-skeleton")
end
+ # Scrolls to the top to load the previous page of messages.
+ def scroll_to_top
+ 3.times do
+ page.execute_script(<<~JS)
+ const scroller = document.querySelector("#{PageObjects::Components::Chat::Messages::SELECTOR}");
+ scroller.scrollTop = -scroller.scrollHeight;
+ scroller.dispatchEvent(new Event("scroll"));
+ JS
+ sleep 0.1
+ end
+ end
+
def has_selection_management?
has_css?(".chat-selection-management")
end