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';
|
2022-08-18 20:44:53 +00:00
|
|
|
import type { WebAPIType } from './textsecure/WebAPI';
|
2022-09-15 19:17:15 +00:00
|
|
|
import { assertDev } from './util/assert';
|
2021-04-28 18:36:10 +00:00
|
|
|
import { isNotNil } from './util/isNotNil';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { getServiceIdsForE164s } from './util/getServiceIdsForE164s';
|
2021-04-28 18:36:10 +00:00
|
|
|
|
|
|
|
export async function updateConversationsWithUuidLookup({
|
|
|
|
conversationController,
|
|
|
|
conversations,
|
2022-08-18 20:44:53 +00:00
|
|
|
server,
|
2021-04-28 18:36:10 +00:00
|
|
|
}: 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-08-18 20:44:53 +00:00
|
|
|
server: Pick<WebAPIType, 'cdsLookup' | '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;
|
|
|
|
}
|
|
|
|
|
2024-02-13 21:41:48 +00:00
|
|
|
const { entries: serverLookup } = await getServiceIdsForE164s(server, e164s);
|
2021-04-28 18:36:10 +00:00
|
|
|
|
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;
|
|
|
|
|
2022-08-18 20:44:53 +00:00
|
|
|
const pairFromServer = serverLookup.get(e164);
|
|
|
|
if (pairFromServer) {
|
2022-12-05 22:46:54 +00:00
|
|
|
const { conversation: maybeFinalConversation } =
|
2022-08-09 21:39:00 +00:00
|
|
|
conversationController.maybeMergeContacts({
|
2022-08-18 20:44:53 +00:00
|
|
|
aci: pairFromServer.aci,
|
|
|
|
pni: pairFromServer.pni,
|
2022-08-09 21:39:00 +00:00
|
|
|
e164,
|
|
|
|
reason: 'updateConversationsWithUuidLookup',
|
|
|
|
});
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(
|
2022-02-25 23:20:48 +00:00
|
|
|
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.
|
2023-08-10 16:43:33 +00:00
|
|
|
let finalServiceId = finalConversation.getServiceId();
|
|
|
|
if (!pairFromServer && finalServiceId) {
|
2024-07-24 00:31:40 +00:00
|
|
|
const doesAccountExist =
|
|
|
|
await server.checkAccountExistence(finalServiceId);
|
2022-02-25 23:20:48 +00:00
|
|
|
if (!doesAccountExist) {
|
2023-08-16 20:54:39 +00:00
|
|
|
finalConversation.updateServiceId(undefined);
|
2023-08-10 16:43:33 +00:00
|
|
|
finalServiceId = undefined;
|
2022-02-25 23:20:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
if (!finalConversation.get('e164') || !finalServiceId) {
|
2022-02-25 23:20:48 +00:00
|
|
|
finalConversation.setUnregistered();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2021-04-28 18:36:10 +00:00
|
|
|
}
|