sendToGroup: Combine into final send result even in error scenarios

This commit is contained in:
Scott Nonnenberg 2022-02-24 17:22:19 -08:00 committed by GitHub
parent 1d89ffcc2d
commit de942e1af2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 45 additions and 23 deletions

View File

@ -617,43 +617,65 @@ export async function sendToGroupViaSenderKey(options: {
deviceIds,
});
};
const normalSendResult = await window.textsecure.messaging.sendGroupProto({
contentHint,
groupId,
options: { ...sendOptions, online },
proto: contentMessage,
recipients: normalSendRecipients,
sendLogCallback,
timestamp,
});
try {
const normalSendResult = await window.textsecure.messaging.sendGroupProto({
contentHint,
groupId,
options: { ...sendOptions, online },
proto: contentMessage,
recipients: normalSendRecipients,
sendLogCallback,
timestamp,
});
return mergeSendResult({
result: normalSendResult,
senderKeyRecipients,
senderKeyRecipientsWithDevices,
});
} catch (error: unknown) {
if (error instanceof SendMessageProtoError) {
const callbackResult = mergeSendResult({
result: error,
senderKeyRecipients,
senderKeyRecipientsWithDevices,
});
throw new SendMessageProtoError(callbackResult);
}
throw error;
}
}
// Utility Methods
function mergeSendResult({
result,
senderKeyRecipients,
senderKeyRecipientsWithDevices,
}: {
result: CallbackResultType | SendMessageProtoError;
senderKeyRecipients: Array<string>;
senderKeyRecipientsWithDevices: Record<string, Array<number>>;
}): CallbackResultType {
return {
dataMessage: contentMessage.dataMessage
? Proto.DataMessage.encode(contentMessage.dataMessage).finish()
: undefined,
errors: normalSendResult.errors,
failoverIdentifiers: normalSendResult.failoverIdentifiers,
...result,
successfulIdentifiers: [
...(normalSendResult.successfulIdentifiers || []),
...(result.successfulIdentifiers || []),
...senderKeyRecipients,
],
unidentifiedDeliveries: [
...(normalSendResult.unidentifiedDeliveries || []),
...(result.unidentifiedDeliveries || []),
...senderKeyRecipients,
],
contentHint,
timestamp,
contentProto: Buffer.from(Proto.Content.encode(contentMessage).finish()),
recipients: {
...normalSendResult.recipients,
...result.recipients,
...senderKeyRecipientsWithDevices,
},
};
}
// Utility Methods
const MAX_SENDER_KEY_EXPIRE_DURATION = 90 * DAY;
function getSenderKeyExpireDuration(): number {