Lazily fetch profiles when modifying groups

This commit is contained in:
Fedor Indutny 2022-07-11 11:50:14 -07:00 committed by GitHub
parent 712c9597c5
commit 07cc399550
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 21 deletions

View file

@ -1472,12 +1472,43 @@ export async function modifyGroupV2({
let refreshedCredentials = false;
const profileFetchQueue = new PQueue({
concurrency: 3,
});
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt += 1) {
log.info(`modifyGroupV2/${logId}: Starting attempt ${attempt}`);
try {
// eslint-disable-next-line no-await-in-loop
await window.waitForEmptyEventQueue();
// Fetch profiles for contacts that do not have credentials (or have
// expired credentials)
{
const membersMissingCredentials = usingCredentialsFrom.filter(member =>
member.hasProfileKeyCredentialExpired()
);
const logIds = membersMissingCredentials.map(member =>
member.idForLogging()
);
if (logIds.length !== 0) {
log.info(`modifyGroupV2/${logId}: Fetching profiles for ${logIds}`);
}
for (const member of membersMissingCredentials) {
member.set({
profileKeyCredential: null,
profileKeyCredentialExpiration: null,
});
}
// eslint-disable-next-line no-await-in-loop
await profileFetchQueue.addAll(
membersMissingCredentials.map(member => () => member.getProfiles())
);
}
log.info(`modifyGroupV2/${logId}: Queuing attempt ${attempt}`);
// eslint-disable-next-line no-await-in-loop
@ -1557,10 +1588,12 @@ export async function modifyGroupV2({
const logIds = usingCredentialsFrom.map(member =>
member.idForLogging()
);
log.warn(
`modifyGroupV2/${logId}: Profile key credentials were not ` +
`up-to-date. Updating profiles for ${logIds} and retrying`
);
if (logIds.length !== 0) {
log.warn(
`modifyGroupV2/${logId}: Profile key credentials were not ` +
`up-to-date. Updating profiles for ${logIds} and retrying`
);
}
for (const member of usingCredentialsFrom) {
member.set({
@ -1569,9 +1602,6 @@ export async function modifyGroupV2({
});
}
const profileFetchQueue = new PQueue({
concurrency: 3,
});
// eslint-disable-next-line no-await-in-loop
await profileFetchQueue.addAll(
usingCredentialsFrom.map(member => () => member.getProfiles())
@ -1741,6 +1771,17 @@ export async function createGroupV2({
const ourACI = window.storage.user.getCheckedUuid(UUIDKind.ACI).toString();
const ourConversation =
window.ConversationController.getOurConversationOrThrow();
if (ourConversation.hasProfileKeyCredentialExpired()) {
log.info(`createGroupV2/${logId}: fetching our own credentials`);
ourConversation.set({
profileKeyCredential: null,
profileKeyCredentialExpiration: null,
});
await ourConversation.getProfiles();
}
const membersV2: Array<GroupV2MemberType> = [
{
uuid: ourACI,
@ -1770,7 +1811,7 @@ export async function createGroupV2({
}
// Refresh our local data to be sure
if (!contact.get('profileKey') || !contact.get('profileKeyCredential')) {
if (contact.hasProfileKeyCredentialExpired()) {
await contact.getProfiles();
}