2021-11-29 07:40:46 +01:00
|
|
|
import { LastMessageStatusType } from '../state/ducks/conversations';
|
2018-04-12 00:49:46 +02:00
|
|
|
import { Message } from './Message';
|
|
|
|
|
|
|
|
interface ConversationLastMessageUpdate {
|
2019-01-14 22:49:58 +01:00
|
|
|
lastMessage: string;
|
2021-11-29 07:40:46 +01:00
|
|
|
lastMessageStatus: LastMessageStatusType;
|
|
|
|
timestamp: number | undefined;
|
2018-04-12 00:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const createLastMessageUpdate = ({
|
|
|
|
currentTimestamp,
|
|
|
|
lastMessage,
|
2018-07-18 05:25:55 +02:00
|
|
|
lastMessageStatus,
|
2018-04-12 00:49:46 +02:00
|
|
|
lastMessageNotificationText,
|
|
|
|
}: {
|
2019-01-14 22:49:58 +01:00
|
|
|
currentTimestamp?: number;
|
|
|
|
lastMessage?: Message;
|
2021-11-29 07:40:46 +01:00
|
|
|
lastMessageStatus?: LastMessageStatusType;
|
2019-01-14 22:49:58 +01:00
|
|
|
lastMessageNotificationText?: string;
|
2018-04-12 00:49:46 +02:00
|
|
|
}): ConversationLastMessageUpdate => {
|
2019-01-14 22:49:58 +01:00
|
|
|
if (!lastMessage) {
|
2018-04-12 00:49:46 +02:00
|
|
|
return {
|
|
|
|
lastMessage: '',
|
2021-11-29 07:40:46 +01:00
|
|
|
lastMessageStatus: undefined,
|
|
|
|
timestamp: undefined,
|
2018-04-12 00:49:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-18 00:58:34 +01:00
|
|
|
const { expirationTimerUpdate } = lastMessage;
|
2019-01-14 22:49:58 +01:00
|
|
|
const isExpireTimerUpdateFromSync = Boolean(
|
|
|
|
expirationTimerUpdate && expirationTimerUpdate.fromSync
|
|
|
|
);
|
2019-04-12 23:06:34 +02:00
|
|
|
|
2021-01-18 00:58:34 +01:00
|
|
|
const shouldUpdateTimestamp = Boolean(!isExpireTimerUpdateFromSync);
|
2021-04-22 10:03:58 +02:00
|
|
|
const newTimestamp = shouldUpdateTimestamp ? lastMessage.sent_at : currentTimestamp;
|
2018-04-12 00:49:46 +02:00
|
|
|
|
|
|
|
return {
|
2021-01-18 00:58:34 +01:00
|
|
|
lastMessage: lastMessageNotificationText || '',
|
2021-11-29 07:40:46 +01:00
|
|
|
lastMessageStatus: lastMessageStatus || undefined,
|
|
|
|
timestamp: newTimestamp || undefined,
|
2018-04-12 00:49:46 +02:00
|
|
|
};
|
|
|
|
};
|