1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-27 12:36:14 +01:00

refactor out callbacks from folderService

This commit is contained in:
Kyle Spearrin 2017-10-17 00:03:27 -04:00
parent ea1ef54c95
commit ba4e2eae15
7 changed files with 151 additions and 173 deletions

View File

@ -1,17 +1,17 @@
angular angular
.module('bit.settings') .module('bit.settings')
.controller('settingsEditFolderController', function ($scope, $stateParams, folderService, toastr, $q, $state, SweetAlert, .controller('settingsEditFolderController', function ($scope, $stateParams, folderService, toastr, $state, SweetAlert,
utilsService, $analytics, i18nService) { utilsService, $analytics, i18nService) {
$scope.i18n = i18nService; $scope.i18n = i18nService;
$scope.folder = {}; $scope.folder = {};
var folderId = $stateParams.folderId; var folderId = $stateParams.folderId;
$('#name').focus(); $('#name').focus();
folderService.get(folderId, function (folder) { folderService.get(folderId).then(function (folder) {
$q.when(folder.decrypt()).then(function (model) { return folder.decrypt();
$scope.folder = model; }).then(function (model) {
}); $scope.folder = model;
}); });
utilsService.initListSectionItemListeners($(document), angular); utilsService.initListSectionItemListeners($(document), angular);
@ -23,9 +23,9 @@
return; return;
} }
$scope.savePromise = $q.when(folderService.encrypt(model)).then(function (folderModel) { $scope.savePromise = folderService.encrypt(model).then(function (folderModel) {
var folder = new Folder(folderModel, true); var folder = new Folder(folderModel, true);
return $q.when(folderService.saveWithServer(folder)).then(function (folder) { return folderService.saveWithServer(folder).then(function (folder) {
$analytics.eventTrack('Edited Folder'); $analytics.eventTrack('Edited Folder');
toastr.success(i18nService.editedFolder); toastr.success(i18nService.editedFolder);
$state.go('folders', { animation: 'out-slide-down' }); $state.go('folders', { animation: 'out-slide-down' });
@ -42,7 +42,7 @@
cancelButtonText: i18nService.no cancelButtonText: i18nService.no
}, function (confirmed) { }, function (confirmed) {
if (confirmed) { if (confirmed) {
$q.when(folderService.deleteWithServer(folderId)).then(function () { folderService.deleteWithServer(folderId).then(function () {
$analytics.eventTrack('Deleted Folder'); $analytics.eventTrack('Deleted Folder');
toastr.success(i18nService.deletedFolder); toastr.success(i18nService.deletedFolder);
$state.go('folders', { $state.go('folders', {

View File

@ -7,8 +7,7 @@
load(); load();
function load() { function load() {
var foldersPromise = $q.when(folderService.getAllDecrypted()); folderService.getAllDecrypted().then(function (folders) {
foldersPromise.then(function (folders) {
if (folders.length > 0 && folders[0].id === null) { if (folders.length > 0 && folders[0].id === null) {
// remove the "none" folder // remove the "none" folder
$scope.folders = folders.slice(1); $scope.folders = folders.slice(1);

View File

@ -45,14 +45,12 @@
var decLogins = []; var decLogins = [];
var promises = []; var promises = [];
var folderPromise = $q.when(folderService.getAllDecrypted()); var folderPromise = folderService.getAllDecrypted().then(function (folders) {
folderPromise.then(function (folders) {
decFolders = folders; decFolders = folders;
}); });
promises.push(folderPromise); promises.push(folderPromise);
var loginPromise = $q.when(cipherService.getAllDecrypted()); var loginPromise = cipherService.getAllDecrypted().then(function (logins) {
loginPromise.then(function (logins) {
decLogins = logins; decLogins = logins;
}); });
promises.push(loginPromise); promises.push(loginPromise);

View File

@ -29,14 +29,12 @@
var promises = []; var promises = [];
if ($scope.folder.id) { if ($scope.folder.id) {
var folderDeferred = $q.defer(); var getPromise = folderService.get($scope.folder.id).then(function (folder) {
folderService.get($scope.folder.id, function (folder) { return folder.decrypt();
folder.decrypt().then(function (model) { }).then(function (model) {
decFolder = model; decFolder = model;
folderDeferred.resolve();
});
}); });
promises.push(folderDeferred.promise); promises.push(getPromise);
} }
var cipherPromise = cipherService.getAllDecryptedForFolder($scope.folder.id).then(function (ciphers) { var cipherPromise = cipherService.getAllDecryptedForFolder($scope.folder.id).then(function (ciphers) {

View File

@ -191,8 +191,10 @@ function initApiService() {
// Folder APIs // Folder APIs
ApiService.prototype.postFolder = function (folderRequest, success, error) { ApiService.prototype.postFolder = function (folderRequest) {
var self = this; var self = this,
deferred = Q.defer();
handleTokenState(self).then(function (tokenHeader) { handleTokenState(self).then(function (tokenHeader) {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
@ -202,19 +204,21 @@ function initApiService() {
dataType: 'json', dataType: 'json',
headers: tokenHeader, headers: tokenHeader,
success: function (response) { success: function (response) {
success(new FolderResponse(response)); deferred.resolve(new FolderResponse(response));
}, },
error: function (jqXHR, textStatus, errorThrown) { error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self); handleError(deferred.reject, jqXHR, false, self);
} }
}); });
}, function (jqXHR) { }, function (jqXHR) {
handleError(error, jqXHR, true, self); handleError(deferred.reject, jqXHR, true, self);
}); });
}; };
ApiService.prototype.putFolder = function (id, folderRequest, success, error) { ApiService.prototype.putFolder = function (id, folderRequest) {
var self = this; var self = this,
deferred = Q.defer();
handleTokenState(self).then(function (tokenHeader) { handleTokenState(self).then(function (tokenHeader) {
$.ajax({ $.ajax({
type: 'PUT', type: 'PUT',
@ -224,19 +228,21 @@ function initApiService() {
dataType: 'json', dataType: 'json',
headers: tokenHeader, headers: tokenHeader,
success: function (response) { success: function (response) {
success(new FolderResponse(response)); deferred.resolve(new FolderResponse(response));
}, },
error: function (jqXHR, textStatus, errorThrown) { error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self); handleError(deferred.reject, jqXHR, false, self);
} }
}); });
}, function (jqXHR) { }, function (jqXHR) {
handleError(error, jqXHR, true, self); handleError(deferred.reject, jqXHR, true, self);
}); });
}; };
ApiService.prototype.deleteFolder = function (id, success, error) { ApiService.prototype.deleteFolder = function (id) {
var self = this; var self = this,
deferred = Q.defer();
handleTokenState(self).then(function (tokenHeader) { handleTokenState(self).then(function (tokenHeader) {
$.ajax({ $.ajax({
type: 'DELETE', type: 'DELETE',
@ -244,14 +250,14 @@ function initApiService() {
dataType: 'text', dataType: 'text',
headers: tokenHeader, headers: tokenHeader,
success: function (response) { success: function (response) {
success(); deferred.resolve();
}, },
error: function (jqXHR, textStatus, errorThrown) { error: function (jqXHR, textStatus, errorThrown) {
handleError(error, jqXHR, false, self); handleError(deferred.reject, jqXHR, false, self);
} }
}); });
}, function (jqXHR) { }, function (jqXHR) {
handleError(error, jqXHR, true, self); handleError(deferred.reject, jqXHR, true, self);
}); });
}; };

View File

@ -222,7 +222,7 @@ function initCipherService() {
return self.getAll(); return self.getAll();
}).then(function (ciphers) { }).then(function (ciphers) {
if (ciphers === true) { if (ciphers === true) {
return; return true;
} }
var promises = []; var promises = [];
@ -405,7 +405,7 @@ function initCipherService() {
CipherService.prototype.replace = function (ciphers) { CipherService.prototype.replace = function (ciphers) {
var self = this; var self = this;
self.userService.getUserIdPromise().then(function (userId) { return self.userService.getUserIdPromise().then(function (userId) {
return self.utilsService.saveObjToStorage('ciphers_' + userId, ciphers); return self.utilsService.saveObjToStorage('ciphers_' + userId, ciphers);
}).then(function () { }).then(function () {
self.decryptedCipherCache = null; self.decryptedCipherCache = null;
@ -423,7 +423,7 @@ function initCipherService() {
var self = this, var self = this,
key = null; key = null;
self.userService.getUserIdPromise().then(function () { return self.userService.getUserIdPromise().then(function () {
key = 'ciphers_' + userId; key = 'ciphers_' + userId;
return self.utilsService.getObjFromStorage(key); return self.utilsService.getObjFromStorage(key);
}).then(function (ciphers) { }).then(function (ciphers) {
@ -535,7 +535,7 @@ function initCipherService() {
var self = this, var self = this,
key = null; key = null;
self.userService.getUserIdPromise().then(function () { return self.userService.getUserIdPromise().then(function () {
key = 'ciphers_' + userId; key = 'ciphers_' + userId;
return self.utilsService.getObjFromStorage(key); return self.utilsService.getObjFromStorage(key);
}).then(function (ciphers) { }).then(function (ciphers) {

View File

@ -25,92 +25,86 @@ function initFolderService() {
}); });
}; };
FolderService.prototype.get = function (id, callback) { FolderService.prototype.get = function (id) {
if (!callback || typeof callback !== 'function') { var self = this;
throw 'callback function required';
}
this.userService.getUserId(function (userId) { return self.userService.getUserIdPromise().then(function (userId) {
var foldersKey = 'folders_' + userId; return self.utilsService.getObjFromStorage('folders_' + userId);
}).then(function (folders) {
if (folders && id in folders) {
return new Folder(folders[id]);
}
chrome.storage.local.get(foldersKey, function (obj) { return null;
var folders = obj[foldersKey];
if (folders && id in folders) {
callback(new Folder(folders[id]));
return;
}
callback(null);
});
}); });
}; };
FolderService.prototype.getAll = function (callback) { FolderService.prototype.getAll = function () {
if (!callback || typeof callback !== 'function') { var self = this;
throw 'callback function required';
}
this.userService.getUserId(function (userId) { return self.userService.getUserIdPromise().then(function (userId) {
var foldersKey = 'folders_' + userId; return self.utilsService.getObjFromStorage('folders_' + userId);
}).then(function (folders) {
var response = [];
for (var id in folders) {
var folder = folders[id];
response.push(new Folder(folder));
}
chrome.storage.local.get(foldersKey, function (obj) { return response;
var folders = obj[foldersKey];
var response = [];
for (var id in folders) {
var folder = folders[id];
response.push(new Folder(folder));
}
callback(response);
});
}); });
}; };
FolderService.prototype.getAllDecrypted = function () { FolderService.prototype.getAllDecrypted = function () {
var deferred = Q.defer(); if (this.decryptedFolderCache) {
var self = this; return Q(this.decryptedFolderCache);
}
var deferred = Q.defer(),
self = this,
decFolders = [{
id: null,
name: self.i18nService.noneFolder
}];
self.cryptoService.getKey().then(function (key) { self.cryptoService.getKey().then(function (key) {
if (!key) { if (!key) {
deferred.reject(); deferred.reject();
return; return true;
} }
if (self.decryptedFolderCache) { return self.getAll();
deferred.resolve(self.decryptedFolderCache);
}).then(function (folders) {
if (folders === true) {
return; return;
} }
var promises = []; var promises = [];
var decFolders = [{ for (var i = 0; i < folders.length; i++) {
id: null, /* jshint ignore:start */
name: self.i18nService.noneFolder promises.push(folders[i].decrypt().then(function (folder) {
}]; decFolders.push(folder);
self.getAll(function (folders) { }));
for (var i = 0; i < folders.length; i++) { /* jshint ignore:end */
/* jshint ignore:start */ }
promises.push(folders[i].decrypt().then(function (folder) {
decFolders.push(folder);
}));
/* jshint ignore:end */
}
Q.all(promises).then(function () { return Q.all(promises);
if (decFolders.length > 0) { }).then(function (stop) {
self.decryptedFolderCache = decFolders; if (stop === true) {
} return;
deferred.resolve(decFolders); }
});
}); self.decryptedFolderCache = decFolders;
deferred.resolve(self.decryptedFolderCache);
}); });
return deferred.promise; return deferred.promise;
}; };
FolderService.prototype.saveWithServer = function (folder) { FolderService.prototype.saveWithServer = function (folder) {
var deferred = Q.defer(); var deferred = Q.defer(),
self = this,
var self = this,
request = new FolderRequest(folder); request = new FolderRequest(folder);
if (!folder.id) { if (!folder.id) {
@ -128,53 +122,45 @@ function initFolderService() {
folder.id = response.id; folder.id = response.id;
self.userService.getUserId(function (userId) { self.userService.getUserId(function (userId) {
var data = new FolderData(response, userId); var data = new FolderData(response, userId);
self.upsert(data, function () { return self.upsert(data);
deferred.resolve(folder); }).then(function () {
}); deferred.resolve(folder);
}); });
} }
return deferred.promise; return deferred.promise;
}; };
FolderService.prototype.upsert = function (folder, callback) { FolderService.prototype.upsert = function (folder) {
if (!callback || typeof callback !== 'function') { var self = this,
throw 'callback function required'; key = null;
}
var self = this; return self.userService.getUserIdPromise(function (userId) {
key = 'folders_' + userId;
return self.utilsService.getObjFromStorage(key);
}).then(function (folders) {
if (!folders) {
folders = {};
}
self.userService.getUserId(function (userId) { if (folder.constructor === Array) {
var foldersKey = 'folders_' + userId; for (var i = 0; i < folder.length; i++) {
folders[folder[i].id] = folder[i];
chrome.storage.local.get(foldersKey, function (obj) {
var folders = obj[foldersKey];
if (!folders) {
folders = {};
} }
}
else {
folders[folder.id] = folder;
}
if (folder.constructor === Array) { return self.utilsService.saveObjToStorage(key, ciphers);
for (var i = 0; i < folder.length; i++) { }).then(function () {
folders[folder[i].id] = folder[i]; self.decryptedFolderCache = null;
}
}
else {
folders[folder.id] = folder;
}
obj[foldersKey] = folders;
chrome.storage.local.set(obj, function () {
self.decryptedFolderCache = null;
callback();
});
});
}); });
}; };
FolderService.prototype.replace = function (folders) { FolderService.prototype.replace = function (folders) {
var self = this; var self = this;
self.userService.getUserIdPromise().then(function (userId) { return self.userService.getUserIdPromise().then(function (userId) {
return self.utilsService.saveObjToStorage('folders_' + userId, folders); return self.utilsService.saveObjToStorage('folders_' + userId, folders);
}).then(function () { }).then(function () {
self.decryptedFolderCache = null; self.decryptedFolderCache = null;
@ -188,62 +174,53 @@ function initFolderService() {
}); });
}; };
FolderService.prototype.delete = function (id, callback) { FolderService.prototype.delete = function (id) {
if (!callback || typeof callback !== 'function') { var self = this,
throw 'callback function required'; key = null;
}
var self = this;
// TODO: Delete folder reference for associated ciphers // TODO: Delete folder reference for associated ciphers
self.userService.getUserId(function (userId) { return self.userService.getUserIdPromise().then(function () {
var foldersKey = 'folders_' + userId; key = 'folders_' + userId;
return self.utilsService.getObjFromStorage(key);
}).then(function (folders) {
if (!folders) {
return null;
}
chrome.storage.local.get(foldersKey, function (obj) { if (id.constructor === Array) {
var folders = obj[foldersKey]; for (var i = 0; i < id.length; i++) {
if (!folders) { if (id[i] in folders) {
callback(); delete folders[id[i]];
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 if (id in folders) {
} delete folders[id];
else { }
callback(); else {
return; return null;
} }
obj[foldersKey] = folders; return folders;
chrome.storage.local.set(obj, function () { }).then(function (folders) {
self.decryptedFolderCache = null; if (!folders) {
callback(); return false;
}); }
});
return self.utilsService.saveObjToStorage(key, folders);
}).then(function (clearCache) {
if (clearCache !== false) {
self.decryptedFolderCache = null;
}
}); });
}; };
FolderService.prototype.deleteWithServer = function (id) { FolderService.prototype.deleteWithServer = function (id) {
var deferred = Q.defer();
var self = this; var self = this;
self.apiService.deleteFolder(id, function () { return self.apiService.deleteFolder(id).then(function () {
self.delete(id, function () { return self.delete(id);
deferred.resolve();
});
}, function (response) {
handleError(response, deferred);
}); });
return deferred.promise;
}; };
function handleError(error, deferred) { function handleError(error, deferred) {