From 6baae1113b67619a64a28b4e8d06f93f95882dba Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 9 Jan 2018 22:39:38 -0500 Subject: [PATCH] move folder service to jslib --- src/background/main.background.ts | 5 +- src/popup/app/services/background.service.ts | 18 +- src/services/folder.service.ts | 164 ------------------- src/services/lock.service.ts | 2 +- src/services/sync.service.ts | 2 +- 5 files changed, 17 insertions(+), 174 deletions(-) delete mode 100644 src/services/folder.service.ts diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 2ff99f41b7..8eef849d6a 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -7,6 +7,7 @@ import { ContainerService, CryptoService, EnvironmentService, + FolderService, PasswordGenerationService, TokenService, TotpService, @@ -19,6 +20,7 @@ import { AppIdService as AppIdServiceAbstraction, CryptoService as CryptoServiceAbstraction, EnvironmentService as EnvironmentServiceAbstraction, + FolderService as FolderServiceAbstraction, MessagingService as MessagingServiceAbstraction, PasswordGenerationService as PasswordGenerationServiceAbstraction, PlatformUtilsService as PlatformUtilsServiceAbstraction, @@ -45,7 +47,6 @@ import BrowserPlatformUtilsService from '../services/browserPlatformUtils.servic import BrowserStorageService from '../services/browserStorage.service'; import CipherService from '../services/cipher.service'; import CollectionService from '../services/collection.service'; -import FolderService from '../services/folder.service'; import i18nService from '../services/i18n.service'; import LockService from '../services/lock.service'; import SettingsService from '../services/settings.service'; @@ -66,7 +67,7 @@ export default class MainBackground { userService: UserServiceAbstraction; settingsService: SettingsService; cipherService: CipherService; - folderService: FolderService; + folderService: FolderServiceAbstraction; collectionService: CollectionService; lockService: LockService; syncService: SyncService; diff --git a/src/popup/app/services/background.service.ts b/src/popup/app/services/background.service.ts index 7d0755b490..0f2f9de3ef 100644 --- a/src/popup/app/services/background.service.ts +++ b/src/popup/app/services/background.service.ts @@ -1,9 +1,15 @@ +import { ConstantsService } from 'jslib/services/constants.service'; + import { ApiService } from 'jslib/abstractions/api.service'; import { AppIdService } from 'jslib/abstractions/appId.service'; import { CryptoService } from 'jslib/abstractions/crypto.service'; +import { EnvironmentService } from 'jslib/abstractions/environment.service'; +import { FolderService } from 'jslib/abstractions/folder.service'; import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; import { StorageService } from 'jslib/abstractions/storage.service'; import { TokenService } from 'jslib/abstractions/token.service'; +import { TotpService } from 'jslib/abstractions/totp.service'; +import { UserService } from 'jslib/abstractions/user.service'; import { UtilsService } from 'jslib/abstractions/utils.service'; function getBackgroundService(service: string) { @@ -15,10 +21,10 @@ function getBackgroundService(service: string) { export const storageService = getBackgroundService('storageService'); export const tokenService = getBackgroundService('tokenService'); -export const cryptoService = getBackgroundService('cryptoService'); -export const userService = getBackgroundService('userService'); +export const cryptoService = getBackgroundService('cryptoService'); +export const userService = getBackgroundService('userService'); export const apiService = getBackgroundService('apiService'); -export const folderService = getBackgroundService('folderService'); +export const folderService = getBackgroundService('folderService'); export const cipherService = getBackgroundService('cipherService'); export const syncService = getBackgroundService('syncService'); export const autofillService = getBackgroundService('autofillService'); @@ -27,9 +33,9 @@ export const platformUtilsService = getBackgroundService(' export const utilsService = getBackgroundService('utilsService'); export const appIdService = getBackgroundService('appIdService'); export const i18nService = getBackgroundService('i18nService'); -export const constantsService = getBackgroundService('constantsService'); +export const constantsService = getBackgroundService('constantsService'); export const settingsService = getBackgroundService('settingsService'); export const lockService = getBackgroundService('lockService'); -export const totpService = getBackgroundService('totpService'); -export const environmentService = getBackgroundService('environmentService'); +export const totpService = getBackgroundService('totpService'); +export const environmentService = getBackgroundService('environmentService'); export const collectionService = getBackgroundService('collectionService'); diff --git a/src/services/folder.service.ts b/src/services/folder.service.ts deleted file mode 100644 index d2eab378eb..0000000000 --- a/src/services/folder.service.ts +++ /dev/null @@ -1,164 +0,0 @@ -import { FolderData } from 'jslib/models/data'; - -import { Folder } from 'jslib/models/domain'; - -import { FolderRequest } from 'jslib/models/request'; - -import { FolderResponse } from 'jslib/models/response'; - -import { - ApiService, - CryptoService, - StorageService, - UserService, -} from 'jslib/abstractions'; - -const Keys = { - foldersPrefix: 'folders_', -}; - -export default class FolderService { - decryptedFolderCache: any[]; - - constructor(private cryptoService: CryptoService, private userService: UserService, - private i18nService: any, private apiService: ApiService, - private storageService: StorageService) { - } - - clearCache(): void { - this.decryptedFolderCache = null; - } - - async encrypt(model: any): Promise { - const folder = new Folder(); - folder.id = model.id; - folder.name = await this.cryptoService.encrypt(model.name); - return folder; - } - - async get(id: string): Promise { - const userId = await this.userService.getUserId(); - const folders = await this.storageService.get<{ [id: string]: FolderData; }>( - Keys.foldersPrefix + userId); - if (folders == null || !folders.hasOwnProperty(id)) { - return null; - } - - return new Folder(folders[id]); - } - - async getAll(): Promise { - const userId = await this.userService.getUserId(); - const folders = await this.storageService.get<{ [id: string]: FolderData; }>( - Keys.foldersPrefix + userId); - const response: Folder[] = []; - for (const id in folders) { - if (folders.hasOwnProperty(id)) { - response.push(new Folder(folders[id])); - } - } - return response; - } - - async getAllDecrypted(): Promise { - if (this.decryptedFolderCache != null) { - return this.decryptedFolderCache; - } - - const decFolders: any[] = [{ - id: null, - name: this.i18nService.noneFolder, - }]; - - const key = await this.cryptoService.getKey(); - if (key == null) { - throw new Error('No key.'); - } - - 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.decryptedFolderCache = decFolders; - return this.decryptedFolderCache; - } - - async saveWithServer(folder: Folder): Promise { - const request = new FolderRequest(folder); - - let response: FolderResponse; - if (folder.id == null) { - response = await this.apiService.postFolder(request); - folder.id = response.id; - } else { - response = await this.apiService.putFolder(folder.id, request); - } - - const userId = await this.userService.getUserId(); - const data = new FolderData(response, userId); - await this.upsert(data); - } - - async upsert(folder: FolderData | FolderData[]): Promise { - const userId = await this.userService.getUserId(); - let folders = await this.storageService.get<{ [id: string]: FolderData; }>( - Keys.foldersPrefix + userId); - if (folders == null) { - folders = {}; - } - - if (folder instanceof FolderData) { - const f = folder as FolderData; - folders[f.id] = f; - } else { - (folder as FolderData[]).forEach((f) => { - folders[f.id] = f; - }); - } - - await this.storageService.save(Keys.foldersPrefix + userId, folders); - this.decryptedFolderCache = null; - } - - async replace(folders: { [id: string]: FolderData; }): Promise { - const userId = await this.userService.getUserId(); - await this.storageService.save(Keys.foldersPrefix + userId, folders); - this.decryptedFolderCache = null; - } - - async clear(userId: string): Promise { - await this.storageService.remove(Keys.foldersPrefix + userId); - this.decryptedFolderCache = null; - } - - async delete(id: string | string[]): Promise { - const userId = await this.userService.getUserId(); - const folders = await this.storageService.get<{ [id: string]: FolderData; }>( - Keys.foldersPrefix + userId); - if (folders == null) { - return; - } - - if (typeof id === 'string') { - const i = id as string; - delete folders[id]; - } else { - (id as string[]).forEach((i) => { - delete folders[i]; - }); - } - - await this.storageService.save(Keys.foldersPrefix + userId, folders); - this.decryptedFolderCache = null; - } - - async deleteWithServer(id: string): Promise { - await this.apiService.deleteFolder(id); - await this.delete(id); - } -} diff --git a/src/services/lock.service.ts b/src/services/lock.service.ts index 5e8291c198..311a43e8f3 100644 --- a/src/services/lock.service.ts +++ b/src/services/lock.service.ts @@ -1,11 +1,11 @@ import CipherService from './cipher.service'; import CollectionService from './collection.service'; -import FolderService from './folder.service'; import { ConstantsService } from 'jslib/services'; import { CryptoService, + FolderService, PlatformUtilsService, StorageService, } from 'jslib/abstractions'; diff --git a/src/services/sync.service.ts b/src/services/sync.service.ts index 5781980c3d..7134239d6a 100644 --- a/src/services/sync.service.ts +++ b/src/services/sync.service.ts @@ -1,11 +1,11 @@ import CipherService from './cipher.service'; import CollectionService from './collection.service'; -import FolderService from './folder.service'; import SettingsService from './settings.service'; import { ApiService, CryptoService, + FolderService, MessagingService, StorageService, UserService,