1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-22 11:13:46 +00:00

add support for syncing collections

This commit is contained in:
Kyle Spearrin
2017-11-22 13:17:59 -05:00
parent 04cfd8040c
commit 2d81466d25
11 changed files with 231 additions and 3 deletions

View File

@@ -0,0 +1,126 @@
import { CipherString } from '../models/domain/cipherString';
import { Collection } from '../models/domain/collection';
import { CollectionData } from '../models/data/collectionData';
import ApiService from './api.service';
import CryptoService from './crypto.service';
import UserService from './user.service';
import UtilsService from './utils.service';
const Keys = {
collectionsPrefix: 'collections_',
};
export default class CollectionService {
decryptedCollectionCache: any[];
constructor(private cryptoService: CryptoService, private userService: UserService,
private apiService: ApiService) {
}
clearCache(): void {
this.decryptedCollectionCache = null;
}
async get(id: string): Promise<Collection> {
const userId = await this.userService.getUserId();
const collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>(
Keys.collectionsPrefix + userId);
if (collections == null || !collections.hasOwnProperty(id)) {
return null;
}
return new Collection(collections[id]);
}
async getAll(): Promise<Collection[]> {
const userId = await this.userService.getUserId();
const collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>(
Keys.collectionsPrefix + userId);
const response: Collection[] = [];
for (const id in collections) {
if (collections.hasOwnProperty(id)) {
response.push(new Collection(collections[id]));
}
}
return response;
}
async getAllDecrypted(): Promise<any[]> {
if (this.decryptedCollectionCache != null) {
return this.decryptedCollectionCache;
}
const key = await this.cryptoService.getKey();
if (key == null) {
throw new Error('No key.');
}
const decFolders: any[] = [];
const promises: Array<Promise<any>> = [];
const folders = await this.getAll();
folders.forEach((folder) => {
promises.push(folder.decrypt().then((f: any) => {
decFolders.push(f);
}));
});
await Promise.all(promises);
this.decryptedCollectionCache = decFolders;
return this.decryptedCollectionCache;
}
async upsert(collection: CollectionData | CollectionData[]): Promise<any> {
const userId = await this.userService.getUserId();
let collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>(
Keys.collectionsPrefix + userId);
if (collections == null) {
collections = {};
}
if (collection instanceof CollectionData) {
const c = collection as CollectionData;
collections[c.id] = c;
} else {
(collection as CollectionData[]).forEach((c) => {
collections[c.id] = c;
});
}
await UtilsService.saveObjToStorage(Keys.collectionsPrefix + userId, collections);
this.decryptedCollectionCache = null;
}
async replace(collections: { [id: string]: CollectionData; }): Promise<any> {
const userId = await this.userService.getUserId();
await UtilsService.saveObjToStorage(Keys.collectionsPrefix + userId, collections);
this.decryptedCollectionCache = null;
}
async clear(userId: string): Promise<any> {
await UtilsService.removeFromStorage(Keys.collectionsPrefix + userId);
this.decryptedCollectionCache = null;
}
async delete(id: string | string[]): Promise<any> {
const userId = await this.userService.getUserId();
const collections = await UtilsService.getObjFromStorage<{ [id: string]: CollectionData; }>(
Keys.collectionsPrefix + userId);
if (collections == null) {
return;
}
if (typeof id === 'string') {
const i = id as string;
delete collections[id];
} else {
(id as string[]).forEach((i) => {
delete collections[i];
});
}
await UtilsService.saveObjToStorage(Keys.collectionsPrefix + userId, collections);
this.decryptedCollectionCache = null;
}
}

View File

@@ -7,7 +7,6 @@ import { FolderRequest } from '../models/request/folderRequest';
import { FolderResponse } from '../models/response/folderResponse';
import ApiService from './api.service';
import ConstantsService from './constants.service';
import CryptoService from './crypto.service';
import UserService from './user.service';
import UtilsService from './utils.service';

View File

@@ -1,7 +1,9 @@
import { CipherData } from '../models/data/cipherData';
import { CollectionData } from '../models/data/collectionData';
import { FolderData } from '../models/data/folderData';
import { CipherResponse } from '../models/response/cipherResponse';
import { CollectionResponse } from '../models/response/collectionResponse';
import { DomainsResponse } from '../models/response/domainsResponse';
import { FolderResponse } from '../models/response/folderResponse';
import { ProfileResponse } from '../models/response/profileResponse';
@@ -9,6 +11,7 @@ import { SyncResponse } from '../models/response/syncResponse';
import ApiService from './api.service';
import CipherService from './cipher.service';
import CollectionService from './collection.service';
import CryptoService from './crypto.service';
import FolderService from './folder.service';
import SettingsService from './settings.service';
@@ -25,7 +28,7 @@ export default class SyncService {
constructor(private userService: UserService, private apiService: ApiService,
private settingsService: SettingsService, private folderService: FolderService,
private cipherService: CipherService, private cryptoService: CryptoService,
private logoutCallback: Function) {
private collectionService: CollectionService, private logoutCallback: Function) {
}
async getLastSync() {
@@ -84,6 +87,7 @@ export default class SyncService {
await this.syncProfile(response.profile);
await this.syncFolders(userId, response.folders);
await this.syncCollections(response.collections);
await this.syncCiphers(userId, response.ciphers);
await this.syncSettings(userId, response.domains);
@@ -141,6 +145,14 @@ export default class SyncService {
return await this.folderService.replace(folders);
}
private async syncCollections(response: CollectionResponse[]) {
const collections: { [id: string]: CollectionData; } = {};
response.forEach((c) => {
collections[c.id] = new CollectionData(c);
});
return await this.collectionService.replace(collections);
}
private async syncCiphers(userId: string, response: CipherResponse[]) {
const ciphers: { [id: string]: CipherData; } = {};
response.forEach((c) => {