signal-desktop/ts/services/MessageUpdater.ts

57 lines
1.7 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
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';
import { SeenStatus } from '../MessageSeenStatus';
2023-03-27 23:48:57 +00:00
import { queueUpdateMessage } from '../util/messageBatcher';
function markReadOrViewed(
messageAttrs: Readonly<MessageAttributesType>,
readStatus: ReadStatus.Read | ReadStatus.Viewed,
timestamp: undefined | number,
skipSave: boolean
): MessageAttributesType {
const oldReadStatus = messageAttrs.readStatus ?? ReadStatus.Read;
const newReadStatus = maxReadStatus(oldReadStatus, readStatus);
const nextMessageAttributes: MessageAttributesType = {
...messageAttrs,
2022-09-21 23:54:48 +00:00
readAt: timestamp,
readStatus: newReadStatus,
seenStatus: SeenStatus.Seen,
};
const { id: messageId, expireTimer, expirationStartTimestamp } = messageAttrs;
if (expireTimer && !expirationStartTimestamp) {
nextMessageAttributes.expirationStartTimestamp = Math.min(
Date.now(),
timestamp || Date.now()
);
}
2021-09-23 18:16:09 +00:00
notificationService.removeBy({ messageId });
if (!skipSave) {
2023-03-27 23:48:57 +00:00
queueUpdateMessage(nextMessageAttributes);
}
return nextMessageAttributes;
}
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);