move folder service to jslib

This commit is contained in:
Kyle Spearrin 2018-01-09 22:39:38 -05:00
parent 893c41d7cf
commit 6baae1113b
5 changed files with 17 additions and 174 deletions

View File

@ -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;

View File

@ -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<T>(service: string) {
@ -15,10 +21,10 @@ function getBackgroundService<T>(service: string) {
export const storageService = getBackgroundService<StorageService>('storageService');
export const tokenService = getBackgroundService<TokenService>('tokenService');
export const cryptoService = getBackgroundService<any>('cryptoService');
export const userService = getBackgroundService<any>('userService');
export const cryptoService = getBackgroundService<CryptoService>('cryptoService');
export const userService = getBackgroundService<UserService>('userService');
export const apiService = getBackgroundService<ApiService>('apiService');
export const folderService = getBackgroundService<any>('folderService');
export const folderService = getBackgroundService<FolderService>('folderService');
export const cipherService = getBackgroundService<CryptoService>('cipherService');
export const syncService = getBackgroundService<any>('syncService');
export const autofillService = getBackgroundService<any>('autofillService');
@ -27,9 +33,9 @@ export const platformUtilsService = getBackgroundService<PlatformUtilsService>('
export const utilsService = getBackgroundService<UtilsService>('utilsService');
export const appIdService = getBackgroundService<AppIdService>('appIdService');
export const i18nService = getBackgroundService<any>('i18nService');
export const constantsService = getBackgroundService<any>('constantsService');
export const constantsService = getBackgroundService<ConstantsService>('constantsService');
export const settingsService = getBackgroundService<any>('settingsService');
export const lockService = getBackgroundService<any>('lockService');
export const totpService = getBackgroundService<any>('totpService');
export const environmentService = getBackgroundService<any>('environmentService');
export const totpService = getBackgroundService<TotpService>('totpService');
export const environmentService = getBackgroundService<EnvironmentService>('environmentService');
export const collectionService = getBackgroundService<any>('collectionService');

View File

@ -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<Folder> {
const folder = new Folder();
folder.id = model.id;
folder.name = await this.cryptoService.encrypt(model.name);
return folder;
}
async get(id: string): Promise<Folder> {
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<Folder[]> {
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<any[]> {
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<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.decryptedFolderCache = decFolders;
return this.decryptedFolderCache;
}
async saveWithServer(folder: Folder): Promise<any> {
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<any> {
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<any> {
const userId = await this.userService.getUserId();
await this.storageService.save(Keys.foldersPrefix + userId, folders);
this.decryptedFolderCache = null;
}
async clear(userId: string): Promise<any> {
await this.storageService.remove(Keys.foldersPrefix + userId);
this.decryptedFolderCache = null;
}
async delete(id: string | string[]): Promise<any> {
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<any> {
await this.apiService.deleteFolder(id);
await this.delete(id);
}
}

View File

@ -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';

View File

@ -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,