Add basic implementation of Conversation.updateFromLastMessage
This commit is contained in:
45
ts/types/Conversation.ts
Normal file
45
ts/types/Conversation.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import is from '@sindresorhus/is';
|
||||
import { Message } from './Message';
|
||||
|
||||
|
||||
interface ConversationLastMessageUpdate {
|
||||
lastMessage: string | null;
|
||||
timestamp: number | null;
|
||||
}
|
||||
|
||||
export const createLastMessageUpdate = ({
|
||||
currentLastMessageText,
|
||||
currentTimestamp,
|
||||
lastMessage,
|
||||
lastMessageNotificationText,
|
||||
}: {
|
||||
currentLastMessageText: string | null,
|
||||
currentTimestamp: number | null,
|
||||
lastMessage: Message | null,
|
||||
lastMessageNotificationText: string | null,
|
||||
}): ConversationLastMessageUpdate => {
|
||||
if (lastMessage === null) {
|
||||
return {
|
||||
lastMessage: '',
|
||||
timestamp: null,
|
||||
};
|
||||
}
|
||||
|
||||
const { type } = lastMessage;
|
||||
const isVerifiedChangeMessage = type === 'verified-change';
|
||||
const isExpiringMessage = is.object(lastMessage.expirationTimerUpdate);
|
||||
const shouldUpdateTimestamp = !isVerifiedChangeMessage && !isExpiringMessage;
|
||||
|
||||
const newTimestamp = shouldUpdateTimestamp ?
|
||||
lastMessage.sent_at :
|
||||
currentTimestamp;
|
||||
|
||||
const shouldUpdateLastMessageText = !isVerifiedChangeMessage;
|
||||
const newLastMessageText = shouldUpdateLastMessageText ?
|
||||
lastMessageNotificationText : currentLastMessageText;
|
||||
|
||||
return {
|
||||
lastMessage: newLastMessageText,
|
||||
timestamp: newTimestamp,
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user