2021-05-07 01:15:25 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
import type { MessageAttributesType } from '../model-types.d';
|
|
|
|
import { ReadStatus, maxReadStatus } from '../messages/MessageReadStatus';
|
2021-09-23 18:16:09 +00:00
|
|
|
import { notificationService } from './notifications';
|
2022-04-29 23:42:47 +00:00
|
|
|
import { SeenStatus } from '../MessageSeenStatus';
|
2023-03-27 23:48:57 +00:00
|
|
|
import { queueUpdateMessage } from '../util/messageBatcher';
|
2021-05-07 01:15:25 +00:00
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
function markReadOrViewed(
|
|
|
|
messageAttrs: Readonly<MessageAttributesType>,
|
|
|
|
readStatus: ReadStatus.Read | ReadStatus.Viewed,
|
|
|
|
timestamp: undefined | number,
|
|
|
|
skipSave: boolean
|
2021-05-07 01:15:25 +00:00
|
|
|
): MessageAttributesType {
|
2021-08-12 18:15:55 +00:00
|
|
|
const oldReadStatus = messageAttrs.readStatus ?? ReadStatus.Read;
|
|
|
|
const newReadStatus = maxReadStatus(oldReadStatus, readStatus);
|
|
|
|
|
|
|
|
const nextMessageAttributes: MessageAttributesType = {
|
2021-05-07 01:15:25 +00:00
|
|
|
...messageAttrs,
|
2022-09-21 23:54:48 +00:00
|
|
|
readAt: timestamp,
|
2021-08-12 18:15:55 +00:00
|
|
|
readStatus: newReadStatus,
|
2022-04-29 23:42:47 +00:00
|
|
|
seenStatus: SeenStatus.Seen,
|
2021-05-07 01:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const { id: messageId, expireTimer, expirationStartTimestamp } = messageAttrs;
|
|
|
|
|
|
|
|
if (expireTimer && !expirationStartTimestamp) {
|
|
|
|
nextMessageAttributes.expirationStartTimestamp = Math.min(
|
|
|
|
Date.now(),
|
2021-08-12 18:15:55 +00:00
|
|
|
timestamp || Date.now()
|
2021-05-07 01:15:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-23 18:16:09 +00:00
|
|
|
notificationService.removeBy({ messageId });
|
2021-05-07 01:15:25 +00:00
|
|
|
|
|
|
|
if (!skipSave) {
|
2023-03-27 23:48:57 +00:00
|
|
|
queueUpdateMessage(nextMessageAttributes);
|
2021-05-07 01:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nextMessageAttributes;
|
|
|
|
}
|
2021-08-12 18:15:55 +00:00
|
|
|
|
|
|
|
export const markRead = (
|
|
|
|
messageAttrs: Readonly<MessageAttributesType>,
|
|
|
|
readAt?: number,
|
|
|
|
{ skipSave = false } = {}
|
|
|
|
): MessageAttributesType =>
|
|
|
|
markReadOrViewed(messageAttrs, ReadStatus.Read, readAt, skipSave);
|
|
|
|
|
|
|
|
export const markViewed = (
|
|
|
|
messageAttrs: Readonly<MessageAttributesType>,
|
|
|
|
viewedAt?: number,
|
|
|
|
{ skipSave = false } = {}
|
|
|
|
): MessageAttributesType =>
|
|
|
|
markReadOrViewed(messageAttrs, ReadStatus.Viewed, viewedAt, skipSave);
|