Signal-Desktop/ts/types/message/initializeAttachmentMetadat...

49 lines
1.4 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2022 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import * as Attachment from '../Attachment';
import * as IndexedDB from '../IndexedDB';
import type { MessageAttributesType } from '../../model-types.d';
2021-11-11 22:43:05 +00:00
const hasAttachment =
(predicate: (value: Attachment.AttachmentType) => boolean) =>
(message: MessageAttributesType): IndexedDB.IndexablePresence =>
IndexedDB.toIndexablePresence((message.attachments || []).some(predicate));
const hasFileAttachment = hasAttachment(Attachment.isFile);
const hasVisualMediaAttachment = hasAttachment(Attachment.isVisualMedia);
2018-04-13 20:25:52 +00:00
export const initializeAttachmentMetadata = async (
message: MessageAttributesType
): Promise<MessageAttributesType> => {
if (message.type === 'verified-change') {
return message;
}
if (message.type === 'profile-change') {
return message;
}
2019-08-05 20:53:15 +00:00
if (message.messageTimer || message.isViewOnce) {
2019-06-26 19:33:13 +00:00
return message;
}
const attachments = (message.attachments || []).filter(
2021-07-14 23:39:52 +00:00
(attachment: Attachment.AttachmentType) =>
attachment.contentType !== 'text/x-signal-plain'
2018-04-14 02:09:56 +00:00
);
const hasAttachments = IndexedDB.toIndexableBoolean(attachments.length > 0);
const hasFileAttachments = hasFileAttachment({ ...message, attachments });
const hasVisualMediaAttachments = hasVisualMediaAttachment({
...message,
attachments,
});
2018-04-13 20:25:52 +00:00
return {
...message,
hasAttachments,
hasFileAttachments,
hasVisualMediaAttachments,
};
2018-04-13 20:25:52 +00:00
};