From 5c98e941986b0d123d6f38fdc0bfe13e6a708ece Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 9 Jan 2018 23:12:14 -0500 Subject: [PATCH] move collection service tio jslib --- src/background/main.background.ts | 5 +- src/popup/app/services/background.service.ts | 6 +- src/services/collection.service.ts | 126 ------------------- src/services/lock.service.ts | 3 +- src/services/sync.service.ts | 3 +- 5 files changed, 9 insertions(+), 134 deletions(-) delete mode 100644 src/services/collection.service.ts diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 863f4e2f37..9a9659d6ee 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -4,6 +4,7 @@ import { ApiService, AppIdService, CipherService, + CollectionService, ConstantsService, ContainerService, CryptoService, @@ -21,6 +22,7 @@ import { ApiService as ApiServiceAbstraction, AppIdService as AppIdServiceAbstraction, CipherService as CipherServiceAbstraction, + CollectionService as CollectionServiceAbstraction, CryptoService as CryptoServiceAbstraction, EnvironmentService as EnvironmentServiceAbstraction, FolderService as FolderServiceAbstraction, @@ -49,7 +51,6 @@ import AutofillService from '../services/autofill.service'; import BrowserMessagingService from '../services/browserMessaging.service'; import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service'; import BrowserStorageService from '../services/browserStorage.service'; -import CollectionService from '../services/collection.service'; import i18nService from '../services/i18n.service'; import LockService from '../services/lock.service'; import SyncService from '../services/sync.service'; @@ -70,7 +71,7 @@ export default class MainBackground { settingsService: SettingsServiceAbstraction; cipherService: CipherServiceAbstraction; folderService: FolderServiceAbstraction; - collectionService: CollectionService; + collectionService: CollectionServiceAbstraction; lockService: LockService; syncService: SyncService; passwordGenerationService: PasswordGenerationServiceAbstraction; diff --git a/src/popup/app/services/background.service.ts b/src/popup/app/services/background.service.ts index ce3fc0e59b..8b330a3034 100644 --- a/src/popup/app/services/background.service.ts +++ b/src/popup/app/services/background.service.ts @@ -3,9 +3,11 @@ import { ConstantsService } from 'jslib/services/constants.service'; import { ApiService } from 'jslib/abstractions/api.service'; import { AppIdService } from 'jslib/abstractions/appId.service'; import { CipherService } from 'jslib/abstractions/cipher.service'; +import { CollectionService } from 'jslib/abstractions/collection.service'; import { CryptoService } from 'jslib/abstractions/crypto.service'; import { EnvironmentService } from 'jslib/abstractions/environment.service'; import { FolderService } from 'jslib/abstractions/folder.service'; +import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service'; import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; import { SettingsService } from 'jslib/abstractions/settings.service'; import { StorageService } from 'jslib/abstractions/storage.service'; @@ -30,7 +32,7 @@ export const folderService = getBackgroundService('folderService' export const cipherService = getBackgroundService('cipherService'); export const syncService = getBackgroundService('syncService'); export const autofillService = getBackgroundService('autofillService'); -export const passwordGenerationService = getBackgroundService('passwordGenerationService'); +export const passwordGenerationService = getBackgroundService('passwordGenerationService'); export const platformUtilsService = getBackgroundService('platformUtilsService'); export const utilsService = getBackgroundService('utilsService'); export const appIdService = getBackgroundService('appIdService'); @@ -40,4 +42,4 @@ export const settingsService = getBackgroundService('settingsSe export const lockService = getBackgroundService('lockService'); export const totpService = getBackgroundService('totpService'); export const environmentService = getBackgroundService('environmentService'); -export const collectionService = getBackgroundService('collectionService'); +export const collectionService = getBackgroundService('collectionService'); diff --git a/src/services/collection.service.ts b/src/services/collection.service.ts deleted file mode 100644 index 76bd128969..0000000000 --- a/src/services/collection.service.ts +++ /dev/null @@ -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 { - 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 { - 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 { - 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> = []; - 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 { - 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 { - const userId = await this.userService.getUserId(); - await this.storageService.save(Keys.collectionsPrefix + userId, collections); - this.decryptedCollectionCache = null; - } - - async clear(userId: string): Promise { - await this.storageService.remove(Keys.collectionsPrefix + userId); - this.decryptedCollectionCache = null; - } - - async delete(id: string | string[]): Promise { - 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; - } -} diff --git a/src/services/lock.service.ts b/src/services/lock.service.ts index a7487f1ec9..59fef582f8 100644 --- a/src/services/lock.service.ts +++ b/src/services/lock.service.ts @@ -1,11 +1,10 @@ -import CollectionService from './collection.service'; - import { ConstantsService, } from 'jslib/services'; import { CipherService, + CollectionService, CryptoService, FolderService, PlatformUtilsService, diff --git a/src/services/sync.service.ts b/src/services/sync.service.ts index e533cd2a15..914bb6299e 100644 --- a/src/services/sync.service.ts +++ b/src/services/sync.service.ts @@ -1,8 +1,7 @@ -import CollectionService from './collection.service'; - import { ApiService, CipherService, + CollectionService, CryptoService, FolderService, MessagingService,