mirror of
https://github.com/bitwarden/browser
synced 2025-12-21 18:53:29 +00:00
move collection service tio jslib
This commit is contained in:
@@ -1,126 +0,0 @@
|
||||
import {
|
||||
CryptoService,
|
||||
StorageService,
|
||||
UserService,
|
||||
} from 'jslib/abstractions';
|
||||
|
||||
import { CollectionData } from 'jslib/models/data';
|
||||
|
||||
import { Collection } from 'jslib/models/domain';
|
||||
|
||||
const Keys = {
|
||||
collectionsPrefix: 'collections_',
|
||||
};
|
||||
|
||||
export default class CollectionService {
|
||||
decryptedCollectionCache: any[];
|
||||
|
||||
constructor(private cryptoService: CryptoService, private userService: UserService,
|
||||
private storageService: StorageService) {
|
||||
}
|
||||
|
||||
clearCache(): void {
|
||||
this.decryptedCollectionCache = null;
|
||||
}
|
||||
|
||||
async get(id: string): Promise<Collection> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const collections = await this.storageService.get<{ [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 this.storageService.get<{ [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 this.storageService.get<{ [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 this.storageService.save(Keys.collectionsPrefix + userId, collections);
|
||||
this.decryptedCollectionCache = null;
|
||||
}
|
||||
|
||||
async replace(collections: { [id: string]: CollectionData; }): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
await this.storageService.save(Keys.collectionsPrefix + userId, collections);
|
||||
this.decryptedCollectionCache = null;
|
||||
}
|
||||
|
||||
async clear(userId: string): Promise<any> {
|
||||
await this.storageService.remove(Keys.collectionsPrefix + userId);
|
||||
this.decryptedCollectionCache = null;
|
||||
}
|
||||
|
||||
async delete(id: string | string[]): Promise<any> {
|
||||
const userId = await this.userService.getUserId();
|
||||
const collections = await this.storageService.get<{ [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 this.storageService.save(Keys.collectionsPrefix + userId, collections);
|
||||
this.decryptedCollectionCache = null;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
import CollectionService from './collection.service';
|
||||
|
||||
import {
|
||||
ConstantsService,
|
||||
} from 'jslib/services';
|
||||
|
||||
import {
|
||||
CipherService,
|
||||
CollectionService,
|
||||
CryptoService,
|
||||
FolderService,
|
||||
PlatformUtilsService,
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import CollectionService from './collection.service';
|
||||
|
||||
import {
|
||||
ApiService,
|
||||
CipherService,
|
||||
CollectionService,
|
||||
CryptoService,
|
||||
FolderService,
|
||||
MessagingService,
|
||||
|
||||
Reference in New Issue
Block a user