2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2022-03-04 21:14:52 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { partition } from 'lodash';
|
|
|
|
import * as log from '../logging/log';
|
|
|
|
import { isLongMessage } from '../types/MIME';
|
2022-04-22 18:35:14 +00:00
|
|
|
import { getMessageIdForLogging } from './idForLogging';
|
2022-03-04 21:14:52 +00:00
|
|
|
import {
|
|
|
|
copyStickerToAttachments,
|
|
|
|
savePackMetadata,
|
|
|
|
getStickerPackStatus,
|
|
|
|
} from '../types/Stickers';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataWriter } from '../sql/Client';
|
2022-03-04 21:14:52 +00:00
|
|
|
|
2024-05-23 21:06:41 +00:00
|
|
|
import type { AttachmentType, ThumbnailType } from '../types/Attachment';
|
2022-06-13 21:39:35 +00:00
|
|
|
import type { EmbeddedContactType } from '../types/EmbeddedContact';
|
|
|
|
import type {
|
2023-03-27 23:48:57 +00:00
|
|
|
EditHistoryType,
|
2022-06-13 21:39:35 +00:00
|
|
|
MessageAttributesType,
|
|
|
|
QuotedMessageType,
|
|
|
|
} from '../model-types.d';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2023-03-27 23:48:57 +00:00
|
|
|
import {
|
|
|
|
getAttachmentSignature,
|
|
|
|
isDownloading,
|
|
|
|
isDownloaded,
|
|
|
|
} from '../types/Attachment';
|
2022-06-13 21:39:35 +00:00
|
|
|
import type { StickerType } from '../types/Stickers';
|
|
|
|
import type { LinkPreviewType } from '../types/message/LinkPreviews';
|
2023-04-20 16:31:59 +00:00
|
|
|
import { isNotNil } from './isNotNil';
|
2024-04-16 00:11:48 +00:00
|
|
|
import {
|
|
|
|
AttachmentDownloadManager,
|
|
|
|
AttachmentDownloadUrgency,
|
|
|
|
} from '../jobs/AttachmentDownloadManager';
|
2024-09-03 22:00:51 +00:00
|
|
|
import { AttachmentDownloadSource } from '../sql/Interface';
|
2022-06-13 21:39:35 +00:00
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
export type MessageAttachmentsDownloadedType = {
|
2022-05-23 23:07:41 +00:00
|
|
|
bodyAttachment?: AttachmentType;
|
2022-03-04 21:14:52 +00:00
|
|
|
attachments: Array<AttachmentType>;
|
2023-03-27 23:48:57 +00:00
|
|
|
editHistory?: Array<EditHistoryType>;
|
2022-06-13 21:39:35 +00:00
|
|
|
preview: Array<LinkPreviewType>;
|
2022-03-04 21:14:52 +00:00
|
|
|
contact: Array<EmbeddedContactType>;
|
|
|
|
quote?: QuotedMessageType;
|
2022-06-13 21:39:35 +00:00
|
|
|
sticker?: StickerType;
|
2022-03-04 21:14:52 +00:00
|
|
|
};
|
|
|
|
|
2023-10-05 16:16:50 +00:00
|
|
|
function getAttachmentSignatureSafe(
|
|
|
|
attachment: AttachmentType
|
|
|
|
): string | undefined {
|
|
|
|
try {
|
|
|
|
return getAttachmentSignature(attachment);
|
|
|
|
} catch {
|
|
|
|
log.warn(
|
|
|
|
'queueAttachmentDownloads: attachment was missing digest',
|
|
|
|
attachment.blurHash
|
|
|
|
);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
// Receive logic
|
|
|
|
// NOTE: If you're changing any logic in this function that deals with the
|
|
|
|
// count then you'll also have to modify ./hasAttachmentsDownloads
|
|
|
|
export async function queueAttachmentDownloads(
|
2024-04-16 00:11:48 +00:00
|
|
|
message: MessageAttributesType,
|
2024-09-03 22:00:51 +00:00
|
|
|
{
|
|
|
|
urgency = AttachmentDownloadUrgency.STANDARD,
|
|
|
|
source = AttachmentDownloadSource.STANDARD,
|
|
|
|
}: {
|
|
|
|
urgency?: AttachmentDownloadUrgency;
|
|
|
|
source?: AttachmentDownloadSource;
|
|
|
|
} = {}
|
2023-10-04 00:12:57 +00:00
|
|
|
): Promise<MessageAttachmentsDownloadedType | undefined> {
|
2022-03-04 21:14:52 +00:00
|
|
|
const attachmentsToQueue = message.attachments || [];
|
|
|
|
const messageId = message.id;
|
|
|
|
const idForLogging = getMessageIdForLogging(message);
|
|
|
|
|
|
|
|
let count = 0;
|
2022-05-23 23:07:41 +00:00
|
|
|
let bodyAttachment;
|
2022-03-04 21:14:52 +00:00
|
|
|
|
2023-03-27 23:48:57 +00:00
|
|
|
const idLog = `queueAttachmentDownloads(${idForLogging}})`;
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
log.info(
|
2023-03-27 23:48:57 +00:00
|
|
|
`${idLog}: Queueing ${attachmentsToQueue.length} attachment downloads`
|
2022-03-04 21:14:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const [longMessageAttachments, normalAttachments] = partition(
|
|
|
|
attachmentsToQueue,
|
|
|
|
attachment => isLongMessage(attachment.contentType)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (longMessageAttachments.length > 1) {
|
2023-03-27 23:48:57 +00:00
|
|
|
log.error(`${idLog}: Received more than one long message attachment`);
|
2022-03-04 21:14:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 00:11:48 +00:00
|
|
|
if (longMessageAttachments.length > 0) {
|
2022-05-23 23:07:41 +00:00
|
|
|
[bodyAttachment] = longMessageAttachments;
|
|
|
|
}
|
2024-09-23 19:24:41 +00:00
|
|
|
|
2022-05-23 23:07:41 +00:00
|
|
|
if (!bodyAttachment && message.bodyAttachment) {
|
|
|
|
bodyAttachment = message.bodyAttachment;
|
|
|
|
}
|
|
|
|
|
2024-09-23 19:24:41 +00:00
|
|
|
const bodyAttachmentsToDownload = [
|
|
|
|
bodyAttachment,
|
|
|
|
...(message.editHistory
|
|
|
|
?.slice(1) // first entry is the same as the root level message!
|
|
|
|
.map(editHistory => editHistory.bodyAttachment) ?? []),
|
|
|
|
]
|
|
|
|
.filter(isNotNil)
|
|
|
|
.filter(attachment => !isDownloaded(attachment));
|
|
|
|
|
|
|
|
if (bodyAttachmentsToDownload.length) {
|
|
|
|
log.info(
|
|
|
|
`${idLog}: Queueing ${bodyAttachmentsToDownload.length} long message attachment download`
|
|
|
|
);
|
|
|
|
await Promise.all(
|
|
|
|
bodyAttachmentsToDownload.map(attachment =>
|
|
|
|
AttachmentDownloadManager.addJob({
|
|
|
|
attachment,
|
|
|
|
messageId,
|
|
|
|
attachmentType: 'long-message',
|
|
|
|
receivedAt: message.received_at,
|
|
|
|
sentAt: message.sent_at,
|
|
|
|
urgency,
|
|
|
|
source,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
count += bodyAttachmentsToDownload.length;
|
2022-03-04 21:14:52 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 00:11:48 +00:00
|
|
|
if (normalAttachments.length > 0) {
|
|
|
|
log.info(
|
|
|
|
`${idLog}: Queueing ${normalAttachments.length} normal attachment downloads`
|
|
|
|
);
|
|
|
|
}
|
2023-03-27 23:48:57 +00:00
|
|
|
const { attachments, count: attachmentsCount } = await queueNormalAttachments(
|
2024-04-16 00:11:48 +00:00
|
|
|
{
|
|
|
|
idLog,
|
|
|
|
messageId,
|
|
|
|
attachments: normalAttachments,
|
|
|
|
otherAttachments: message.editHistory?.flatMap(x => x.attachments ?? []),
|
|
|
|
receivedAt: message.received_at,
|
|
|
|
sentAt: message.sent_at,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
2022-03-04 21:14:52 +00:00
|
|
|
);
|
2023-03-27 23:48:57 +00:00
|
|
|
count += attachmentsCount;
|
2022-03-04 21:14:52 +00:00
|
|
|
|
|
|
|
const previewsToQueue = message.preview || [];
|
2024-04-16 00:11:48 +00:00
|
|
|
if (previewsToQueue.length > 0) {
|
|
|
|
log.info(
|
|
|
|
`${idLog}: Queueing ${previewsToQueue.length} preview attachment downloads`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const { preview, count: previewCount } = await queuePreviews({
|
2023-03-27 23:48:57 +00:00
|
|
|
idLog,
|
|
|
|
messageId,
|
2024-04-16 00:11:48 +00:00
|
|
|
previews: previewsToQueue,
|
|
|
|
otherPreviews: message.editHistory?.flatMap(x => x.preview ?? []),
|
|
|
|
receivedAt: message.received_at,
|
|
|
|
sentAt: message.sent_at,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2024-04-16 00:11:48 +00:00
|
|
|
});
|
2023-03-27 23:48:57 +00:00
|
|
|
count += previewCount;
|
2022-03-04 21:14:52 +00:00
|
|
|
|
2024-04-16 00:11:48 +00:00
|
|
|
const numQuoteAttachments = message.quote?.attachments?.length ?? 0;
|
|
|
|
if (numQuoteAttachments > 0) {
|
|
|
|
log.info(
|
|
|
|
`${idLog}: Queueing ${numQuoteAttachments} ` +
|
|
|
|
'quote attachment downloads'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const { quote, count: thumbnailCount } = await queueQuoteAttachments({
|
2023-04-20 16:31:59 +00:00
|
|
|
idLog,
|
|
|
|
messageId,
|
2024-04-16 00:11:48 +00:00
|
|
|
quote: message.quote,
|
|
|
|
otherQuotes: message.editHistory?.map(x => x.quote).filter(isNotNil) ?? [],
|
|
|
|
receivedAt: message.received_at,
|
|
|
|
sentAt: message.sent_at,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2024-04-16 00:11:48 +00:00
|
|
|
});
|
2023-04-20 16:31:59 +00:00
|
|
|
count += thumbnailCount;
|
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
const contactsToQueue = message.contact || [];
|
2024-04-16 00:11:48 +00:00
|
|
|
if (contactsToQueue.length > 0) {
|
|
|
|
log.info(
|
|
|
|
`${idLog}: Queueing ${contactsToQueue.length} contact attachment downloads`
|
|
|
|
);
|
|
|
|
}
|
2022-03-04 21:14:52 +00:00
|
|
|
const contact = await Promise.all(
|
2024-04-16 00:11:48 +00:00
|
|
|
contactsToQueue.map(async item => {
|
2022-03-04 21:14:52 +00:00
|
|
|
if (!item.avatar || !item.avatar.avatar) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
// We've already downloaded this!
|
|
|
|
if (item.avatar.avatar.path) {
|
2023-03-27 23:48:57 +00:00
|
|
|
log.info(`${idLog}: Contact attachment already downloaded`);
|
2022-03-04 21:14:52 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
avatar: {
|
|
|
|
...item.avatar,
|
2024-04-16 00:11:48 +00:00
|
|
|
avatar: await AttachmentDownloadManager.addJob({
|
|
|
|
attachment: item.avatar.avatar,
|
2022-03-04 21:14:52 +00:00
|
|
|
messageId,
|
2024-04-16 00:11:48 +00:00
|
|
|
attachmentType: 'contact',
|
|
|
|
receivedAt: message.received_at,
|
|
|
|
sentAt: message.sent_at,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2022-03-04 21:14:52 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
let { sticker } = message;
|
|
|
|
if (sticker && sticker.data && sticker.data.path) {
|
2023-03-27 23:48:57 +00:00
|
|
|
log.info(`${idLog}: Sticker attachment already downloaded`);
|
2022-03-04 21:14:52 +00:00
|
|
|
} else if (sticker) {
|
2023-03-27 23:48:57 +00:00
|
|
|
log.info(`${idLog}: Queueing sticker download`);
|
2022-03-04 21:14:52 +00:00
|
|
|
count += 1;
|
|
|
|
const { packId, stickerId, packKey } = sticker;
|
|
|
|
|
|
|
|
const status = getStickerPackStatus(packId);
|
|
|
|
let data: AttachmentType | undefined;
|
|
|
|
|
|
|
|
if (status && (status === 'downloaded' || status === 'installed')) {
|
|
|
|
try {
|
|
|
|
data = await copyStickerToAttachments(packId, stickerId);
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
2023-03-27 23:48:57 +00:00
|
|
|
`${idLog}: Problem copying sticker (${packId}, ${stickerId}) to attachments:`,
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(error)
|
2022-03-04 21:14:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-02-14 20:19:40 +00:00
|
|
|
if (!data) {
|
|
|
|
if (sticker.data) {
|
2024-04-16 00:11:48 +00:00
|
|
|
data = await AttachmentDownloadManager.addJob({
|
|
|
|
attachment: sticker.data,
|
2024-02-14 20:19:40 +00:00
|
|
|
messageId,
|
2024-04-16 00:11:48 +00:00
|
|
|
attachmentType: 'sticker',
|
|
|
|
receivedAt: message.received_at,
|
|
|
|
sentAt: message.sent_at,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2024-02-14 20:19:40 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
log.error(`${idLog}: Sticker data was missing`);
|
|
|
|
}
|
2022-03-04 21:14:52 +00:00
|
|
|
}
|
|
|
|
if (!status) {
|
|
|
|
// Save the packId/packKey for future download/install
|
2022-12-21 18:41:48 +00:00
|
|
|
void savePackMetadata(packId, packKey, { messageId });
|
2022-03-04 21:14:52 +00:00
|
|
|
} else {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.addStickerPackReference(messageId, packId);
|
2022-03-04 21:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
throw new Error('queueAttachmentDownloads: Failed to fetch sticker data');
|
|
|
|
}
|
|
|
|
|
|
|
|
sticker = {
|
|
|
|
...sticker,
|
|
|
|
packId,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-27 23:48:57 +00:00
|
|
|
let { editHistory } = message;
|
|
|
|
if (editHistory) {
|
|
|
|
log.info(`${idLog}: Looping through ${editHistory.length} edits`);
|
|
|
|
editHistory = await Promise.all(
|
|
|
|
editHistory.map(async edit => {
|
|
|
|
const { attachments: editAttachments, count: editAttachmentsCount } =
|
2024-04-16 00:11:48 +00:00
|
|
|
await queueNormalAttachments({
|
2023-03-27 23:48:57 +00:00
|
|
|
idLog,
|
|
|
|
messageId,
|
2024-04-16 00:11:48 +00:00
|
|
|
attachments: edit.attachments,
|
|
|
|
otherAttachments: attachments,
|
|
|
|
receivedAt: message.received_at,
|
|
|
|
sentAt: message.sent_at,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2024-04-16 00:11:48 +00:00
|
|
|
});
|
2023-03-27 23:48:57 +00:00
|
|
|
count += editAttachmentsCount;
|
2023-04-20 16:31:59 +00:00
|
|
|
if (editAttachmentsCount !== 0) {
|
|
|
|
log.info(
|
|
|
|
`${idLog}: Queueing ${editAttachmentsCount} normal attachment ` +
|
|
|
|
`downloads (edited:${edit.timestamp})`
|
|
|
|
);
|
|
|
|
}
|
2023-03-27 23:48:57 +00:00
|
|
|
|
|
|
|
const { preview: editPreview, count: editPreviewCount } =
|
2024-04-16 00:11:48 +00:00
|
|
|
await queuePreviews({
|
|
|
|
idLog,
|
|
|
|
messageId,
|
|
|
|
previews: edit.preview,
|
|
|
|
otherPreviews: preview,
|
|
|
|
receivedAt: message.received_at,
|
|
|
|
sentAt: message.sent_at,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2024-04-16 00:11:48 +00:00
|
|
|
});
|
2023-03-27 23:48:57 +00:00
|
|
|
count += editPreviewCount;
|
2023-04-20 16:31:59 +00:00
|
|
|
if (editPreviewCount !== 0) {
|
|
|
|
log.info(
|
|
|
|
`${idLog}: Queueing ${editPreviewCount} preview attachment ` +
|
|
|
|
`downloads (edited:${edit.timestamp})`
|
|
|
|
);
|
|
|
|
}
|
2023-03-27 23:48:57 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...edit,
|
|
|
|
attachments: editAttachments,
|
|
|
|
preview: editPreview,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`${idLog}: Queued ${count} total attachment downloads`);
|
2022-03-04 21:14:52 +00:00
|
|
|
|
|
|
|
if (count <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
attachments,
|
2023-03-27 23:48:57 +00:00
|
|
|
bodyAttachment,
|
2022-03-04 21:14:52 +00:00
|
|
|
contact,
|
2023-03-27 23:48:57 +00:00
|
|
|
editHistory,
|
|
|
|
preview,
|
2022-03-04 21:14:52 +00:00
|
|
|
quote,
|
|
|
|
sticker,
|
|
|
|
};
|
|
|
|
}
|
2023-03-27 23:48:57 +00:00
|
|
|
|
2024-04-16 00:11:48 +00:00
|
|
|
async function queueNormalAttachments({
|
|
|
|
idLog,
|
|
|
|
messageId,
|
|
|
|
attachments = [],
|
|
|
|
otherAttachments,
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2024-04-16 00:11:48 +00:00
|
|
|
}: {
|
|
|
|
idLog: string;
|
|
|
|
messageId: string;
|
|
|
|
attachments: MessageAttributesType['attachments'];
|
|
|
|
otherAttachments: MessageAttributesType['attachments'];
|
|
|
|
receivedAt: number;
|
|
|
|
sentAt: number;
|
|
|
|
urgency: AttachmentDownloadUrgency;
|
2024-09-03 22:00:51 +00:00
|
|
|
source: AttachmentDownloadSource;
|
2024-04-16 00:11:48 +00:00
|
|
|
}): Promise<{
|
2023-03-27 23:48:57 +00:00
|
|
|
attachments: Array<AttachmentType>;
|
|
|
|
count: number;
|
|
|
|
}> {
|
|
|
|
// Look through "otherAttachments" which can either be attachments in the
|
|
|
|
// edit history or the message's attachments and see if any of the attachments
|
|
|
|
// are the same. If they are let's replace it so that we don't download more
|
|
|
|
// than once.
|
|
|
|
// We don't also register the signatures for "attachments" because they would
|
|
|
|
// then not be added to the AttachmentDownloads job.
|
|
|
|
const attachmentSignatures: Map<string, AttachmentType> = new Map();
|
|
|
|
otherAttachments?.forEach(attachment => {
|
2023-10-05 16:16:50 +00:00
|
|
|
const signature = getAttachmentSignatureSafe(attachment);
|
2023-04-20 16:31:59 +00:00
|
|
|
if (signature) {
|
|
|
|
attachmentSignatures.set(signature, attachment);
|
|
|
|
}
|
2023-03-27 23:48:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let count = 0;
|
|
|
|
const nextAttachments = await Promise.all(
|
2024-04-16 00:11:48 +00:00
|
|
|
attachments.map(attachment => {
|
2023-03-27 23:48:57 +00:00
|
|
|
if (!attachment) {
|
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
// We've already downloaded this!
|
|
|
|
if (isDownloaded(attachment)) {
|
|
|
|
log.info(`${idLog}: Normal attachment already downloaded`);
|
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
|
2023-10-05 16:16:50 +00:00
|
|
|
const signature = getAttachmentSignatureSafe(attachment);
|
2023-03-27 23:48:57 +00:00
|
|
|
const existingAttachment = signature
|
|
|
|
? attachmentSignatures.get(signature)
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
// We've already downloaded this elsewhere!
|
|
|
|
if (
|
|
|
|
existingAttachment &&
|
|
|
|
(isDownloading(existingAttachment) || isDownloaded(existingAttachment))
|
|
|
|
) {
|
|
|
|
log.info(
|
|
|
|
`${idLog}: Normal attachment already downloaded in other attachments. Replacing`
|
|
|
|
);
|
|
|
|
// Incrementing count so that we update the message's fields downstream
|
|
|
|
count += 1;
|
|
|
|
return existingAttachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
|
2024-04-16 00:11:48 +00:00
|
|
|
return AttachmentDownloadManager.addJob({
|
|
|
|
attachment,
|
2023-03-27 23:48:57 +00:00
|
|
|
messageId,
|
2024-04-16 00:11:48 +00:00
|
|
|
attachmentType: 'attachment',
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2023-03-27 23:48:57 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
attachments: nextAttachments,
|
|
|
|
count,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLinkPreviewSignature(preview: LinkPreviewType): string | undefined {
|
|
|
|
const { image, url } = preview;
|
|
|
|
|
|
|
|
if (!image) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-05 16:16:50 +00:00
|
|
|
const signature = getAttachmentSignatureSafe(image);
|
|
|
|
if (!signature) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<${url}>${signature}`;
|
2023-03-27 23:48:57 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 00:11:48 +00:00
|
|
|
async function queuePreviews({
|
|
|
|
idLog,
|
|
|
|
messageId,
|
|
|
|
previews = [],
|
|
|
|
otherPreviews,
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2024-04-16 00:11:48 +00:00
|
|
|
}: {
|
|
|
|
idLog: string;
|
|
|
|
messageId: string;
|
|
|
|
previews: MessageAttributesType['preview'];
|
|
|
|
otherPreviews: MessageAttributesType['preview'];
|
|
|
|
receivedAt: number;
|
|
|
|
sentAt: number;
|
|
|
|
urgency: AttachmentDownloadUrgency;
|
2024-09-03 22:00:51 +00:00
|
|
|
source: AttachmentDownloadSource;
|
2024-04-16 00:11:48 +00:00
|
|
|
}): Promise<{ preview: Array<LinkPreviewType>; count: number }> {
|
2023-03-27 23:48:57 +00:00
|
|
|
// Similar to queueNormalAttachments' logic for detecting same attachments
|
|
|
|
// except here we also pick by link preview URL.
|
|
|
|
const previewSignatures: Map<string, LinkPreviewType> = new Map();
|
|
|
|
otherPreviews?.forEach(preview => {
|
|
|
|
const signature = getLinkPreviewSignature(preview);
|
|
|
|
if (!signature) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
previewSignatures.set(signature, preview);
|
|
|
|
});
|
|
|
|
|
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
const preview = await Promise.all(
|
2024-04-16 00:11:48 +00:00
|
|
|
previews.map(async item => {
|
2023-03-27 23:48:57 +00:00
|
|
|
if (!item.image) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
// We've already downloaded this!
|
|
|
|
if (isDownloaded(item.image)) {
|
|
|
|
log.info(`${idLog}: Preview attachment already downloaded`);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
const signature = getLinkPreviewSignature(item);
|
|
|
|
const existingPreview = signature
|
|
|
|
? previewSignatures.get(signature)
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
// We've already downloaded this elsewhere!
|
|
|
|
if (
|
|
|
|
existingPreview &&
|
|
|
|
(isDownloading(existingPreview.image) ||
|
|
|
|
isDownloaded(existingPreview.image))
|
|
|
|
) {
|
|
|
|
log.info(`${idLog}: Preview already downloaded elsewhere. Replacing`);
|
|
|
|
// Incrementing count so that we update the message's fields downstream
|
|
|
|
count += 1;
|
|
|
|
return existingPreview;
|
|
|
|
}
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
return {
|
|
|
|
...item,
|
2024-04-16 00:11:48 +00:00
|
|
|
image: await AttachmentDownloadManager.addJob({
|
|
|
|
attachment: item.image,
|
2023-03-27 23:48:57 +00:00
|
|
|
messageId,
|
2024-04-16 00:11:48 +00:00
|
|
|
attachmentType: 'preview',
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2023-03-27 23:48:57 +00:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
preview,
|
|
|
|
count,
|
|
|
|
};
|
|
|
|
}
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
function getQuoteThumbnailSignature(
|
|
|
|
quote: QuotedMessageType,
|
|
|
|
thumbnail?: AttachmentType
|
|
|
|
): string | undefined {
|
|
|
|
if (!thumbnail) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2023-10-05 16:16:50 +00:00
|
|
|
const signature = getAttachmentSignatureSafe(thumbnail);
|
|
|
|
if (!signature) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return `<${quote.id}>${signature}`;
|
2023-04-20 16:31:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 00:11:48 +00:00
|
|
|
async function queueQuoteAttachments({
|
|
|
|
idLog,
|
|
|
|
messageId,
|
|
|
|
quote,
|
|
|
|
otherQuotes,
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2024-04-16 00:11:48 +00:00
|
|
|
}: {
|
|
|
|
idLog: string;
|
|
|
|
messageId: string;
|
|
|
|
quote: QuotedMessageType | undefined;
|
|
|
|
otherQuotes: ReadonlyArray<QuotedMessageType>;
|
|
|
|
receivedAt: number;
|
|
|
|
sentAt: number;
|
|
|
|
urgency: AttachmentDownloadUrgency;
|
2024-09-03 22:00:51 +00:00
|
|
|
source: AttachmentDownloadSource;
|
2024-04-16 00:11:48 +00:00
|
|
|
}): Promise<{ quote?: QuotedMessageType; count: number }> {
|
2023-04-20 16:31:59 +00:00
|
|
|
let count = 0;
|
|
|
|
if (!quote) {
|
|
|
|
return { quote, count };
|
|
|
|
}
|
|
|
|
|
|
|
|
const quoteAttachmentsToQueue =
|
|
|
|
quote && quote.attachments ? quote.attachments : [];
|
|
|
|
if (quoteAttachmentsToQueue.length === 0) {
|
|
|
|
return { quote, count };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Similar to queueNormalAttachments' logic for detecting same attachments
|
|
|
|
// except here we also pick by quote sent timestamp.
|
2024-05-23 21:06:41 +00:00
|
|
|
const thumbnailSignatures: Map<string, ThumbnailType> = new Map();
|
2023-04-20 16:31:59 +00:00
|
|
|
otherQuotes.forEach(otherQuote => {
|
|
|
|
for (const attachment of otherQuote.attachments) {
|
|
|
|
const signature = getQuoteThumbnailSignature(
|
|
|
|
otherQuote,
|
|
|
|
attachment.thumbnail
|
|
|
|
);
|
2024-05-23 21:06:41 +00:00
|
|
|
if (!signature || !attachment.thumbnail) {
|
2023-04-20 16:31:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-05-23 21:06:41 +00:00
|
|
|
thumbnailSignatures.set(signature, attachment.thumbnail);
|
2023-04-20 16:31:59 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
quote: {
|
|
|
|
...quote,
|
|
|
|
attachments: await Promise.all(
|
2024-04-16 00:11:48 +00:00
|
|
|
quote.attachments.map(async item => {
|
2023-04-20 16:31:59 +00:00
|
|
|
if (!item.thumbnail) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
// We've already downloaded this!
|
|
|
|
if (isDownloaded(item.thumbnail)) {
|
|
|
|
log.info(`${idLog}: Quote attachment already downloaded`);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
const signature = getQuoteThumbnailSignature(quote, item.thumbnail);
|
|
|
|
const existingThumbnail = signature
|
|
|
|
? thumbnailSignatures.get(signature)
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
// We've already downloaded this elsewhere!
|
|
|
|
if (
|
|
|
|
existingThumbnail &&
|
|
|
|
(isDownloading(existingThumbnail) ||
|
|
|
|
isDownloaded(existingThumbnail))
|
|
|
|
) {
|
|
|
|
log.info(
|
|
|
|
`${idLog}: Preview already downloaded elsewhere. Replacing`
|
|
|
|
);
|
|
|
|
// Incrementing count so that we update the message's fields downstream
|
|
|
|
count += 1;
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
thumbnail: existingThumbnail,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
return {
|
|
|
|
...item,
|
2024-04-16 00:11:48 +00:00
|
|
|
thumbnail: await AttachmentDownloadManager.addJob({
|
|
|
|
attachment: item.thumbnail,
|
2023-04-20 16:31:59 +00:00
|
|
|
messageId,
|
2024-04-16 00:11:48 +00:00
|
|
|
attachmentType: 'quote',
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
|
|
|
urgency,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2023-04-20 16:31:59 +00:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
})
|
|
|
|
),
|
|
|
|
},
|
|
|
|
count,
|
|
|
|
};
|
|
|
|
}
|