From fd8339e2ff1140e641fde0b142ad13411a059c22 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Mon, 15 Mar 2021 16:44:43 -0700 Subject: [PATCH] Fix for UnregisteredUserError handling when fetching UUIDs --- .../state/selectors/conversations_test.ts | 22 +++++++++---------- .../util/isConversationUnregistered_test.ts | 16 +++++++------- ts/textsecure/OutgoingMessage.ts | 5 +++++ ts/util/isConversationUnregistered.ts | 2 +- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/ts/test-both/state/selectors/conversations_test.ts b/ts/test-both/state/selectors/conversations_test.ts index 5360b6f99ffd..167a8fd88a75 100644 --- a/ts/test-both/state/selectors/conversations_test.ts +++ b/ts/test-both/state/selectors/conversations_test.ts @@ -501,7 +501,8 @@ describe('both/state/selectors/conversations', () => { 'convo-5': { ...getDefaultConversation('convo-5'), discoveredUnregisteredAt: new Date(1999, 3, 20).getTime(), - title: 'Should Be Dropped (unregistered)', + name: 'In System Contacts (and unregistered too long ago)', + title: 'B. Sorted Second', }, 'convo-6': { ...getDefaultConversation('convo-6'), @@ -511,8 +512,7 @@ describe('both/state/selectors/conversations', () => { 'convo-7': { ...getDefaultConversation('convo-7'), discoveredUnregisteredAt: Date.now(), - name: 'In System Contacts (and only recently unregistered)', - title: 'B. Sorted Second', + title: 'Should Be Dropped (unregistered)', }, }); return result; @@ -540,7 +540,7 @@ describe('both/state/selectors/conversations', () => { const ids = result.map(contact => contact.id); assert.deepEqual(ids, [ 'convo-1', - 'convo-7', + 'convo-5', 'convo-6', 'our-conversation-id', ]); @@ -551,7 +551,7 @@ describe('both/state/selectors/conversations', () => { const result = getComposeContacts(state); const ids = result.map(contact => contact.id); - assert.deepEqual(ids, ['convo-1', 'convo-7']); + assert.deepEqual(ids, ['convo-1', 'convo-5']); }); }); @@ -590,14 +590,14 @@ describe('both/state/selectors/conversations', () => { 'convo-5': { ...getDefaultConversation('convo-5'), discoveredUnregisteredAt: new Date(1999, 3, 20).getTime(), - name: 'My Name', - title: 'Should Be Dropped (unregistered)', + name: 'In System Contacts (and unregistered too long ago)', + title: 'C. Sorted Third', }, 'convo-6': { ...getDefaultConversation('convo-6'), discoveredUnregisteredAt: Date.now(), - name: 'In System Contacts (and only recently unregistered)', - title: 'C. Sorted Third', + name: 'My Name', + title: 'Should Be Dropped (unregistered)', }, }, composer: { @@ -624,7 +624,7 @@ describe('both/state/selectors/conversations', () => { const result = getCandidateContactsForNewGroup(state); const ids = result.map(contact => contact.id); - assert.deepEqual(ids, ['convo-1', 'convo-6']); + assert.deepEqual(ids, ['convo-1', 'convo-5']); }); it('can search for contacts', () => { @@ -632,7 +632,7 @@ describe('both/state/selectors/conversations', () => { const result = getCandidateContactsForNewGroup(state); const ids = result.map(contact => contact.id); - assert.deepEqual(ids, ['convo-1', 'convo-6']); + assert.deepEqual(ids, ['convo-1', 'convo-5']); }); }); diff --git a/ts/test-both/util/isConversationUnregistered_test.ts b/ts/test-both/util/isConversationUnregistered_test.ts index c00a3f5bd31d..bed76f26f7a5 100644 --- a/ts/test-both/util/isConversationUnregistered_test.ts +++ b/ts/test-both/util/isConversationUnregistered_test.ts @@ -13,35 +13,35 @@ describe('isConversationUnregistered', () => { ); }); - it('returns false if passed a time fewer than 6 hours ago', () => { - assert.isFalse( + it('returns true if passed a time fewer than 6 hours ago', () => { + assert.isTrue( isConversationUnregistered({ discoveredUnregisteredAt: Date.now() }) ); const fiveHours = 1000 * 60 * 60 * 5; - assert.isFalse( + assert.isTrue( isConversationUnregistered({ discoveredUnregisteredAt: Date.now() - fiveHours, }) ); }); - it('returns false if passed a time in the future', () => { - assert.isFalse( + it('returns true if passed a time in the future', () => { + assert.isTrue( isConversationUnregistered({ discoveredUnregisteredAt: Date.now() + 123 }) ); }); - it('returns true if passed a time more than 6 hours ago', () => { + it('returns false if passed a time more than 6 hours ago', () => { const oneMinute = 1000 * 60; const sixHours = 1000 * 60 * 60 * 6; - assert.isTrue( + assert.isFalse( isConversationUnregistered({ discoveredUnregisteredAt: Date.now() - sixHours - oneMinute, }) ); - assert.isTrue( + assert.isFalse( isConversationUnregistered({ discoveredUnregisteredAt: new Date(1999, 3, 20).getTime(), }) diff --git a/ts/textsecure/OutgoingMessage.ts b/ts/textsecure/OutgoingMessage.ts index bb372fd2fb2d..5179002f6c79 100644 --- a/ts/textsecure/OutgoingMessage.ts +++ b/ts/textsecure/OutgoingMessage.ts @@ -633,6 +633,11 @@ export default class OutgoingMessage { }); identifier = uuid; } else { + const c = window.ConversationController.get(identifier); + if (c) { + c.setUnregistered(); + } + throw new UnregisteredUserError( identifier, new Error('User is not registered') diff --git a/ts/util/isConversationUnregistered.ts b/ts/util/isConversationUnregistered.ts index e50b0f1197e8..5a324b7f125b 100644 --- a/ts/util/isConversationUnregistered.ts +++ b/ts/util/isConversationUnregistered.ts @@ -8,6 +8,6 @@ export function isConversationUnregistered({ }: Readonly<{ discoveredUnregisteredAt?: number }>): boolean { return Boolean( discoveredUnregisteredAt && - discoveredUnregisteredAt < Date.now() - SIX_HOURS + discoveredUnregisteredAt > Date.now() - SIX_HOURS ); }