signal-desktop/ts/util/wrapWithSyncMessageSend.ts

105 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-02-16 18:36:21 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as log from '../logging/log';
import { SendMessageProtoError } from '../textsecure/Errors';
import { getSendOptions } from './getSendOptions';
import { handleMessageSend } from './handleMessageSend';
2023-06-29 19:17:27 +00:00
import { getTaggedConversationUuid } from './getConversationUuid';
2022-02-16 18:36:21 +00:00
import type { CallbackResultType } from '../textsecure/Types.d';
import type { ConversationModel } from '../models/conversations';
import type { SendTypesType } from './handleMessageSend';
import type MessageSender from '../textsecure/SendMessage';
import { areAllErrorsUnregistered } from '../jobs/helpers/areAllErrorsUnregistered';
export async function wrapWithSyncMessageSend({
conversation,
2022-11-29 02:07:26 +00:00
logId: parentLogId,
2022-02-16 18:36:21 +00:00
messageIds,
send,
sendType,
timestamp,
}: {
conversation: ConversationModel;
logId: string;
messageIds: Array<string>;
send: (sender: MessageSender) => Promise<CallbackResultType>;
sendType: SendTypesType;
timestamp: number;
}): Promise<void> {
2022-11-29 02:07:26 +00:00
const logId = `wrapWithSyncMessageSend(${parentLogId}, ${timestamp})`;
2022-02-16 18:36:21 +00:00
const sender = window.textsecure.messaging;
if (!sender) {
2022-11-29 02:07:26 +00:00
throw new Error(`${logId}: textsecure.messaging is not available!`);
2022-02-16 18:36:21 +00:00
}
let response: CallbackResultType | undefined;
let error: Error | undefined;
let didSuccessfullySendOne = false;
try {
response = await handleMessageSend(send(sender), { messageIds, sendType });
didSuccessfullySendOne = true;
} catch (thrown) {
if (thrown instanceof SendMessageProtoError) {
didSuccessfullySendOne = Boolean(
thrown.successfulIdentifiers && thrown.successfulIdentifiers.length > 0
);
error = thrown;
}
if (thrown instanceof Error) {
error = thrown;
} else {
2022-11-29 02:07:26 +00:00
log.error(`${logId}: Thrown value was not an Error, returning early`);
2022-02-16 18:36:21 +00:00
throw error;
}
}
if (!response && !error) {
2022-11-29 02:07:26 +00:00
throw new Error(`${logId}: message send didn't return result or error!`);
2022-02-16 18:36:21 +00:00
}
const dataMessage =
response?.dataMessage ||
(error instanceof SendMessageProtoError ? error.dataMessage : undefined);
if (didSuccessfullySendOne) {
if (!dataMessage) {
2022-11-29 02:07:26 +00:00
log.error(`${logId}: dataMessage was not returned by send!`);
2022-02-16 18:36:21 +00:00
} else {
2022-11-29 02:07:26 +00:00
log.info(`${logId}: Sending sync message... `);
2022-02-16 18:36:21 +00:00
const ourConversation =
window.ConversationController.getOurConversationOrThrow();
const options = await getSendOptions(ourConversation.attributes, {
syncMessage: true,
});
await handleMessageSend(
sender.sendSyncMessage({
destination: conversation.get('e164'),
2023-06-29 19:17:27 +00:00
destinationUuid: getTaggedConversationUuid(conversation.attributes),
2022-02-16 18:36:21 +00:00
encodedDataMessage: dataMessage,
expirationStartTimestamp: null,
options,
timestamp,
urgent: false,
2022-02-16 18:36:21 +00:00
}),
{ messageIds, sendType: sendType === 'message' ? 'sentSync' : sendType }
);
}
}
if (error instanceof Error) {
if (areAllErrorsUnregistered(conversation.attributes, error)) {
log.info(
2022-11-29 02:07:26 +00:00
`${logId}: Group send failures were all UnregisteredUserError, ` +
2023-01-01 11:41:40 +00:00
'returning successfully.'
2022-02-16 18:36:21 +00:00
);
return;
}
throw error;
}
}