mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 07:43:35 +00:00
Move lastSync State (#10272)
This commit is contained in:
@@ -59,7 +59,5 @@ export abstract class StateService<T extends Account = Account> {
|
||||
getDuckDuckGoSharedKey: (options?: StorageOptions) => Promise<string>;
|
||||
setDuckDuckGoSharedKey: (value: string, options?: StorageOptions) => Promise<void>;
|
||||
getIsAuthenticated: (options?: StorageOptions) => Promise<boolean>;
|
||||
getLastSync: (options?: StorageOptions) => Promise<string>;
|
||||
setLastSync: (value: string, options?: StorageOptions) => Promise<void>;
|
||||
getUserId: (options?: StorageOptions) => Promise<string>;
|
||||
}
|
||||
|
||||
@@ -95,7 +95,6 @@ export class AccountProfile {
|
||||
name?: string;
|
||||
email?: string;
|
||||
emailVerified?: boolean;
|
||||
lastSync?: string;
|
||||
userId?: string;
|
||||
|
||||
static fromJSON(obj: Jsonify<AccountProfile>): AccountProfile {
|
||||
|
||||
@@ -301,23 +301,6 @@ export class StateService<
|
||||
);
|
||||
}
|
||||
|
||||
async getLastSync(options?: StorageOptions): Promise<string> {
|
||||
return (
|
||||
await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskMemoryOptions()))
|
||||
)?.profile?.lastSync;
|
||||
}
|
||||
|
||||
async setLastSync(value: string, options?: StorageOptions): Promise<void> {
|
||||
const account = await this.getAccount(
|
||||
this.reconcileOptions(options, await this.defaultOnDiskMemoryOptions()),
|
||||
);
|
||||
account.profile.lastSync = value;
|
||||
await this.saveAccount(
|
||||
account,
|
||||
this.reconcileOptions(options, await this.defaultOnDiskMemoryOptions()),
|
||||
);
|
||||
}
|
||||
|
||||
async getUserId(options?: StorageOptions): Promise<string> {
|
||||
return (
|
||||
await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions()))
|
||||
|
||||
@@ -110,6 +110,7 @@ export const CRYPTO_MEMORY = new StateDefinition("crypto", "memory");
|
||||
export const DESKTOP_SETTINGS_DISK = new StateDefinition("desktopSettings", "disk");
|
||||
export const ENVIRONMENT_DISK = new StateDefinition("environment", "disk");
|
||||
export const ENVIRONMENT_MEMORY = new StateDefinition("environment", "memory");
|
||||
export const SYNC_DISK = new StateDefinition("sync", "disk", { web: "memory" });
|
||||
export const THEMING_DISK = new StateDefinition("theming", "disk", { web: "disk-local" });
|
||||
export const TRANSLATION_DISK = new StateDefinition("translation", "disk", { web: "disk-local" });
|
||||
export const TASK_SCHEDULER_DISK = new StateDefinition("taskScheduler", "disk");
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
import { SendData } from "../../tools/send/models/data/send.data";
|
||||
import { SendApiService } from "../../tools/send/services/send-api.service.abstraction";
|
||||
import { InternalSendService } from "../../tools/send/services/send.service.abstraction";
|
||||
import { UserId } from "../../types/guid";
|
||||
import { CipherService } from "../../vault/abstractions/cipher.service";
|
||||
import { CollectionService } from "../../vault/abstractions/collection.service";
|
||||
import { FolderApiServiceAbstraction } from "../../vault/abstractions/folder/folder-api.service.abstraction";
|
||||
@@ -22,6 +23,12 @@ import { FolderData } from "../../vault/models/data/folder.data";
|
||||
import { LogService } from "../abstractions/log.service";
|
||||
import { StateService } from "../abstractions/state.service";
|
||||
import { MessageSender } from "../messaging";
|
||||
import { StateProvider, SYNC_DISK, UserKeyDefinition } from "../state";
|
||||
|
||||
const LAST_SYNC_DATE = new UserKeyDefinition<Date>(SYNC_DISK, "lastSync", {
|
||||
deserializer: (d) => (d != null ? new Date(d) : null),
|
||||
clearOn: ["logout"],
|
||||
});
|
||||
|
||||
/**
|
||||
* Core SyncService Logic EXCEPT for fullSync so that implementations can differ.
|
||||
@@ -42,25 +49,26 @@ export abstract class CoreSyncService implements SyncService {
|
||||
protected readonly authService: AuthService,
|
||||
protected readonly sendService: InternalSendService,
|
||||
protected readonly sendApiService: SendApiService,
|
||||
protected readonly stateProvider: StateProvider,
|
||||
) {}
|
||||
|
||||
abstract fullSync(forceSync: boolean, allowThrowOnError?: boolean): Promise<boolean>;
|
||||
|
||||
async getLastSync(): Promise<Date> {
|
||||
if ((await this.stateService.getUserId()) == null) {
|
||||
const userId = await firstValueFrom(this.accountService.activeAccount$.pipe(map((a) => a?.id)));
|
||||
if (userId == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const lastSync = await this.stateService.getLastSync();
|
||||
if (lastSync) {
|
||||
return new Date(lastSync);
|
||||
}
|
||||
|
||||
return null;
|
||||
return await firstValueFrom(this.lastSync$(userId));
|
||||
}
|
||||
|
||||
async setLastSync(date: Date, userId?: string): Promise<any> {
|
||||
await this.stateService.setLastSync(date.toJSON(), { userId: userId });
|
||||
lastSync$(userId: UserId) {
|
||||
return this.stateProvider.getUser(userId, LAST_SYNC_DATE).state$;
|
||||
}
|
||||
|
||||
async setLastSync(date: Date, userId: UserId): Promise<void> {
|
||||
await this.stateProvider.getUser(userId, LAST_SYNC_DATE).update(() => date);
|
||||
}
|
||||
|
||||
async syncUpsertFolder(notification: SyncFolderNotification, isEdit: boolean): Promise<boolean> {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { firstValueFrom } from "rxjs";
|
||||
import { firstValueFrom, map } from "rxjs";
|
||||
|
||||
import { UserDecryptionOptionsServiceAbstraction } from "../../../../auth/src/common/abstractions";
|
||||
import { LogoutReason } from "../../../../auth/src/common/types";
|
||||
@@ -17,6 +17,7 @@ import { AvatarService } from "../../auth/abstractions/avatar.service";
|
||||
import { KeyConnectorService } from "../../auth/abstractions/key-connector.service";
|
||||
import { InternalMasterPasswordServiceAbstraction } from "../../auth/abstractions/master-password.service.abstraction";
|
||||
import { TokenService } from "../../auth/abstractions/token.service";
|
||||
import { AuthenticationStatus } from "../../auth/enums/authentication-status";
|
||||
import { ForceSetPasswordReason } from "../../auth/models/domain/force-set-password-reason";
|
||||
import { DomainSettingsService } from "../../autofill/services/domain-settings.service";
|
||||
import { BillingAccountProfileStateService } from "../../billing/abstractions";
|
||||
@@ -42,6 +43,7 @@ import { LogService } from "../abstractions/log.service";
|
||||
import { StateService } from "../abstractions/state.service";
|
||||
import { MessageSender } from "../messaging";
|
||||
import { sequentialize } from "../misc/sequentialize";
|
||||
import { StateProvider } from "../state";
|
||||
|
||||
import { CoreSyncService } from "./core-sync.service";
|
||||
|
||||
@@ -73,6 +75,7 @@ export class DefaultSyncService extends CoreSyncService {
|
||||
private billingAccountProfileStateService: BillingAccountProfileStateService,
|
||||
private tokenService: TokenService,
|
||||
authService: AuthService,
|
||||
stateProvider: StateProvider,
|
||||
) {
|
||||
super(
|
||||
stateService,
|
||||
@@ -87,14 +90,16 @@ export class DefaultSyncService extends CoreSyncService {
|
||||
authService,
|
||||
sendService,
|
||||
sendApiService,
|
||||
stateProvider,
|
||||
);
|
||||
}
|
||||
|
||||
@sequentialize(() => "fullSync")
|
||||
override async fullSync(forceSync: boolean, allowThrowOnError = false): Promise<boolean> {
|
||||
const userId = await firstValueFrom(this.accountService.activeAccount$.pipe(map((a) => a?.id)));
|
||||
this.syncStarted();
|
||||
const isAuthenticated = await this.stateService.getIsAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
const authStatus = await firstValueFrom(this.authService.authStatusFor$(userId));
|
||||
if (authStatus === AuthenticationStatus.LoggedOut) {
|
||||
return this.syncCompleted(false);
|
||||
}
|
||||
|
||||
@@ -110,7 +115,7 @@ export class DefaultSyncService extends CoreSyncService {
|
||||
}
|
||||
|
||||
if (!needsSync) {
|
||||
await this.setLastSync(now);
|
||||
await this.setLastSync(now, userId);
|
||||
return this.syncCompleted(false);
|
||||
}
|
||||
|
||||
@@ -126,7 +131,7 @@ export class DefaultSyncService extends CoreSyncService {
|
||||
await this.syncSettings(response.domains);
|
||||
await this.syncPolicies(response.policies);
|
||||
|
||||
await this.setLastSync(now);
|
||||
await this.setLastSync(now, userId);
|
||||
return this.syncCompleted(true);
|
||||
} catch (e) {
|
||||
if (allowThrowOnError) {
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { Observable } from "rxjs";
|
||||
|
||||
import {
|
||||
SyncCipherNotification,
|
||||
SyncFolderNotification,
|
||||
SyncSendNotification,
|
||||
} from "../../models/response/notification.response";
|
||||
import { UserId } from "../../types/guid";
|
||||
|
||||
/**
|
||||
* A class encapsulating sync operations and data.
|
||||
@@ -20,15 +23,16 @@ export abstract class SyncService {
|
||||
* Gets the date of the last sync for the currently active user.
|
||||
*
|
||||
* @returns The date of the last sync or null if there is no active user or the active user has not synced before.
|
||||
*
|
||||
* @deprecated Use {@link lastSync$} to get an observable stream of a given users last sync date instead.
|
||||
*/
|
||||
abstract getLastSync(): Promise<Date>;
|
||||
abstract getLastSync(): Promise<Date | null>;
|
||||
|
||||
/**
|
||||
* Updates a users last sync date.
|
||||
* @param date The date to be set as the users last sync date.
|
||||
* @param userId The userId of the user to update the last sync date for.
|
||||
* Retrieves a stream of the given users last sync date. Or null if the user has not synced before.
|
||||
* @param userId The user id of the user to get the stream for.
|
||||
*/
|
||||
abstract setLastSync(date: Date, userId?: string): Promise<void>;
|
||||
abstract lastSync$(userId: UserId): Observable<Date | null>;
|
||||
|
||||
/**
|
||||
* Optionally does a full sync operation including going to the server to gather the source
|
||||
|
||||
@@ -64,13 +64,15 @@ import { PasswordOptionsMigrator } from "./migrations/63-migrate-password-settin
|
||||
import { GeneratorHistoryMigrator } from "./migrations/64-migrate-generator-history";
|
||||
import { ForwarderOptionsMigrator } from "./migrations/65-migrate-forwarder-settings";
|
||||
import { MoveFinalDesktopSettingsMigrator } from "./migrations/66-move-final-desktop-settings";
|
||||
import { RemoveUnassignedItemsBannerDismissed } from "./migrations/67-remove-unassigned-items-banner-dismissed";
|
||||
import { MoveLastSyncDate } from "./migrations/68-move-last-sync-date";
|
||||
import { MoveBiometricAutoPromptToAccount } from "./migrations/7-move-biometric-auto-prompt-to-account";
|
||||
import { MoveStateVersionMigrator } from "./migrations/8-move-state-version";
|
||||
import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-settings-to-global";
|
||||
import { MinVersionMigrator } from "./migrations/min-version";
|
||||
|
||||
export const MIN_VERSION = 3;
|
||||
export const CURRENT_VERSION = 66;
|
||||
export const CURRENT_VERSION = 68;
|
||||
export type MinVersion = typeof MIN_VERSION;
|
||||
|
||||
export function createMigrationBuilder() {
|
||||
@@ -138,7 +140,9 @@ export function createMigrationBuilder() {
|
||||
.with(PasswordOptionsMigrator, 62, 63)
|
||||
.with(GeneratorHistoryMigrator, 63, 64)
|
||||
.with(ForwarderOptionsMigrator, 64, 65)
|
||||
.with(MoveFinalDesktopSettingsMigrator, 65, CURRENT_VERSION);
|
||||
.with(MoveFinalDesktopSettingsMigrator, 65, 66)
|
||||
.with(RemoveUnassignedItemsBannerDismissed, 66, 67)
|
||||
.with(MoveLastSyncDate, 67, CURRENT_VERSION);
|
||||
}
|
||||
|
||||
export async function currentVersion(
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import { runMigrator } from "../migration-helper.spec";
|
||||
|
||||
import { MoveLastSyncDate } from "./68-move-last-sync-date";
|
||||
|
||||
describe("MoveLastSyncDate", () => {
|
||||
const sut = new MoveLastSyncDate(67, 68);
|
||||
it("migrates data", async () => {
|
||||
const output = await runMigrator(sut, {
|
||||
global_account_accounts: {
|
||||
user1: null,
|
||||
user2: null,
|
||||
user3: null,
|
||||
user4: null,
|
||||
user5: null,
|
||||
},
|
||||
user1: {
|
||||
profile: {
|
||||
lastSync: "2024-07-24T14:27:25.703Z",
|
||||
},
|
||||
},
|
||||
user2: {},
|
||||
user3: { profile: null },
|
||||
user4: { profile: {} },
|
||||
user5: { profile: { lastSync: null } },
|
||||
});
|
||||
|
||||
expect(output).toEqual({
|
||||
global_account_accounts: {
|
||||
user1: null,
|
||||
user2: null,
|
||||
user3: null,
|
||||
user4: null,
|
||||
user5: null,
|
||||
},
|
||||
user1: {
|
||||
profile: {},
|
||||
},
|
||||
user2: {},
|
||||
user3: { profile: null },
|
||||
user4: { profile: {} },
|
||||
user5: { profile: { lastSync: null } },
|
||||
user_user1_sync_lastSync: "2024-07-24T14:27:25.703Z",
|
||||
});
|
||||
});
|
||||
|
||||
it("rolls back data", async () => {
|
||||
const output = await runMigrator(
|
||||
sut,
|
||||
{
|
||||
global_account_accounts: {
|
||||
user1: null,
|
||||
user2: null,
|
||||
user3: null,
|
||||
user4: null,
|
||||
user5: null,
|
||||
},
|
||||
user1: {
|
||||
profile: {
|
||||
extraProperty: "hello",
|
||||
},
|
||||
},
|
||||
user2: {},
|
||||
user3: { profile: null },
|
||||
user4: { profile: {} },
|
||||
user5: { profile: { lastSync: null } },
|
||||
user_user1_sync_lastSync: "2024-07-24T14:27:25.703Z",
|
||||
},
|
||||
"rollback",
|
||||
);
|
||||
|
||||
expect(output).toEqual({
|
||||
global_account_accounts: {
|
||||
user1: null,
|
||||
user2: null,
|
||||
user3: null,
|
||||
user4: null,
|
||||
user5: null,
|
||||
},
|
||||
user1: {
|
||||
profile: {
|
||||
lastSync: "2024-07-24T14:27:25.703Z",
|
||||
extraProperty: "hello",
|
||||
},
|
||||
},
|
||||
user2: {},
|
||||
user3: { profile: null },
|
||||
user4: { profile: {} },
|
||||
user5: { profile: { lastSync: null } },
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,49 @@
|
||||
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
|
||||
import { Migrator } from "../migrator";
|
||||
|
||||
type ExpectedAccount = {
|
||||
profile?: {
|
||||
lastSync?: string;
|
||||
};
|
||||
};
|
||||
|
||||
const LAST_SYNC_KEY: KeyDefinitionLike = {
|
||||
key: "lastSync",
|
||||
stateDefinition: {
|
||||
name: "sync",
|
||||
},
|
||||
};
|
||||
|
||||
export class MoveLastSyncDate extends Migrator<67, 68> {
|
||||
async migrate(helper: MigrationHelper): Promise<void> {
|
||||
async function migrateAccount(userId: string, account: ExpectedAccount) {
|
||||
const value = account?.profile?.lastSync;
|
||||
if (value != null) {
|
||||
await helper.setToUser(userId, LAST_SYNC_KEY, value);
|
||||
|
||||
delete account.profile.lastSync;
|
||||
await helper.set(userId, account);
|
||||
}
|
||||
}
|
||||
|
||||
const accounts = await helper.getAccounts<ExpectedAccount>();
|
||||
await Promise.all(accounts.map(({ userId, account }) => migrateAccount(userId, account)));
|
||||
}
|
||||
|
||||
async rollback(helper: MigrationHelper): Promise<void> {
|
||||
async function rollbackAccount(userId: string, account: ExpectedAccount) {
|
||||
const value = await helper.getFromUser<string>(userId, LAST_SYNC_KEY);
|
||||
|
||||
if (value != null) {
|
||||
account ??= {};
|
||||
account.profile ??= {};
|
||||
account.profile.lastSync = value;
|
||||
await helper.set(userId, account);
|
||||
await helper.removeFromUser(userId, LAST_SYNC_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
const accounts = await helper.getAccounts<ExpectedAccount>();
|
||||
await Promise.all(accounts.map(({ userId, account }) => rollbackAccount(userId, account)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user