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 { handleMessageSend } from '../../util/handleMessageSend';
|
|
|
|
import { getSendOptions } from '../../util/getSendOptions';
|
|
|
|
import {
|
|
|
|
isDirectConversation,
|
2022-04-25 21:03:47 +00:00
|
|
|
isGroup,
|
2022-02-16 18:36:21 +00:00
|
|
|
isGroupV2,
|
|
|
|
} from '../../util/whatTypeOfConversation';
|
|
|
|
import { SignalService as Proto } from '../../protobuf';
|
|
|
|
import {
|
|
|
|
handleMultipleSendErrors,
|
|
|
|
maybeExpandErrors,
|
|
|
|
} from './handleMultipleSendErrors';
|
|
|
|
import { ourProfileKeyService } from '../../services/ourProfileKey';
|
|
|
|
|
|
|
|
import type { ConversationModel } from '../../models/conversations';
|
|
|
|
import type {
|
|
|
|
ConversationQueueJobBundle,
|
|
|
|
ProfileKeyJobData,
|
|
|
|
} from '../conversationJobQueue';
|
|
|
|
import type { CallbackResultType } from '../../textsecure/Types.d';
|
2022-02-22 20:34:57 +00:00
|
|
|
import { isConversationUnregistered } from '../../util/isConversationUnregistered';
|
2022-04-25 21:03:47 +00:00
|
|
|
import type { ConversationAttributesType } from '../../model-types.d';
|
|
|
|
import {
|
|
|
|
OutgoingIdentityKeyError,
|
|
|
|
SendMessageChallengeError,
|
|
|
|
SendMessageProtoError,
|
|
|
|
UnregisteredUserError,
|
|
|
|
} from '../../textsecure/Errors';
|
2023-02-13 18:02:40 +00:00
|
|
|
import { shouldSendToConversation } from './shouldSendToConversation';
|
2023-04-11 03:54:43 +00:00
|
|
|
import { sendToGroup } from '../../util/sendToGroup';
|
2022-04-25 21:03:47 +00:00
|
|
|
|
|
|
|
export function canAllErrorsBeIgnored(
|
|
|
|
conversation: ConversationAttributesType,
|
|
|
|
error: unknown
|
|
|
|
): boolean {
|
|
|
|
if (
|
|
|
|
error instanceof OutgoingIdentityKeyError ||
|
|
|
|
error instanceof SendMessageChallengeError ||
|
|
|
|
error instanceof UnregisteredUserError
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Boolean(
|
|
|
|
isGroup(conversation) &&
|
|
|
|
error instanceof SendMessageProtoError &&
|
|
|
|
error.errors?.every(
|
|
|
|
item =>
|
|
|
|
item instanceof OutgoingIdentityKeyError ||
|
|
|
|
item instanceof SendMessageChallengeError ||
|
|
|
|
item instanceof UnregisteredUserError
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
// Note: because we don't have a recipient map, we will resend this message to folks that
|
|
|
|
// got it on the first go-round, if some sends fail. This is okay, because a recipient
|
|
|
|
// getting your profileKey again is just fine.
|
|
|
|
export async function sendProfileKey(
|
|
|
|
conversation: ConversationModel,
|
|
|
|
{
|
|
|
|
isFinalAttempt,
|
2022-06-13 21:39:35 +00:00
|
|
|
messaging,
|
2022-02-16 18:36:21 +00:00
|
|
|
shouldContinue,
|
|
|
|
timestamp,
|
|
|
|
timeRemaining,
|
|
|
|
log,
|
|
|
|
}: ConversationQueueJobBundle,
|
|
|
|
data: ProfileKeyJobData
|
|
|
|
): Promise<void> {
|
|
|
|
if (!shouldContinue) {
|
|
|
|
log.info('Ran out of time. Giving up on sending profile key');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-31 00:44:06 +00:00
|
|
|
if (!data?.isOneTimeSend && !conversation.get('profileSharing')) {
|
2022-02-16 18:36:21 +00:00
|
|
|
log.info('No longer sharing profile. Cancelling job.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const profileKey = await ourProfileKeyService.get();
|
|
|
|
if (!profileKey) {
|
|
|
|
log.info('Unable to fetch profile. Cancelling job.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`starting profile key share to ${conversation.idForLogging()} with timestamp ${timestamp}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const { revision } = data;
|
|
|
|
const sendOptions = await getSendOptions(conversation.attributes);
|
|
|
|
const { ContentHint } = Proto.UnidentifiedSenderMessage.Message;
|
|
|
|
const contentHint = ContentHint.RESENDABLE;
|
|
|
|
const sendType = 'profileKeyUpdate';
|
|
|
|
|
|
|
|
let sendPromise: Promise<CallbackResultType>;
|
|
|
|
|
|
|
|
// Note: flags and the profileKey itself are all that matter in the proto.
|
|
|
|
|
2023-02-13 18:02:40 +00:00
|
|
|
if (!shouldSendToConversation(conversation, log)) {
|
2022-12-20 22:17:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-16 18:36:21 +00:00
|
|
|
|
|
|
|
if (isDirectConversation(conversation.attributes)) {
|
2022-02-22 20:34:57 +00:00
|
|
|
if (isConversationUnregistered(conversation.attributes)) {
|
|
|
|
log.info(
|
|
|
|
`conversation ${conversation.idForLogging()} is unregistered; refusing to send`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
const proto = await messaging.getContentMessage({
|
2022-02-16 18:36:21 +00:00
|
|
|
flags: Proto.DataMessage.Flags.PROFILE_KEY_UPDATE,
|
|
|
|
profileKey,
|
|
|
|
recipients: conversation.getRecipients(),
|
2024-08-21 16:03:28 +00:00
|
|
|
expireTimerVersion: undefined,
|
2022-02-16 18:36:21 +00:00
|
|
|
timestamp,
|
2022-08-15 21:53:33 +00:00
|
|
|
includePniSignatureMessage: true,
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
2022-06-13 21:39:35 +00:00
|
|
|
sendPromise = messaging.sendIndividualProto({
|
2022-02-16 18:36:21 +00:00
|
|
|
contentHint,
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId: conversation.getSendTarget(),
|
2022-02-16 18:36:21 +00:00
|
|
|
options: sendOptions,
|
|
|
|
proto,
|
|
|
|
timestamp,
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: false,
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (isGroupV2(conversation.attributes) && !isNumber(revision)) {
|
|
|
|
log.error('No revision provided, but conversation is GroupV2');
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const ourAci = window.textsecure.storage.user.getCheckedAci();
|
|
|
|
if (!conversation.hasMember(ourAci)) {
|
2022-12-20 22:17:51 +00:00
|
|
|
log.info(
|
|
|
|
`We are not part of group ${conversation.idForLogging()}; refusing to send`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:36:21 +00:00
|
|
|
const groupV2Info = conversation.getGroupV2Info();
|
|
|
|
if (groupV2Info && isNumber(revision)) {
|
|
|
|
groupV2Info.revision = revision;
|
|
|
|
}
|
|
|
|
|
2023-04-11 03:54:43 +00:00
|
|
|
sendPromise = sendToGroup({
|
2022-02-16 18:36:21 +00:00
|
|
|
contentHint,
|
|
|
|
groupSendOptions: {
|
|
|
|
flags: Proto.DataMessage.Flags.PROFILE_KEY_UPDATE,
|
|
|
|
groupV2: groupV2Info,
|
|
|
|
profileKey,
|
|
|
|
timestamp,
|
|
|
|
},
|
|
|
|
messageId: undefined,
|
|
|
|
sendOptions,
|
|
|
|
sendTarget: conversation.toSenderKeyTarget(),
|
|
|
|
sendType,
|
2022-07-01 16:55:13 +00:00
|
|
|
urgent: false,
|
2022-02-16 18:36:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await handleMessageSend(sendPromise, {
|
|
|
|
messageIds: [],
|
|
|
|
sendType,
|
|
|
|
});
|
|
|
|
} catch (error: unknown) {
|
2022-04-25 21:03:47 +00:00
|
|
|
if (canAllErrorsBeIgnored(conversation.attributes, error)) {
|
2022-02-16 18:36:21 +00:00
|
|
|
log.info(
|
2023-01-01 11:41:40 +00:00
|
|
|
'Group send failures were all OutgoingIdentityKeyError, SendMessageChallengeError, or UnregisteredUserError. Returning successfully.'
|
2022-02-16 18:36:21 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await handleMultipleSendErrors({
|
|
|
|
errors: maybeExpandErrors(error),
|
|
|
|
isFinalAttempt,
|
|
|
|
log,
|
|
|
|
timeRemaining,
|
|
|
|
toThrow: error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|