1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-12 14:23:32 +00:00

Created sync service and supported folder/site service methods

This commit is contained in:
Kyle Spearrin
2016-09-06 20:41:17 -04:00
parent 0402ec648d
commit 5a39d4c73e
4 changed files with 379 additions and 42 deletions

View File

@@ -48,7 +48,113 @@ function initFolderService() {
});
};
function handleError() {
// TODO: check for unauth or forbidden and logout
}
FolderService.prototype.saveWithServer = function (folder, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
var self = this,
request = new FolderRequest(folder);
if (!folder.id) {
self.apiService.postFolder(request, apiSuccess, handleError);
}
else {
self.apiService.putFolder(folder.id, request, apiSuccess, handleError);
}
function apiSuccess(response) {
folder.id = response.id;
userService.getUserId(function (userId) {
var data = new FolderData(response, userId);
self.upsert(data, function () {
callback(folder);
});
});
}
};
FolderService.prototype.upsert = function (folder, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
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 () {
callback();
});
});
});
};
FolderService.prototype.replace = function (folders, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
userService.getUserId(function (userId) {
var obj = {};
obj['folders_' + userId] = folders;
chrome.storage.local.set(obj, function () {
callback();
});
});
};
FolderService.prototype.delete = function (id, callback) {
if (!callback || typeof callback !== 'function') {
throw 'callback function required';
}
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 () {
callback();
});
});
});
};
};