diff --git a/ts/RemoteConfig.ts b/ts/RemoteConfig.ts index 7c31a0108b..8be82ec26c 100644 --- a/ts/RemoteConfig.ts +++ b/ts/RemoteConfig.ts @@ -65,11 +65,6 @@ export function restoreRemoteConfigFromStorage(): void { config = window.storage.get('remoteConfig') || {}; } -export async function initRemoteConfig(server: WebAPIType): Promise { - restoreRemoteConfigFromStorage(); - await maybeRefreshRemoteConfig(server); -} - export function onChange( key: ConfigKeyType, fn: ConfigListenerType @@ -83,7 +78,7 @@ export function onChange( }; } -export const refreshRemoteConfig = async ( +export const _refreshRemoteConfig = async ( server: WebAPIType ): Promise => { const now = Date.now(); @@ -93,7 +88,7 @@ export const refreshRemoteConfig = async ( if (Math.abs(serverTimeSkew) > HOUR) { log.warn( - 'Remote Config: sever clock skew detected. ' + + 'Remote Config: severe clock skew detected. ' + `Server time ${serverTimestamp}, local time ${now}` ); } @@ -154,12 +149,21 @@ export const refreshRemoteConfig = async ( }; export const maybeRefreshRemoteConfig = throttle( - refreshRemoteConfig, + _refreshRemoteConfig, // Only fetch remote configuration if the last fetch was more than two hours ago 2 * 60 * 60 * 1000, { trailing: false } ); +export async function forceRefreshRemoteConfig( + server: WebAPIType, + reason: string +): Promise { + log.info(`forceRefreshRemoteConfig: ${reason}`); + maybeRefreshRemoteConfig.cancel(); + await _refreshRemoteConfig(server); +} + export function isEnabled(name: ConfigKeyType): boolean { return get(config, [name, 'enabled'], false); } diff --git a/ts/background.ts b/ts/background.ts index 1c198da463..de1fbf0977 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -400,7 +400,9 @@ export async function startApp(): Promise { } return server.getSocketStatus(); }; + let accountManager: AccountManager; + let isInRegistration = false; window.getAccountManager = () => { if (accountManager) { return accountManager; @@ -411,12 +413,14 @@ export async function startApp(): Promise { accountManager = new window.textsecure.AccountManager(server); accountManager.addEventListener('startRegistration', () => { + isInRegistration = true; pauseProcessing(); backupReady.reject(new Error('startRegistration')); backupReady = explodePromise(); }); accountManager.addEventListener('registration', () => { + isInRegistration = false; window.Whisper.events.trigger('userChanged', false); drop(Registration.markDone()); @@ -1038,8 +1042,6 @@ export async function startApp(): Promise { } }); - void window.Signal.RemoteConfig.initRemoteConfig(server); - const retryPlaceholders = new RetryPlaceholders({ retryReceiptLifespan: HOUR, }); @@ -1489,10 +1491,6 @@ export async function startApp(): Promise { strictAssert(server !== undefined, 'WebAPI not ready'); - // Cancel throttled calls to refreshRemoteConfig since our auth changed. - window.Signal.RemoteConfig.maybeRefreshRemoteConfig.cancel(); - drop(window.Signal.RemoteConfig.maybeRefreshRemoteConfig(server)); - drop(connect(true)); // Connect messageReceiver back to websocket @@ -1594,7 +1592,9 @@ export async function startApp(): Promise { const onOnline = () => { log.info('background: online'); - if (!remotelyExpired) { + // Do not attempt to connect while expired or in-the-middle of + // registration + if (!remotelyExpired && !isInRegistration) { drop(connect()); } }; @@ -1848,11 +1848,14 @@ export async function startApp(): Promise { drop(AttachmentBackupManager.start()); } - if (connectCount === 0) { + if (connectCount === 0 || firstRun) { try { // Force a re-fetch before we process our queue. We may want to turn on // something which changes how we process incoming messages! - await window.Signal.RemoteConfig.refreshRemoteConfig(server); + await window.Signal.RemoteConfig.forceRefreshRemoteConfig( + server, + `connectCount=${connectCount} firstRun=${firstRun}` + ); const expiration = window.Signal.RemoteConfig.getValue( 'desktop.clientExpiration' diff --git a/ts/test-both/helpers/RemoteConfigStub.ts b/ts/test-both/helpers/RemoteConfigStub.ts index e0da5d557a..68db67f6a7 100644 --- a/ts/test-both/helpers/RemoteConfigStub.ts +++ b/ts/test-both/helpers/RemoteConfigStub.ts @@ -1,7 +1,7 @@ // Copyright 2022 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only -import { refreshRemoteConfig } from '../../RemoteConfig'; +import { _refreshRemoteConfig } from '../../RemoteConfig'; import type { WebAPIType, RemoteConfigResponseType, @@ -16,5 +16,5 @@ export async function updateRemoteConfig( }, } as Partial as unknown as WebAPIType; - await refreshRemoteConfig(fakeServer); + await _refreshRemoteConfig(fakeServer); }