Create contacts during processing of group updates

This commit is contained in:
Fedor Indutny 2022-01-27 16:35:11 -08:00 committed by GitHub
parent a1d11010b5
commit 54136ecc31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 15 deletions

View File

@ -12,6 +12,7 @@ import {
import type { ClientZkGroupCipher } from '@signalapp/signal-client/zkgroup';
import { v4 as getGuid } from 'uuid';
import LRU from 'lru-cache';
import PQueue from 'p-queue';
import * as log from './logging/log';
import {
getCredentialsForToday,
@ -2794,6 +2795,8 @@ async function updateGroup(
},
{ viaSync = false } = {}
): Promise<void> {
const logId = conversation.idForLogging();
const { newAttributes, groupChangeMessages, members } = updates;
const ourUuid = window.textsecure.storage.user.getCheckedUuid();
@ -2859,6 +2862,38 @@ async function updateGroup(
};
});
const contactsWithoutProfileKey = new Array<ConversationModel>();
// Capture profile key for each member in the group, if we don't have it yet
members.forEach(member => {
const contact = window.ConversationController.getOrCreate(
member.uuid,
'private'
);
if (member.profileKey && !contact.get('profileKey')) {
contactsWithoutProfileKey.push(contact);
contact.setProfileKey(member.profileKey);
}
});
if (contactsWithoutProfileKey.length !== 0) {
log.info(
`updateGroup/${logId}: fetching ` +
`${contactsWithoutProfileKey.length} missing profiles`
);
const profileFetchQueue = new PQueue({
concurrency: 3,
});
await profileFetchQueue.addAll(
contactsWithoutProfileKey.map(contact => () => {
const active = contact.getActiveProfileFetch();
return active || contact.getProfiles();
})
);
}
if (changeMessagesToSave.length > 0) {
await window.Signal.Data.saveMessages(changeMessagesToSave, {
forceSave: true,
@ -2871,15 +2906,6 @@ async function updateGroup(
});
}
// Capture profile key for each member in the group, if we don't have it yet
members.forEach(member => {
const contact = window.ConversationController.get(member.uuid);
if (member.profileKey && contact && !contact.get('profileKey')) {
contact.setProfileKey(member.profileKey);
}
});
// No need for convo.updateLastMessage(), 'newmessage' handler does that
}

View File

@ -213,6 +213,8 @@ export class ConversationModel extends window.Backbone
private isInReduxBatch = false;
private _activeProfileFetch?: Promise<void>;
override defaults(): Partial<ConversationAttributesType> {
return {
unreadCount: 0,
@ -4634,12 +4636,29 @@ export class ConversationModel extends window.Backbone
const queue = new PQueue({
concurrency: 3,
});
await queue.addAll(
conversations.map(
conversation => () =>
getProfile(conversation.get('uuid'), conversation.get('e164'))
)
);
// Convert Promise<void[]> that is returned by addAll() to Promise<void>
const promise = (async () => {
await queue.addAll(
conversations.map(
conversation => () =>
getProfile(conversation.get('uuid'), conversation.get('e164'))
)
);
})();
this._activeProfileFetch = promise;
try {
await promise;
} finally {
if (this._activeProfileFetch === promise) {
this._activeProfileFetch = undefined;
}
}
}
getActiveProfileFetch(): Promise<void> | undefined {
return this._activeProfileFetch;
}
async setEncryptedProfileName(encryptedName: string): Promise<void> {