2022-02-16 18:36:21 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { isNumber } from 'lodash';
|
|
|
|
|
|
|
|
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';
|
|
|
|
import { getMessageById } from '../../messages/getMessageById';
|
|
|
|
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';
|
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 {
|
|
|
|
AttachmentType,
|
|
|
|
ContactWithHydratedAvatar,
|
2022-11-02 23:48:38 +00:00
|
|
|
ReactionType,
|
2022-04-11 20:57:44 +00:00
|
|
|
} from '../../textsecure/SendMessage';
|
2022-02-16 18:36:21 +00:00
|
|
|
import type { LinkPreviewType } from '../../types/message/LinkPreviews';
|
2023-04-10 16:31:45 +00:00
|
|
|
import { BodyRange } from '../../types/BodyRange';
|
|
|
|
import type { StoryContextType } from '../../types/Util';
|
2022-02-16 18:36:21 +00:00
|
|
|
import type { LoggerType } from '../../types/Logging';
|
2022-07-13 00:37:21 +00:00
|
|
|
import type { StickerWithHydratedData } from '../../types/Stickers';
|
|
|
|
import type { QuotedMessageType } from '../../model-types.d';
|
2022-02-16 18:36:21 +00:00
|
|
|
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';
|
2022-11-11 04:10:30 +00:00
|
|
|
import type { UUIDStringType } from '../../types/UUID';
|
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;
|
|
|
|
|
|
|
|
const { messageId, revision } = data;
|
|
|
|
const message = await getMessageById(messageId);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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`);
|
2022-05-18 20:44:51 +00:00
|
|
|
await markMessageFailed(message, [
|
|
|
|
new Error('Message send ran out of time'),
|
|
|
|
]);
|
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 {
|
|
|
|
allRecipientIdentifiers,
|
|
|
|
recipientIdentifiersWithoutMe,
|
2022-05-31 23:20:45 +00:00
|
|
|
sentRecipientIdentifiers,
|
2022-05-31 19:46:56 +00:00
|
|
|
untrustedUuids,
|
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,
|
|
|
|
});
|
|
|
|
|
2022-05-31 19:46:56 +00:00
|
|
|
if (untrustedUuids.length) {
|
2022-02-16 18:36:21 +00:00
|
|
|
window.reduxActions.conversations.conversationStoppedByMissingVerification(
|
|
|
|
{
|
|
|
|
conversationId: conversation.id,
|
2022-05-31 19:46:56 +00:00
|
|
|
untrustedUuids,
|
2022-02-16 18:36:21 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
throw new Error(
|
2022-05-31 19:46:56 +00:00
|
|
|
`Message ${messageId} sending blocked because ${untrustedUuids.length} conversation(s) were untrusted. Failing this attempt.`
|
2022-02-16 18:36:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allRecipientIdentifiers.length) {
|
|
|
|
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,
|
|
|
|
mentions,
|
|
|
|
messageTimestamp,
|
|
|
|
preview,
|
|
|
|
quote,
|
|
|
|
sticker,
|
2022-11-02 23:48:38 +00:00
|
|
|
storyMessage,
|
2022-03-11 23:14:32 +00:00
|
|
|
storyContext,
|
2022-11-02 23:48:38 +00:00
|
|
|
reaction,
|
2022-02-16 18:36:21 +00:00
|
|
|
} = await getMessageSendData({ log, message });
|
|
|
|
|
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`
|
|
|
|
);
|
|
|
|
await markMessageFailed(message, [
|
|
|
|
new Error('Could not react to story'),
|
|
|
|
]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
let messageSendPromise: Promise<CallbackResultType | void>;
|
|
|
|
|
|
|
|
if (recipientIdentifiersWithoutMe.length === 0) {
|
2022-05-31 23:20:45 +00:00
|
|
|
if (
|
|
|
|
!isMe(conversation.attributes) &&
|
|
|
|
!isGroup(conversation.attributes) &&
|
|
|
|
sentRecipientIdentifiers.length === 0
|
|
|
|
) {
|
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
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void markMessageFailed(message, [new Error('No valid recipients')]);
|
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');
|
2022-06-13 21:39:35 +00:00
|
|
|
const dataMessage = await messaging.getDataMessage({
|
2022-02-16 18:36:21 +00:00
|
|
|
attachments,
|
|
|
|
body,
|
2022-04-11 20:57:44 +00:00
|
|
|
contact,
|
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
expireTimer,
|
2022-02-16 18:36:21 +00:00
|
|
|
groupV2: conversation.getGroupV2Info({
|
|
|
|
members: recipientIdentifiersWithoutMe,
|
|
|
|
}),
|
|
|
|
preview,
|
|
|
|
profileKey,
|
|
|
|
quote,
|
|
|
|
recipients: allRecipientIdentifiers,
|
|
|
|
sticker,
|
2022-11-04 13:22:07 +00:00
|
|
|
storyContext,
|
2022-02-16 18:36:21 +00:00
|
|
|
timestamp: messageTimestamp,
|
2022-11-02 23:48:38 +00:00
|
|
|
reaction,
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
|
|
|
messageSendPromise = message.sendSyncMessageOnly(dataMessage, saveErrors);
|
|
|
|
} 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({
|
|
|
|
members: recipientIdentifiersWithoutMe,
|
|
|
|
});
|
|
|
|
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,
|
2022-04-11 20:57:44 +00:00
|
|
|
contact,
|
2022-02-16 18:36:21 +00:00
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
expireTimer,
|
|
|
|
groupV1: conversation.getGroupV1Info(
|
|
|
|
recipientIdentifiersWithoutMe
|
|
|
|
),
|
|
|
|
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,
|
2022-02-16 18:36:21 +00:00
|
|
|
timestamp: messageTimestamp,
|
|
|
|
mentions,
|
|
|
|
},
|
|
|
|
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`
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void markMessageFailed(message, [
|
2022-02-22 20:34:57 +00:00
|
|
|
new Error('Message request was not accepted'),
|
|
|
|
]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isConversationUnregistered(conversation.attributes)) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is unregistered; refusing to send`
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void markMessageFailed(message, [
|
2022-02-22 20:34:57 +00:00
|
|
|
new Error('Contact no longer has a Signal account'),
|
|
|
|
]);
|
|
|
|
return;
|
|
|
|
}
|
2022-02-25 02:40:56 +00:00
|
|
|
if (conversation.isBlocked()) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is blocked; refusing to send`
|
|
|
|
);
|
2022-12-21 18:41:48 +00:00
|
|
|
void markMessageFailed(message, [new Error('Contact is blocked')]);
|
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');
|
2022-06-13 21:39:35 +00:00
|
|
|
innerPromise = messaging.sendMessageToIdentifier({
|
2022-02-16 18:36:21 +00:00
|
|
|
attachments,
|
2022-04-11 20:57:44 +00:00
|
|
|
contact,
|
|
|
|
contentHint: ContentHint.RESENDABLE,
|
2022-02-16 18:36:21 +00:00
|
|
|
deletedForEveryoneTimestamp,
|
|
|
|
expireTimer,
|
|
|
|
groupId: undefined,
|
2022-04-11 20:57:44 +00:00
|
|
|
identifier: recipientIdentifiersWithoutMe[0],
|
|
|
|
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,
|
2022-04-11 20:57:44 +00:00
|
|
|
timestamp: messageTimestamp,
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
messageSendPromise = message.send(
|
|
|
|
handleMessageSend(innerPromise, {
|
|
|
|
messageIds: [messageId],
|
|
|
|
sendType: 'message',
|
|
|
|
}),
|
|
|
|
saveErrors
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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 =
|
|
|
|
!messageSendErrors.length || didSendToEveryone(message);
|
|
|
|
if (!didFullySend) {
|
|
|
|
throw new Error('message did not fully send');
|
|
|
|
}
|
|
|
|
} catch (thrownError: unknown) {
|
|
|
|
const errors = [thrownError, ...messageSendErrors];
|
|
|
|
await handleMultipleSendErrors({
|
|
|
|
errors,
|
|
|
|
isFinalAttempt,
|
|
|
|
log,
|
|
|
|
markFailed: () => markMessageFailed(message, messageSendErrors),
|
|
|
|
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,
|
|
|
|
}: Readonly<{
|
2022-05-31 19:46:56 +00:00
|
|
|
log: LoggerType;
|
2022-02-16 18:36:21 +00:00
|
|
|
conversation: ConversationModel;
|
|
|
|
message: MessageModel;
|
|
|
|
}>): {
|
|
|
|
allRecipientIdentifiers: Array<string>;
|
|
|
|
recipientIdentifiersWithoutMe: Array<string>;
|
2022-05-31 23:20:45 +00:00
|
|
|
sentRecipientIdentifiers: Array<string>;
|
2022-11-11 04:10:30 +00:00
|
|
|
untrustedUuids: Array<UUIDStringType>;
|
2022-02-16 18:36:21 +00:00
|
|
|
} {
|
|
|
|
const allRecipientIdentifiers: Array<string> = [];
|
|
|
|
const recipientIdentifiersWithoutMe: Array<string> = [];
|
2022-11-11 04:10:30 +00:00
|
|
|
const untrustedUuids: Array<UUIDStringType> = [];
|
2022-05-31 23:20:45 +00:00
|
|
|
const sentRecipientIdentifiers: Array<string> = [];
|
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
|
|
|
|
|
|
|
Object.entries(message.get('sendStateByConversationId') || {}).forEach(
|
|
|
|
([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()) {
|
2022-05-31 19:46:56 +00:00
|
|
|
const uuid = recipient.get('uuid');
|
|
|
|
if (!uuid) {
|
|
|
|
log.error(
|
|
|
|
`sendNormalMessage/getMessageRecipients: Untrusted conversation ${recipient.idForLogging()} missing UUID.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
untrustedUuids.push(uuid);
|
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)) {
|
|
|
|
sentRecipientIdentifiers.push(recipientIdentifier);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
allRecipientIdentifiers.push(recipientIdentifier);
|
|
|
|
if (!isRecipientMe) {
|
|
|
|
recipientIdentifiersWithoutMe.push(recipientIdentifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
allRecipientIdentifiers,
|
|
|
|
recipientIdentifiersWithoutMe,
|
2022-05-31 23:20:45 +00:00
|
|
|
sentRecipientIdentifiers,
|
2022-05-31 19:46:56 +00:00
|
|
|
untrustedUuids,
|
2022-02-16 18:36:21 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getMessageSendData({
|
|
|
|
log,
|
|
|
|
message,
|
|
|
|
}: Readonly<{
|
|
|
|
log: LoggerType;
|
|
|
|
message: MessageModel;
|
|
|
|
}>): Promise<{
|
|
|
|
attachments: Array<AttachmentType>;
|
|
|
|
body: undefined | string;
|
2022-04-11 20:57:44 +00:00
|
|
|
contact?: Array<ContactWithHydratedAvatar>;
|
2022-02-16 18:36:21 +00:00
|
|
|
deletedForEveryoneTimestamp: undefined | number;
|
2022-11-16 20:18:02 +00:00
|
|
|
expireTimer: undefined | DurationInSeconds;
|
2023-04-10 16:31:45 +00:00
|
|
|
mentions: undefined | ReadonlyArray<BodyRange<BodyRange.Mention>>;
|
2022-02-16 18:36:21 +00:00
|
|
|
messageTimestamp: number;
|
|
|
|
preview: Array<LinkPreviewType>;
|
2022-07-13 00:37:21 +00:00
|
|
|
quote: QuotedMessageType | null;
|
|
|
|
sticker: StickerWithHydratedData | 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
|
|
|
}> {
|
|
|
|
const {
|
|
|
|
loadAttachmentData,
|
2022-04-11 20:57:44 +00:00
|
|
|
loadContactData,
|
2022-02-16 18:36:21 +00:00
|
|
|
loadPreviewData,
|
|
|
|
loadQuoteData,
|
|
|
|
loadStickerData,
|
|
|
|
} = window.Signal.Migrations;
|
|
|
|
|
|
|
|
let messageTimestamp: number;
|
|
|
|
const sentAt = message.get('sent_at');
|
|
|
|
const timestamp = message.get('timestamp');
|
|
|
|
if (sentAt) {
|
|
|
|
messageTimestamp = sentAt;
|
|
|
|
} else if (timestamp) {
|
|
|
|
log.error('message lacked sent_at. Falling back to timestamp');
|
|
|
|
messageTimestamp = timestamp;
|
|
|
|
} else {
|
|
|
|
log.error(
|
|
|
|
'message lacked sent_at and timestamp. Falling back to current time'
|
|
|
|
);
|
|
|
|
messageTimestamp = Date.now();
|
|
|
|
}
|
|
|
|
|
2022-03-11 23:14:32 +00:00
|
|
|
const storyId = message.get('storyId');
|
|
|
|
|
2022-04-11 20:57:44 +00:00
|
|
|
const [attachmentsWithData, contact, preview, quote, sticker, storyMessage] =
|
2022-03-11 23:14:32 +00:00
|
|
|
await Promise.all([
|
|
|
|
// 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.
|
|
|
|
Promise.all((message.get('attachments') ?? []).map(loadAttachmentData)),
|
2022-04-11 20:57:44 +00:00
|
|
|
message.cachedOutgoingContactData ||
|
|
|
|
loadContactData(message.get('contact')),
|
2022-03-11 23:14:32 +00:00
|
|
|
message.cachedOutgoingPreviewData ||
|
|
|
|
loadPreviewData(message.get('preview')),
|
|
|
|
message.cachedOutgoingQuoteData || loadQuoteData(message.get('quote')),
|
|
|
|
message.cachedOutgoingStickerData ||
|
|
|
|
loadStickerData(message.get('sticker')),
|
|
|
|
storyId ? getMessageById(storyId) : undefined,
|
|
|
|
]);
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
const { body, attachments } = window.Whisper.Message.getLongMessageAttachment(
|
|
|
|
{
|
|
|
|
body: message.get('body'),
|
|
|
|
attachments: attachmentsWithData,
|
|
|
|
now: messageTimestamp,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-11-02 23:48:38 +00:00
|
|
|
const storyReaction = message.get('storyReaction');
|
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
return {
|
|
|
|
attachments,
|
|
|
|
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-04-10 16:31:45 +00:00
|
|
|
mentions: message.get('bodyRanges')?.filter(BodyRange.isMention),
|
2022-02-16 18:36:21 +00:00
|
|
|
messageTimestamp,
|
|
|
|
preview,
|
|
|
|
quote,
|
|
|
|
sticker,
|
2022-11-02 23:48:38 +00:00
|
|
|
storyMessage,
|
2022-03-11 23:14:32 +00:00
|
|
|
storyContext: storyMessage
|
|
|
|
? {
|
|
|
|
authorUuid: storyMessage.get('sourceUuid'),
|
|
|
|
timestamp: storyMessage.get('sent_at'),
|
|
|
|
}
|
2022-03-04 23:20:47 +00:00
|
|
|
: undefined,
|
2022-11-02 23:48:38 +00:00
|
|
|
reaction: storyReaction
|
|
|
|
? {
|
|
|
|
...storyReaction,
|
|
|
|
remove: false,
|
|
|
|
}
|
|
|
|
: undefined,
|
2022-02-16 18:36:21 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function markMessageFailed(
|
|
|
|
message: MessageModel,
|
|
|
|
errors: Array<Error>
|
|
|
|
): Promise<void> {
|
|
|
|
message.markFailed();
|
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, {
|
|
|
|
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function didSendToEveryone(message: Readonly<MessageModel>): boolean {
|
|
|
|
const sendStateByConversationId =
|
|
|
|
message.get('sendStateByConversationId') || {};
|
|
|
|
return Object.values(sendStateByConversationId).every(sendState =>
|
|
|
|
isSent(sendState.status)
|
|
|
|
);
|
|
|
|
}
|