Skip to content

Commit

Permalink
sendToGroup: Combine into final send result even in error scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Feb 25, 2022
1 parent 1d89ffc commit de942e1
Showing 1 changed file with 45 additions and 23 deletions.
68 changes: 45 additions & 23 deletions ts/util/sendToGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit de942e1

Please sign in to comment.