2021-04-28 18:36:10 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
import type { ConversationController } from './ConversationController';
|
|
|
|
import type { ConversationModel } from './models/conversations';
|
|
|
|
import type SendMessage from './textsecure/SendMessage';
|
2021-04-28 18:36:10 +00:00
|
|
|
import { assert } from './util/assert';
|
|
|
|
import { getOwn } from './util/getOwn';
|
|
|
|
import { isNotNil } from './util/isNotNil';
|
|
|
|
|
|
|
|
export async function updateConversationsWithUuidLookup({
|
|
|
|
conversationController,
|
|
|
|
conversations,
|
|
|
|
messaging,
|
|
|
|
}: Readonly<{
|
|
|
|
conversationController: Pick<
|
|
|
|
ConversationController,
|
2022-08-09 21:39:00 +00:00
|
|
|
'maybeMergeContacts' | 'get'
|
2021-04-28 18:36:10 +00:00
|
|
|
>;
|
|
|
|
conversations: ReadonlyArray<ConversationModel>;
|
2022-02-25 23:20:48 +00:00
|
|
|
messaging: Pick<SendMessage, 'getUuidsForE164s' | 'checkAccountExistence'>;
|
2021-04-28 18:36:10 +00:00
|
|
|
}>): Promise<void> {
|
|
|
|
const e164s = conversations
|
|
|
|
.map(conversation => conversation.get('e164'))
|
|
|
|
.filter(isNotNil);
|
|
|
|
if (!e164s.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const serverLookup = await messaging.getUuidsForE164s(e164s);
|
|
|
|
|
2022-02-25 23:20:48 +00:00
|
|
|
await Promise.all(
|
|
|
|
conversations.map(async conversation => {
|
|
|
|
const e164 = conversation.get('e164');
|
|
|
|
if (!e164) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let finalConversation: ConversationModel;
|
|
|
|
|
|
|
|
const uuidFromServer = getOwn(serverLookup, e164);
|
|
|
|
if (uuidFromServer) {
|
|
|
|
const maybeFinalConversation =
|
2022-08-09 21:39:00 +00:00
|
|
|
conversationController.maybeMergeContacts({
|
|
|
|
aci: uuidFromServer,
|
|
|
|
e164,
|
|
|
|
reason: 'updateConversationsWithUuidLookup',
|
|
|
|
});
|
2022-02-25 23:20:48 +00:00
|
|
|
assert(
|
|
|
|
maybeFinalConversation,
|
|
|
|
'updateConversationsWithUuidLookup: expected a conversation to be found or created'
|
|
|
|
);
|
|
|
|
finalConversation = maybeFinalConversation;
|
|
|
|
} else {
|
|
|
|
finalConversation = conversation;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We got no uuid from CDS so either the person is now unregistered or
|
|
|
|
// they can't be looked up by a phone number. Check that uuid still exists,
|
|
|
|
// and if not - drop it.
|
|
|
|
let finalUuid = finalConversation.getUuid();
|
|
|
|
if (!uuidFromServer && finalUuid) {
|
|
|
|
const doesAccountExist = await messaging.checkAccountExistence(
|
|
|
|
finalUuid
|
|
|
|
);
|
|
|
|
if (!doesAccountExist) {
|
|
|
|
finalConversation.updateUuid(undefined);
|
|
|
|
finalUuid = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!finalConversation.get('e164') || !finalUuid) {
|
|
|
|
finalConversation.setUnregistered();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2021-04-28 18:36:10 +00:00
|
|
|
}
|