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:
parent
ea1ef54c95
commit
ba4e2eae15
@ -1,17 +1,17 @@
|
||||
angular
|
||||
.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) {
|
||||
$scope.i18n = i18nService;
|
||||
$scope.folder = {};
|
||||
var folderId = $stateParams.folderId;
|
||||
$('#name').focus();
|
||||
|
||||
folderService.get(folderId, function (folder) {
|
||||
$q.when(folder.decrypt()).then(function (model) {
|
||||
$scope.folder = model;
|
||||
});
|
||||
folderService.get(folderId).then(function (folder) {
|
||||
return folder.decrypt();
|
||||
}).then(function (model) {
|
||||
$scope.folder = model;
|
||||
});
|
||||
|
||||
utilsService.initListSectionItemListeners($(document), angular);
|
||||
@ -23,9 +23,9 @@
|
||||
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);
|
||||
return $q.when(folderService.saveWithServer(folder)).then(function (folder) {
|
||||
return folderService.saveWithServer(folder).then(function (folder) {
|
||||
$analytics.eventTrack('Edited Folder');
|
||||
toastr.success(i18nService.editedFolder);
|
||||
$state.go('folders', { animation: 'out-slide-down' });
|
||||
@ -42,7 +42,7 @@
|
||||
cancelButtonText: i18nService.no
|
||||
}, function (confirmed) {
|
||||
if (confirmed) {
|
||||
$q.when(folderService.deleteWithServer(folderId)).then(function () {
|
||||
folderService.deleteWithServer(folderId).then(function () {
|
||||
$analytics.eventTrack('Deleted Folder');
|
||||
toastr.success(i18nService.deletedFolder);
|
||||
$state.go('folders', {
|
||||
|
@ -7,8 +7,7 @@
|
||||
|
||||
load();
|
||||
function load() {
|
||||
var foldersPromise = $q.when(folderService.getAllDecrypted());
|
||||
foldersPromise.then(function (folders) {
|
||||
folderService.getAllDecrypted().then(function (folders) {
|
||||
if (folders.length > 0 && folders[0].id === null) {
|
||||
// remove the "none" folder
|
||||
$scope.folders = folders.slice(1);
|
||||
|
@ -45,14 +45,12 @@
|
||||
var decLogins = [];
|
||||
var promises = [];
|
||||
|
||||
var folderPromise = $q.when(folderService.getAllDecrypted());
|
||||
folderPromise.then(function (folders) {
|
||||
var folderPromise = folderService.getAllDecrypted().then(function (folders) {
|
||||
decFolders = folders;
|
||||
});
|
||||
promises.push(folderPromise);
|
||||
|
||||
var loginPromise = $q.when(cipherService.getAllDecrypted());
|
||||
loginPromise.then(function (logins) {
|
||||
var loginPromise = cipherService.getAllDecrypted().then(function (logins) {
|
||||
decLogins = logins;
|
||||
});
|
||||
promises.push(loginPromise);
|
||||
|
@ -29,14 +29,12 @@
|
||||
var promises = [];
|
||||
|
||||
if ($scope.folder.id) {
|
||||
var folderDeferred = $q.defer();
|
||||
folderService.get($scope.folder.id, function (folder) {
|
||||
folder.decrypt().then(function (model) {
|
||||
decFolder = model;
|
||||
folderDeferred.resolve();
|
||||
});
|
||||
var getPromise = folderService.get($scope.folder.id).then(function (folder) {
|
||||
return folder.decrypt();
|
||||
}).then(function (model) {
|
||||
decFolder = model;
|
||||
});
|
||||
promises.push(folderDeferred.promise);
|
||||
promises.push(getPromise);
|
||||
}
|
||||
|
||||
var cipherPromise = cipherService.getAllDecryptedForFolder($scope.folder.id).then(function (ciphers) {
|
||||
|
@ -191,8 +191,10 @@ function initApiService() {
|
||||
|
||||
// Folder APIs
|
||||
|
||||
ApiService.prototype.postFolder = function (folderRequest, success, error) {
|
||||
var self = this;
|
||||
ApiService.prototype.postFolder = function (folderRequest) {
|
||||
var self = this,
|
||||
deferred = Q.defer();
|
||||
|
||||
handleTokenState(self).then(function (tokenHeader) {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
@ -202,19 +204,21 @@ function initApiService() {
|
||||
dataType: 'json',
|
||||
headers: tokenHeader,
|
||||
success: function (response) {
|
||||
success(new FolderResponse(response));
|
||||
deferred.resolve(new FolderResponse(response));
|
||||
},
|
||||
error: function (jqXHR, textStatus, errorThrown) {
|
||||
handleError(error, jqXHR, false, self);
|
||||
handleError(deferred.reject, jqXHR, false, self);
|
||||
}
|
||||
});
|
||||
}, function (jqXHR) {
|
||||
handleError(error, jqXHR, true, self);
|
||||
handleError(deferred.reject, jqXHR, true, self);
|
||||
});
|
||||
};
|
||||
|
||||
ApiService.prototype.putFolder = function (id, folderRequest, success, error) {
|
||||
var self = this;
|
||||
ApiService.prototype.putFolder = function (id, folderRequest) {
|
||||
var self = this,
|
||||
deferred = Q.defer();
|
||||
|
||||
handleTokenState(self).then(function (tokenHeader) {
|
||||
$.ajax({
|
||||
type: 'PUT',
|
||||
@ -224,19 +228,21 @@ function initApiService() {
|
||||
dataType: 'json',
|
||||
headers: tokenHeader,
|
||||
success: function (response) {
|
||||
success(new FolderResponse(response));
|
||||
deferred.resolve(new FolderResponse(response));
|
||||
},
|
||||
error: function (jqXHR, textStatus, errorThrown) {
|
||||
handleError(error, jqXHR, false, self);
|
||||
handleError(deferred.reject, jqXHR, false, self);
|
||||
}
|
||||
});
|
||||
}, function (jqXHR) {
|
||||
handleError(error, jqXHR, true, self);
|
||||
handleError(deferred.reject, jqXHR, true, self);
|
||||
});
|
||||
};
|
||||
|
||||
ApiService.prototype.deleteFolder = function (id, success, error) {
|
||||
var self = this;
|
||||
ApiService.prototype.deleteFolder = function (id) {
|
||||
var self = this,
|
||||
deferred = Q.defer();
|
||||
|
||||
handleTokenState(self).then(function (tokenHeader) {
|
||||
$.ajax({
|
||||
type: 'DELETE',
|
||||
@ -244,14 +250,14 @@ function initApiService() {
|
||||
dataType: 'text',
|
||||
headers: tokenHeader,
|
||||
success: function (response) {
|
||||
success();
|
||||
deferred.resolve();
|
||||
},
|
||||
error: function (jqXHR, textStatus, errorThrown) {
|
||||
handleError(error, jqXHR, false, self);
|
||||
handleError(deferred.reject, jqXHR, false, self);
|
||||
}
|
||||
});
|
||||
}, function (jqXHR) {
|
||||
handleError(error, jqXHR, true, self);
|
||||
handleError(deferred.reject, jqXHR, true, self);
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -222,7 +222,7 @@ function initCipherService() {
|
||||
return self.getAll();
|
||||
}).then(function (ciphers) {
|
||||
if (ciphers === true) {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
var promises = [];
|
||||
@ -405,7 +405,7 @@ function initCipherService() {
|
||||
|
||||
CipherService.prototype.replace = function (ciphers) {
|
||||
var self = this;
|
||||
self.userService.getUserIdPromise().then(function (userId) {
|
||||
return self.userService.getUserIdPromise().then(function (userId) {
|
||||
return self.utilsService.saveObjToStorage('ciphers_' + userId, ciphers);
|
||||
}).then(function () {
|
||||
self.decryptedCipherCache = null;
|
||||
@ -423,7 +423,7 @@ function initCipherService() {
|
||||
var self = this,
|
||||
key = null;
|
||||
|
||||
self.userService.getUserIdPromise().then(function () {
|
||||
return self.userService.getUserIdPromise().then(function () {
|
||||
key = 'ciphers_' + userId;
|
||||
return self.utilsService.getObjFromStorage(key);
|
||||
}).then(function (ciphers) {
|
||||
@ -535,7 +535,7 @@ function initCipherService() {
|
||||
var self = this,
|
||||
key = null;
|
||||
|
||||
self.userService.getUserIdPromise().then(function () {
|
||||
return self.userService.getUserIdPromise().then(function () {
|
||||
key = 'ciphers_' + userId;
|
||||
return self.utilsService.getObjFromStorage(key);
|
||||
}).then(function (ciphers) {
|
||||
|
@ -25,92 +25,86 @@ function initFolderService() {
|
||||
});
|
||||
};
|
||||
|
||||
FolderService.prototype.get = function (id, callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
FolderService.prototype.get = function (id) {
|
||||
var self = this;
|
||||
|
||||
this.userService.getUserId(function (userId) {
|
||||
var foldersKey = 'folders_' + userId;
|
||||
return self.userService.getUserIdPromise().then(function (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) {
|
||||
var folders = obj[foldersKey];
|
||||
if (folders && id in folders) {
|
||||
callback(new Folder(folders[id]));
|
||||
return;
|
||||
}
|
||||
|
||||
callback(null);
|
||||
});
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
FolderService.prototype.getAll = function (callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
FolderService.prototype.getAll = function () {
|
||||
var self = this;
|
||||
|
||||
this.userService.getUserId(function (userId) {
|
||||
var foldersKey = 'folders_' + userId;
|
||||
return self.userService.getUserIdPromise().then(function (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) {
|
||||
var folders = obj[foldersKey];
|
||||
var response = [];
|
||||
for (var id in folders) {
|
||||
var folder = folders[id];
|
||||
response.push(new Folder(folder));
|
||||
}
|
||||
|
||||
callback(response);
|
||||
});
|
||||
return response;
|
||||
});
|
||||
};
|
||||
|
||||
FolderService.prototype.getAllDecrypted = function () {
|
||||
var deferred = Q.defer();
|
||||
var self = this;
|
||||
if (this.decryptedFolderCache) {
|
||||
return Q(this.decryptedFolderCache);
|
||||
}
|
||||
|
||||
var deferred = Q.defer(),
|
||||
self = this,
|
||||
decFolders = [{
|
||||
id: null,
|
||||
name: self.i18nService.noneFolder
|
||||
}];
|
||||
|
||||
self.cryptoService.getKey().then(function (key) {
|
||||
if (!key) {
|
||||
deferred.reject();
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (self.decryptedFolderCache) {
|
||||
deferred.resolve(self.decryptedFolderCache);
|
||||
return self.getAll();
|
||||
|
||||
}).then(function (folders) {
|
||||
if (folders === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
var promises = [];
|
||||
var decFolders = [{
|
||||
id: null,
|
||||
name: self.i18nService.noneFolder
|
||||
}];
|
||||
self.getAll(function (folders) {
|
||||
for (var i = 0; i < folders.length; i++) {
|
||||
/* jshint ignore:start */
|
||||
promises.push(folders[i].decrypt().then(function (folder) {
|
||||
decFolders.push(folder);
|
||||
}));
|
||||
/* jshint ignore:end */
|
||||
}
|
||||
for (var i = 0; i < folders.length; i++) {
|
||||
/* jshint ignore:start */
|
||||
promises.push(folders[i].decrypt().then(function (folder) {
|
||||
decFolders.push(folder);
|
||||
}));
|
||||
/* jshint ignore:end */
|
||||
}
|
||||
|
||||
Q.all(promises).then(function () {
|
||||
if (decFolders.length > 0) {
|
||||
self.decryptedFolderCache = decFolders;
|
||||
}
|
||||
deferred.resolve(decFolders);
|
||||
});
|
||||
});
|
||||
return Q.all(promises);
|
||||
}).then(function (stop) {
|
||||
if (stop === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
self.decryptedFolderCache = decFolders;
|
||||
deferred.resolve(self.decryptedFolderCache);
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
FolderService.prototype.saveWithServer = function (folder) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
var self = this,
|
||||
var deferred = Q.defer(),
|
||||
self = this,
|
||||
request = new FolderRequest(folder);
|
||||
|
||||
if (!folder.id) {
|
||||
@ -128,53 +122,45 @@ function initFolderService() {
|
||||
folder.id = response.id;
|
||||
self.userService.getUserId(function (userId) {
|
||||
var data = new FolderData(response, userId);
|
||||
self.upsert(data, function () {
|
||||
deferred.resolve(folder);
|
||||
});
|
||||
return self.upsert(data);
|
||||
}).then(function () {
|
||||
deferred.resolve(folder);
|
||||
});
|
||||
}
|
||||
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
FolderService.prototype.upsert = function (folder, callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
FolderService.prototype.upsert = function (folder) {
|
||||
var self = this,
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
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 () {
|
||||
self.decryptedFolderCache = null;
|
||||
callback();
|
||||
});
|
||||
});
|
||||
return self.utilsService.saveObjToStorage(key, ciphers);
|
||||
}).then(function () {
|
||||
self.decryptedFolderCache = null;
|
||||
});
|
||||
};
|
||||
|
||||
FolderService.prototype.replace = function (folders) {
|
||||
var self = this;
|
||||
self.userService.getUserIdPromise().then(function (userId) {
|
||||
return self.userService.getUserIdPromise().then(function (userId) {
|
||||
return self.utilsService.saveObjToStorage('folders_' + userId, folders);
|
||||
}).then(function () {
|
||||
self.decryptedFolderCache = null;
|
||||
@ -188,62 +174,53 @@ function initFolderService() {
|
||||
});
|
||||
};
|
||||
|
||||
FolderService.prototype.delete = function (id, callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
|
||||
var self = this;
|
||||
FolderService.prototype.delete = function (id) {
|
||||
var self = this,
|
||||
key = null;
|
||||
|
||||
// TODO: Delete folder reference for associated ciphers
|
||||
|
||||
self.userService.getUserId(function (userId) {
|
||||
var foldersKey = 'folders_' + userId;
|
||||
return self.userService.getUserIdPromise().then(function () {
|
||||
key = 'folders_' + userId;
|
||||
return self.utilsService.getObjFromStorage(key);
|
||||
}).then(function (folders) {
|
||||
if (!folders) {
|
||||
return null;
|
||||
}
|
||||
|
||||
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]];
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
else if (id in folders) {
|
||||
delete folders[id];
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
obj[foldersKey] = folders;
|
||||
chrome.storage.local.set(obj, function () {
|
||||
self.decryptedFolderCache = null;
|
||||
callback();
|
||||
});
|
||||
});
|
||||
return folders;
|
||||
}).then(function (folders) {
|
||||
if (!folders) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self.utilsService.saveObjToStorage(key, folders);
|
||||
}).then(function (clearCache) {
|
||||
if (clearCache !== false) {
|
||||
self.decryptedFolderCache = null;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
FolderService.prototype.deleteWithServer = function (id) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
var self = this;
|
||||
self.apiService.deleteFolder(id, function () {
|
||||
self.delete(id, function () {
|
||||
deferred.resolve();
|
||||
});
|
||||
}, function (response) {
|
||||
handleError(response, deferred);
|
||||
return self.apiService.deleteFolder(id).then(function () {
|
||||
return self.delete(id);
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
function handleError(error, deferred) {
|
||||
|
Loading…
Reference in New Issue
Block a user