Add basic implementation of Conversation.updateFromLastMessage

This commit is contained in:
Daniel Gasienica
2018-04-11 18:49:46 -04:00
parent cca5db3237
commit 44debd123d
2 changed files with 147 additions and 0 deletions

45
ts/types/Conversation.ts Normal file
View 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,
};
};