2023-04-20 16:31:59 +00:00
|
|
|
// Copyright 2023 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
import { v4 as generateUuid } from 'uuid';
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
import type { DraftBodyRanges } from '../types/BodyRange';
|
|
|
|
import type { LinkPreviewType } from '../types/message/LinkPreviews';
|
|
|
|
import type {
|
|
|
|
MessageAttributesType,
|
|
|
|
QuotedMessageType,
|
|
|
|
} from '../model-types.d';
|
|
|
|
import * as log from '../logging/log';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader, DataWriter } from '../sql/Client';
|
2023-04-20 16:31:59 +00:00
|
|
|
import type { AttachmentType } from '../types/Attachment';
|
|
|
|
import { ErrorWithToast } from '../types/ErrorWithToast';
|
|
|
|
import { SendStatus } from '../messages/MessageSendState';
|
|
|
|
import { ToastType } from '../types/Toast';
|
2023-08-16 20:54:39 +00:00
|
|
|
import type { AciString } from '../types/ServiceId';
|
2023-11-08 01:55:48 +00:00
|
|
|
import { canEditMessage, isWithinMaxEdits } from './canEditMessage';
|
2023-04-20 16:31:59 +00:00
|
|
|
import {
|
|
|
|
conversationJobQueue,
|
|
|
|
conversationQueueJobEnum,
|
|
|
|
} from '../jobs/conversationJobQueue';
|
|
|
|
import { concat, filter, map, repeat, zipObject, find } from './iterables';
|
|
|
|
import { getConversationIdForLogging } from './idForLogging';
|
|
|
|
import { isQuoteAMatch } from '../messages/helpers';
|
2023-10-04 00:12:57 +00:00
|
|
|
import { __DEPRECATED$getMessageById } from '../messages/getMessageById';
|
2023-04-20 16:31:59 +00:00
|
|
|
import { handleEditMessage } from './handleEditMessage';
|
|
|
|
import { incrementMessageCounter } from './incrementMessageCounter';
|
|
|
|
import { isGroupV1 } from './whatTypeOfConversation';
|
|
|
|
import { isNotNil } from './isNotNil';
|
|
|
|
import { isSignalConversation } from './isSignalConversation';
|
|
|
|
import { strictAssert } from './assert';
|
|
|
|
import { timeAndLogIfTooLong } from './timeAndLogIfTooLong';
|
|
|
|
import { makeQuote } from './makeQuote';
|
2023-05-16 17:37:12 +00:00
|
|
|
import { getMessageSentTimestamp } from './getMessageSentTimestamp';
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
const SEND_REPORT_THRESHOLD_MS = 25;
|
|
|
|
|
|
|
|
export async function sendEditedMessage(
|
|
|
|
conversationId: string,
|
|
|
|
{
|
|
|
|
body,
|
|
|
|
bodyRanges,
|
|
|
|
preview,
|
|
|
|
quoteSentAt,
|
2023-08-16 20:54:39 +00:00
|
|
|
quoteAuthorAci,
|
2023-04-20 16:31:59 +00:00
|
|
|
targetMessageId,
|
|
|
|
}: {
|
|
|
|
body?: string;
|
|
|
|
bodyRanges?: DraftBodyRanges;
|
|
|
|
preview: Array<LinkPreviewType>;
|
|
|
|
quoteSentAt?: number;
|
2023-08-16 20:54:39 +00:00
|
|
|
quoteAuthorAci?: AciString;
|
2023-04-20 16:31:59 +00:00
|
|
|
targetMessageId: string;
|
|
|
|
}
|
|
|
|
): Promise<void> {
|
|
|
|
const { messaging } = window.textsecure;
|
|
|
|
strictAssert(messaging, 'messaging not available');
|
|
|
|
|
|
|
|
const conversation = window.ConversationController.get(conversationId);
|
|
|
|
strictAssert(conversation, 'no conversation found');
|
|
|
|
|
|
|
|
const idLog = `sendEditedMessage(${getConversationIdForLogging(
|
|
|
|
conversation.attributes
|
|
|
|
)})`;
|
|
|
|
|
2023-10-04 00:12:57 +00:00
|
|
|
const targetMessage = await __DEPRECATED$getMessageById(targetMessageId);
|
2023-04-20 16:31:59 +00:00
|
|
|
strictAssert(targetMessage, 'could not find message to edit');
|
|
|
|
|
|
|
|
if (isGroupV1(conversation.attributes)) {
|
|
|
|
log.warn(`${idLog}: can't send to gv1`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSignalConversation(conversation.attributes)) {
|
|
|
|
log.warn(`${idLog}: can't send to Signal`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-08 01:55:48 +00:00
|
|
|
if (
|
|
|
|
!canEditMessage(targetMessage.attributes) ||
|
|
|
|
!isWithinMaxEdits(targetMessage.attributes)
|
|
|
|
) {
|
2023-04-20 16:31:59 +00:00
|
|
|
throw new ErrorWithToast(
|
|
|
|
`${idLog}: cannot edit`,
|
|
|
|
ToastType.CannotEditMessage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const timestamp = Date.now();
|
2023-05-16 17:37:12 +00:00
|
|
|
const targetSentTimestamp = getMessageSentTimestamp(
|
|
|
|
targetMessage.attributes,
|
|
|
|
{
|
|
|
|
log,
|
|
|
|
}
|
|
|
|
);
|
2023-04-20 16:31:59 +00:00
|
|
|
|
2023-05-12 17:09:44 +00:00
|
|
|
log.info(`${idLog}: edited(${timestamp}) original(${targetSentTimestamp})`);
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
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;
|
2023-08-16 20:54:39 +00:00
|
|
|
if (quoteSentAt !== undefined && quoteAuthorAci !== undefined) {
|
2023-04-20 16:31:59 +00:00
|
|
|
const existingQuote = targetMessage.get('quote');
|
|
|
|
|
|
|
|
// Keep the quote if unchanged.
|
|
|
|
if (quoteSentAt === existingQuote?.id) {
|
|
|
|
quote = existingQuote;
|
|
|
|
} else {
|
2024-07-22 18:16:33 +00:00
|
|
|
const messages = await DataReader.getMessagesBySentAt(quoteSentAt);
|
2023-04-20 16:31:59 +00:00
|
|
|
const matchingMessage = find(messages, item =>
|
|
|
|
isQuoteAMatch(item, conversationId, {
|
|
|
|
id: quoteSentAt,
|
2023-08-16 20:54:39 +00:00
|
|
|
authorAci: quoteAuthorAci,
|
2023-04-20 16:31:59 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
if (matchingMessage) {
|
|
|
|
quote = await makeQuote(matchingMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 23:24:19 +00:00
|
|
|
const ourConversation =
|
|
|
|
window.ConversationController.getOurConversationOrThrow();
|
|
|
|
const fromId = ourConversation.id;
|
|
|
|
|
|
|
|
// Create the send state for later use
|
|
|
|
const recipientMaybeConversations = map(
|
|
|
|
conversation.getRecipients(),
|
|
|
|
identifier => window.ConversationController.get(identifier)
|
|
|
|
);
|
|
|
|
const recipientConversations = filter(recipientMaybeConversations, isNotNil);
|
|
|
|
const recipientConversationIds = concat(
|
|
|
|
map(recipientConversations, c => c.id),
|
|
|
|
[fromId]
|
|
|
|
);
|
|
|
|
const sendStateByConversationId = zipObject(
|
|
|
|
recipientConversationIds,
|
|
|
|
repeat({
|
|
|
|
status: SendStatus.Pending,
|
|
|
|
updatedAt: timestamp,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
// An ephemeral message that we just use to handle the edit
|
|
|
|
const tmpMessage: MessageAttributesType = {
|
|
|
|
attachments: attachments?.map((attachment, index) =>
|
|
|
|
fixNewAttachment(attachment, `attachment:${index}`)
|
|
|
|
),
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}),
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2023-04-20 16:31:59 +00:00
|
|
|
quote,
|
|
|
|
received_at: incrementMessageCounter(),
|
|
|
|
received_at_ms: timestamp,
|
2023-08-15 23:24:19 +00:00
|
|
|
sendStateByConversationId,
|
2023-04-20 16:31:59 +00:00
|
|
|
sent_at: timestamp,
|
|
|
|
timestamp,
|
|
|
|
type: 'outgoing',
|
|
|
|
};
|
|
|
|
|
2023-08-07 16:36:37 +00:00
|
|
|
// Takes care of putting the message in the edit history, replacing the
|
|
|
|
// main message's values, and updating the conversation's properties.
|
|
|
|
await handleEditMessage(targetMessage.attributes, {
|
2023-04-20 16:31:59 +00:00
|
|
|
conversationId,
|
|
|
|
fromId,
|
2023-05-31 18:14:38 +00:00
|
|
|
fromDevice: window.storage.user.getDeviceId() ?? 1,
|
2023-04-20 16:31:59 +00:00
|
|
|
message: tmpMessage,
|
2023-08-07 16:36:37 +00:00
|
|
|
});
|
2023-04-20 16:31:59 +00:00
|
|
|
|
2023-08-15 23:24:19 +00:00
|
|
|
// Reset send state prior to send
|
|
|
|
targetMessage.set({ sendStateByConversationId });
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
// Inserting the send into a job and saving it to the message
|
|
|
|
await timeAndLogIfTooLong(
|
|
|
|
SEND_REPORT_THRESHOLD_MS,
|
|
|
|
() =>
|
|
|
|
conversationJobQueue.add(
|
|
|
|
{
|
|
|
|
type: conversationQueueJobEnum.enum.NormalMessage,
|
|
|
|
conversationId,
|
|
|
|
messageId: targetMessageId,
|
|
|
|
revision: conversation.get('revision'),
|
2023-11-17 18:16:48 +00:00
|
|
|
editedMessageTimestamp: timestamp,
|
2023-04-20 16:31:59 +00:00
|
|
|
},
|
|
|
|
async jobToInsert => {
|
|
|
|
log.info(
|
|
|
|
`${idLog}: saving message ${targetMessageId} and job ${jobToInsert.id}`
|
|
|
|
);
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.saveMessage(targetMessage.attributes, {
|
2023-04-20 16:31:59 +00:00
|
|
|
jobToInsert,
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2023-04-20 16:31:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
),
|
|
|
|
duration => `${idLog}: db save took ${duration}ms`
|
|
|
|
);
|
|
|
|
|
|
|
|
// Does the same render dance that models/conversations does when we call
|
|
|
|
// enqueueMessageForSend. Calls redux actions, clears drafts, unarchives, and
|
|
|
|
// updates storage service if needed.
|
|
|
|
await timeAndLogIfTooLong(
|
|
|
|
SEND_REPORT_THRESHOLD_MS,
|
|
|
|
async () => {
|
|
|
|
conversation.beforeMessageSend({
|
2024-07-25 23:29:49 +00:00
|
|
|
message: targetMessage.attributes,
|
2023-04-20 16:31:59 +00:00
|
|
|
dontClearDraft: false,
|
|
|
|
dontAddMessage: true,
|
|
|
|
now: timestamp,
|
|
|
|
});
|
|
|
|
},
|
2023-11-17 18:16:48 +00:00
|
|
|
duration => `${idLog}: batchDispatch took ${duration}ms`
|
2023-04-20 16:31:59 +00:00
|
|
|
);
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.updateConversation(conversation.attributes);
|
2023-04-20 16:31:59 +00:00
|
|
|
}
|