1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-14 15:23:33 +00:00

Move lastSync State (#10272)

This commit is contained in:
Justin Baur
2024-08-06 15:01:42 -04:00
committed by GitHub
parent 887b98988a
commit dcb21c2685
22 changed files with 198 additions and 110 deletions

View File

@@ -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>;
}

View File

@@ -95,7 +95,6 @@ export class AccountProfile {
name?: string;
email?: string;
emailVerified?: boolean;
lastSync?: string;
userId?: string;
static fromJSON(obj: Jsonify<AccountProfile>): AccountProfile {

View File

@@ -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()))

View File

@@ -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");

View File

@@ -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> {

View File

@@ -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) {

View File

@@ -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