BetterDiscordApp-v2/client/src/modules/settings.js

131 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-01-30 11:01:24 +01:00
/**
* BetterDiscord Settings Module
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import defaultSettings from '../data/user.settings.default';
2018-02-13 21:05:38 +01:00
import Globals from './globals';
import CssEditor from './csseditor';
2018-02-13 23:30:58 +01:00
import Events from './events';
import { Utils, FileUtils, ClientLogger as Logger } from 'common';
2018-03-01 20:00:24 +01:00
import { SettingsSet, SettingUpdatedEvent } from 'structs';
2018-02-12 16:19:11 +01:00
import path from 'path';
2018-01-30 11:01:24 +01:00
2018-03-02 20:34:53 +01:00
export default new class Settings {
constructor() {
this.settings = [];
}
async loadSettings() {
2018-02-12 16:19:11 +01:00
try {
await FileUtils.ensureDirectory(this.dataPath);
const settingsPath = path.resolve(this.dataPath, 'user.settings.json');
const user_config = await FileUtils.readJsonFromFile(settingsPath);
const { settings, scss, css_editor_bounds, css_editor_files } = user_config;
2018-02-12 16:19:11 +01:00
2018-03-01 20:00:24 +01:00
this.settings = defaultSettings.map(set => {
const newSet = new SettingsSet(set);
newSet.merge(settings.find(s => s.id === newSet.id));
newSet.setSaved();
newSet.on('setting-updated', event => {
const { category, setting, value, old_value } = event;
Logger.log('Settings', `${newSet.id}/${category.id}/${setting.id} was changed from ${old_value} to ${value}`);
Events.emit('setting-updated', event);
Events.emit(`setting-updated-${newSet.id}_${category.id}_${setting.id}`, event);
});
newSet.on('settings-updated', async (event) => {
await this.saveSettings();
Events.emit('settings-updated', event);
});
return newSet;
});
2018-02-13 21:05:38 +01:00
CssEditor.updateScss(scss, true);
CssEditor.editor_bounds = css_editor_bounds || {};
CssEditor.files = css_editor_files || [];
2018-02-12 16:19:11 +01:00
} catch (err) {
// There was an error loading settings
// This probably means that the user doesn't have any settings yet
Logger.err('Settings', err);
}
}
2018-03-02 20:34:53 +01:00
async saveSettings() {
2018-02-12 16:19:11 +01:00
try {
await FileUtils.ensureDirectory(this.dataPath);
const settingsPath = path.resolve(this.dataPath, 'user.settings.json');
await FileUtils.writeJsonToFile(settingsPath, {
2018-03-02 20:34:53 +01:00
settings: this.settings.map(set => set.strip()),
scss: CssEditor.scss,
css_editor_bounds: {
width: CssEditor.editor_bounds.width,
height: CssEditor.editor_bounds.height,
x: CssEditor.editor_bounds.x,
y: CssEditor.editor_bounds.y
},
css_editor_files: CssEditor.files
2018-02-12 16:19:11 +01:00
});
2018-03-01 20:00:24 +01:00
for (let set of this.getSettings) {
set.setSaved();
}
2018-02-12 16:19:11 +01:00
} catch (err) {
2018-03-01 20:00:24 +01:00
// There was an error saving settings
2018-02-12 16:19:11 +01:00
Logger.err('Settings', err);
2018-03-01 20:00:24 +01:00
throw err;
2018-02-12 16:19:11 +01:00
}
}
2018-03-02 20:34:53 +01:00
getSet(set_id) {
return this.getSettings.find(s => s.id === set_id);
}
2018-03-02 20:34:53 +01:00
get core() { return this.getSet('core') }
get ui() { return this.getSet('ui') }
get emotes() { return this.getSet('emotes') }
get security() { return this.getSet('security') }
2018-03-01 20:00:24 +01:00
2018-03-02 20:34:53 +01:00
getCategory(set_id, category_id) {
const set = this.getSet(set_id);
2018-03-01 20:00:24 +01:00
return set ? set.getCategory(category_id) : undefined;
}
2018-03-02 20:34:53 +01:00
getSetting(set_id, category_id, setting_id) {
2018-03-01 20:00:24 +01:00
const set = this.getSet(set_id);
return set ? set.getSetting(category_id, setting_id) : undefined;
}
2018-03-02 20:34:53 +01:00
get(set_id, category_id, setting_id) {
2018-03-01 20:00:24 +01:00
const set = this.getSet(set_id);
return set ? set.get(category_id, setting_id) : undefined;
}
2018-03-02 20:34:53 +01:00
async mergeSettings(set_id, newSettings) {
const set = this.getSet(set_id);
if (!set) return;
2018-03-01 20:00:24 +01:00
return await set.merge(newSettings);
}
2018-03-02 20:34:53 +01:00
setSetting(set_id, category_id, setting_id, value) {
2018-03-01 20:00:24 +01:00
const setting = this.getSetting(set_id, category_id, setting_id);
if (!setting) throw {message: `Tried to set ${set_id}/${category_id}/${setting_id}, which doesn't exist`};
setting.value = value;
2018-02-12 18:54:40 +01:00
}
2018-03-02 20:34:53 +01:00
get getSettings() {
return this.settings;
2018-02-12 16:19:11 +01:00
}
2018-03-02 20:34:53 +01:00
get dataPath() {
2018-02-12 16:19:11 +01:00
return this._dataPath ? this._dataPath : (this._dataPath = Globals.getObject('paths').find(p => p.id === 'data').path);
2018-01-30 11:01:24 +01:00
}
}