From 31bd7a69556002f84986c24c53c125a4d57b15d5 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 2 Oct 2023 14:32:25 +0200 Subject: [PATCH] MOBILE-4362 messages: Update sender name when last message changes The sender name displayed in group conversations page wasn't updated when a NEW_MESSAGE_EVENT was received. Also, if last message was deleted we still displayed a sender name when there were no messages. --- .../messages/pages/discussion/discussion.ts | 15 ++++++------ .../pages/discussions-35/discussions.ts | 2 +- .../group-conversations.html | 2 +- .../group-conversations.ts | 23 +++++++++++++++---- src/addons/messages/services/messages.ts | 3 ++- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/addons/messages/pages/discussion/discussion.ts b/src/addons/messages/pages/discussion/discussion.ts index fcc8dc0c1..307167767 100644 --- a/src/addons/messages/pages/discussion/discussion.ts +++ b/src/addons/messages/pages/discussion/discussion.ts @@ -65,7 +65,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView protected messagesBeingSent = 0; protected pagesLoaded = 1; - protected lastMessage = { text: '', timecreated: 0 }; + protected lastMessage?: { text: string; timecreated: number }; protected keepMessageMap: {[hash: string]: boolean} = {}; protected syncObserver: CoreEventObserver; protected oldContentHeight = 0; @@ -465,7 +465,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView // If we received a new message while using group messaging, force mark messages as read. const last = this.messages[this.messages.length - 1]; - const forceMark = this.groupMessagingEnabled && last && last.useridfrom != this.currentUserId && this.lastMessage.text != '' + const forceMark = this.groupMessagingEnabled && last && last.useridfrom !== this.currentUserId && !!this.lastMessage && (last.text !== this.lastMessage.text || last.timecreated !== this.lastMessage.timecreated); // Notify that there can be a new message. @@ -773,14 +773,14 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView * Notify the last message found so discussions list controller can tell if last message should be updated. */ protected notifyNewMessage(): void { - const last = this.messages[this.messages.length - 1]; + const last = this.messages[this.messages.length - 1] as AddonMessagesConversationMessageFormatted | undefined; let trigger = false; if (!last) { - this.lastMessage = { text: '', timecreated: 0 }; + this.lastMessage = undefined; trigger = true; - } else if (last.text !== this.lastMessage.text || last.timecreated !== this.lastMessage.timecreated) { + } else if (last.text !== this.lastMessage?.text || last.timecreated !== this.lastMessage?.timecreated) { this.lastMessage = { text: last.text || '', timecreated: last.timecreated }; trigger = true; } @@ -790,8 +790,9 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView CoreEvents.trigger(AddonMessagesProvider.NEW_MESSAGE_EVENT, { conversationId: this.conversationId, userId: this.userId, - message: this.lastMessage.text, - timecreated: this.lastMessage.timecreated, + message: this.lastMessage?.text, + timecreated: this.lastMessage?.timecreated ?? 0, + userFrom: last?.useridfrom ? this.members[last.useridfrom] : undefined, isfavourite: !!this.conversation?.isfavourite, type: this.conversation?.type, }, this.siteId); diff --git a/src/addons/messages/pages/discussions-35/discussions.ts b/src/addons/messages/pages/discussions-35/discussions.ts index 3b597d0c7..f82f985ae 100644 --- a/src/addons/messages/pages/discussions-35/discussions.ts +++ b/src/addons/messages/pages/discussions-35/discussions.ts @@ -89,7 +89,7 @@ export class AddonMessagesDiscussions35Page implements OnInit, OnDestroy { }); } else if (discussion.message) { // An existing discussion has a new message, update the last message. - discussion.message.message = data.message; + discussion.message.message = data.message ?? ''; discussion.message.timecreated = data.timecreated; } } diff --git a/src/addons/messages/pages/group-conversations/group-conversations.html b/src/addons/messages/pages/group-conversations/group-conversations.html index 109cedd73..9cca75e2d 100644 --- a/src/addons/messages/pages/group-conversations/group-conversations.html +++ b/src/addons/messages/pages/group-conversations/group-conversations.html @@ -185,7 +185,7 @@

-

+

{{ 'addon.messages.you' | translate }} diff --git a/src/addons/messages/pages/group-conversations/group-conversations.ts b/src/addons/messages/pages/group-conversations/group-conversations.ts index d250f26ad..0250072b8 100644 --- a/src/addons/messages/pages/group-conversations/group-conversations.ts +++ b/src/addons/messages/pages/group-conversations/group-conversations.ts @@ -112,7 +112,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { this.newMessagesObserver = CoreEvents.on( AddonMessagesProvider.NEW_MESSAGE_EVENT, (data) => { - // Check if the new message belongs to the option that is currently expanded. + // Check if the new message belongs to the option that is currently expanded. const expandedOption = this.getExpandedOption(); const messageOption = this.getConversationOption(data); @@ -124,7 +124,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { const conversation = this.findConversation(data.conversationId, data.userId, expandedOption); if (conversation === undefined) { - // Probably a new conversation, refresh the list. + // Probably a new conversation, refresh the list. this.loaded = false; this.refreshData().finally(() => { this.loaded = true; @@ -132,19 +132,34 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { return; } - if (conversation.lastmessage != data.message || conversation.lastmessagedate != data.timecreated / 1000) { + + if (data.message === undefined) { + conversation.lastmessage = undefined; + conversation.lastmessagedate = undefined; + conversation.sentfromcurrentuser = undefined; + + return; + } + + if (conversation.lastmessage !== data.message || conversation.lastmessagedate !== data.timecreated / 1000) { const isNewer = data.timecreated / 1000 > (conversation.lastmessagedate || 0); // An existing conversation has a new message, update the last message. conversation.lastmessage = data.message; conversation.lastmessagedate = data.timecreated / 1000; + if (data.userFrom) { + conversation.sentfromcurrentuser = data.userFrom.id === this.currentUserId; + if (conversation.type === AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP) { + conversation.members[0] = data.userFrom; + } + } // Sort the affected list. const option = this.getConversationOption(conversation); option.conversations = AddonMessages.sortConversations(option.conversations || []); if (isNewer) { - // The last message is newer than the previous one, scroll to top to keep viewing the conversation. + // The last message is newer than the previous one, scroll to top to keep viewing the conversation. this.content?.scrollToTop(); } } diff --git a/src/addons/messages/services/messages.ts b/src/addons/messages/services/messages.ts index 8ec8b37f9..5fbe30c06 100644 --- a/src/addons/messages/services/messages.ts +++ b/src/addons/messages/services/messages.ts @@ -3669,8 +3669,9 @@ export type AddonMessagesReadChangedEventData = { export type AddonMessagesNewMessagedEventData = { conversationId?: number; userId?: number; - message: string; + message?: string; // If undefined it means the conversation has no messages, e.g. last message was deleted. timecreated: number; + userFrom?: AddonMessagesConversationMember; isfavourite: boolean; type?: number; };