signal-desktop/ts/util/makeQuote.ts

135 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-03-27 23:48:57 +00:00
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2024-05-23 21:06:41 +00:00
import type { AttachmentType } from '../types/Attachment';
2023-03-27 23:48:57 +00:00
import type {
MessageAttributesType,
2024-05-23 21:06:41 +00:00
QuotedAttachmentType,
2023-03-27 23:48:57 +00:00
QuotedMessageType,
} from '../model-types.d';
import type { LinkPreviewType } from '../types/message/LinkPreviews';
import type { StickerType } from '../types/Stickers';
import { IMAGE_JPEG, IMAGE_GIF } from '../types/MIME';
import { getAuthor } from '../messages/helpers';
2023-03-27 23:48:57 +00:00
import { getQuoteBodyText } from './getQuoteBodyText';
import { isGIF } from '../types/Attachment';
import { isGiftBadge, isTapToView } from '../state/selectors/message';
import * as log from '../logging/log';
2023-03-27 23:48:57 +00:00
import { map, take, collect } from './iterables';
import { strictAssert } from './assert';
import { getMessageSentTimestamp } from './getMessageSentTimestamp';
2024-07-11 19:44:09 +00:00
import { getLocalAttachmentUrl } from './getLocalAttachmentUrl';
2023-03-27 23:48:57 +00:00
export async function makeQuote(
quotedMessage: MessageAttributesType
): Promise<QuotedMessageType> {
const contact = getAuthor(quotedMessage);
2023-03-27 23:48:57 +00:00
strictAssert(contact, 'makeQuote: no contact');
const {
attachments,
bodyRanges,
id: messageId,
payment,
preview,
sticker,
} = quotedMessage;
const quoteId = getMessageSentTimestamp(quotedMessage, { log });
2023-03-27 23:48:57 +00:00
return {
2023-08-16 20:54:39 +00:00
authorAci: contact.getCheckedAci('makeQuote'),
2023-03-27 23:48:57 +00:00
attachments: isTapToView(quotedMessage)
2024-05-23 21:06:41 +00:00
? [{ contentType: IMAGE_JPEG }]
2023-03-27 23:48:57 +00:00
: await getQuoteAttachment(attachments, preview, sticker),
payment,
bodyRanges,
id: quoteId,
isViewOnce: isTapToView(quotedMessage),
isGiftBadge: isGiftBadge(quotedMessage),
messageId,
referencedMessageNotFound: false,
text: getQuoteBodyText(quotedMessage, quoteId),
};
}
export async function getQuoteAttachment(
attachments?: Array<AttachmentType>,
preview?: Array<LinkPreviewType>,
sticker?: StickerType
2024-05-23 21:06:41 +00:00
): Promise<Array<QuotedAttachmentType>> {
2024-07-11 19:44:09 +00:00
const { loadAttachmentData } = window.Signal.Migrations;
2023-03-27 23:48:57 +00:00
if (attachments && attachments.length) {
const attachmentsToUse = Array.from(take(attachments, 1));
const isGIFQuote = isGIF(attachmentsToUse);
return Promise.all(
map(attachmentsToUse, async attachment => {
const { path, fileName, thumbnail, contentType } = attachment;
if (!path) {
return {
contentType: isGIFQuote ? IMAGE_GIF : contentType,
2024-05-23 21:06:41 +00:00
fileName,
thumbnail,
2023-03-27 23:48:57 +00:00
};
}
return {
contentType: isGIFQuote ? IMAGE_GIF : contentType,
2024-05-23 21:06:41 +00:00
fileName,
2023-03-27 23:48:57 +00:00
thumbnail: thumbnail
? {
...(await loadAttachmentData(thumbnail)),
objectUrl: thumbnail.path
2024-07-11 19:44:09 +00:00
? getLocalAttachmentUrl(thumbnail)
2023-03-27 23:48:57 +00:00
: undefined,
}
2024-05-23 21:06:41 +00:00
: undefined,
2023-03-27 23:48:57 +00:00
};
})
);
}
if (preview && preview.length) {
const previewImages = collect(preview, prev => prev.image);
const previewImagesToUse = take(previewImages, 1);
return Promise.all(
map(previewImagesToUse, async image => {
const { contentType } = image;
return {
contentType,
thumbnail: image
? {
...(await loadAttachmentData(image)),
objectUrl: image.path
2024-07-11 19:44:09 +00:00
? getLocalAttachmentUrl(image)
2023-03-27 23:48:57 +00:00
: undefined,
}
2024-05-23 21:06:41 +00:00
: undefined,
2023-03-27 23:48:57 +00:00
};
})
);
}
if (sticker && sticker.data && sticker.data.path) {
const { path, contentType } = sticker.data;
return [
{
contentType,
thumbnail: {
...(await loadAttachmentData(sticker.data)),
2024-07-11 19:44:09 +00:00
objectUrl: path ? getLocalAttachmentUrl(sticker.data) : undefined,
2023-03-27 23:48:57 +00:00
},
},
];
}
return [];
}