Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/updaters/olm-account-updater.js
// @flow | // @flow | ||||
import type { Account as OlmAccount } from '@commapp/olm'; | import type { Account as OlmAccount } from '@commapp/olm'; | ||||
import { ServerError } from 'lib/utils/errors.js'; | import { ServerError } from 'lib/utils/errors.js'; | ||||
import sleep from 'lib/utils/sleep.js'; | import sleep from 'lib/utils/sleep.js'; | ||||
import { SQL, dbQuery } from '../database/database.js'; | import { SQL, dbQuery } from '../database/database.js'; | ||||
import { unpickleOlmAccount } from '../utils/olm-utils.js'; | import { unpickleOlmAccount } from '../utils/olm-utils.js'; | ||||
const maxOlmAccountUpdateRetriesCount = 5; | const maxOlmAccountUpdateRetriesCount = 5; | ||||
const olmAccountUpdateRetryDelay = 200; | const olmAccountUpdateRetryDelay = 200; | ||||
async function fetchCallUpdateOlmAccount<T>( | async function fetchCallUpdateOlmAccount<T>( | ||||
olmAccountType: 'content' | 'notifications', | olmAccountType: 'content' | 'notifications', | ||||
callback: (account: OlmAccount) => Promise<T>, | callback: (account: OlmAccount, picklingKey: string) => Promise<T>, | ||||
): Promise<T> { | ): Promise<T> { | ||||
const isContent = olmAccountType === 'content'; | const isContent = olmAccountType === 'content'; | ||||
let retriesLeft = maxOlmAccountUpdateRetriesCount; | let retriesLeft = maxOlmAccountUpdateRetriesCount; | ||||
while (retriesLeft > 0) { | while (retriesLeft > 0) { | ||||
const [olmAccountResult] = await dbQuery( | const [olmAccountResult] = await dbQuery( | ||||
SQL` | SQL` | ||||
SELECT version, pickling_key, pickled_olm_account | SELECT version, pickling_key, pickled_olm_account | ||||
Show All 13 Lines | const [ | ||||
pickled_olm_account: pickledAccount, | pickled_olm_account: pickledAccount, | ||||
}, | }, | ||||
] = olmAccountResult; | ] = olmAccountResult; | ||||
const account = await unpickleOlmAccount({ | const account = await unpickleOlmAccount({ | ||||
picklingKey, | picklingKey, | ||||
pickledAccount, | pickledAccount, | ||||
}); | }); | ||||
const result = await callback(account); | const result = await callback(account, picklingKey); | ||||
const updatedAccount = account.pickle(picklingKey); | const updatedAccount = account.pickle(picklingKey); | ||||
const [transactionResult] = await dbQuery( | const [transactionResult] = await dbQuery( | ||||
SQL` | SQL` | ||||
START TRANSACTION; | START TRANSACTION; | ||||
SELECT version INTO @currentVersion | SELECT version INTO @currentVersion | ||||
FROM olm_accounts | FROM olm_accounts | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |