Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(app): rekey moved-instance accounts, store order and tokens

Hampusae507c1b2c00
diff --git a/fluxer_app/src/features/auth/state/AccountStorage.ts b/fluxer_app/src/features/auth/state/AccountStorage.ts
index 84835e0a2..828c92ba0 100644
--- a/fluxer_app/src/features/auth/state/AccountStorage.ts
+++ b/fluxer_app/src/features/auth/state/AccountStorage.ts
@@ -28,8 +28,7 @@ import type {
} from '@app/features/auth/state/StoredAccountInventoryContract';
import {storedAccountRecordsFromInventory} from '@app/features/auth/state/StoredAccountInventoryPolicy';
import {Logger} from '@app/features/platform/utils/AppLogger';
-
-export {CrossInstanceAccountCollisionError} from '@app/features/auth/state/AccountStorageContract';
+import {isRecord} from '@fluxer/record_utils/src/RecordGuards';
const logger = new Logger('AccountStorage');
@@ -181,6 +180,36 @@ class AccountStorage {
return {status, source: 'idb', ...plan, written: changed.length};
}
+ async rekeyAccountsOnInstance(instance: RuntimeConfigSnapshot): Promise<Map<string, string>> {
+ const moves = new Map<string, string>();
+ if (this.activeRepository().source !== 'idb') {
+ return moves;
+ }
+ const rekeyed: Array<StoredAccount> = [];
+ for (const value of await this.browserRepository.listRaw()) {
+ if (!isRecord(value) || typeof value.userId !== 'string' || typeof value.storageKey !== 'string') {
+ continue;
+ }
+ const record = value as unknown as StoredAccount & {storageKey: string};
+ const storageKey = accountStorageKey(record.userId, instance);
+ if (
+ storageKey === null ||
+ storageKey === record.storageKey ||
+ accountKeyUserId(record.storageKey) !== record.userId ||
+ !runtimeConfigSnapshotsAreSameInstance(record.instance, instance)
+ ) {
+ continue;
+ }
+ rekeyed.push({...record, storageKey});
+ moves.set(record.storageKey, storageKey);
+ }
+ if (rekeyed.length > 0) {
+ await this.browserRepository.writeMigration(rekeyed);
+ logger.info(`Re-keyed ${rekeyed.length} stored accounts onto ${instance.apiEndpoint}`);
+ }
+ return moves;
+ }
+
async importAccounts(records: ReadonlyArray<StoredAccount>): Promise<void> {
const active = this.activeRepository();
for (const record of records) {
@@ -219,6 +248,20 @@ class AccountStorage {
);
}
+ async updateAccountSwitcherPositions(positions: ReadonlyMap<string, number>): Promise<void> {
+ for (const [accountKey, switcherPosition] of positions) {
+ await this.updateAccountRecord(accountKey, 'switcher position', (record) =>
+ record.switcherPosition === switcherPosition ? record : {...record, switcherPosition},
+ );
+ }
+ }
+
+ async updateAccountToken(accountKey: string, expectedToken: string, token: string): Promise<void> {
+ await this.updateAccountRecord(accountKey, 'token', (record) =>
+ record.token === expectedToken ? {...record, token} : record,
+ );
+ }
+
private activeRepository(): ActiveAccountRepository {
const desktopRepository = resolveDesktopAccountStorageRepository();
if (desktopRepository !== null) {
diff --git a/fluxer_app/src/features/auth/state/BrowserAccountStorageRepository.ts b/fluxer_app/src/features/auth/state/BrowserAccountStorageRepository.ts
index 3848f56e7..1d78e9b5e 100644
--- a/fluxer_app/src/features/auth/state/BrowserAccountStorageRepository.ts
+++ b/fluxer_app/src/features/auth/state/BrowserAccountStorageRepository.ts
@@ -88,7 +88,7 @@ export class BrowserAccountStorageRepository {
);
}
- private async listRaw(): Promise<Array<unknown>> {
+ async listRaw(): Promise<Array<unknown>> {
const database = await this.openStore();
if (database instanceof BrowserAccountFallbackStore) {
return database.list();