You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { Message } from './Message';
|
|
|
|
interface ConversationLastMessageUpdate {
|
|
lastMessage: string;
|
|
lastMessageStatus: string | null;
|
|
timestamp: number | null;
|
|
}
|
|
|
|
export const createLastMessageUpdate = ({
|
|
currentTimestamp,
|
|
lastMessage,
|
|
lastMessageStatus,
|
|
lastMessageNotificationText,
|
|
}: {
|
|
currentTimestamp?: number;
|
|
lastMessage?: Message;
|
|
lastMessageStatus?: string;
|
|
lastMessageNotificationText?: string;
|
|
}): ConversationLastMessageUpdate => {
|
|
if (!lastMessage) {
|
|
return {
|
|
lastMessage: '',
|
|
lastMessageStatus: null,
|
|
timestamp: null,
|
|
};
|
|
}
|
|
|
|
const { type, expirationTimerUpdate } = lastMessage;
|
|
const isVerifiedChangeMessage = type === 'verified-change';
|
|
const isExpireTimerUpdateFromSync = Boolean(
|
|
expirationTimerUpdate && expirationTimerUpdate.fromSync
|
|
);
|
|
|
|
const shouldUpdateTimestamp = Boolean(
|
|
!isVerifiedChangeMessage && !isExpireTimerUpdateFromSync
|
|
);
|
|
const newTimestamp = shouldUpdateTimestamp
|
|
? lastMessage.sent_at
|
|
: currentTimestamp;
|
|
|
|
const shouldUpdateLastMessageText = !isVerifiedChangeMessage;
|
|
const newLastMessageText = shouldUpdateLastMessageText
|
|
? lastMessageNotificationText
|
|
: '';
|
|
|
|
return {
|
|
lastMessage: newLastMessageText || '',
|
|
lastMessageStatus: lastMessageStatus || null,
|
|
timestamp: newTimestamp || null,
|
|
};
|
|
};
|