From 105f38c04aca7e67893677e48a2481e3a28c82b2 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Mon, 5 Dec 2022 14:47:01 +0100 Subject: [PATCH] FIX: ensures chat channel metadata date is updated (#19314) --- .../components/chat-channel-metadata.js | 20 +++++++++-------- .../javascripts/discourse/services/chat.js | 13 ++++------- .../components/chat-channel-metadata-test.js | 8 ++++++- .../javascripts/unit/services/chat-test.js | 22 +++++++++++++++++++ 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-channel-metadata.js b/plugins/chat/assets/javascripts/discourse/components/chat-channel-metadata.js index 34796d5657c..404cd7ebd4e 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat-channel-metadata.js +++ b/plugins/chat/assets/javascripts/discourse/components/chat-channel-metadata.js @@ -1,16 +1,18 @@ import Component from "@glimmer/component"; - export default class ChatChannelMetadata extends Component { unreadIndicator = false; get lastMessageFormatedDate() { - return moment(this.args.channel.last_message_sent_at).calendar(null, { - sameDay: "LT", - nextDay: "[Tomorrow]", - nextWeek: "dddd", - lastDay: "[Yesterday]", - lastWeek: "dddd", - sameElse: "l", - }); + return moment(this.args.channel.get("last_message_sent_at")).calendar( + null, + { + sameDay: "LT", + nextDay: "[Tomorrow]", + nextWeek: "dddd", + lastDay: "[Yesterday]", + lastWeek: "dddd", + sameElse: "l", + } + ); } } diff --git a/plugins/chat/assets/javascripts/discourse/services/chat.js b/plugins/chat/assets/javascripts/discourse/services/chat.js index aa866f4e153..eba100a4d4a 100644 --- a/plugins/chat/assets/javascripts/discourse/services/chat.js +++ b/plugins/chat/assets/javascripts/discourse/services/chat.js @@ -748,16 +748,11 @@ export default class Chat extends Service { } } this.userChatChannelTrackingStateChanged(); + channel.set("last_message_sent_at", new Date()); - // Update last_message_sent_at timestamp for channel if direct message - const dmChatChannel = (this.directMessageChannels || []).findBy( - "id", - parseInt(channel.id, 10) - ); - if (dmChatChannel) { - dmChatChannel.set("last_message_sent_at", new Date()); - this.reSortDirectMessageChannels(); - } + (this.directMessageChannels || []) + .findBy("id", parseInt(channel.id, 10)) + ?.reSortDirectMessageChannels?.(); }, channel.message_bus_last_ids.new_messages ); diff --git a/plugins/chat/test/javascripts/components/chat-channel-metadata-test.js b/plugins/chat/test/javascripts/components/chat-channel-metadata-test.js index 8605b4fb534..285e10416f4 100644 --- a/plugins/chat/test/javascripts/components/chat-channel-metadata-test.js +++ b/plugins/chat/test/javascripts/components/chat-channel-metadata-test.js @@ -9,12 +9,18 @@ module("Discourse Chat | Component | chat-channel-metadata", function (hooks) { setupRenderingTest(hooks); test("displays last message sent at", async function (assert) { - const lastMessageSentAt = moment(); + let lastMessageSentAt = moment().subtract(1, "day"); this.channel = fabricators.directMessageChatChannel({ last_message_sent_at: lastMessageSentAt, }); await render(hbs``); + assert.dom(".chat-channel-metadata__date").hasText("Yesterday"); + + lastMessageSentAt = moment(); + this.channel.set("last_message_sent_at", lastMessageSentAt); + await render(hbs``); + assert .dom(".chat-channel-metadata__date") .hasText(lastMessageSentAt.format("LT")); diff --git a/plugins/chat/test/javascripts/unit/services/chat-test.js b/plugins/chat/test/javascripts/unit/services/chat-test.js index f08d6730dab..9aa59b49b4e 100644 --- a/plugins/chat/test/javascripts/unit/services/chat-test.js +++ b/plugins/chat/test/javascripts/unit/services/chat-test.js @@ -134,6 +134,28 @@ acceptance("Discourse Chat | Unit | Service | chat", function (needs) { ); }); + test("new message", async function (assert) { + setupMockPresenceChannel(this.chatService); + await this.chatService.forceRefreshChannels(); + + await publishToMessageBus("/chat/1/new-messages", { + user_id: this.currentUser.id, + username: this.currentUser.username, + message_id: 124, + }); + + assert.equal( + this.currentUser.chat_channel_tracking_state[1].chat_message_id, + 124, + "updates tracking state last message id to the message id sent by current user" + ); + assert.equal( + this.currentUser.chat_channel_tracking_state[1].unread_count, + 2, + "does not increment unread count" + ); + }); + test("/chat/:channelId/new-messages - message from current user", async function (assert) { setupMockPresenceChannel(this.chatService); await this.chatService.forceRefreshChannels();