2018-02-19 19:07:19 +01:00
|
|
|
import { FolderData } from '../models/data/folderData';
|
2018-01-10 04:39:43 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { Folder } from '../models/domain/folder';
|
2018-07-17 23:22:45 +02:00
|
|
|
import { SymmetricCryptoKey } from '../models/domain/symmetricCryptoKey';
|
2018-01-10 04:39:43 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { FolderRequest } from '../models/request/folderRequest';
|
2018-01-10 04:39:43 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { FolderResponse } from '../models/response/folderResponse';
|
2018-01-10 04:39:43 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { FolderView } from '../models/view/folderView';
|
2018-01-25 20:57:42 +01:00
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
import { ApiService } from '../abstractions/api.service';
|
2018-06-25 20:54:47 +02:00
|
|
|
import { CipherService } from '../abstractions/cipher.service';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CryptoService } from '../abstractions/crypto.service';
|
|
|
|
import { FolderService as FolderServiceAbstraction } from '../abstractions/folder.service';
|
|
|
|
import { I18nService } from '../abstractions/i18n.service';
|
|
|
|
import { StorageService } from '../abstractions/storage.service';
|
|
|
|
import { UserService } from '../abstractions/user.service';
|
2018-06-25 20:54:47 +02:00
|
|
|
import { CipherData } from '../models/data/cipherData';
|
2018-01-10 04:39:43 +01:00
|
|
|
|
2018-07-06 21:00:55 +02:00
|
|
|
import { Utils } from '../misc/utils';
|
|
|
|
|
2018-01-10 04:39:43 +01:00
|
|
|
const Keys = {
|
|
|
|
foldersPrefix: 'folders_',
|
2018-06-25 20:54:47 +02:00
|
|
|
ciphersPrefix: 'ciphers_',
|
2018-01-10 04:39:43 +01:00
|
|
|
};
|
|
|
|
|
2018-01-23 23:29:15 +01:00
|
|
|
export class FolderService implements FolderServiceAbstraction {
|
2018-01-25 20:57:42 +01:00
|
|
|
decryptedFolderCache: FolderView[];
|
2018-01-10 04:39:43 +01:00
|
|
|
|
|
|
|
constructor(private cryptoService: CryptoService, private userService: UserService,
|
2018-06-25 20:54:47 +02:00
|
|
|
private apiService: ApiService, private storageService: StorageService,
|
|
|
|
private i18nService: I18nService, private cipherService: CipherService) { }
|
2018-01-10 04:39:43 +01:00
|
|
|
|
|
|
|
clearCache(): void {
|
|
|
|
this.decryptedFolderCache = null;
|
|
|
|
}
|
|
|
|
|
2018-07-17 23:22:45 +02:00
|
|
|
async encrypt(model: FolderView, key?: SymmetricCryptoKey): Promise<Folder> {
|
2018-01-10 04:39:43 +01:00
|
|
|
const folder = new Folder();
|
|
|
|
folder.id = model.id;
|
2018-07-17 23:22:45 +02:00
|
|
|
folder.name = await this.cryptoService.encrypt(model.name, key);
|
2018-01-10 04:39:43 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-25 20:57:42 +01:00
|
|
|
async getAllDecrypted(): Promise<FolderView[]> {
|
2018-01-10 04:39:43 +01:00
|
|
|
if (this.decryptedFolderCache != null) {
|
|
|
|
return this.decryptedFolderCache;
|
|
|
|
}
|
|
|
|
|
2018-06-13 23:14:26 +02:00
|
|
|
const hasKey = await this.cryptoService.hasKey();
|
|
|
|
if (!hasKey) {
|
2018-01-10 04:39:43 +01:00
|
|
|
throw new Error('No key.');
|
|
|
|
}
|
|
|
|
|
2018-02-28 04:54:38 +01:00
|
|
|
const decFolders: FolderView[] = [];
|
2018-01-10 04:39:43 +01:00
|
|
|
const promises: Array<Promise<any>> = [];
|
|
|
|
const folders = await this.getAll();
|
|
|
|
folders.forEach((folder) => {
|
2018-01-25 20:57:42 +01:00
|
|
|
promises.push(folder.decrypt().then((f) => decFolders.push(f)));
|
2018-01-10 04:39:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
2018-07-06 21:00:55 +02:00
|
|
|
decFolders.sort(Utils.getSortFunction(this.i18nService, 'name'));
|
2018-02-28 04:54:38 +01:00
|
|
|
|
|
|
|
const noneFolder = new FolderView();
|
2018-06-25 20:54:47 +02:00
|
|
|
noneFolder.name = this.i18nService.t('noneFolder');
|
2018-02-28 04:54:38 +01:00
|
|
|
decFolders.push(noneFolder);
|
|
|
|
|
2018-01-10 04:39:43 +01:00
|
|
|
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;
|
2018-06-25 20:54:47 +02:00
|
|
|
|
|
|
|
// Items in a deleted folder are re-assigned to "No Folder"
|
|
|
|
const ciphers = await this.storageService.get<{ [id: string]: CipherData; }>(Keys.ciphersPrefix + userId);
|
|
|
|
if (ciphers != null) {
|
|
|
|
const updates: CipherData[] = [];
|
|
|
|
for (const cId in ciphers) {
|
|
|
|
if (ciphers[cId].folderId === id) {
|
|
|
|
ciphers[cId].folderId = null;
|
|
|
|
updates.push(ciphers[cId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updates.length > 0) {
|
|
|
|
this.cipherService.upsert(updates);
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 04:39:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async deleteWithServer(id: string): Promise<any> {
|
|
|
|
await this.apiService.deleteFolder(id);
|
|
|
|
await this.delete(id);
|
|
|
|
}
|
|
|
|
}
|