Skip to content

Commit

Permalink
Use checkAccountExistence
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal committed Feb 25, 2022
1 parent 5c9718f commit 033b483
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 35 deletions.
39 changes: 36 additions & 3 deletions ts/test-electron/updateConversationsWithUuidLookup_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,23 @@ describe('updateConversationsWithUuidLookup', () => {
let sinonSandbox: sinon.SinonSandbox;

let fakeGetUuidsForE164s: sinon.SinonStub;
let fakeMessaging: Pick<SendMessage, 'getUuidsForE164s'>;
let fakeCheckAccountExistence: sinon.SinonStub;
let fakeMessaging: Pick<
SendMessage,
'getUuidsForE164s' | 'checkAccountExistence'
>;

beforeEach(() => {
sinonSandbox = sinon.createSandbox();

sinonSandbox.stub(window.Signal.Data, 'updateConversation');

fakeGetUuidsForE164s = sinonSandbox.stub().resolves({});
fakeMessaging = { getUuidsForE164s: fakeGetUuidsForE164s };
fakeCheckAccountExistence = sinonSandbox.stub().resolves(false);
fakeMessaging = {
getUuidsForE164s: fakeGetUuidsForE164s,
checkAccountExistence: fakeCheckAccountExistence,
};
});

afterEach(() => {
Expand Down Expand Up @@ -186,7 +194,7 @@ describe('updateConversationsWithUuidLookup', () => {
);
});

it("doesn't mark conversations unregistered if we already had a UUID for them, even if the server doesn't return one", async () => {
it("doesn't mark conversations unregistered if we already had a UUID for them, even if the account exists on server", async () => {
const existingUuid = UUID.generate().toString();
const conversation = createConversation({
e164: '+13215559876',
Expand All @@ -198,6 +206,7 @@ describe('updateConversationsWithUuidLookup', () => {
);

fakeGetUuidsForE164s.resolves({ '+13215559876': null });
fakeCheckAccountExistence.resolves(true);

await updateConversationsWithUuidLookup({
conversationController: new FakeConversationController([conversation]),
Expand All @@ -208,4 +217,28 @@ describe('updateConversationsWithUuidLookup', () => {
assert.strictEqual(conversation.get('uuid'), existingUuid);
assert.isUndefined(conversation.get('discoveredUnregisteredAt'));
});

it('marks conversations unregistered if we already had a UUID for them, even if the account does not exist on server', async () => {
const existingUuid = UUID.generate().toString();
const conversation = createConversation({
e164: '+13215559876',
uuid: existingUuid,
});
assert.isUndefined(
conversation.get('discoveredUnregisteredAt'),
'Test was not set up correctly'
);

fakeGetUuidsForE164s.resolves({ '+13215559876': null });
fakeCheckAccountExistence.resolves(false);

await updateConversationsWithUuidLookup({
conversationController: new FakeConversationController([conversation]),
conversations: [conversation],
messaging: fakeMessaging,
});

assert.strictEqual(conversation.get('uuid'), existingUuid);
assert.isNumber(conversation.get('discoveredUnregisteredAt'));
});
});
80 changes: 48 additions & 32 deletions ts/updateConversationsWithUuidLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function updateConversationsWithUuidLookup({
'ensureContactIds' | 'get'
>;
conversations: ReadonlyArray<ConversationModel>;
messaging: Pick<SendMessage, 'getUuidsForE164s'>;
messaging: Pick<SendMessage, 'getUuidsForE164s' | 'checkAccountExistence'>;
}>): Promise<void> {
const e164s = conversations
.map(conversation => conversation.get('e164'))
Expand All @@ -29,35 +29,51 @@ export async function updateConversationsWithUuidLookup({

const serverLookup = await messaging.getUuidsForE164s(e164s);

conversations.forEach(conversation => {
const e164 = conversation.get('e164');
if (!e164) {
return;
}

let finalConversation: ConversationModel;

const uuidFromServer = getOwn(serverLookup, e164);
if (uuidFromServer) {
const finalConversationId = conversationController.ensureContactIds({
e164,
uuid: uuidFromServer,
highTrust: true,
reason: 'updateConversationsWithUuidLookup',
});
const maybeFinalConversation =
conversationController.get(finalConversationId);
assert(
maybeFinalConversation,
'updateConversationsWithUuidLookup: expected a conversation to be found or created'
);
finalConversation = maybeFinalConversation;
} else {
finalConversation = conversation;
}

if (!finalConversation.get('e164') || !finalConversation.get('uuid')) {
finalConversation.setUnregistered();
}
});
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 finalConversationId = conversationController.ensureContactIds({
e164,
uuid: uuidFromServer,
highTrust: true,
reason: 'updateConversationsWithUuidLookup',
});
const maybeFinalConversation =
conversationController.get(finalConversationId);
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();
}
})
);
}

0 comments on commit 033b483

Please sign in to comment.