diff --git a/ts/background.ts b/ts/background.ts index 46e9799b0f99..85cf9cd6e653 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -1742,9 +1742,33 @@ export async function startApp(): Promise { strictAssert(server !== undefined, 'WebAPI not connected'); + let contactSyncComplete: Promise | undefined; + const areWePrimaryDevice = + window.ConversationController.areWePrimaryDevice(); + + const waitForEvent = createTaskWithTimeout( + (event: string): Promise => { + const { promise, resolve } = explodePromise(); + window.Whisper.events.once(event, () => resolve()); + return promise; + }, + 'connect:waitForEvent', + { timeout: 2 * durations.MINUTE } + ); + try { connectPromise = explodePromise(); + if (firstRun === true && !areWePrimaryDevice) { + contactSyncComplete = waitForEvent('contactSync:complete'); + + // Send the contact sync message immediately (don't wait until after backup is + // downloaded & imported) + await singleProtoJobQueue.add( + MessageSender.getRequestContactSyncMessage() + ); + } + // Wait for backup to be downloaded try { await backupReady.promise; @@ -1831,7 +1855,7 @@ export async function startApp(): Promise { !firstRun && connectCount === 1 && newVersion && - window.textsecure.storage.user.getDeviceId() !== 1 + !areWePrimaryDevice ) { log.info('Boot after upgrading. Requesting contact sync'); @@ -1854,8 +1878,6 @@ export async function startApp(): Promise { } } - const deviceId = window.textsecure.storage.user.getDeviceId(); - if (!window.textsecure.storage.user.getAci()) { log.error('UUID not captured during registration, unlinking'); return unlinkAndDisconnect(); @@ -1883,7 +1905,7 @@ export async function startApp(): Promise { return unlinkAndDisconnect(); } - if (firstRun === true && deviceId !== 1) { + if (firstRun === true && !areWePrimaryDevice) { if (!window.storage.get('accountEntropyPool')) { const lastSent = window.storage.get('accountEntropyPoolLastRequestTime') ?? 0; @@ -1904,17 +1926,6 @@ export async function startApp(): Promise { ); } } - - const waitForEvent = createTaskWithTimeout( - (event: string): Promise => { - const { promise, resolve } = explodePromise(); - window.Whisper.events.once(event, () => resolve()); - return promise; - }, - 'firstRun:waitForEvent', - { timeout: 2 * durations.MINUTE } - ); - let storageServiceSyncComplete: Promise; if (window.ConversationController.areWePrimaryDevice()) { storageServiceSyncComplete = Promise.resolve(); @@ -1924,8 +1935,6 @@ export async function startApp(): Promise { ); } - const contactSyncComplete = waitForEvent('contactSync:complete'); - log.info('firstRun: requesting initial sync'); setIsInitialSync(true); @@ -1938,9 +1947,6 @@ export async function startApp(): Promise { ), singleProtoJobQueue.add(MessageSender.getRequestBlockSyncMessage()), runStorageService({ reason: 'firstRun/initialSync' }), - singleProtoJobQueue.add( - MessageSender.getRequestContactSyncMessage() - ), ]); } catch (error) { log.error( @@ -1950,7 +1956,10 @@ export async function startApp(): Promise { } log.info('firstRun: waiting for storage service and contact sync'); - + strictAssert( + contactSyncComplete, + 'contact sync is awaited during first run' + ); try { await Promise.all([storageServiceSyncComplete, contactSyncComplete]); } catch (error) {