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.
main
Dani Palou 2023-10-02 14:32:25 +02:00
parent 28235b0553
commit 31bd7a6955
5 changed files with 31 additions and 14 deletions

View File

@ -65,7 +65,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView
protected messagesBeingSent = 0; protected messagesBeingSent = 0;
protected pagesLoaded = 1; protected pagesLoaded = 1;
protected lastMessage = { text: '', timecreated: 0 }; protected lastMessage?: { text: string; timecreated: number };
protected keepMessageMap: {[hash: string]: boolean} = {}; protected keepMessageMap: {[hash: string]: boolean} = {};
protected syncObserver: CoreEventObserver; protected syncObserver: CoreEventObserver;
protected oldContentHeight = 0; 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. // If we received a new message while using group messaging, force mark messages as read.
const last = this.messages[this.messages.length - 1]; 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); && (last.text !== this.lastMessage.text || last.timecreated !== this.lastMessage.timecreated);
// Notify that there can be a new message. // 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. * Notify the last message found so discussions list controller can tell if last message should be updated.
*/ */
protected notifyNewMessage(): void { 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; let trigger = false;
if (!last) { if (!last) {
this.lastMessage = { text: '', timecreated: 0 }; this.lastMessage = undefined;
trigger = true; 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 }; this.lastMessage = { text: last.text || '', timecreated: last.timecreated };
trigger = true; trigger = true;
} }
@ -790,8 +790,9 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView
CoreEvents.trigger(AddonMessagesProvider.NEW_MESSAGE_EVENT, { CoreEvents.trigger(AddonMessagesProvider.NEW_MESSAGE_EVENT, {
conversationId: this.conversationId, conversationId: this.conversationId,
userId: this.userId, userId: this.userId,
message: this.lastMessage.text, message: this.lastMessage?.text,
timecreated: this.lastMessage.timecreated, timecreated: this.lastMessage?.timecreated ?? 0,
userFrom: last?.useridfrom ? this.members[last.useridfrom] : undefined,
isfavourite: !!this.conversation?.isfavourite, isfavourite: !!this.conversation?.isfavourite,
type: this.conversation?.type, type: this.conversation?.type,
}, this.siteId); }, this.siteId);

View File

@ -89,7 +89,7 @@ export class AddonMessagesDiscussions35Page implements OnInit, OnDestroy {
}); });
} else if (discussion.message) { } else if (discussion.message) {
// An existing discussion has a new message, update the last 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; discussion.message.timecreated = data.timecreated;
} }
} }

View File

@ -185,7 +185,7 @@
<p *ngIf="conversation.subname"> <p *ngIf="conversation.subname">
<core-format-text [text]="conversation.subname" contextLevel="system" [contextInstanceId]="0"></core-format-text> <core-format-text [text]="conversation.subname" contextLevel="system" [contextInstanceId]="0"></core-format-text>
</p> </p>
<p class="addon-message-last-message"> <p *ngIf="conversation.lastmessage !== undefined" class="addon-message-last-message">
<span *ngIf="conversation.sentfromcurrentuser" class="addon-message-last-message-user"> <span *ngIf="conversation.sentfromcurrentuser" class="addon-message-last-message-user">
{{ 'addon.messages.you' | translate }} {{ 'addon.messages.you' | translate }}
</span> </span>

View File

@ -112,7 +112,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
this.newMessagesObserver = CoreEvents.on( this.newMessagesObserver = CoreEvents.on(
AddonMessagesProvider.NEW_MESSAGE_EVENT, AddonMessagesProvider.NEW_MESSAGE_EVENT,
(data) => { (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 expandedOption = this.getExpandedOption();
const messageOption = this.getConversationOption(data); const messageOption = this.getConversationOption(data);
@ -124,7 +124,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
const conversation = this.findConversation(data.conversationId, data.userId, expandedOption); const conversation = this.findConversation(data.conversationId, data.userId, expandedOption);
if (conversation === undefined) { if (conversation === undefined) {
// Probably a new conversation, refresh the list. // Probably a new conversation, refresh the list.
this.loaded = false; this.loaded = false;
this.refreshData().finally(() => { this.refreshData().finally(() => {
this.loaded = true; this.loaded = true;
@ -132,19 +132,34 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
return; 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); const isNewer = data.timecreated / 1000 > (conversation.lastmessagedate || 0);
// An existing conversation has a new message, update the last message. // An existing conversation has a new message, update the last message.
conversation.lastmessage = data.message; conversation.lastmessage = data.message;
conversation.lastmessagedate = data.timecreated / 1000; 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. // Sort the affected list.
const option = this.getConversationOption(conversation); const option = this.getConversationOption(conversation);
option.conversations = AddonMessages.sortConversations(option.conversations || []); option.conversations = AddonMessages.sortConversations(option.conversations || []);
if (isNewer) { 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(); this.content?.scrollToTop();
} }
} }

View File

@ -3669,8 +3669,9 @@ export type AddonMessagesReadChangedEventData = {
export type AddonMessagesNewMessagedEventData = { export type AddonMessagesNewMessagedEventData = {
conversationId?: number; conversationId?: number;
userId?: number; userId?: number;
message: string; message?: string; // If undefined it means the conversation has no messages, e.g. last message was deleted.
timecreated: number; timecreated: number;
userFrom?: AddonMessagesConversationMember;
isfavourite: boolean; isfavourite: boolean;
type?: number; type?: number;
}; };