Skip to content

Commit

Permalink
fix: Filter DeleteMessage when computing the oldest loaded message (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc committed Aug 30, 2023
1 parent 255e13b commit 4baea20
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/script/conversation/MessageRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ export class MessageRepository {
*/
public async deleteMessageById(conversationEntity: Conversation, messageId: string): Promise<number> {
const isLastDeleted =
conversationEntity.isShowingLastReceivedMessage() && conversationEntity.getNewestMessage()?.id === messageId;
conversationEntity.hasLastReceivedMessageLoaded() && conversationEntity.getNewestMessage()?.id === messageId;

const deleteCount = await this.eventService.deleteEvent(conversationEntity.id, messageId);
const previousMessage = conversationEntity.getNewestMessage();
Expand Down
21 changes: 13 additions & 8 deletions src/script/entity/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {ConversationStatus} from '../conversation/ConversationStatus';
import {ConversationVerificationState} from '../conversation/ConversationVerificationState';
import {NOTIFICATION_STATE} from '../conversation/NotificationSetting';
import {ConversationError} from '../error/ConversationError';
import {isContentMessage} from '../guards/Message';
import {isContentMessage, isDeleteMessage} from '../guards/Message';
import {StatusType} from '../message/StatusType';
import {ConversationRecord} from '../storage/record/ConversationRecord';
import {TeamState} from '../team/TeamState';
Expand Down Expand Up @@ -689,11 +689,13 @@ export class Conversation {
if (alreadyAdded) {
return false;
}
if (this.isShowingLastReceivedMessage()) {
if (this.hasLastReceivedMessageLoaded()) {
this.updateTimestamps(messageEntity);
this.incomingMessages.remove(({id}) => messageEntity.id === id);
// If the last received message is currently in memory, we can add this message to the displayed messages
this.messages_unordered.push(messageEntity);
} else {
// If the conversation is not loaded, we will add this message to the incoming messages (but not to the messages displayed)
this.incomingMessages.push(messageEntity);
}
amplify.publish(WebAppEvents.CONVERSATION.MESSAGE.ADDED, messageEntity);
Expand Down Expand Up @@ -917,10 +919,14 @@ export class Conversation {
}

/**
* Get the first message of the conversation.
* Get the oldest loaded message of the conversation.
*/
getOldestMessage(): Message | undefined {
return this.messages()[0];
return this.messages().find(
message =>
// Deleted message should be ignored since they might have a timestamp in the past (the timestamp of a delete message is the timestamp of the message that was deleted)
!isDeleteMessage(message),
);
}

/**
Expand Down Expand Up @@ -1018,10 +1024,9 @@ export class Conversation {
return participantCount <= config.MAX_VIDEO_PARTICIPANTS;
}

readonly isShowingLastReceivedMessage = (): boolean => {
return this.getNewestMessage()?.timestamp()
? this.getNewestMessage().timestamp() >= this.last_event_timestamp()
: true;
readonly hasLastReceivedMessageLoaded = (): boolean => {
const newestMessage = this.getNewestMessage();
return newestMessage?.timestamp() ? newestMessage.timestamp() >= this.last_event_timestamp() : true;
};

serialize(): ConversationRecord {
Expand Down
4 changes: 4 additions & 0 deletions src/script/guards/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import {Draft} from 'Util/DraftStateUtil';

import {ContentMessage} from '../entity/message/ContentMessage';
import {DeleteMessage} from '../entity/message/DeleteMessage';
import {MemberMessage} from '../entity/message/MemberMessage';
import {SuperType} from '../message/SuperType';

Expand All @@ -31,6 +32,9 @@ export const isReadableMessage = (message: any): message is ContentMessage =>
export const isContentMessage = (message: any): message is ContentMessage =>
message && 'super_type' in message && message.super_type === SuperType.CONTENT;

export const isDeleteMessage = (message: any): message is DeleteMessage =>
message && 'super_type' in message && message.super_type === SuperType.DELETE;

export const isMemberMessage = (message: any | undefined | null): message is MemberMessage =>
message && 'super_type' in message && message.super_type === SuperType.MEMBER;

Expand Down

0 comments on commit 4baea20

Please sign in to comment.