2016-09-21 21:21:50 +02:00
|
|
|
function FolderService(cryptoService, userService, apiService) {
|
2016-09-04 06:34:24 +02:00
|
|
|
this.cryptoService = cryptoService;
|
|
|
|
this.userService = userService;
|
|
|
|
this.apiService = apiService;
|
2016-09-16 03:09:56 +02:00
|
|
|
this.decryptedFolderCache = null;
|
2016-09-04 06:34:24 +02:00
|
|
|
|
|
|
|
initFolderService();
|
|
|
|
};
|
|
|
|
|
|
|
|
function initFolderService() {
|
2016-09-16 03:09:56 +02:00
|
|
|
FolderService.prototype.encrypt = function (folder) {
|
|
|
|
var model = {
|
|
|
|
id: folder.id
|
|
|
|
};
|
|
|
|
|
2016-09-17 05:47:50 +02:00
|
|
|
return cryptoService.encrypt(folder.name).then(function (cs) {
|
2016-09-16 03:09:56 +02:00
|
|
|
model.name = cs;
|
2016-09-17 05:47:50 +02:00
|
|
|
return model;
|
2016-09-16 03:09:56 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-05 07:49:44 +02:00
|
|
|
FolderService.prototype.get = function (id, callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
2016-09-04 06:34:24 +02:00
|
|
|
|
2016-09-05 07:49:44 +02:00
|
|
|
this.userService.getUserId(function (userId) {
|
|
|
|
var foldersKey = 'folders_' + userId;
|
2016-09-04 06:34:24 +02:00
|
|
|
|
|
|
|
chrome.storage.local.get(foldersKey, function (obj) {
|
|
|
|
var folders = obj[foldersKey];
|
2016-09-21 21:21:50 +02:00
|
|
|
if (folders && id in folders) {
|
2016-09-04 06:34:24 +02:00
|
|
|
callback(new Folder(folders[id]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
});
|
2016-09-05 07:49:44 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
FolderService.prototype.getAll = function (callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
2016-09-04 06:34:24 +02:00
|
|
|
|
2016-09-05 07:49:44 +02:00
|
|
|
this.userService.getUserId(function (userId) {
|
|
|
|
var foldersKey = 'folders_' + userId;
|
2016-09-04 06:34:24 +02:00
|
|
|
|
|
|
|
chrome.storage.local.get(foldersKey, function (obj) {
|
|
|
|
var folders = obj[foldersKey];
|
|
|
|
var response = [];
|
|
|
|
for (var id in folders) {
|
|
|
|
var folder = folders[id];
|
|
|
|
response.push(new Folder(folder));
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(response);
|
|
|
|
});
|
2016-09-05 07:49:44 +02:00
|
|
|
});
|
|
|
|
};
|
2016-09-04 06:34:24 +02:00
|
|
|
|
2016-09-16 03:09:56 +02:00
|
|
|
FolderService.prototype.getAllDecrypted = function () {
|
|
|
|
var deferred = Q.defer();
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
if (self.decryptedFolderCache) {
|
|
|
|
deferred.resolve(self.decryptedFolderCache);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
var promises = [];
|
2016-09-21 06:43:29 +02:00
|
|
|
var decFolders = [{
|
|
|
|
id: null,
|
|
|
|
name: '(none)'
|
|
|
|
}];
|
2016-09-16 03:09:56 +02:00
|
|
|
self.getAll(function (folders) {
|
|
|
|
for (var i = 0; i < folders.length; i++) {
|
|
|
|
promises.push(folders[i].decrypt().then(function (folder) {
|
|
|
|
decFolders.push(folder);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
Q.all(promises).then(function () {
|
2016-09-22 23:00:56 +02:00
|
|
|
if (decFolders.length > 0) {
|
|
|
|
self.decryptedFolderCache = decFolders;
|
|
|
|
}
|
|
|
|
deferred.resolve(decFolders);
|
2016-09-16 03:09:56 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2016-09-17 05:47:50 +02:00
|
|
|
FolderService.prototype.saveWithServer = function (folder) {
|
|
|
|
var deferred = Q.defer();
|
2016-09-07 02:41:17 +02:00
|
|
|
|
|
|
|
var self = this,
|
|
|
|
request = new FolderRequest(folder);
|
|
|
|
|
|
|
|
if (!folder.id) {
|
2016-09-21 21:21:50 +02:00
|
|
|
self.apiService.postFolder(request, apiSuccess, function (response) {
|
|
|
|
handleError(response, deferred)
|
|
|
|
});
|
2016-09-07 02:41:17 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-09-21 21:21:50 +02:00
|
|
|
self.apiService.putFolder(folder.id, request, apiSuccess, function (response) {
|
|
|
|
handleError(response, deferred)
|
|
|
|
});
|
2016-09-07 02:41:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function apiSuccess(response) {
|
|
|
|
folder.id = response.id;
|
|
|
|
userService.getUserId(function (userId) {
|
|
|
|
var data = new FolderData(response, userId);
|
|
|
|
self.upsert(data, function () {
|
2016-09-17 05:47:50 +02:00
|
|
|
deferred.resolve(folder);
|
2016-09-07 02:41:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-09-17 05:47:50 +02:00
|
|
|
|
|
|
|
return deferred.promise;
|
2016-09-07 02:41:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
FolderService.prototype.upsert = function (folder, callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2016-09-17 05:52:50 +02:00
|
|
|
var self = this;
|
|
|
|
|
2016-09-07 02:41:17 +02:00
|
|
|
userService.getUserId(function (userId) {
|
|
|
|
var foldersKey = 'folders_' + userId;
|
|
|
|
|
|
|
|
chrome.storage.local.get(foldersKey, function (obj) {
|
|
|
|
var folders = obj[foldersKey];
|
|
|
|
if (!folders) {
|
|
|
|
folders = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (folder.constructor === Array) {
|
|
|
|
for (var i = 0; i < folder.length; i++) {
|
|
|
|
folders[folder[i].id] = folder[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
folders[folder.id] = folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj[foldersKey] = folders;
|
|
|
|
|
|
|
|
chrome.storage.local.set(obj, function () {
|
2016-09-17 05:52:50 +02:00
|
|
|
self.decryptedFolderCache = null;
|
2016-09-07 02:41:17 +02:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
FolderService.prototype.replace = function (folders, callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2016-09-17 05:52:50 +02:00
|
|
|
var self = this;
|
|
|
|
|
2016-09-07 02:41:17 +02:00
|
|
|
userService.getUserId(function (userId) {
|
|
|
|
var obj = {};
|
|
|
|
obj['folders_' + userId] = folders;
|
|
|
|
chrome.storage.local.set(obj, function () {
|
2016-09-17 05:52:50 +02:00
|
|
|
self.decryptedFolderCache = null;
|
2016-09-07 02:41:17 +02:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-21 06:04:59 +02:00
|
|
|
FolderService.prototype.clear = function (userId, callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2016-09-22 23:00:56 +02:00
|
|
|
var self = this;
|
|
|
|
|
2016-09-21 06:04:59 +02:00
|
|
|
chrome.storage.local.remove('folders_' + userId, function () {
|
|
|
|
self.decryptedFolderCache = null;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-07 02:41:17 +02:00
|
|
|
FolderService.prototype.delete = function (id, callback) {
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
throw 'callback function required';
|
|
|
|
}
|
|
|
|
|
2016-09-17 05:52:50 +02:00
|
|
|
var self = this;
|
|
|
|
|
2016-09-07 02:41:17 +02:00
|
|
|
userService.getUserId(function (userId) {
|
|
|
|
var foldersKey = 'folders_' + userId;
|
|
|
|
|
|
|
|
chrome.storage.local.get(foldersKey, function (obj) {
|
|
|
|
var folders = obj[foldersKey];
|
|
|
|
if (!folders) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id.constructor === Array) {
|
|
|
|
for (var i = 0; i < id.length; i++) {
|
|
|
|
if (id[i] in folders) {
|
|
|
|
delete folders[id[i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (id in folders) {
|
|
|
|
delete folders[id];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj[foldersKey] = folders;
|
|
|
|
chrome.storage.local.set(obj, function () {
|
2016-09-17 05:52:50 +02:00
|
|
|
self.decryptedFolderCache = null;
|
2016-09-07 02:41:17 +02:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2016-09-21 21:21:50 +02:00
|
|
|
|
2016-09-22 20:16:24 +02:00
|
|
|
FolderService.prototype.deleteWithServer = function (id) {
|
|
|
|
var deferred = Q.defer();
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
self.apiService.deleteCipher(id, function () {
|
|
|
|
self.delete(id, function () {
|
|
|
|
deferred.resolve();
|
|
|
|
});
|
|
|
|
}, function (response) {
|
|
|
|
handleError(response, deferred)
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
|
2016-09-21 21:21:50 +02:00
|
|
|
function handleError(error, deferred) {
|
|
|
|
deferred.reject(error);
|
|
|
|
}
|
2016-09-04 06:34:24 +02:00
|
|
|
};
|