diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index 87071b7484f..9e7fc3f08b7 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -693,7 +693,6 @@ export default class MainBackground { this.folderApiService, this.organizationService, this.sendApiService, - this.stateProvider, logoutCallback, ); this.eventUploadService = new EventUploadService( @@ -1077,7 +1076,7 @@ export default class MainBackground { await this.eventUploadService.uploadEvents(userId); await Promise.all([ - this.syncService.setLastSync(new Date(0), userId as UserId), + this.syncService.setLastSync(new Date(0), userId), this.cryptoService.clearKeys(userId), this.settingsService.clear(userId), this.cipherService.clear(userId), diff --git a/apps/cli/src/bw.ts b/apps/cli/src/bw.ts index e8548e74a03..67c1be16f84 100644 --- a/apps/cli/src/bw.ts +++ b/apps/cli/src/bw.ts @@ -551,7 +551,6 @@ export class Main { this.folderApiService, this.organizationService, this.sendApiService, - this.stateProvider, async (expired: boolean) => await this.logout(), ); diff --git a/apps/desktop/src/app/app.component.ts b/apps/desktop/src/app/app.component.ts index afc37005df4..a42f80fd6f5 100644 --- a/apps/desktop/src/app/app.component.ts +++ b/apps/desktop/src/app/app.component.ts @@ -571,7 +571,7 @@ export class AppComponent implements OnInit, OnDestroy { let preLogoutActiveUserId; try { await this.eventUploadService.uploadEvents(userBeingLoggedOut); - await this.syncService.setLastSync(new Date(0), userBeingLoggedOut as UserId); + await this.syncService.setLastSync(new Date(0), userBeingLoggedOut); await this.cryptoService.clearKeys(userBeingLoggedOut); await this.settingsService.clear(userBeingLoggedOut); await this.cipherService.clear(userBeingLoggedOut); diff --git a/apps/web/src/app/core/state/state.service.ts b/apps/web/src/app/core/state/state.service.ts index b46c2b590a0..1ad3bd25c31 100644 --- a/apps/web/src/app/core/state/state.service.ts +++ b/apps/web/src/app/core/state/state.service.ts @@ -80,4 +80,14 @@ export class StateService extends BaseStateService { options = this.reconcileOptions(options, await this.defaultInMemoryOptions()); return await super.setEncryptedSends(value, options); } + + override async getLastSync(options?: StorageOptions): Promise { + options = this.reconcileOptions(options, await this.defaultInMemoryOptions()); + return await super.getLastSync(options); + } + + override async setLastSync(value: string, options?: StorageOptions): Promise { + options = this.reconcileOptions(options, await this.defaultInMemoryOptions()); + return await super.setLastSync(value, options); + } } diff --git a/libs/angular/src/services/jslib-services.module.ts b/libs/angular/src/services/jslib-services.module.ts index ec2080e1b4d..3803e959115 100644 --- a/libs/angular/src/services/jslib-services.module.ts +++ b/libs/angular/src/services/jslib-services.module.ts @@ -139,10 +139,10 @@ import { ValidationService } from "@bitwarden/common/platform/services/validatio import { WebCryptoFunctionService } from "@bitwarden/common/platform/services/web-crypto-function.service"; import { ActiveUserStateProvider, - DerivedStateProvider, GlobalStateProvider, SingleUserStateProvider, StateProvider, + DerivedStateProvider, } from "@bitwarden/common/platform/state"; /* eslint-disable import/no-restricted-paths -- We need the implementations to inject, but generally these should not be accessed */ import { DefaultActiveUserStateProvider } from "@bitwarden/common/platform/state/implementations/default-active-user-state.provider"; @@ -517,7 +517,6 @@ import { ModalService } from "./modal.service"; FolderApiServiceAbstraction, OrganizationServiceAbstraction, SendApiServiceAbstraction, - StateProvider, LOGOUT_CALLBACK, ], }, diff --git a/libs/common/src/platform/abstractions/state.service.ts b/libs/common/src/platform/abstractions/state.service.ts index 1f15379109a..7f079be45f8 100644 --- a/libs/common/src/platform/abstractions/state.service.ts +++ b/libs/common/src/platform/abstractions/state.service.ts @@ -328,6 +328,8 @@ export abstract class StateService { setKeyHash: (value: string, options?: StorageOptions) => Promise; getLastActive: (options?: StorageOptions) => Promise; setLastActive: (value: number, options?: StorageOptions) => Promise; + getLastSync: (options?: StorageOptions) => Promise; + setLastSync: (value: string, options?: StorageOptions) => Promise; getLocalData: (options?: StorageOptions) => Promise<{ [cipherId: string]: LocalData }>; setLocalData: ( value: { [cipherId: string]: LocalData }, diff --git a/libs/common/src/platform/models/domain/account.ts b/libs/common/src/platform/models/domain/account.ts index 21c4e69c32a..7ab98eec7d8 100644 --- a/libs/common/src/platform/models/domain/account.ts +++ b/libs/common/src/platform/models/domain/account.ts @@ -181,6 +181,7 @@ export class AccountProfile { forceSetPasswordReason?: ForceSetPasswordReason; hasPremiumPersonally?: boolean; hasPremiumFromOrganization?: boolean; + lastSync?: string; userId?: string; usesKeyConnector?: boolean; keyHash?: string; diff --git a/libs/common/src/platform/services/state.service.ts b/libs/common/src/platform/services/state.service.ts index 504a92a58cc..cb3b3c8c870 100644 --- a/libs/common/src/platform/services/state.service.ts +++ b/libs/common/src/platform/services/state.service.ts @@ -1619,6 +1619,23 @@ export class StateService< await this.storageService.save(keys.accountActivity, accountActivity, options); } + async getLastSync(options?: StorageOptions): Promise { + return ( + await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskMemoryOptions())) + )?.profile?.lastSync; + } + + async setLastSync(value: string, options?: StorageOptions): Promise { + 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 getLocalData(options?: StorageOptions): Promise<{ [cipherId: string]: LocalData }> { return ( await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskLocalOptions())) diff --git a/libs/common/src/platform/state/state-definitions.ts b/libs/common/src/platform/state/state-definitions.ts index 03269b6808d..c5be07023e8 100644 --- a/libs/common/src/platform/state/state-definitions.ts +++ b/libs/common/src/platform/state/state-definitions.ts @@ -44,8 +44,6 @@ export const BILLING_DISK = new StateDefinition("billing", "disk"); export const FOLDER_DISK = new StateDefinition("folder", "disk", { web: "memory" }); -export const SYNC_STATE = new StateDefinition("sync", "disk", { web: "memory" }); - export const VAULT_SETTINGS_DISK = new StateDefinition("vaultSettings", "disk", { web: "disk-local", }); diff --git a/libs/common/src/state-migrations/migrate.ts b/libs/common/src/state-migrations/migrate.ts index f09a1fe7ae0..7ed50c4206d 100644 --- a/libs/common/src/state-migrations/migrate.ts +++ b/libs/common/src/state-migrations/migrate.ts @@ -20,8 +20,9 @@ import { CollapsedGroupingsMigrator } from "./migrations/22-move-collapsed-group import { MoveBiometricPromptsToStateProviders } from "./migrations/23-move-biometric-prompts-to-state-providers"; import { SmOnboardingTasksMigrator } from "./migrations/24-move-sm-onboarding-key-to-state-providers"; import { ClearClipboardDelayMigrator } from "./migrations/25-move-clear-clipboard-to-autofill-settings-state-provider"; -import { BadgeSettingsMigrator } from "./migrations/26-move-badge-settings-to-state-providers"; -import { MoveBiometricUnlockToStateProviders } from "./migrations/27-move-biometric-unlock-to-state-providers"; +import { RevertLastSyncMigrator } from "./migrations/26-revert-move-last-sync-to-state-provider"; +import { BadgeSettingsMigrator } from "./migrations/27-move-badge-settings-to-state-providers"; +import { MoveBiometricUnlockToStateProviders } from "./migrations/28-move-biometric-unlock-to-state-providers"; import { FixPremiumMigrator } from "./migrations/3-fix-premium"; import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked"; import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys"; @@ -32,7 +33,7 @@ import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-setting import { MinVersionMigrator } from "./migrations/min-version"; export const MIN_VERSION = 2; -export const CURRENT_VERSION = 27; +export const CURRENT_VERSION = 28; export type MinVersion = typeof MIN_VERSION; export function createMigrationBuilder() { @@ -61,8 +62,9 @@ export function createMigrationBuilder() { .with(MoveBiometricPromptsToStateProviders, 22, 23) .with(SmOnboardingTasksMigrator, 23, 24) .with(ClearClipboardDelayMigrator, 24, 25) - .with(BadgeSettingsMigrator, 25, 26) - .with(MoveBiometricUnlockToStateProviders, 26, CURRENT_VERSION); + .with(RevertLastSyncMigrator, 25, 26) + .with(BadgeSettingsMigrator, 26, 27) + .with(MoveBiometricUnlockToStateProviders, 27, CURRENT_VERSION); } export async function currentVersion( diff --git a/libs/common/src/state-migrations/migrations/15-move-folder-state-to-state-provider.spec.ts b/libs/common/src/state-migrations/migrations/15-move-folder-state-to-state-provider.spec.ts index d990bf8f14b..05e791f76bc 100644 --- a/libs/common/src/state-migrations/migrations/15-move-folder-state-to-state-provider.spec.ts +++ b/libs/common/src/state-migrations/migrations/15-move-folder-state-to-state-provider.spec.ts @@ -1,4 +1,4 @@ -import { any, MockProxy } from "jest-mock-extended"; +import { MockProxy, any } from "jest-mock-extended"; import { MigrationHelper } from "../migration-helper"; import { mockMigrationHelper } from "../migration-helper.spec"; diff --git a/libs/common/src/state-migrations/migrations/26-revert-move-last-sync-to-state-provider.spec.ts b/libs/common/src/state-migrations/migrations/26-revert-move-last-sync-to-state-provider.spec.ts new file mode 100644 index 00000000000..19fc7133383 --- /dev/null +++ b/libs/common/src/state-migrations/migrations/26-revert-move-last-sync-to-state-provider.spec.ts @@ -0,0 +1,112 @@ +import { any, MockProxy } from "jest-mock-extended"; + +import { MigrationHelper } from "../migration-helper"; +import { mockMigrationHelper } from "../migration-helper.spec"; + +import { RevertLastSyncMigrator } from "./26-revert-move-last-sync-to-state-provider"; + +function rollbackJSON() { + return { + global: { + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user-1", "user-2"], + "user-1": { + profile: { + lastSync: "2024-01-24T00:00:00.000Z", + otherStuff: "otherStuff4", + }, + otherStuff: "otherStuff5", + }, + }; +} + +function exampleJSON() { + return { + "user_user-1_sync_lastSync": "2024-01-24T00:00:00.000Z", + "user_user-2_sync_lastSync": null as any, + global: { + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user-1", "user-2"], + "user-1": { + profile: { + lastSync: "2024-01-24T00:00:00.000Z", + otherStuff: "otherStuff4", + }, + otherStuff: "otherStuff5", + }, + }; +} + +describe("LastSyncMigrator", () => { + let helper: MockProxy; + let sut: RevertLastSyncMigrator; + + const keyDefinitionLike = { + key: "lastSync", + stateDefinition: { + name: "sync", + }, + }; + + describe("rollback", () => { + beforeEach(() => { + helper = mockMigrationHelper(rollbackJSON(), 26); + sut = new RevertLastSyncMigrator(25, 26); + }); + + it("should remove lastSync from all accounts", async () => { + await sut.rollback(helper); + expect(helper.set).toHaveBeenCalledWith("user-1", { + profile: { + otherStuff: "otherStuff4", + }, + otherStuff: "otherStuff5", + }); + }); + + it("should set lastSync provider value for each account", async () => { + await sut.rollback(helper); + + expect(helper.setToUser).toHaveBeenCalledWith( + "user-1", + keyDefinitionLike, + "2024-01-24T00:00:00.000Z", + ); + + expect(helper.setToUser).toHaveBeenCalledWith("user-2", keyDefinitionLike, null); + }); + }); + + describe("migrate", () => { + beforeEach(() => { + helper = mockMigrationHelper(exampleJSON(), 25); + sut = new RevertLastSyncMigrator(25, 26); + }); + + it.each(["user-1", "user-2"])("should null out new values", async (userId) => { + await sut.migrate(helper); + + expect(helper.setToUser).toHaveBeenCalledWith(userId, keyDefinitionLike, null); + }); + + it("should add lastSync back to accounts", async () => { + await sut.migrate(helper); + + expect(helper.set).toHaveBeenCalledWith("user-1", { + profile: { + lastSync: "2024-01-24T00:00:00.000Z", + otherStuff: "otherStuff4", + }, + otherStuff: "otherStuff5", + }); + }); + + it("should not try to restore values to missing accounts", async () => { + await sut.rollback(helper); + + expect(helper.set).not.toHaveBeenCalledWith("user-2", any()); + }); + }); +}); diff --git a/libs/common/src/state-migrations/migrations/26-revert-move-last-sync-to-state-provider.ts b/libs/common/src/state-migrations/migrations/26-revert-move-last-sync-to-state-provider.ts new file mode 100644 index 00000000000..ef9c1b37fa8 --- /dev/null +++ b/libs/common/src/state-migrations/migrations/26-revert-move-last-sync-to-state-provider.ts @@ -0,0 +1,47 @@ +import { KeyDefinitionLike, MigrationHelper } from "../migration-helper"; +import { Migrator } from "../migrator"; + +type ExpectedAccountType = { + profile?: { + lastSync?: string; + }; +}; + +const LAST_SYNC_KEY: KeyDefinitionLike = { + key: "lastSync", + stateDefinition: { + name: "sync", + }, +}; + +export class RevertLastSyncMigrator extends Migrator<25, 26> { + async rollback(helper: MigrationHelper): Promise { + const accounts = await helper.getAccounts(); + async function rollbackAccount(userId: string, account: ExpectedAccountType): Promise { + const value = account?.profile?.lastSync; + await helper.setToUser(userId, LAST_SYNC_KEY, value ?? null); + if (value != null) { + delete account.profile.lastSync; + await helper.set(userId, account); + } + } + + await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]); + } + async migrate(helper: MigrationHelper): Promise { + const accounts = await helper.getAccounts(); + + async function migrateAccount(userId: string, account: ExpectedAccountType): Promise { + const value = await helper.getFromUser(userId, LAST_SYNC_KEY); + if (account) { + account.profile = Object.assign(account.profile ?? {}, { + lastSync: value, + }); + await helper.set(userId, account); + } + await helper.setToUser(userId, LAST_SYNC_KEY, null); + } + + await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]); + } +} diff --git a/libs/common/src/state-migrations/migrations/26-move-badge-settings-to-state-providers.spec.ts b/libs/common/src/state-migrations/migrations/27-move-badge-settings-to-state-providers.spec.ts similarity index 93% rename from libs/common/src/state-migrations/migrations/26-move-badge-settings-to-state-providers.spec.ts rename to libs/common/src/state-migrations/migrations/27-move-badge-settings-to-state-providers.spec.ts index 9e6ae77041c..dbc15ea94b3 100644 --- a/libs/common/src/state-migrations/migrations/26-move-badge-settings-to-state-providers.spec.ts +++ b/libs/common/src/state-migrations/migrations/27-move-badge-settings-to-state-providers.spec.ts @@ -3,7 +3,7 @@ import { any, MockProxy } from "jest-mock-extended"; import { StateDefinitionLike, MigrationHelper } from "../migration-helper"; import { mockMigrationHelper } from "../migration-helper.spec"; -import { BadgeSettingsMigrator } from "./26-move-badge-settings-to-state-providers"; +import { BadgeSettingsMigrator } from "./27-move-badge-settings-to-state-providers"; function exampleJSON() { return { @@ -77,8 +77,8 @@ describe("BadgeSettingsMigrator", () => { describe("migrate", () => { beforeEach(() => { - helper = mockMigrationHelper(exampleJSON(), 25); - sut = new BadgeSettingsMigrator(25, 26); + helper = mockMigrationHelper(exampleJSON(), 26); + sut = new BadgeSettingsMigrator(26, 27); }); it("should remove disableBadgeCounter setting from all accounts", async () => { @@ -117,8 +117,8 @@ describe("BadgeSettingsMigrator", () => { describe("rollback", () => { beforeEach(() => { - helper = mockMigrationHelper(rollbackJSON(), 26); - sut = new BadgeSettingsMigrator(25, 26); + helper = mockMigrationHelper(rollbackJSON(), 27); + sut = new BadgeSettingsMigrator(26, 27); }); it("should null out new values for each account", async () => { diff --git a/libs/common/src/state-migrations/migrations/26-move-badge-settings-to-state-providers.ts b/libs/common/src/state-migrations/migrations/27-move-badge-settings-to-state-providers.ts similarity index 97% rename from libs/common/src/state-migrations/migrations/26-move-badge-settings-to-state-providers.ts rename to libs/common/src/state-migrations/migrations/27-move-badge-settings-to-state-providers.ts index 090cb5a7900..376e5aefea9 100644 --- a/libs/common/src/state-migrations/migrations/26-move-badge-settings-to-state-providers.ts +++ b/libs/common/src/state-migrations/migrations/27-move-badge-settings-to-state-providers.ts @@ -14,7 +14,7 @@ const enableBadgeCounterKeyDefinition: KeyDefinitionLike = { key: "enableBadgeCounter", }; -export class BadgeSettingsMigrator extends Migrator<25, 26> { +export class BadgeSettingsMigrator extends Migrator<26, 27> { async migrate(helper: MigrationHelper): Promise { // account state (e.g. account settings -> state provider framework keys) const accounts = await helper.getAccounts(); diff --git a/libs/common/src/state-migrations/migrations/27-move-biometric-unlock-to-state-providers.spec.ts b/libs/common/src/state-migrations/migrations/28-move-biometric-unlock-to-state-providers.spec.ts similarity index 90% rename from libs/common/src/state-migrations/migrations/27-move-biometric-unlock-to-state-providers.spec.ts rename to libs/common/src/state-migrations/migrations/28-move-biometric-unlock-to-state-providers.spec.ts index 89693dff570..7ef242e8d04 100644 --- a/libs/common/src/state-migrations/migrations/27-move-biometric-unlock-to-state-providers.spec.ts +++ b/libs/common/src/state-migrations/migrations/28-move-biometric-unlock-to-state-providers.spec.ts @@ -6,7 +6,7 @@ import { mockMigrationHelper } from "../migration-helper.spec"; import { BIOMETRIC_UNLOCK_ENABLED, MoveBiometricUnlockToStateProviders, -} from "./27-move-biometric-unlock-to-state-providers"; +} from "./28-move-biometric-unlock-to-state-providers"; function exampleJSON() { return { @@ -52,8 +52,8 @@ describe("MoveBiometricPromptsToStateProviders migrator", () => { describe("migrate", () => { beforeEach(() => { - helper = mockMigrationHelper(exampleJSON(), 26); - sut = new MoveBiometricUnlockToStateProviders(26, 27); + helper = mockMigrationHelper(exampleJSON(), 27); + sut = new MoveBiometricUnlockToStateProviders(27, 28); }); it("removes biometricUnlock from all accounts", async () => { @@ -85,8 +85,8 @@ describe("MoveBiometricPromptsToStateProviders migrator", () => { describe("rollback", () => { beforeEach(() => { - helper = mockMigrationHelper(rollbackJSON(), 27); - sut = new MoveBiometricUnlockToStateProviders(26, 27); + helper = mockMigrationHelper(rollbackJSON(), 28); + sut = new MoveBiometricUnlockToStateProviders(27, 28); }); it("nulls out new values", async () => { diff --git a/libs/common/src/state-migrations/migrations/27-move-biometric-unlock-to-state-providers.ts b/libs/common/src/state-migrations/migrations/28-move-biometric-unlock-to-state-providers.ts similarity index 99% rename from libs/common/src/state-migrations/migrations/27-move-biometric-unlock-to-state-providers.ts rename to libs/common/src/state-migrations/migrations/28-move-biometric-unlock-to-state-providers.ts index 147f7c7f06e..ae4f86e3d5e 100644 --- a/libs/common/src/state-migrations/migrations/27-move-biometric-unlock-to-state-providers.ts +++ b/libs/common/src/state-migrations/migrations/28-move-biometric-unlock-to-state-providers.ts @@ -12,7 +12,7 @@ export const BIOMETRIC_UNLOCK_ENABLED: KeyDefinitionLike = { stateDefinition: { name: "biometricSettings" }, }; -export class MoveBiometricUnlockToStateProviders extends Migrator<26, 27> { +export class MoveBiometricUnlockToStateProviders extends Migrator<27, 28> { async migrate(helper: MigrationHelper): Promise { const legacyAccounts = await helper.getAccounts(); diff --git a/libs/common/src/vault/abstractions/sync/sync.service.abstraction.ts b/libs/common/src/vault/abstractions/sync/sync.service.abstraction.ts index a7c23cb5910..cfe73317555 100644 --- a/libs/common/src/vault/abstractions/sync/sync.service.abstraction.ts +++ b/libs/common/src/vault/abstractions/sync/sync.service.abstraction.ts @@ -1,18 +1,14 @@ -import { Observable } from "rxjs"; - import { SyncCipherNotification, SyncFolderNotification, SyncSendNotification, } from "../../../models/response/notification.response"; -import { UserId } from "../../../types/guid"; export abstract class SyncService { syncInProgress: boolean; - lastSync$: Observable; getLastSync: () => Promise; - setLastSync: (date: Date, userId?: UserId) => Promise; + setLastSync: (date: Date, userId?: string) => Promise; fullSync: (forceSync: boolean, allowThrowOnError?: boolean) => Promise; syncUpsertFolder: (notification: SyncFolderNotification, isEdit: boolean) => Promise; syncDeleteFolder: (notification: SyncFolderNotification) => Promise; diff --git a/libs/common/src/vault/services/sync/sync.service.ts b/libs/common/src/vault/services/sync/sync.service.ts index 676fdf65ff1..c0105af7584 100644 --- a/libs/common/src/vault/services/sync/sync.service.ts +++ b/libs/common/src/vault/services/sync/sync.service.ts @@ -1,5 +1,3 @@ -import { firstValueFrom, map } from "rxjs"; - import { ApiService } from "../../../abstractions/api.service"; import { SettingsService } from "../../../abstractions/settings.service"; import { InternalOrganizationServiceAbstraction } from "../../../admin-console/abstractions/organization/organization.service.abstraction"; @@ -25,12 +23,10 @@ import { MessagingService } from "../../../platform/abstractions/messaging.servi import { StateService } from "../../../platform/abstractions/state.service"; import { sequentialize } from "../../../platform/misc/sequentialize"; import { AccountDecryptionOptions } from "../../../platform/models/domain/account"; -import { KeyDefinition, StateProvider, SYNC_STATE } from "../../../platform/state"; import { SendData } from "../../../tools/send/models/data/send.data"; import { SendResponse } from "../../../tools/send/models/response/send.response"; 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 { FolderApiServiceAbstraction } from "../../../vault/abstractions/folder/folder-api.service.abstraction"; import { InternalFolderService } from "../../../vault/abstractions/folder/folder.service.abstraction"; @@ -43,22 +39,8 @@ import { CollectionService } from "../../abstractions/collection.service"; import { CollectionData } from "../../models/data/collection.data"; import { CollectionDetailsResponse } from "../../models/response/collection.response"; -const LAST_SYNC_KEY = new KeyDefinition(SYNC_STATE, "lastSync", { - deserializer: (value) => value, -}); - export class SyncService implements SyncServiceAbstraction { - private lastSyncState = this.stateProvider.getActive(LAST_SYNC_KEY); - syncInProgress = false; - lastSync$ = this.lastSyncState.state$.pipe( - map((value) => { - if (value == null) { - return null; - } - return new Date(value); - }), - ); constructor( private apiService: ApiService, @@ -77,20 +59,24 @@ export class SyncService implements SyncServiceAbstraction { private folderApiService: FolderApiServiceAbstraction, private organizationService: InternalOrganizationServiceAbstraction, private sendApiService: SendApiService, - private stateProvider: StateProvider, private logoutCallback: (expired: boolean) => Promise, ) {} - async getLastSync(): Promise { - return await firstValueFrom(this.lastSync$); + async getLastSync(): Promise { + if ((await this.stateService.getUserId()) == null) { + return null; + } + + const lastSync = await this.stateService.getLastSync(); + if (lastSync) { + return new Date(lastSync); + } + + return null; } - async setLastSync(date: Date, userId?: UserId): Promise { - if (userId !== undefined) { - await this.stateProvider.getUser(userId, LAST_SYNC_KEY).update(() => date.toJSON()); - } else { - await this.lastSyncState.update(() => date.toJSON()); - } + async setLastSync(date: Date, userId?: string): Promise { + await this.stateService.setLastSync(date.toJSON(), { userId: userId }); } @sequentialize(() => "fullSync")