Simplify group request making

This commit is contained in:
Fedor Indutny 2024-05-02 23:39:04 +02:00 committed by GitHub
parent bd5134a7ce
commit 480c1f4882
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -353,7 +353,7 @@ export async function getPreJoinGroupInfo(
Bytes.fromBase64(masterKeyBase64) Bytes.fromBase64(masterKeyBase64)
); );
return makeRequestWithTemporalRetry({ return makeRequestWithCredentials({
logId: `getPreJoinInfo/groupv2(${data.id})`, logId: `getPreJoinInfo/groupv2(${data.id})`,
publicParams: Bytes.toBase64(data.publicParams), publicParams: Bytes.toBase64(data.publicParams),
secretParams: Bytes.toBase64(data.secretParams), secretParams: Bytes.toBase64(data.secretParams),
@ -462,7 +462,7 @@ async function uploadAvatar(
}).finish(); }).finish();
const ciphertext = encryptGroupBlob(clientZkGroupCipher, blobPlaintext); const ciphertext = encryptGroupBlob(clientZkGroupCipher, blobPlaintext);
const key = await makeRequestWithTemporalRetry({ const key = await makeRequestWithCredentials({
logId: `uploadGroupAvatar/${logId}`, logId: `uploadGroupAvatar/${logId}`,
publicParams, publicParams,
secretParams, secretParams,
@ -1458,7 +1458,7 @@ async function uploadGroupChange({
throw new Error('uploadGroupChange: group was missing publicParams!'); throw new Error('uploadGroupChange: group was missing publicParams!');
} }
return makeRequestWithTemporalRetry({ return makeRequestWithCredentials({
logId: `uploadGroupChange/${logId}`, logId: `uploadGroupChange/${logId}`,
publicParams: group.publicParams, publicParams: group.publicParams,
secretParams: group.secretParams, secretParams: group.secretParams,
@ -1676,7 +1676,7 @@ export function deriveGroupFields(masterKey: Uint8Array): GroupFields {
return fresh; return fresh;
} }
async function makeRequestWithTemporalRetry<T>({ async function makeRequestWithCredentials<T>({
logId, logId,
publicParams, publicParams,
secretParams, secretParams,
@ -1688,17 +1688,17 @@ async function makeRequestWithTemporalRetry<T>({
request: (sender: MessageSender, options: GroupCredentialsType) => Promise<T>; request: (sender: MessageSender, options: GroupCredentialsType) => Promise<T>;
}): Promise<T> { }): Promise<T> {
const groupCredentials = getCheckedGroupCredentialsForToday( const groupCredentials = getCheckedGroupCredentialsForToday(
`makeRequestWithTemporalRetry/${logId}` `makeRequestWithCredentials/${logId}`
); );
const sender = window.textsecure.messaging; const sender = window.textsecure.messaging;
if (!sender) { if (!sender) {
throw new Error( throw new Error(
`makeRequestWithTemporalRetry/${logId}: textsecure.messaging is not available!` `makeRequestWithCredentials/${logId}: textsecure.messaging is not available!`
); );
} }
log.info(`makeRequestWithTemporalRetry/${logId}: starting`); log.info(`makeRequestWithCredentials/${logId}: starting`);
const todayOptions = getGroupCredentials({ const todayOptions = getGroupCredentials({
authCredentialBase64: groupCredentials.today.credential, authCredentialBase64: groupCredentials.today.credential,
@ -1707,25 +1707,7 @@ async function makeRequestWithTemporalRetry<T>({
serverPublicParamsBase64: window.getServerPublicParams(), serverPublicParamsBase64: window.getServerPublicParams(),
}); });
try { return request(sender, todayOptions);
return await request(sender, todayOptions);
} catch (todayError) {
if (todayError.code === TEMPORAL_AUTH_REJECTED_CODE) {
log.warn(
`makeRequestWithTemporalRetry/${logId}: Trying again with tomorrow's credentials`
);
const tomorrowOptions = getGroupCredentials({
authCredentialBase64: groupCredentials.tomorrow.credential,
groupPublicParamsBase64: publicParams,
groupSecretParamsBase64: secretParams,
serverPublicParamsBase64: window.getServerPublicParams(),
});
return request(sender, tomorrowOptions);
}
throw todayError;
}
} }
export async function fetchMembershipProof({ export async function fetchMembershipProof({
@ -1745,7 +1727,7 @@ export async function fetchMembershipProof({
throw new Error('fetchMembershipProof: group was missing secretParams!'); throw new Error('fetchMembershipProof: group was missing secretParams!');
} }
const response = await makeRequestWithTemporalRetry({ const response = await makeRequestWithCredentials({
logId: 'fetchMembershipProof', logId: 'fetchMembershipProof',
publicParams, publicParams,
secretParams, secretParams,
@ -1897,7 +1879,7 @@ export async function createGroupV2(
}); });
try { try {
await makeRequestWithTemporalRetry({ await makeRequestWithCredentials({
logId: `createGroupV2/${logId}`, logId: `createGroupV2/${logId}`,
publicParams, publicParams,
secretParams, secretParams,
@ -2053,7 +2035,7 @@ export async function hasV1GroupBeenMigrated(
const fields = deriveGroupFields(masterKeyBuffer); const fields = deriveGroupFields(masterKeyBuffer);
try { try {
await makeRequestWithTemporalRetry({ await makeRequestWithCredentials({
logId: `getGroup/${logId}`, logId: `getGroup/${logId}`,
publicParams: Bytes.toBase64(fields.publicParams), publicParams: Bytes.toBase64(fields.publicParams),
secretParams: Bytes.toBase64(fields.secretParams), secretParams: Bytes.toBase64(fields.secretParams),
@ -2410,7 +2392,7 @@ export async function initiateMigrationToGroupV2(
}); });
try { try {
await makeRequestWithTemporalRetry({ await makeRequestWithCredentials({
logId: `createGroup/${logId}`, logId: `createGroup/${logId}`,
publicParams, publicParams,
secretParams, secretParams,
@ -2731,7 +2713,7 @@ export async function respondToGroupV2Migration({
let firstGroupState: Proto.IGroup | null | undefined; let firstGroupState: Proto.IGroup | null | undefined;
try { try {
const response: GroupLogResponseType = await makeRequestWithTemporalRetry({ const response: GroupLogResponseType = await makeRequestWithCredentials({
logId: `getGroupLog/${logId}`, logId: `getGroupLog/${logId}`,
publicParams, publicParams,
secretParams, secretParams,
@ -2755,7 +2737,7 @@ export async function respondToGroupV2Migration({
`respondToGroupV2Migration/${logId}: Failed to access log endpoint; fetching full group state` `respondToGroupV2Migration/${logId}: Failed to access log endpoint; fetching full group state`
); );
try { try {
firstGroupState = await makeRequestWithTemporalRetry({ firstGroupState = await makeRequestWithCredentials({
logId: `getGroup/${logId}`, logId: `getGroup/${logId}`,
publicParams, publicParams,
secretParams, secretParams,
@ -3601,7 +3583,7 @@ async function updateGroupViaPreJoinInfo({
// No password, but if we're already pending approval, we can access this without it. // No password, but if we're already pending approval, we can access this without it.
const inviteLinkPassword = undefined; const inviteLinkPassword = undefined;
const preJoinInfo = await makeRequestWithTemporalRetry({ const preJoinInfo = await makeRequestWithCredentials({
logId: `getPreJoinInfo/${logId}`, logId: `getPreJoinInfo/${logId}`,
publicParams, publicParams,
secretParams, secretParams,
@ -3669,7 +3651,7 @@ async function updateGroupViaState({
throw new Error('updateGroupViaState: group was missing publicParams!'); throw new Error('updateGroupViaState: group was missing publicParams!');
} }
const groupState = await makeRequestWithTemporalRetry({ const groupState = await makeRequestWithCredentials({
logId: `getGroup/${logId}`, logId: `getGroup/${logId}`,
publicParams, publicParams,
secretParams, secretParams,
@ -3813,7 +3795,7 @@ async function updateGroupViaLogs({
const changes: Array<Proto.IGroupChanges> = []; const changes: Array<Proto.IGroupChanges> = [];
do { do {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
response = await makeRequestWithTemporalRetry({ response = await makeRequestWithCredentials({
logId: `getGroupLog/${logId}`, logId: `getGroupLog/${logId}`,
publicParams, publicParams,
secretParams, secretParams,