Files
Signal-Desktop/ts/types/Conversation.ts
Daniel Gasienica 424965f876 🎨 Autoformat code
2018-04-25 15:24:51 -04:00

49 lines
1.2 KiB
TypeScript

/**
* @prettier
*/
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,
};
};