mirror of
https://github.com/bitwarden/desktop.git
synced 2024-11-08 09:32:07 +01:00
settings service to ts
This commit is contained in:
parent
ee11bb5137
commit
2211c569d7
@ -5,7 +5,6 @@
|
||||
<script type="text/javascript" src="lib/tldjs/tld.js"></script>
|
||||
<script type="text/javascript" src="lib/forge/forge.js"></script>
|
||||
<script type="text/javascript" src="models/domainModels.js"></script>
|
||||
<script type="text/javascript" src="services/settingsService.js"></script>
|
||||
<script type="text/javascript" src="services/folderService.js"></script>
|
||||
<script type="text/javascript" src="services/cipherService.js"></script>
|
||||
<script type="text/javascript" src="services/syncService.js"></script>
|
||||
|
@ -7,6 +7,7 @@ import EnvironmentService from './services/environment.service';
|
||||
import i18nService from './services/i18nService.js';
|
||||
import LockService from './services/lockService.js';
|
||||
import PasswordGenerationService from './services/passwordGeneration.service';
|
||||
import SettingsService from './services/settings.service';
|
||||
import TokenService from './services/token.service';
|
||||
import TotpService from './services/totp.service';
|
||||
import UserService from './services/user.service';
|
||||
@ -86,7 +87,7 @@ var bg_isBackground = true,
|
||||
window.bg_apiService = bg_apiService = new ApiService(bg_tokenService, logout);
|
||||
window.bg_environmentService = bg_environmentService = new EnvironmentService(bg_apiService);
|
||||
window.bg_userService = bg_userService = new UserService(bg_tokenService);
|
||||
window.bg_settingsService = bg_settingsService = new SettingsService(bg_userService, bg_utilsService);
|
||||
window.bg_settingsService = bg_settingsService = new SettingsService(bg_userService);
|
||||
window.bg_cipherService = bg_cipherService = new CipherService(bg_cryptoService, bg_userService, bg_apiService, bg_settingsService, bg_utilsService,
|
||||
bg_constantsService);
|
||||
window.bg_folderService = bg_folderService = new FolderService(bg_cryptoService, bg_userService, bg_apiService, bg_i18nService, bg_utilsService);
|
||||
|
61
src/services/settings.service.ts
Normal file
61
src/services/settings.service.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import UserService from './user.service';
|
||||
import UtilsService from './utils.service';
|
||||
|
||||
const Keys = {
|
||||
settingsPrefix: 'settings_',
|
||||
equivalentDomains: 'equivalentDomains',
|
||||
};
|
||||
|
||||
export default class SettingsService {
|
||||
private settingsCache: any;
|
||||
|
||||
constructor(private userService: UserService) {
|
||||
}
|
||||
|
||||
clearCache(): void {
|
||||
this.settingsCache = null;
|
||||
}
|
||||
|
||||
getEquivalentDomains(): Promise<any> {
|
||||
return this.getSettingsKey(Keys.equivalentDomains);
|
||||
}
|
||||
|
||||
async setEquivalentDomains(equivalentDomains: any) {
|
||||
await this.setSettingsKey(Keys.equivalentDomains, equivalentDomains);
|
||||
}
|
||||
|
||||
async clear(userId: string): Promise<void> {
|
||||
await UtilsService.removeFromStorage(Keys.settingsPrefix + userId);
|
||||
this.settingsCache = null;
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
private async getSettings(): Promise<any> {
|
||||
if (this.settingsCache == null) {
|
||||
const userId = await this.userService.getUserId();
|
||||
this.settingsCache = UtilsService.getObjFromStorage(Keys.settingsPrefix + userId);
|
||||
}
|
||||
return this.settingsCache;
|
||||
}
|
||||
|
||||
private async getSettingsKey(key: string): Promise<any> {
|
||||
const settings = await this.getSettings();
|
||||
if (settings != null && settings[key]) {
|
||||
return settings[key];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private async setSettingsKey(key: string, value: any): Promise<void> {
|
||||
const userId = await this.userService.getUserId();
|
||||
let settings = await this.getSettings();
|
||||
if (!settings) {
|
||||
settings = {};
|
||||
}
|
||||
|
||||
settings[key] = value;
|
||||
await UtilsService.saveObjToStorage(Keys.settingsPrefix + userId, settings);
|
||||
this.settingsCache = settings;
|
||||
}
|
||||
}
|
@ -1,99 +0,0 @@
|
||||
function SettingsService(userService, utilsService) {
|
||||
this.userService = userService;
|
||||
this.utilsService = utilsService;
|
||||
this.settingsCache = null;
|
||||
|
||||
initSettingsService();
|
||||
}
|
||||
|
||||
function initSettingsService() {
|
||||
SettingsService.prototype.clearCache = function () {
|
||||
this.settingsCache = null;
|
||||
};
|
||||
|
||||
SettingsService.prototype.getSettings = function (callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
|
||||
var self = this;
|
||||
|
||||
if (self.settingsCache) {
|
||||
callback(self.settingsCache);
|
||||
return;
|
||||
}
|
||||
|
||||
this.userService.getUserId().then(function (userId) {
|
||||
var key = 'settings_' + userId;
|
||||
chrome.storage.local.get(key, function (obj) {
|
||||
self.settingsCache = obj[key];
|
||||
callback(self.settingsCache);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
SettingsService.prototype.getEquivalentDomains = function (callback) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
getSettingsKey(this, 'equivalentDomains', function (domains) {
|
||||
deferred.resolve(domains);
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
function getSettingsKey(self, key, callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
|
||||
self.getSettings(function (settings) {
|
||||
if (settings && settings[key]) {
|
||||
callback(settings[key]);
|
||||
return;
|
||||
}
|
||||
|
||||
callback(null);
|
||||
});
|
||||
}
|
||||
|
||||
SettingsService.prototype.setEquivalentDomains = function (equivalentDomains, callback) {
|
||||
setSettingsKey(this, 'equivalentDomains', equivalentDomains, callback);
|
||||
};
|
||||
|
||||
function setSettingsKey(self, key, value, callback) {
|
||||
if (!callback || typeof callback !== 'function') {
|
||||
throw 'callback function required';
|
||||
}
|
||||
|
||||
self.userService.getUserId().then(function (userId) {
|
||||
var settingsKey = 'settings_' + userId;
|
||||
|
||||
self.getSettings(function (settings) {
|
||||
if (!settings) {
|
||||
settings = {};
|
||||
}
|
||||
settings[key] = value;
|
||||
|
||||
var obj = {};
|
||||
obj[settingsKey] = settings;
|
||||
|
||||
chrome.storage.local.set(obj, function () {
|
||||
self.settingsCache = settings;
|
||||
callback();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
SettingsService.prototype.clear = function (userId) {
|
||||
var self = this;
|
||||
return self.utilsService.removeFromStorage('settings_' + userId).then(function () {
|
||||
self.settingsCache = null;
|
||||
});
|
||||
};
|
||||
|
||||
function handleError(error, deferred) {
|
||||
deferred.reject(error);
|
||||
}
|
||||
}
|
@ -147,8 +147,6 @@ function initSyncService() {
|
||||
}
|
||||
|
||||
function syncSettings(self, userId, response) {
|
||||
var deferred = Q.defer();
|
||||
|
||||
var eqDomains = [];
|
||||
if (response && response.equivalentDomains) {
|
||||
eqDomains = eqDomains.concat(response.equivalentDomains);
|
||||
@ -161,11 +159,7 @@ function initSyncService() {
|
||||
}
|
||||
}
|
||||
|
||||
self.settingsService.setEquivalentDomains(eqDomains, function () {
|
||||
deferred.resolve();
|
||||
});
|
||||
|
||||
return deferred.promise;
|
||||
return self.settingsService.setEquivalentDomains(eqDomains);
|
||||
}
|
||||
|
||||
SyncService.prototype.getLastSync = function (callback) {
|
||||
|
Loading…
Reference in New Issue
Block a user