2022-02-16 18:36:21 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { isNumber } from 'lodash';
|
2023-04-20 16:31:59 +00:00
|
|
|
import PQueue from 'p-queue';
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
import * as Errors from '../../types/errors';
|
2022-11-02 23:48:38 +00:00
|
|
|
import { strictAssert } from '../../util/assert';
|
2022-02-16 18:36:21 +00:00
|
|
|
import type { MessageModel } from '../../models/messages';
|
2023-10-04 00:12:57 +00:00
|
|
|
import { __DEPRECATED$getMessageById } from '../../messages/getMessageById';
|
2022-02-16 18:36:21 +00:00
|
|
|
import type { ConversationModel } from '../../models/conversations';
|
2022-05-05 16:27:56 +00:00
|
|
|
import { isGroup, isGroupV2, isMe } from '../../util/whatTypeOfConversation';
|
2022-02-16 18:36:21 +00:00
|
|
|
import { getSendOptions } from '../../util/getSendOptions';
|
|
|
|
import { SignalService as Proto } from '../../protobuf';
|
|
|
|
import { handleMessageSend } from '../../util/handleMessageSend';
|
2022-11-02 23:48:38 +00:00
|
|
|
import { findAndFormatContact } from '../../util/findAndFormatContact';
|
2023-04-20 16:31:59 +00:00
|
|
|
import { uploadAttachment } from '../../util/uploadAttachment';
|
2022-02-16 18:36:21 +00:00
|
|
|
import type { CallbackResultType } from '../../textsecure/Types.d';
|
|
|
|
import { isSent } from '../../messages/MessageSendState';
|
2022-11-02 23:48:38 +00:00
|
|
|
import { isOutgoing, canReact } from '../../state/selectors/message';
|
2022-04-11 20:57:44 +00:00
|
|
|
import type {
|
2022-11-02 23:48:38 +00:00
|
|
|
ReactionType,
|
2023-04-20 16:31:59 +00:00
|
|
|
OutgoingQuoteType,
|
|
|
|
OutgoingQuoteAttachmentType,
|
|
|
|
OutgoingLinkPreviewType,
|
|
|
|
OutgoingStickerType,
|
2022-04-11 20:57:44 +00:00
|
|
|
} from '../../textsecure/SendMessage';
|
2023-04-20 16:31:59 +00:00
|
|
|
import type {
|
|
|
|
AttachmentType,
|
|
|
|
UploadedAttachmentType,
|
|
|
|
AttachmentWithHydratedData,
|
|
|
|
} from '../../types/Attachment';
|
2023-10-04 00:09:31 +00:00
|
|
|
import { copyCdnFields } from '../../util/attachments';
|
2023-12-02 01:02:46 +00:00
|
|
|
import { LONG_MESSAGE } from '../../types/MIME';
|
2023-04-14 18:16:28 +00:00
|
|
|
import type { RawBodyRange } from '../../types/BodyRange';
|
2023-04-20 16:31:59 +00:00
|
|
|
import type {
|
|
|
|
EmbeddedContactWithHydratedAvatar,
|
|
|
|
EmbeddedContactWithUploadedAvatar,
|
|
|
|
} from '../../types/EmbeddedContact';
|
2023-04-10 16:31:45 +00:00
|
|
|
import type { StoryContextType } from '../../types/Util';
|
2022-02-16 18:36:21 +00:00
|
|
|
import type { LoggerType } from '../../types/Logging';
|
|
|
|
import type {
|
|
|
|
ConversationQueueJobBundle,
|
|
|
|
NormalMessageSendJobData,
|
|
|
|
} from '../conversationJobQueue';
|
|
|
|
|
|
|
|
import { handleMultipleSendErrors } from './handleMultipleSendErrors';
|
|
|
|
import { ourProfileKeyService } from '../../services/ourProfileKey';
|
2022-02-22 20:34:57 +00:00
|
|
|
import { isConversationUnregistered } from '../../util/isConversationUnregistered';
|
|
|
|
import { isConversationAccepted } from '../../util/isConversationAccepted';
|
2022-07-13 00:37:21 +00:00
|
|
|
import { sendToGroup } from '../../util/sendToGroup';
|
2022-11-16 20:18:02 +00:00
|
|
|
import type { DurationInSeconds } from '../../util/durations';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../../types/ServiceId';
|
2023-09-14 17:04:48 +00:00
|
|
|
import { normalizeAci } from '../../util/normalizeAci';
|
2023-04-20 16:31:59 +00:00
|
|
|
import * as Bytes from '../../Bytes';
|
2023-11-17 18:16:48 +00:00
|
|
|
import {
|
|
|
|
getPropForTimestamp,
|
|
|
|
getTargetOfThisEditTimestamp,
|
|
|
|
setPropForTimestamp,
|
|
|
|
} from '../../util/editHelpers';
|
|
|
|
import { getMessageSentTimestamp } from '../../util/getMessageSentTimestamp';
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
const LONG_ATTACHMENT_LIMIT = 2048;
|
|
|
|
const MAX_CONCURRENT_ATTACHMENT_UPLOADS = 5;
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
export async function sendNormalMessage(
|
|
|
|
conversation: ConversationModel,
|
|
|
|
{
|
|
|
|
isFinalAttempt,
|
2022-06-13 21:39:35 +00:00
|
|
|
messaging,
|
2022-02-16 18:36:21 +00:00
|
|
|
shouldContinue,
|
|
|
|
timeRemaining,
|
|
|
|
log,
|
|
|
|
}: ConversationQueueJobBundle,
|
|
|
|
data: NormalMessageSendJobData
|
|
|
|
): Promise<void> {
|
|
|
|
const { Message } = window.Signal.Types;
|
|
|
|
|
2023-05-15 21:26:36 +00:00
|
|
|
const { messageId, revision, editedMessageTimestamp } = data;
|
2023-10-04 00:12:57 +00:00
|
|
|
const message = await __DEPRECATED$getMessageById(messageId);
|
2022-02-16 18:36:21 +00:00
|
|
|
if (!message) {
|
|
|
|
log.info(
|
|
|
|
`message ${messageId} was not found, maybe because it was deleted. Giving up on sending it`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const messageConversation = message.getConversation();
|
|
|
|
if (messageConversation !== conversation) {
|
|
|
|
log.error(
|
|
|
|
`Message conversation '${messageConversation?.idForLogging()}' does not match job conversation ${conversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isOutgoing(message.attributes)) {
|
|
|
|
log.error(
|
|
|
|
`message ${messageId} was not an outgoing message to begin with. This is probably a bogus job. Giving up on sending it`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.isErased() || message.get('deletedForEveryone')) {
|
|
|
|
log.info(`message ${messageId} was erased. Giving up on sending it`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
// The original timestamp for this message
|
|
|
|
const messageTimestamp = getMessageSentTimestamp(message.attributes, {
|
|
|
|
includeEdits: false,
|
|
|
|
log,
|
|
|
|
});
|
|
|
|
// The timestamp for the thing we're sending now, whether a first send or an edit
|
|
|
|
const targetTimestamp = editedMessageTimestamp || messageTimestamp;
|
|
|
|
// The timestamp identifying the target of this edit; could be the original timestamp
|
|
|
|
// or the most recent edit prior to this one
|
|
|
|
const targetOfThisEditTimestamp = getTargetOfThisEditTimestamp({
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
let messageSendErrors: Array<Error> = [];
|
|
|
|
|
|
|
|
// We don't want to save errors on messages unless we're giving up. If it's our
|
|
|
|
// final attempt, we know upfront that we want to give up. However, we might also
|
|
|
|
// want to give up if (1) we get a 508 from the server, asking us to please stop
|
|
|
|
// (2) we get a 428 from the server, flagging the message for spam (3) some other
|
|
|
|
// reason not known at the time of this writing.
|
|
|
|
//
|
|
|
|
// This awkward callback lets us hold onto errors we might want to save, so we can
|
|
|
|
// decide whether to save them later on.
|
|
|
|
const saveErrors = isFinalAttempt
|
|
|
|
? undefined
|
|
|
|
: (errors: Array<Error>) => {
|
|
|
|
messageSendErrors = errors;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!shouldContinue) {
|
|
|
|
log.info(`message ${messageId} ran out of time. Giving up on sending it`);
|
2023-11-17 18:16:48 +00:00
|
|
|
await markMessageFailed({
|
|
|
|
message,
|
|
|
|
errors: [new Error('Message send ran out of time')],
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2022-02-16 18:36:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let profileKey: Uint8Array | undefined;
|
|
|
|
if (conversation.get('profileSharing')) {
|
|
|
|
profileKey = await ourProfileKeyService.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
let originalError: Error | undefined;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const {
|
2023-08-10 16:43:33 +00:00
|
|
|
allRecipientServiceIds,
|
|
|
|
recipientServiceIdsWithoutMe,
|
|
|
|
sentRecipientServiceIds,
|
|
|
|
untrustedServiceIds,
|
2022-02-16 18:36:21 +00:00
|
|
|
} = getMessageRecipients({
|
2022-05-31 19:46:56 +00:00
|
|
|
log,
|
2022-02-16 18:36:21 +00:00
|
|
|
message,
|
|
|
|
conversation,
|
2023-11-17 18:16:48 +00:00
|
|
|
targetTimestamp,
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (untrustedServiceIds.length) {
|
2022-02-16 18:36:21 +00:00
|
|
|
window.reduxActions.conversations.conversationStoppedByMissingVerification(
|
|
|
|
{
|
|
|
|
conversationId: conversation.id,
|
2023-08-10 16:43:33 +00:00
|
|
|
untrustedServiceIds,
|
2022-02-16 18:36:21 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
throw new Error(
|
2023-08-10 16:43:33 +00:00
|
|
|
`Message ${messageId} sending blocked because ${untrustedServiceIds.length} conversation(s) were untrusted. Failing this attempt.`
|
2022-02-16 18:36:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (!allRecipientServiceIds.length) {
|
2022-02-16 18:36:21 +00:00
|
|
|
log.warn(
|
|
|
|
`trying to send message ${messageId} but it looks like it was already sent to everyone. This is unexpected, but we're giving up`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
attachments,
|
|
|
|
body,
|
2022-04-11 20:57:44 +00:00
|
|
|
contact,
|
2022-02-16 18:36:21 +00:00
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
expireTimer,
|
2023-04-14 18:16:28 +00:00
|
|
|
bodyRanges,
|
2022-02-16 18:36:21 +00:00
|
|
|
preview,
|
|
|
|
quote,
|
2023-04-20 16:31:59 +00:00
|
|
|
reaction,
|
2022-02-16 18:36:21 +00:00
|
|
|
sticker,
|
2022-11-02 23:48:38 +00:00
|
|
|
storyMessage,
|
2022-03-11 23:14:32 +00:00
|
|
|
storyContext,
|
2023-11-17 18:16:48 +00:00
|
|
|
} = await getMessageSendData({ log, message, targetTimestamp });
|
2022-02-16 18:36:21 +00:00
|
|
|
|
2022-11-02 23:48:38 +00:00
|
|
|
if (reaction) {
|
|
|
|
strictAssert(
|
|
|
|
storyMessage,
|
|
|
|
'Only story reactions can be sent as normal messages'
|
|
|
|
);
|
|
|
|
|
|
|
|
const ourConversationId =
|
|
|
|
window.ConversationController.getOurConversationIdOrThrow();
|
|
|
|
|
|
|
|
if (
|
|
|
|
!canReact(
|
|
|
|
storyMessage.attributes,
|
|
|
|
ourConversationId,
|
|
|
|
findAndFormatContact
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
log.info(
|
|
|
|
`could not react to ${messageId}. Removing this pending reaction`
|
|
|
|
);
|
2023-11-17 18:16:48 +00:00
|
|
|
await markMessageFailed({
|
|
|
|
message,
|
|
|
|
errors: [new Error('Could not react to story')],
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2022-11-02 23:48:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
log.info(
|
|
|
|
'Sending normal message;',
|
|
|
|
`editedMessageTimestamp=${editedMessageTimestamp},`,
|
|
|
|
`storyMessage=${Boolean(storyMessage)}`
|
|
|
|
);
|
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
let messageSendPromise: Promise<CallbackResultType | void>;
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (recipientServiceIdsWithoutMe.length === 0) {
|
2022-05-31 23:20:45 +00:00
|
|
|
if (
|
|
|
|
!isMe(conversation.attributes) &&
|
|
|
|
!isGroup(conversation.attributes) &&
|
2023-08-10 16:43:33 +00:00
|
|
|
sentRecipientServiceIds.length === 0
|
2022-05-31 23:20:45 +00:00
|
|
|
) {
|
2022-05-05 16:27:56 +00:00
|
|
|
log.info(
|
2022-05-31 23:20:45 +00:00
|
|
|
'No recipients; not sending to ourselves or to group, and no successful sends. Failing job.'
|
2022-05-05 16:27:56 +00:00
|
|
|
);
|
2023-11-17 18:16:48 +00:00
|
|
|
void markMessageFailed({
|
|
|
|
message,
|
|
|
|
errors: [new Error('No valid recipients')],
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2022-05-05 16:27:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're sending to Note to Self or a 'lonely group' with just us in it
|
2022-11-04 13:22:07 +00:00
|
|
|
// or sending a story to a group where all other users don't have the stories
|
|
|
|
// capabilities (effectively a 'lonely group' in the context of stories)
|
2022-02-16 18:36:21 +00:00
|
|
|
log.info('sending sync message only');
|
2023-05-31 15:54:45 +00:00
|
|
|
const dataMessage = await messaging.getDataOrEditMessage({
|
2022-02-16 18:36:21 +00:00
|
|
|
attachments,
|
|
|
|
body,
|
2023-04-14 18:16:28 +00:00
|
|
|
bodyRanges,
|
2022-04-11 20:57:44 +00:00
|
|
|
contact,
|
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
expireTimer,
|
2022-02-16 18:36:21 +00:00
|
|
|
groupV2: conversation.getGroupV2Info({
|
2023-08-10 16:43:33 +00:00
|
|
|
members: recipientServiceIdsWithoutMe,
|
2022-02-16 18:36:21 +00:00
|
|
|
}),
|
|
|
|
preview,
|
|
|
|
profileKey,
|
|
|
|
quote,
|
2023-08-10 16:43:33 +00:00
|
|
|
recipients: allRecipientServiceIds,
|
2022-02-16 18:36:21 +00:00
|
|
|
sticker,
|
2022-11-04 13:22:07 +00:00
|
|
|
storyContext,
|
2023-11-17 18:16:48 +00:00
|
|
|
targetTimestampForEdit: editedMessageTimestamp
|
|
|
|
? targetOfThisEditTimestamp
|
|
|
|
: undefined,
|
|
|
|
timestamp: targetTimestamp,
|
2022-11-02 23:48:38 +00:00
|
|
|
reaction,
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
2023-11-17 18:16:48 +00:00
|
|
|
messageSendPromise = message.sendSyncMessageOnly({
|
|
|
|
dataMessage,
|
|
|
|
saveErrors,
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2022-02-16 18:36:21 +00:00
|
|
|
} else {
|
|
|
|
const conversationType = conversation.get('type');
|
|
|
|
const sendOptions = await getSendOptions(conversation.attributes);
|
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
|
|
|
|
|
|
|
let innerPromise: Promise<CallbackResultType>;
|
|
|
|
if (conversationType === Message.GROUP) {
|
|
|
|
// Note: this will happen for all old jobs queued beore 5.32.x
|
|
|
|
if (isGroupV2(conversation.attributes) && !isNumber(revision)) {
|
|
|
|
log.error('No revision provided, but conversation is GroupV2');
|
|
|
|
}
|
|
|
|
|
|
|
|
const groupV2Info = conversation.getGroupV2Info({
|
2023-08-10 16:43:33 +00:00
|
|
|
members: recipientServiceIdsWithoutMe,
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
|
|
|
if (groupV2Info && isNumber(revision)) {
|
|
|
|
groupV2Info.revision = revision;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info('sending group message');
|
|
|
|
innerPromise = conversation.queueJob(
|
|
|
|
'conversationQueue/sendNormalMessage',
|
2022-05-23 22:08:13 +00:00
|
|
|
abortSignal =>
|
2022-07-13 00:37:21 +00:00
|
|
|
sendToGroup({
|
2022-05-23 22:08:13 +00:00
|
|
|
abortSignal,
|
2022-02-16 18:36:21 +00:00
|
|
|
contentHint: ContentHint.RESENDABLE,
|
|
|
|
groupSendOptions: {
|
|
|
|
attachments,
|
2023-04-14 18:16:28 +00:00
|
|
|
bodyRanges,
|
2022-04-11 20:57:44 +00:00
|
|
|
contact,
|
2022-02-16 18:36:21 +00:00
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
expireTimer,
|
|
|
|
groupV2: groupV2Info,
|
|
|
|
messageText: body,
|
|
|
|
preview,
|
|
|
|
profileKey,
|
|
|
|
quote,
|
|
|
|
sticker,
|
2022-04-22 18:45:45 +00:00
|
|
|
storyContext,
|
2022-11-02 23:48:38 +00:00
|
|
|
reaction,
|
2023-11-17 18:16:48 +00:00
|
|
|
targetTimestampForEdit: editedMessageTimestamp
|
|
|
|
? targetOfThisEditTimestamp
|
|
|
|
: undefined,
|
|
|
|
timestamp: targetTimestamp,
|
2022-02-16 18:36:21 +00:00
|
|
|
},
|
|
|
|
messageId,
|
|
|
|
sendOptions,
|
|
|
|
sendTarget: conversation.toSenderKeyTarget(),
|
|
|
|
sendType: 'message',
|
2022-10-07 17:02:08 +00:00
|
|
|
story: Boolean(storyContext),
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: true,
|
2022-02-16 18:36:21 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
2022-02-22 20:34:57 +00:00
|
|
|
if (!isConversationAccepted(conversation.attributes)) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is not accepted; refusing to send`
|
|
|
|
);
|
2023-11-17 18:16:48 +00:00
|
|
|
void markMessageFailed({
|
|
|
|
message,
|
|
|
|
errors: [new Error('Message request was not accepted')],
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2022-02-22 20:34:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isConversationUnregistered(conversation.attributes)) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is unregistered; refusing to send`
|
|
|
|
);
|
2023-11-17 18:16:48 +00:00
|
|
|
void markMessageFailed({
|
|
|
|
message,
|
|
|
|
errors: [new Error('Contact no longer has a Signal account')],
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2022-02-22 20:34:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 02:40:56 +00:00
|
|
|
if (conversation.isBlocked()) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is blocked; refusing to send`
|
|
|
|
);
|
2023-11-17 18:16:48 +00:00
|
|
|
void markMessageFailed({
|
|
|
|
message,
|
|
|
|
errors: [new Error('Contact is blocked')],
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2022-02-25 02:40:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-22 20:34:57 +00:00
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
log.info('sending direct message');
|
2023-08-10 16:43:33 +00:00
|
|
|
innerPromise = messaging.sendMessageToServiceId({
|
2022-02-16 18:36:21 +00:00
|
|
|
attachments,
|
2023-04-14 18:16:28 +00:00
|
|
|
bodyRanges,
|
2022-04-11 20:57:44 +00:00
|
|
|
contact,
|
|
|
|
contentHint: ContentHint.RESENDABLE,
|
2022-02-16 18:36:21 +00:00
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
expireTimer,
|
|
|
|
groupId: undefined,
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: recipientServiceIdsWithoutMe[0],
|
2022-04-11 20:57:44 +00:00
|
|
|
messageText: body,
|
2022-02-16 18:36:21 +00:00
|
|
|
options: sendOptions,
|
2022-04-11 20:57:44 +00:00
|
|
|
preview,
|
|
|
|
profileKey,
|
|
|
|
quote,
|
|
|
|
sticker,
|
2022-03-11 23:14:32 +00:00
|
|
|
storyContext,
|
2022-11-02 23:48:38 +00:00
|
|
|
reaction,
|
2023-11-17 18:16:48 +00:00
|
|
|
targetTimestampForEdit: editedMessageTimestamp
|
|
|
|
? targetOfThisEditTimestamp
|
|
|
|
: undefined,
|
|
|
|
timestamp: targetTimestamp,
|
2022-10-07 17:02:08 +00:00
|
|
|
// Note: 1:1 story replies should not set story=true - they aren't group sends
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: true,
|
2022-08-15 21:53:33 +00:00
|
|
|
includePniSignatureMessage: true,
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
messageSendPromise = message.send({
|
|
|
|
promise: handleMessageSend(innerPromise, {
|
2022-02-16 18:36:21 +00:00
|
|
|
messageIds: [messageId],
|
|
|
|
sendType: 'message',
|
|
|
|
}),
|
2023-11-17 18:16:48 +00:00
|
|
|
saveErrors,
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
// Because message.send swallows and processes errors, we'll await the inner promise
|
|
|
|
// to get the SendMessageProtoError, which gives us information upstream
|
|
|
|
// processors need to detect certain kinds of situations.
|
|
|
|
try {
|
|
|
|
await innerPromise;
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
originalError = error;
|
|
|
|
} else {
|
|
|
|
log.error(
|
|
|
|
`promiseForError threw something other than an error: ${Errors.toLogFormat(
|
|
|
|
error
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await messageSendPromise;
|
|
|
|
|
|
|
|
const didFullySend =
|
2023-11-17 18:16:48 +00:00
|
|
|
!messageSendErrors.length ||
|
|
|
|
didSendToEveryone({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
targetTimestamp: editedMessageTimestamp || messageTimestamp,
|
|
|
|
});
|
2022-02-16 18:36:21 +00:00
|
|
|
if (!didFullySend) {
|
|
|
|
throw new Error('message did not fully send');
|
|
|
|
}
|
|
|
|
} catch (thrownError: unknown) {
|
|
|
|
const errors = [thrownError, ...messageSendErrors];
|
|
|
|
await handleMultipleSendErrors({
|
|
|
|
errors,
|
|
|
|
isFinalAttempt,
|
|
|
|
log,
|
2023-11-17 18:16:48 +00:00
|
|
|
markFailed: () =>
|
|
|
|
markMessageFailed({
|
|
|
|
message,
|
|
|
|
errors: messageSendErrors,
|
|
|
|
targetTimestamp,
|
|
|
|
}),
|
2022-02-16 18:36:21 +00:00
|
|
|
timeRemaining,
|
|
|
|
// In the case of a failed group send thrownError will not be SentMessageProtoError,
|
|
|
|
// but we should have been able to harvest the original error. In the Note to Self
|
|
|
|
// send case, thrownError will be the error we care about, and we won't have an
|
|
|
|
// originalError.
|
|
|
|
toThrow: originalError || thrownError,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMessageRecipients({
|
2022-05-31 19:46:56 +00:00
|
|
|
log,
|
2022-02-16 18:36:21 +00:00
|
|
|
conversation,
|
|
|
|
message,
|
2023-11-17 18:16:48 +00:00
|
|
|
targetTimestamp,
|
2022-02-16 18:36:21 +00:00
|
|
|
}: Readonly<{
|
2022-05-31 19:46:56 +00:00
|
|
|
log: LoggerType;
|
2022-02-16 18:36:21 +00:00
|
|
|
conversation: ConversationModel;
|
|
|
|
message: MessageModel;
|
2023-11-17 18:16:48 +00:00
|
|
|
targetTimestamp: number;
|
2022-02-16 18:36:21 +00:00
|
|
|
}>): {
|
2023-08-10 16:43:33 +00:00
|
|
|
allRecipientServiceIds: Array<ServiceIdString>;
|
|
|
|
recipientServiceIdsWithoutMe: Array<ServiceIdString>;
|
|
|
|
sentRecipientServiceIds: Array<ServiceIdString>;
|
|
|
|
untrustedServiceIds: Array<ServiceIdString>;
|
2022-02-16 18:36:21 +00:00
|
|
|
} {
|
2023-08-10 16:43:33 +00:00
|
|
|
const allRecipientServiceIds: Array<ServiceIdString> = [];
|
|
|
|
const recipientServiceIdsWithoutMe: Array<ServiceIdString> = [];
|
|
|
|
const untrustedServiceIds: Array<ServiceIdString> = [];
|
|
|
|
const sentRecipientServiceIds: Array<ServiceIdString> = [];
|
2022-02-16 18:36:21 +00:00
|
|
|
|
2022-03-04 19:22:31 +00:00
|
|
|
const currentConversationRecipients = conversation.getMemberConversationIds();
|
2022-02-16 18:36:21 +00:00
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
const sendStateByConversationId =
|
|
|
|
getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'sendStateByConversationId',
|
|
|
|
targetTimestamp,
|
|
|
|
}) || {};
|
|
|
|
|
|
|
|
Object.entries(sendStateByConversationId).forEach(
|
2022-02-16 18:36:21 +00:00
|
|
|
([recipientConversationId, sendState]) => {
|
|
|
|
const recipient = window.ConversationController.get(
|
|
|
|
recipientConversationId
|
|
|
|
);
|
|
|
|
if (!recipient) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isRecipientMe = isMe(recipient.attributes);
|
|
|
|
|
|
|
|
if (
|
|
|
|
!currentConversationRecipients.has(recipientConversationId) &&
|
|
|
|
!isRecipientMe
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recipient.isUntrusted()) {
|
2023-08-10 16:43:33 +00:00
|
|
|
const serviceId = recipient.getServiceId();
|
|
|
|
if (!serviceId) {
|
2022-05-31 19:46:56 +00:00
|
|
|
log.error(
|
2023-08-10 16:43:33 +00:00
|
|
|
`sendNormalMessage/getMessageRecipients: Untrusted conversation ${recipient.idForLogging()} missing serviceId.`
|
2022-05-31 19:46:56 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2023-08-10 16:43:33 +00:00
|
|
|
untrustedServiceIds.push(serviceId);
|
2022-03-04 19:22:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (recipient.isUnregistered()) {
|
|
|
|
return;
|
2022-02-16 18:36:21 +00:00
|
|
|
}
|
2023-01-20 19:42:55 +00:00
|
|
|
if (recipient.isBlocked()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
const recipientIdentifier = recipient.getSendTarget();
|
|
|
|
if (!recipientIdentifier) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-31 23:20:45 +00:00
|
|
|
if (isSent(sendState.status)) {
|
2023-08-10 16:43:33 +00:00
|
|
|
sentRecipientServiceIds.push(recipientIdentifier);
|
2022-05-31 23:20:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
allRecipientServiceIds.push(recipientIdentifier);
|
2022-02-16 18:36:21 +00:00
|
|
|
if (!isRecipientMe) {
|
2023-08-10 16:43:33 +00:00
|
|
|
recipientServiceIdsWithoutMe.push(recipientIdentifier);
|
2022-02-16 18:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
2023-08-10 16:43:33 +00:00
|
|
|
allRecipientServiceIds,
|
|
|
|
recipientServiceIdsWithoutMe,
|
|
|
|
sentRecipientServiceIds,
|
|
|
|
untrustedServiceIds,
|
2022-02-16 18:36:21 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getMessageSendData({
|
|
|
|
log,
|
|
|
|
message,
|
2023-11-17 18:16:48 +00:00
|
|
|
targetTimestamp,
|
2022-02-16 18:36:21 +00:00
|
|
|
}: Readonly<{
|
|
|
|
log: LoggerType;
|
|
|
|
message: MessageModel;
|
2023-11-17 18:16:48 +00:00
|
|
|
targetTimestamp: number;
|
2022-02-16 18:36:21 +00:00
|
|
|
}>): Promise<{
|
2023-04-20 16:31:59 +00:00
|
|
|
attachments: Array<UploadedAttachmentType>;
|
2022-02-16 18:36:21 +00:00
|
|
|
body: undefined | string;
|
2023-04-20 16:31:59 +00:00
|
|
|
contact?: Array<EmbeddedContactWithUploadedAvatar>;
|
2022-02-16 18:36:21 +00:00
|
|
|
deletedForEveryoneTimestamp: undefined | number;
|
2022-11-16 20:18:02 +00:00
|
|
|
expireTimer: undefined | DurationInSeconds;
|
2023-04-14 18:16:28 +00:00
|
|
|
bodyRanges: undefined | ReadonlyArray<RawBodyRange>;
|
2023-04-20 16:31:59 +00:00
|
|
|
preview: Array<OutgoingLinkPreviewType> | undefined;
|
|
|
|
quote: OutgoingQuoteType | undefined;
|
|
|
|
sticker: OutgoingStickerType | undefined;
|
2022-11-02 23:48:38 +00:00
|
|
|
reaction: ReactionType | undefined;
|
|
|
|
storyMessage?: MessageModel;
|
2022-03-11 23:14:32 +00:00
|
|
|
storyContext?: StoryContextType;
|
2022-02-16 18:36:21 +00:00
|
|
|
}> {
|
2022-03-11 23:14:32 +00:00
|
|
|
const storyId = message.get('storyId');
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
// Figure out if we need to upload message body as an attachment.
|
2023-11-17 18:16:48 +00:00
|
|
|
let body = getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'body',
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2023-04-20 16:31:59 +00:00
|
|
|
let maybeLongAttachment: AttachmentWithHydratedData | undefined;
|
|
|
|
if (body && body.length > LONG_ATTACHMENT_LIMIT) {
|
|
|
|
const data = Bytes.fromString(body);
|
|
|
|
|
|
|
|
maybeLongAttachment = {
|
|
|
|
contentType: LONG_MESSAGE,
|
2023-11-17 18:16:48 +00:00
|
|
|
fileName: `long-message-${targetTimestamp}.txt`,
|
2023-04-20 16:31:59 +00:00
|
|
|
data,
|
|
|
|
size: data.byteLength,
|
|
|
|
};
|
|
|
|
body = body.slice(0, LONG_ATTACHMENT_LIMIT);
|
|
|
|
}
|
2022-02-16 18:36:21 +00:00
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
const uploadQueue = new PQueue({
|
|
|
|
concurrency: MAX_CONCURRENT_ATTACHMENT_UPLOADS,
|
|
|
|
});
|
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
const preUploadAttachments =
|
|
|
|
getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'attachments',
|
|
|
|
targetTimestamp,
|
|
|
|
}) || [];
|
2023-04-20 16:31:59 +00:00
|
|
|
const [
|
|
|
|
uploadedAttachments,
|
|
|
|
maybeUploadedLongAttachment,
|
|
|
|
contact,
|
|
|
|
preview,
|
|
|
|
quote,
|
|
|
|
sticker,
|
|
|
|
storyMessage,
|
|
|
|
] = await Promise.all([
|
|
|
|
uploadQueue.addAll(
|
2023-11-17 18:16:48 +00:00
|
|
|
preUploadAttachments.map(
|
|
|
|
attachment => () =>
|
|
|
|
uploadSingleAttachment({
|
|
|
|
attachment,
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
})
|
2023-04-20 16:31:59 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
uploadQueue.add(async () =>
|
|
|
|
maybeLongAttachment ? uploadAttachment(maybeLongAttachment) : undefined
|
|
|
|
),
|
|
|
|
uploadMessageContacts(message, uploadQueue),
|
2023-11-17 18:16:48 +00:00
|
|
|
uploadMessagePreviews({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
uploadQueue,
|
|
|
|
}),
|
|
|
|
uploadMessageQuote({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
uploadQueue,
|
|
|
|
}),
|
2023-04-20 16:31:59 +00:00
|
|
|
uploadMessageSticker(message, uploadQueue),
|
2023-10-04 00:12:57 +00:00
|
|
|
storyId ? __DEPRECATED$getMessageById(storyId) : undefined,
|
2023-04-20 16:31:59 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Save message after uploading attachments
|
|
|
|
await window.Signal.Data.saveMessage(message.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2023-04-20 16:31:59 +00:00
|
|
|
});
|
2022-02-16 18:36:21 +00:00
|
|
|
|
2022-11-02 23:48:38 +00:00
|
|
|
const storyReaction = message.get('storyReaction');
|
2023-08-16 20:54:39 +00:00
|
|
|
const storySourceServiceId = storyMessage?.get('sourceServiceId');
|
2023-08-10 16:43:33 +00:00
|
|
|
|
|
|
|
let reactionForSend: ReactionType | undefined;
|
|
|
|
if (storyReaction) {
|
2023-08-16 20:54:39 +00:00
|
|
|
const { targetAuthorAci, ...restOfReaction } = storyReaction;
|
2023-08-10 16:43:33 +00:00
|
|
|
|
|
|
|
reactionForSend = {
|
|
|
|
...restOfReaction,
|
|
|
|
targetAuthorAci,
|
|
|
|
remove: false,
|
|
|
|
};
|
|
|
|
}
|
2022-11-02 23:48:38 +00:00
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
return {
|
2023-04-20 16:31:59 +00:00
|
|
|
attachments: [
|
|
|
|
...(maybeUploadedLongAttachment ? [maybeUploadedLongAttachment] : []),
|
|
|
|
...uploadedAttachments,
|
|
|
|
],
|
2022-02-16 18:36:21 +00:00
|
|
|
body,
|
2022-04-11 20:57:44 +00:00
|
|
|
contact,
|
2022-02-16 18:36:21 +00:00
|
|
|
deletedForEveryoneTimestamp: message.get('deletedForEveryoneTimestamp'),
|
|
|
|
expireTimer: message.get('expireTimer'),
|
2023-11-17 18:16:48 +00:00
|
|
|
bodyRanges: getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'bodyRanges',
|
|
|
|
targetTimestamp,
|
|
|
|
}),
|
2022-02-16 18:36:21 +00:00
|
|
|
preview,
|
|
|
|
quote,
|
2023-08-10 16:43:33 +00:00
|
|
|
reaction: reactionForSend,
|
2022-02-16 18:36:21 +00:00
|
|
|
sticker,
|
2022-11-02 23:48:38 +00:00
|
|
|
storyMessage,
|
2022-03-11 23:14:32 +00:00
|
|
|
storyContext: storyMessage
|
|
|
|
? {
|
2023-08-16 20:54:39 +00:00
|
|
|
authorAci: storySourceServiceId
|
2023-08-10 16:43:33 +00:00
|
|
|
? normalizeAci(
|
2023-08-16 20:54:39 +00:00
|
|
|
storySourceServiceId,
|
2023-08-10 16:43:33 +00:00
|
|
|
'sendNormalMessage.storyContext.authorAci'
|
|
|
|
)
|
|
|
|
: undefined,
|
2022-03-11 23:14:32 +00:00
|
|
|
timestamp: storyMessage.get('sent_at'),
|
|
|
|
}
|
2022-03-04 23:20:47 +00:00
|
|
|
: undefined,
|
2023-04-20 16:31:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
async function uploadSingleAttachment({
|
|
|
|
attachment,
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
}: {
|
|
|
|
attachment: AttachmentType;
|
|
|
|
log: LoggerType;
|
|
|
|
message: MessageModel;
|
|
|
|
targetTimestamp: number;
|
|
|
|
}): Promise<UploadedAttachmentType> {
|
2023-04-20 16:31:59 +00:00
|
|
|
const { loadAttachmentData } = window.Signal.Migrations;
|
|
|
|
|
|
|
|
const withData = await loadAttachmentData(attachment);
|
|
|
|
const uploaded = await uploadAttachment(withData);
|
|
|
|
|
|
|
|
// Add digest to the attachment
|
|
|
|
const logId = `uploadSingleAttachment(${message.idForLogging()}`;
|
2023-11-17 18:16:48 +00:00
|
|
|
const oldAttachments = getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'attachments',
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2023-04-20 16:31:59 +00:00
|
|
|
strictAssert(
|
|
|
|
oldAttachments !== undefined,
|
|
|
|
`${logId}: Attachment was uploaded, but message doesn't ` +
|
|
|
|
'have attachments anymore'
|
|
|
|
);
|
|
|
|
|
|
|
|
const index = oldAttachments.indexOf(attachment);
|
|
|
|
strictAssert(
|
|
|
|
index !== -1,
|
|
|
|
`${logId}: Attachment was uploaded, but isn't in the message anymore`
|
|
|
|
);
|
|
|
|
|
|
|
|
const newAttachments = [...oldAttachments];
|
2023-10-04 00:09:31 +00:00
|
|
|
newAttachments[index] = {
|
|
|
|
...newAttachments[index],
|
|
|
|
...copyCdnFields(uploaded),
|
|
|
|
};
|
2023-04-20 16:31:59 +00:00
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
setPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'attachments',
|
|
|
|
targetTimestamp,
|
|
|
|
value: newAttachments,
|
|
|
|
});
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
return uploaded;
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
async function uploadMessageQuote({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
uploadQueue,
|
|
|
|
}: {
|
|
|
|
log: LoggerType;
|
|
|
|
message: MessageModel;
|
|
|
|
targetTimestamp: number;
|
|
|
|
uploadQueue: PQueue;
|
|
|
|
}): Promise<OutgoingQuoteType | undefined> {
|
2023-04-20 16:31:59 +00:00
|
|
|
const { loadQuoteData } = window.Signal.Migrations;
|
|
|
|
|
|
|
|
// We don't update the caches here because (1) we expect the caches to be populated
|
|
|
|
// on initial send, so they should be there in the 99% case (2) if you're retrying
|
|
|
|
// a failed message across restarts, we don't touch the cache for simplicity. If
|
|
|
|
// sends are failing, let's not add the complication of a cache.
|
2023-11-17 18:16:48 +00:00
|
|
|
const startingQuote = getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'quote',
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2023-04-20 16:31:59 +00:00
|
|
|
const loadedQuote =
|
2023-11-17 18:16:48 +00:00
|
|
|
message.cachedOutgoingQuoteData || (await loadQuoteData(startingQuote));
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
if (!loadedQuote) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-07-07 20:34:23 +00:00
|
|
|
const attachmentsAfterThumbnailUpload = await uploadQueue.addAll(
|
|
|
|
loadedQuote.attachments.map(
|
|
|
|
attachment => async (): Promise<OutgoingQuoteAttachmentType> => {
|
2023-04-20 16:31:59 +00:00
|
|
|
const { thumbnail } = attachment;
|
2023-07-07 20:34:23 +00:00
|
|
|
if (!thumbnail) {
|
|
|
|
return {
|
|
|
|
contentType: attachment.contentType,
|
|
|
|
};
|
|
|
|
}
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
const uploaded = await uploadAttachment(thumbnail);
|
|
|
|
|
|
|
|
return {
|
2023-12-02 01:02:46 +00:00
|
|
|
contentType: attachment.contentType,
|
2023-04-20 16:31:59 +00:00
|
|
|
fileName: attachment.fileName,
|
|
|
|
thumbnail: uploaded,
|
|
|
|
};
|
2023-07-07 20:34:23 +00:00
|
|
|
}
|
|
|
|
)
|
2023-04-20 16:31:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Update message with attachment digests
|
|
|
|
const logId = `uploadMessageQuote(${message.idForLogging()}`;
|
2023-11-17 18:16:48 +00:00
|
|
|
const oldQuote = getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'quote',
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2023-04-20 16:31:59 +00:00
|
|
|
strictAssert(oldQuote, `${logId}: Quote is gone after upload`);
|
|
|
|
|
|
|
|
const newQuote = {
|
|
|
|
...oldQuote,
|
|
|
|
attachments: oldQuote.attachments.map((attachment, index) => {
|
2023-04-21 18:26:49 +00:00
|
|
|
if (!attachment.thumbnail) {
|
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
strictAssert(
|
|
|
|
attachment.path === loadedQuote.attachments.at(index)?.path,
|
|
|
|
`${logId}: Quote attachment ${index} was updated from under us`
|
|
|
|
);
|
|
|
|
|
|
|
|
strictAssert(
|
|
|
|
attachment.thumbnail,
|
|
|
|
`${logId}: Quote attachment ${index} no longer has a thumbnail`
|
|
|
|
);
|
|
|
|
|
2023-07-07 20:34:23 +00:00
|
|
|
const attachmentAfterThumbnailUpload =
|
|
|
|
attachmentsAfterThumbnailUpload[index];
|
2023-04-20 16:31:59 +00:00
|
|
|
return {
|
|
|
|
...attachment,
|
|
|
|
thumbnail: {
|
|
|
|
...attachment.thumbnail,
|
2023-10-04 00:09:31 +00:00
|
|
|
...copyCdnFields(attachmentAfterThumbnailUpload.thumbnail),
|
2023-04-20 16:31:59 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
};
|
2023-11-17 18:16:48 +00:00
|
|
|
setPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'quote',
|
|
|
|
targetTimestamp,
|
|
|
|
value: newQuote,
|
|
|
|
});
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
isGiftBadge: loadedQuote.isGiftBadge,
|
|
|
|
id: loadedQuote.id,
|
2023-08-16 20:54:39 +00:00
|
|
|
authorAci: loadedQuote.authorAci
|
|
|
|
? normalizeAci(loadedQuote.authorAci, 'sendNormalMessage.quote.authorAci')
|
2023-08-10 16:43:33 +00:00
|
|
|
: undefined,
|
2023-04-20 16:31:59 +00:00
|
|
|
text: loadedQuote.text,
|
|
|
|
bodyRanges: loadedQuote.bodyRanges,
|
2023-07-07 20:34:23 +00:00
|
|
|
attachments: attachmentsAfterThumbnailUpload,
|
2023-04-20 16:31:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
async function uploadMessagePreviews({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
uploadQueue,
|
|
|
|
}: {
|
|
|
|
log: LoggerType;
|
|
|
|
message: MessageModel;
|
|
|
|
targetTimestamp: number;
|
|
|
|
uploadQueue: PQueue;
|
|
|
|
}): Promise<Array<OutgoingLinkPreviewType> | undefined> {
|
2023-04-20 16:31:59 +00:00
|
|
|
const { loadPreviewData } = window.Signal.Migrations;
|
|
|
|
|
|
|
|
// See uploadMessageQuote for comment on how we do caching for these
|
|
|
|
// attachments.
|
2023-11-17 18:16:48 +00:00
|
|
|
const startingPreview = getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'preview',
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
const loadedPreviews =
|
|
|
|
message.cachedOutgoingPreviewData ||
|
2023-11-17 18:16:48 +00:00
|
|
|
(await loadPreviewData(startingPreview));
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
if (!loadedPreviews) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (loadedPreviews.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploadedPreviews = await uploadQueue.addAll(
|
|
|
|
loadedPreviews.map(
|
|
|
|
preview => async (): Promise<OutgoingLinkPreviewType> => {
|
|
|
|
if (!preview.image) {
|
|
|
|
return {
|
|
|
|
...preview,
|
|
|
|
|
|
|
|
// Pacify typescript
|
|
|
|
image: undefined,
|
|
|
|
};
|
2022-11-02 23:48:38 +00:00
|
|
|
}
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...preview,
|
|
|
|
image: await uploadAttachment(preview.image),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Update message with attachment digests
|
|
|
|
const logId = `uploadMessagePreviews(${message.idForLogging()}`;
|
2023-11-17 18:16:48 +00:00
|
|
|
const oldPreview = getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'preview',
|
|
|
|
targetTimestamp,
|
|
|
|
});
|
2023-04-20 16:31:59 +00:00
|
|
|
strictAssert(oldPreview, `${logId}: Link preview is gone after upload`);
|
|
|
|
|
|
|
|
const newPreview = oldPreview.map((preview, index) => {
|
|
|
|
strictAssert(
|
|
|
|
preview.image?.path === loadedPreviews.at(index)?.image?.path,
|
|
|
|
`${logId}: Preview attachment ${index} was updated from under us`
|
|
|
|
);
|
|
|
|
|
|
|
|
const uploaded = uploadedPreviews.at(index);
|
|
|
|
if (!preview.image || !uploaded?.image) {
|
|
|
|
return preview;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...preview,
|
|
|
|
image: {
|
|
|
|
...preview.image,
|
2023-10-04 00:09:31 +00:00
|
|
|
...copyCdnFields(uploaded.image),
|
2023-04-20 16:31:59 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2023-11-17 18:16:48 +00:00
|
|
|
|
|
|
|
setPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'preview',
|
|
|
|
targetTimestamp,
|
|
|
|
value: newPreview,
|
|
|
|
});
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
return uploadedPreviews;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function uploadMessageSticker(
|
|
|
|
message: MessageModel,
|
|
|
|
uploadQueue: PQueue
|
|
|
|
): Promise<OutgoingStickerType | undefined> {
|
|
|
|
const { loadStickerData } = window.Signal.Migrations;
|
|
|
|
|
|
|
|
// See uploadMessageQuote for comment on how we do caching for these
|
|
|
|
// attachments.
|
2023-04-21 19:33:05 +00:00
|
|
|
const startingSticker = message.get('sticker');
|
|
|
|
const stickerWithData =
|
2023-04-20 16:31:59 +00:00
|
|
|
message.cachedOutgoingStickerData ||
|
2023-04-21 19:33:05 +00:00
|
|
|
(await loadStickerData(startingSticker));
|
2023-04-20 16:31:59 +00:00
|
|
|
|
2023-04-21 19:33:05 +00:00
|
|
|
if (!stickerWithData) {
|
2023-04-20 16:31:59 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-04-21 19:33:05 +00:00
|
|
|
const uploaded = await uploadQueue.add(() =>
|
|
|
|
uploadAttachment(stickerWithData.data)
|
|
|
|
);
|
2023-04-20 16:31:59 +00:00
|
|
|
|
|
|
|
// Add digest to the attachment
|
|
|
|
const logId = `uploadMessageSticker(${message.idForLogging()}`;
|
2023-04-21 19:33:05 +00:00
|
|
|
const existingSticker = message.get('sticker');
|
2023-04-20 16:31:59 +00:00
|
|
|
strictAssert(
|
2023-04-21 19:33:05 +00:00
|
|
|
existingSticker?.data !== undefined,
|
2023-04-20 16:31:59 +00:00
|
|
|
`${logId}: Sticker was uploaded, but message doesn't ` +
|
|
|
|
'have a sticker anymore'
|
|
|
|
);
|
|
|
|
strictAssert(
|
2023-04-21 19:33:05 +00:00
|
|
|
existingSticker.data.path === startingSticker?.data?.path,
|
2023-04-20 16:31:59 +00:00
|
|
|
`${logId}: Sticker was uploaded, but message has a different sticker`
|
|
|
|
);
|
|
|
|
message.set('sticker', {
|
2023-04-21 19:33:05 +00:00
|
|
|
...existingSticker,
|
2023-04-20 16:31:59 +00:00
|
|
|
data: {
|
2023-04-21 19:33:05 +00:00
|
|
|
...existingSticker.data,
|
2023-10-04 00:09:31 +00:00
|
|
|
...copyCdnFields(uploaded),
|
2023-04-20 16:31:59 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2023-04-21 19:33:05 +00:00
|
|
|
...stickerWithData,
|
2023-04-20 16:31:59 +00:00
|
|
|
data: uploaded,
|
2022-02-16 18:36:21 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:31:59 +00:00
|
|
|
async function uploadMessageContacts(
|
|
|
|
message: MessageModel,
|
|
|
|
uploadQueue: PQueue
|
|
|
|
): Promise<Array<EmbeddedContactWithUploadedAvatar> | undefined> {
|
|
|
|
const { loadContactData } = window.Signal.Migrations;
|
|
|
|
|
|
|
|
// See uploadMessageQuote for comment on how we do caching for these
|
|
|
|
// attachments.
|
|
|
|
const contacts =
|
|
|
|
message.cachedOutgoingContactData ||
|
|
|
|
(await loadContactData(message.get('contact')));
|
|
|
|
|
|
|
|
if (!contacts) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (contacts.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploadedContacts = await uploadQueue.addAll(
|
|
|
|
contacts.map(
|
|
|
|
contact => async (): Promise<EmbeddedContactWithUploadedAvatar> => {
|
|
|
|
const avatar = contact.avatar?.avatar;
|
|
|
|
// Pacify typescript
|
|
|
|
if (contact.avatar === undefined || !avatar) {
|
|
|
|
return {
|
|
|
|
...contact,
|
|
|
|
avatar: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploaded = await uploadAttachment(avatar);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...contact,
|
|
|
|
avatar: {
|
|
|
|
...contact.avatar,
|
|
|
|
avatar: uploaded,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add digest to the attachment
|
|
|
|
const logId = `uploadMessageContacts(${message.idForLogging()}`;
|
|
|
|
const oldContact = message.get('contact');
|
|
|
|
strictAssert(oldContact, `${logId}: Contacts are gone after upload`);
|
|
|
|
|
|
|
|
const newContact = oldContact.map((contact, index) => {
|
|
|
|
const loaded: EmbeddedContactWithHydratedAvatar | undefined =
|
|
|
|
contacts.at(index);
|
|
|
|
if (!contact.avatar) {
|
|
|
|
strictAssert(
|
|
|
|
loaded?.avatar === undefined,
|
|
|
|
`${logId}: Avatar erased in the message`
|
|
|
|
);
|
|
|
|
return contact;
|
|
|
|
}
|
|
|
|
|
|
|
|
strictAssert(
|
|
|
|
loaded !== undefined &&
|
|
|
|
loaded.avatar !== undefined &&
|
|
|
|
loaded.avatar.avatar.path === contact.avatar.avatar.path,
|
|
|
|
`${logId}: Avatar has incorrect path`
|
|
|
|
);
|
|
|
|
const uploaded = uploadedContacts.at(index);
|
|
|
|
strictAssert(
|
|
|
|
uploaded !== undefined && uploaded.avatar !== undefined,
|
|
|
|
`${logId}: Avatar wasn't uploaded properly`
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...contact,
|
|
|
|
avatar: {
|
|
|
|
...contact.avatar,
|
|
|
|
avatar: {
|
|
|
|
...contact.avatar.avatar,
|
2023-10-04 00:09:31 +00:00
|
|
|
...copyCdnFields(uploaded.avatar.avatar),
|
2023-04-20 16:31:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
message.set('contact', newContact);
|
|
|
|
|
|
|
|
return uploadedContacts;
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
async function markMessageFailed({
|
|
|
|
errors,
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
}: {
|
|
|
|
errors: Array<Error>;
|
|
|
|
message: MessageModel;
|
|
|
|
targetTimestamp: number;
|
|
|
|
}): Promise<void> {
|
|
|
|
message.markFailed(targetTimestamp);
|
2022-12-21 18:41:48 +00:00
|
|
|
void message.saveErrors(errors, { skipSave: true });
|
2022-02-16 18:36:21 +00:00
|
|
|
await window.Signal.Data.saveMessage(message.attributes, {
|
2023-08-10 16:43:33 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:16:48 +00:00
|
|
|
function didSendToEveryone({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
targetTimestamp,
|
|
|
|
}: {
|
|
|
|
log: LoggerType;
|
|
|
|
message: MessageModel;
|
|
|
|
targetTimestamp: number;
|
|
|
|
}): boolean {
|
2022-02-16 18:36:21 +00:00
|
|
|
const sendStateByConversationId =
|
2023-11-17 18:16:48 +00:00
|
|
|
getPropForTimestamp({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
prop: 'sendStateByConversationId',
|
|
|
|
targetTimestamp,
|
|
|
|
}) || {};
|
2022-02-16 18:36:21 +00:00
|
|
|
return Object.values(sendStateByConversationId).every(sendState =>
|
|
|
|
isSent(sendState.status)
|
|
|
|
);
|
|
|
|
}
|