diff --git a/ts/util/captureDimensionsAndScreenshot.dom.ts b/ts/util/captureDimensionsAndScreenshot.dom.ts index 80dce56450..61a32baba8 100644 --- a/ts/util/captureDimensionsAndScreenshot.dom.ts +++ b/ts/util/captureDimensionsAndScreenshot.dom.ts @@ -16,6 +16,7 @@ import { isImageTypeSupported, isVideoTypeSupported, } from './GoogleChrome.std.js'; +import { strictAssert } from './assert.std.js'; const THUMBNAIL_SIZE = 150; const THUMBNAIL_CONTENT_TYPE = IMAGE_PNG; @@ -80,6 +81,11 @@ export async function captureDimensionsAndScreenshot( const localUrl = getLocalAttachmentUrl(attachment); if (isImageTypeSupported(contentType)) { + if (attachment.thumbnail?.path) { + // Already generated thumbnail / width / height + return attachment; + } + try { const { width, height } = await getImageDimensionsFromURL({ objectUrl: localUrl, @@ -125,6 +131,13 @@ export async function captureDimensionsAndScreenshot( } } + strictAssert(isVideoTypeSupported(contentType), 'enforced by early return'); + + if (attachment.screenshot?.path) { + // Already generated screenshot / width / height + return attachment; + } + let screenshotObjectUrl: string | undefined; try { const { blob, duration } = await makeVideoScreenshot({ diff --git a/ts/util/handleEditMessage.preload.ts b/ts/util/handleEditMessage.preload.ts index 5a7b5c170f..a4662d2181 100644 --- a/ts/util/handleEditMessage.preload.ts +++ b/ts/util/handleEditMessage.preload.ts @@ -137,29 +137,13 @@ export async function handleEditMessage( editAttributes.message ); - // Copies over the attachments from the main message if they're the same - // and they have already been downloaded. - const attachmentSignatures: Map = new Map(); const previewSignatures: Map = new Map(); - const quoteSignatures: Map = new Map(); - - mainMessage.attachments?.forEach(attachment => { - cacheAttachmentBySignature(attachmentSignatures, attachment); - }); mainMessage.preview?.forEach(preview => { if (!preview.image) { return; } cacheAttachmentBySignature(previewSignatures, preview.image); }); - if (mainMessage.quote) { - for (const attachment of mainMessage.quote.attachments) { - if (!attachment.thumbnail) { - continue; - } - cacheAttachmentBySignature(quoteSignatures, attachment.thumbnail); - } - } const nextEditedMessagePreview = upgradedEditedMessageData.preview?.map( preview => { diff --git a/ts/util/sendEditedMessage.preload.ts b/ts/util/sendEditedMessage.preload.ts index eba393fd06..9d77371ca2 100644 --- a/ts/util/sendEditedMessage.preload.ts +++ b/ts/util/sendEditedMessage.preload.ts @@ -11,7 +11,6 @@ import type { } from '../model-types.d.ts'; import { createLogger } from '../logging/log.std.js'; import { DataReader, DataWriter } from '../sql/Client.preload.js'; -import type { AttachmentType } from '../types/Attachment.std.js'; import { ErrorWithToast } from '../types/ErrorWithToast.std.js'; import { SendStatus } from '../messages/MessageSendState.std.js'; import { ToastType } from '../types/Toast.dom.js'; @@ -107,28 +106,6 @@ export async function sendEditedMessage( conversation.clearTypingTimers(); - // Can't send both preview and attachments - const attachments = - preview && preview.length ? [] : targetMessage.get('attachments') || []; - - const fixNewAttachment = ( - attachment: AttachmentType, - temporaryDigest: string - ): AttachmentType => { - // Check if this is an existing attachment or a new attachment coming - // from composer - if (attachment.digest) { - return attachment; - } - - // Generated semi-unique digest so that `handleEditMessage` understand - // it is a new attachment - return { - ...attachment, - digest: `${temporaryDigest}:${attachment.path}`, - }; - }; - let quote: QuotedMessageType | undefined; if (quoteSentAt !== undefined && quoteAuthorAci !== undefined) { const existingQuote = targetMessage.get('quote'); @@ -173,25 +150,20 @@ export async function sendEditedMessage( }) ); + const originalAttachments = targetMessage.get('attachments'); + let previewToSend: Array | undefined = preview; + if (originalAttachments?.length && preview.length) { + log.error('Cannot send message with both attachments and preview'); + previewToSend = undefined; + } + // An ephemeral message that we just use to handle the edit const tmpMessage: MessageAttributesType = { - attachments: attachments?.map((attachment, index) => - fixNewAttachment(attachment, `attachment:${index}`) - ), + attachments: originalAttachments, body, bodyRanges, conversationId, - preview: preview?.map((entry, index) => { - const image = - entry.image && fixNewAttachment(entry.image, `preview:${index}`); - if (entry.image === image) { - return entry; - } - return { - ...entry, - image, - }; - }), + preview: previewToSend, id: generateUuid(), quote, received_at: incrementMessageCounter(),