mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2025-12-20 02:08:57 +00:00
Simplify edit handling of attachments
This commit is contained in:
@@ -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({
|
||||
|
||||
@@ -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<string, AttachmentType> = new Map();
|
||||
const previewSignatures: Map<string, AttachmentType> = new Map();
|
||||
const quoteSignatures: Map<string, AttachmentType> = 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 => {
|
||||
|
||||
@@ -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<LinkPreviewType> | 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(),
|
||||
|
||||
Reference in New Issue
Block a user