diff --git a/keyserver/src/user/login.js b/keyserver/src/user/login.js --- a/keyserver/src/user/login.js +++ b/keyserver/src/user/login.js @@ -71,6 +71,22 @@ async function registerOrLogIn( userInfo: UserCredentials, +): Promise { + return registerOrLogInBase( + userInfo, + callback => fetchCallUpdateOlmAccount('content', callback), + callback => fetchCallUpdateOlmAccount('notifications', callback), + ); +} + +async function registerOrLogInBase( + userInfo: UserCredentials, + getUpdateContentAccount: ( + callback: (account: OlmAccount, picklingKey: string) => Promise | T, + ) => Promise, + getUpdateNotificationsAccount: ( + callback: (account: OlmAccount, picklingKey: string) => Promise | T, + ) => Promise, ): Promise { const rustAPIPromise = getRustAPI(); @@ -78,10 +94,7 @@ identityKeys: notificationsIdentityKeys, prekey: notificationsPrekey, prekeySignature: notificationsPrekeySignature, - } = await fetchCallUpdateOlmAccount( - 'notifications', - retrieveIdentityKeysAndPrekeys, - ); + } = await getUpdateNotificationsAccount(retrieveIdentityKeysAndPrekeys); const contentAccountCallback = (account: OlmAccount) => { const { @@ -116,7 +129,7 @@ }, ] = await Promise.all([ rustAPIPromise, - fetchCallUpdateOlmAccount('content', contentAccountCallback), + getUpdateContentAccount(contentAccountCallback), ]); try { @@ -131,17 +144,17 @@ userInfo.forceLogin, ); await Promise.all([ - fetchCallUpdateOlmAccount('content', markKeysAsPublished), - fetchCallUpdateOlmAccount('notifications', markKeysAsPublished), + getUpdateContentAccount(markKeysAsPublished), + getUpdateNotificationsAccount(markKeysAsPublished), ]); return identity_info; } catch (e) { console.warn('Failed to login user: ' + getMessageForException(e)); const [contentOneTimeKeys, notificationsOneTimeKeys] = await Promise.all([ - fetchCallUpdateOlmAccount('content', (account: OlmAccount) => + getUpdateContentAccount((account: OlmAccount) => getAccountOneTimeKeys(account, ONE_TIME_KEYS_NUMBER), ), - fetchCallUpdateOlmAccount('notifications', (account: OlmAccount) => + getUpdateNotificationsAccount((account: OlmAccount) => getAccountOneTimeKeys(account, ONE_TIME_KEYS_NUMBER), ), ]); @@ -158,8 +171,8 @@ notificationsOneTimeKeys, ); await Promise.all([ - fetchCallUpdateOlmAccount('content', markKeysAsPublished), - fetchCallUpdateOlmAccount('notifications', markKeysAsPublished), + getUpdateContentAccount(markKeysAsPublished), + getUpdateNotificationsAccount(markKeysAsPublished), ]); return identity_info; } catch (err) {