mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 02:03:39 +00:00
Serialize Browser Accounts
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import { Jsonify } from "type-fest";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Account as BaseAccount,
|
Account as BaseAccount,
|
||||||
AccountSettings as BaseAccountSettings,
|
AccountSettings as BaseAccountSettings,
|
||||||
@@ -9,6 +11,14 @@ import { BrowserSendComponentState } from "./browserSendComponentState";
|
|||||||
|
|
||||||
export class AccountSettings extends BaseAccountSettings {
|
export class AccountSettings extends BaseAccountSettings {
|
||||||
vaultTimeout = -1; // On Restart
|
vaultTimeout = -1; // On Restart
|
||||||
|
|
||||||
|
static fromJSON(json: Jsonify<AccountSettings>): AccountSettings {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign(new AccountSettings(), json, super.fromJSON(json));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Account extends BaseAccount {
|
export class Account extends BaseAccount {
|
||||||
@@ -29,4 +39,18 @@ export class Account extends BaseAccount {
|
|||||||
this.ciphers = init?.ciphers ?? new BrowserComponentState();
|
this.ciphers = init?.ciphers ?? new BrowserComponentState();
|
||||||
this.sendType = init?.sendType ?? new BrowserComponentState();
|
this.sendType = init?.sendType ?? new BrowserComponentState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static fromJSON(json: Jsonify<Account>): Account {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign(new Account({}), json, super.fromJSON(json), {
|
||||||
|
settings: AccountSettings.fromJSON(json.settings),
|
||||||
|
groupings: BrowserGroupingsComponentState.fromJSON(json.groupings),
|
||||||
|
send: BrowserSendComponentState.fromJSON(json.send),
|
||||||
|
ciphers: BrowserComponentState.fromJSON(json.ciphers),
|
||||||
|
sendType: BrowserComponentState.fromJSON(json.sendType),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,14 @@
|
|||||||
|
import { Jsonify } from "type-fest";
|
||||||
|
|
||||||
export class BrowserComponentState {
|
export class BrowserComponentState {
|
||||||
scrollY: number;
|
scrollY: number;
|
||||||
searchText: string;
|
searchText: string;
|
||||||
|
|
||||||
|
static fromJSON(json: Jsonify<BrowserComponentState>) {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign(new BrowserComponentState(), json);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { CipherType } from "@bitwarden/common/enums/cipherType";
|
import { CipherType } from "@bitwarden/common/enums/cipherType";
|
||||||
|
import { Utils } from "@bitwarden/common/misc/utils";
|
||||||
import { CipherView } from "@bitwarden/common/models/view/cipher.view";
|
import { CipherView } from "@bitwarden/common/models/view/cipher.view";
|
||||||
import { CollectionView } from "@bitwarden/common/models/view/collection.view";
|
import { CollectionView } from "@bitwarden/common/models/view/collection.view";
|
||||||
import { FolderView } from "@bitwarden/common/models/view/folder.view";
|
import { FolderView } from "@bitwarden/common/models/view/folder.view";
|
||||||
|
import { DeepJsonify } from "@bitwarden/common/types/deep-jsonify";
|
||||||
|
|
||||||
import { BrowserComponentState } from "./browserComponentState";
|
import { BrowserComponentState } from "./browserComponentState";
|
||||||
|
|
||||||
@@ -15,4 +17,28 @@ export class BrowserGroupingsComponentState extends BrowserComponentState {
|
|||||||
folders: FolderView[];
|
folders: FolderView[];
|
||||||
collections: CollectionView[];
|
collections: CollectionView[];
|
||||||
deletedCount: number;
|
deletedCount: number;
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return Utils.merge(this, {
|
||||||
|
collectionCounts: Utils.mapToRecord(this.collectionCounts),
|
||||||
|
folderCounts: Utils.mapToRecord(this.folderCounts),
|
||||||
|
typeCounts: Utils.mapToRecord(this.typeCounts),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJSON(json: DeepJsonify<BrowserGroupingsComponentState>) {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign(new BrowserGroupingsComponentState(), json, {
|
||||||
|
favoriteCiphers: json.favoriteCiphers?.map((c) => CipherView.fromJSON(c)),
|
||||||
|
noFolderCiphers: json.noFolderCiphers?.map((c) => CipherView.fromJSON(c)),
|
||||||
|
ciphers: json.ciphers?.map((c) => CipherView.fromJSON(c)),
|
||||||
|
collectionCounts: Utils.recordToMap(json.collectionCounts),
|
||||||
|
folderCounts: Utils.recordToMap(json.folderCounts),
|
||||||
|
typeCounts: Utils.recordToMap(json.typeCounts),
|
||||||
|
folders: json.folders?.map((f) => FolderView.fromJSON(f)),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,28 @@
|
|||||||
import { SendType } from "@bitwarden/common/enums/sendType";
|
import { SendType } from "@bitwarden/common/enums/sendType";
|
||||||
|
import { Utils } from "@bitwarden/common/misc/utils";
|
||||||
import { SendView } from "@bitwarden/common/models/view/send.view";
|
import { SendView } from "@bitwarden/common/models/view/send.view";
|
||||||
|
import { DeepJsonify } from "@bitwarden/common/types/deep-jsonify";
|
||||||
|
|
||||||
import { BrowserComponentState } from "./browserComponentState";
|
import { BrowserComponentState } from "./browserComponentState";
|
||||||
|
|
||||||
export class BrowserSendComponentState extends BrowserComponentState {
|
export class BrowserSendComponentState extends BrowserComponentState {
|
||||||
sends: SendView[];
|
sends: SendView[];
|
||||||
typeCounts: Map<SendType, number>;
|
typeCounts: Map<SendType, number>;
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return Utils.merge(this, {
|
||||||
|
typeCounts: Utils.mapToRecord(this.typeCounts),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJSON(json: DeepJsonify<BrowserSendComponentState>) {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign(new BrowserSendComponentState(), json, {
|
||||||
|
sends: json.sends?.map((s) => SendView.fromJSON(s)),
|
||||||
|
typeCounts: Utils.recordToMap(json.typeCounts),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -165,12 +165,12 @@ export class SendGroupingsComponent extends BaseSendComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async saveState() {
|
private async saveState() {
|
||||||
this.state = {
|
this.state = Object.assign(new BrowserSendComponentState(), {
|
||||||
scrollY: this.popupUtils.getContentScrollY(window),
|
scrollY: this.popupUtils.getContentScrollY(window),
|
||||||
searchText: this.searchText,
|
searchText: this.searchText,
|
||||||
sends: this.sends,
|
sends: this.sends,
|
||||||
typeCounts: this.typeCounts,
|
typeCounts: this.typeCounts,
|
||||||
};
|
});
|
||||||
await this.stateService.setBrowserSendComponentState(this.state);
|
await this.stateService.setBrowserSendComponentState(this.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -373,7 +373,7 @@ export class VaultFilterComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async saveState() {
|
private async saveState() {
|
||||||
this.state = {
|
this.state = Object.assign(new BrowserGroupingsComponentState(), {
|
||||||
scrollY: this.popupUtils.getContentScrollY(window),
|
scrollY: this.popupUtils.getContentScrollY(window),
|
||||||
searchText: this.searchText,
|
searchText: this.searchText,
|
||||||
favoriteCiphers: this.favoriteCiphers,
|
favoriteCiphers: this.favoriteCiphers,
|
||||||
@@ -385,7 +385,7 @@ export class VaultFilterComponent implements OnInit, OnDestroy {
|
|||||||
folders: this.folders,
|
folders: this.folders,
|
||||||
collections: this.collections,
|
collections: this.collections,
|
||||||
deletedCount: this.deletedCount,
|
deletedCount: this.deletedCount,
|
||||||
};
|
});
|
||||||
await this.browserStateService.setBrowserGroupingComponentState(this.state);
|
await this.browserStateService.setBrowserGroupingComponentState(this.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { DeepJsonify } from "../../types/deep-jsonify";
|
||||||
import { SendFile } from "../domain/send-file";
|
import { SendFile } from "../domain/send-file";
|
||||||
|
|
||||||
import { View } from "./view";
|
import { View } from "./view";
|
||||||
@@ -28,4 +29,12 @@ export class SendFileView implements View {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static fromJSON(json: DeepJsonify<SendFileView>) {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign(new SendFileView(), json);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { DeepJsonify } from "../../types/deep-jsonify";
|
||||||
import { SendText } from "../domain/send-text";
|
import { SendText } from "../domain/send-text";
|
||||||
|
|
||||||
import { View } from "./view";
|
import { View } from "./view";
|
||||||
@@ -17,4 +18,12 @@ export class SendTextView implements View {
|
|||||||
get maskedText(): string {
|
get maskedText(): string {
|
||||||
return this.text != null ? "••••••••" : null;
|
return this.text != null ? "••••••••" : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static fromJSON(json: DeepJsonify<SendTextView>) {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign(new SendTextView(), json);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { SendType } from "../../enums/sendType";
|
import { SendType } from "../../enums/sendType";
|
||||||
import { Utils } from "../../misc/utils";
|
import { Utils } from "../../misc/utils";
|
||||||
|
import { DeepJsonify } from "../../types/deep-jsonify";
|
||||||
import { Send } from "../domain/send";
|
import { Send } from "../domain/send";
|
||||||
import { SymmetricCryptoKey } from "../domain/symmetric-crypto-key";
|
import { SymmetricCryptoKey } from "../domain/symmetric-crypto-key";
|
||||||
|
|
||||||
@@ -65,4 +66,26 @@ export class SendView implements View {
|
|||||||
get pendingDelete(): boolean {
|
get pendingDelete(): boolean {
|
||||||
return this.deletionDate <= new Date();
|
return this.deletionDate <= new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return Utils.merge(this, {
|
||||||
|
key: Utils.fromBufferToB64(this.key),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJSON(json: DeepJsonify<SendView>) {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign(new SendView(), json, {
|
||||||
|
key: Utils.fromB64ToArray(json.key)?.buffer,
|
||||||
|
cryptoKey: SymmetricCryptoKey.fromJSON(json.cryptoKey),
|
||||||
|
text: SendTextView.fromJSON(json.text),
|
||||||
|
file: SendFileView.fromJSON(json.file),
|
||||||
|
revisionDate: json.revisionDate == null ? null : new Date(json.revisionDate),
|
||||||
|
deletionDate: json.deletionDate == null ? null : new Date(json.deletionDate),
|
||||||
|
expirationDate: json.expirationDate == null ? null : new Date(json.expirationDate),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user