Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Filter DeleteMessage when computing the oldest loaded message [WPB-3506] #15686

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 =>
tlebon marked this conversation as resolved.
Show resolved Hide resolved
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
Loading