From bb2c3e54a5c8faa745ed16b8ac90f07be9dc40d9 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 7 Mar 2018 07:06:38 +0200 Subject: [PATCH 1/5] add nedb --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 7cd77276..55e0f401 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "gulp-watch": "^5.0.0", "jquery": "^3.2.1", "lodash": "^4.17.4", + "nedb": "^1.8.0", "node-gyp": "^3.6.2", "node-sass": "^4.7.2", "pump": "^2.0.0", From 30a886d84afcf5cb0355b777526d0110eee8024a Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 7 Mar 2018 10:12:44 +0200 Subject: [PATCH 2/5] Database wrappers --- client/src/index.js | 24 +++++++++------ client/src/modules/database.js | 30 +++++++++++++++++++ client/src/modules/modules.js | 1 + core/src/main.js | 15 +++++++++- core/src/modules/database.js | 53 ++++++++++++++++++++++++++++++++++ core/src/modules/index.js | 1 + package.json | 2 ++ 7 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 client/src/modules/database.js create mode 100644 core/src/modules/database.js diff --git a/client/src/index.js b/client/src/index.js index e774f928..eb4f5f34 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -10,12 +10,13 @@ import { DOM, BdUI, Modals } from 'ui'; import BdCss from './styles/index.scss'; -import { Events, CssEditor, Globals, ExtModuleManager, PluginManager, ThemeManager, ModuleManager, WebpackModules, Settings } from 'modules'; +import { Events, CssEditor, Globals, ExtModuleManager, PluginManager, ThemeManager, ModuleManager, WebpackModules, Settings, Database } from 'modules'; import { ClientLogger as Logger, ClientIPC } from 'common'; class BetterDiscord { constructor() { + window.bddb = Database; window.bdglobals = Globals; window.ClientIPC = ClientIPC; window.css = CssEditor; @@ -32,14 +33,19 @@ class BetterDiscord { } async init() { - await Settings.loadSettings(); - await ModuleManager.initModules(); - await ExtModuleManager.loadAllModules(true); - await PluginManager.loadAllPlugins(true); - await ThemeManager.loadAllThemes(true); - Modals.showContentManagerErrors(); - Events.emit('ready'); - Events.emit('discord-ready'); + try { + await Database.init(); + await Settings.loadSettings(); + await ModuleManager.initModules(); + await ExtModuleManager.loadAllModules(true); + await PluginManager.loadAllPlugins(true); + await ThemeManager.loadAllThemes(true); + Modals.showContentManagerErrors(); + Events.emit('ready'); + Events.emit('discord-ready'); + } catch (err) { + console.log('FAILED TO LOAD!', err); + } } globalReady() { diff --git a/client/src/modules/database.js b/client/src/modules/database.js new file mode 100644 index 00000000..87786c9e --- /dev/null +++ b/client/src/modules/database.js @@ -0,0 +1,30 @@ +/** + * BetterDiscord Database 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 { ClientIPC } from 'bdipc'; + +export default class { + + static async insertOrUpdate(args, data) { + try { + return ClientIPC.send('bd-dba', { action: 'update', args, data }); + } catch (err) { + throw err; + } + } + + static async find(args) { + try { + return ClientIPC.send('bd-dba', { action: 'find', args }); + } catch (err) { + throw err; + } + } +} diff --git a/client/src/modules/modules.js b/client/src/modules/modules.js index db1b5b7c..b93216a3 100644 --- a/client/src/modules/modules.js +++ b/client/src/modules/modules.js @@ -12,3 +12,4 @@ export { default as EventListener } from './eventlistener'; export { default as SocketProxy } from './socketproxy'; export { default as EventHook } from './eventhook'; export { default as Permissions } from './permissionmanager'; +export { default as Database } from './database'; diff --git a/core/src/main.js b/core/src/main.js index cca1689c..9d69928f 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -26,7 +26,7 @@ const __pluginPath = path.resolve(__dirname, '..', '..', 'tests', 'plugins'); const __themePath = path.resolve(__dirname, '..', '..', 'tests', 'themes'); const __modulePath = path.resolve(__dirname, '..', '..', 'tests', 'modules'); -const { Utils, FileUtils, BDIpc, Config, WindowUtils, CSSEditor } = require('./modules'); +const { Utils, FileUtils, BDIpc, Config, WindowUtils, CSSEditor, Database } = require('./modules'); const { BrowserWindow, dialog } = require('electron'); const Common = {}; @@ -42,6 +42,8 @@ const dummyArgs = { ] }; +const dbInstance = new Database(dummyArgs.paths.find(path => path.id === 'data').path + '/storage'); + console.log(dummyArgs); @@ -87,6 +89,17 @@ class Comms { o.reply(result); }); }); + + BDIpc.on('bd-dba', o => { + (async () => { + try { + const ret = await dbInstance.exec(o.args); + o.reply(ret); + } catch (err) { + o.reply(err); + } + })(); + }); } async readFile(o, json) { diff --git a/core/src/modules/database.js b/core/src/modules/database.js new file mode 100644 index 00000000..6e114698 --- /dev/null +++ b/core/src/modules/database.js @@ -0,0 +1,53 @@ +/** + * BetterDiscord Database Module + * Copyright (c) 2015-present JsSucks - https://github.com/JsSucks + * All rights reserved. + * https://github.com/JsSucks - 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. +*/ + +const Datastore = require('nedb'); + +class Database { + + constructor(dbPath) { + this.exec = this.exec.bind(this); + this.update = this.update.bind(this); + this.db = new Datastore({ filename: dbPath, autoload: true }); + } + + async update(cmd) { + return new Promise((resolve, reject) => { + this.db.update(cmd.args, cmd.data, { upsert: true }, (err, docs) => { + if (err) return reject(err); + this.db.persistence.compactDatafile(); + resolve(docs); + }); + }); + } + + async find(cmd) { + console.log('FIND', cmd); + return new Promise((resolve, reject) => { + this.db.find(cmd.args, (err, docs) => { + if (err) return reject(err); + resolve(docs); + }); + }); + } + + async exec(cmd) { + switch (cmd.action) { + case 'update': + return this.update(cmd); + case 'find': + return this.find(cmd); + } + throw 'Invalid Command'; + } + +} + +module.exports = { Database }; diff --git a/core/src/modules/index.js b/core/src/modules/index.js index a2e0d7bc..5f2029d9 100644 --- a/core/src/modules/index.js +++ b/core/src/modules/index.js @@ -2,3 +2,4 @@ export { BDIpc } from './bdipc'; export { Utils, FileUtils, WindowUtils } from './utils'; export { Config } from './config'; export { CSSEditor } from './csseditor'; +export { Database } from './database'; diff --git a/package.json b/package.json index 55e0f401..a14d9fb0 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,10 @@ "gulp-plumber": "^1.2.0", "gulp-watch": "^5.0.0", "jquery": "^3.2.1", + "level": "^3.0.0", "lodash": "^4.17.4", "nedb": "^1.8.0", + "nedb-core": "^3.0.6", "node-gyp": "^3.6.2", "node-sass": "^4.7.2", "pump": "^2.0.0", From 8d33d80e1c617bfa069aa18bdefd12254fc51675 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 7 Mar 2018 10:28:14 +0200 Subject: [PATCH 3/5] Convert content to use database instead of individual configs --- client/src/modules/content.js | 12 +++++++++++- client/src/modules/contentmanager.js | 18 +++++++++++------- client/src/modules/database.js | 4 ++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/client/src/modules/content.js b/client/src/modules/content.js index cba21ed6..0517c78b 100644 --- a/client/src/modules/content.js +++ b/client/src/modules/content.js @@ -10,6 +10,7 @@ import { Utils, FileUtils, ClientLogger as Logger, AsyncEventEmitter } from 'common'; import { Modals } from 'ui'; +import Database from './database'; export default class Content { @@ -72,12 +73,21 @@ export default class Content { */ async saveConfiguration() { try { + /* await FileUtils.writeFile(`${this.contentPath}/user.config.json`, JSON.stringify({ enabled: this.enabled, config: this.settings.strip().settings, data: this.data })); - + */ + Database.insertOrUpdate({ type: 'contentconfig', $or: [{ id: this.id }, { name: this.name }] }, { + type: 'contentconfig', + id: this.id, + name: this.name, + enabled: this.enabled, + config: this.settings.strip().settings, + data: this.data + }); this.settings.setSaved(); } catch (err) { Logger.err(this.name, ['Failed to save configuration', err]); diff --git a/client/src/modules/contentmanager.js b/client/src/modules/contentmanager.js index 789a7975..c35c4b89 100644 --- a/client/src/modules/contentmanager.js +++ b/client/src/modules/contentmanager.js @@ -15,6 +15,7 @@ import path from 'path'; import { Events } from 'modules'; import { SettingsSet, ErrorEvent } from 'structs'; import { Modals } from 'ui'; +import Database from './database'; /** * Base class for external content managing @@ -189,13 +190,16 @@ export default class { }; try { - const readUserConfig = await this.readUserConfig(contentPath); - userConfig.enabled = readUserConfig.enabled || false; - // await userConfig.config.merge({ settings: readUserConfig.config }); - // userConfig.config.setSaved(); - // userConfig.config = userConfig.config.clone({ settings: readUserConfig.config }); - userConfig.config = readUserConfig.config; - userConfig.data = readUserConfig.data || {}; + //const readUserConfig = await this.readUserConfig(contentPath); + const readUserConfig = await Database.find({ type: 'contentconfig', name: readConfig.info.name }); + if (readUserConfig.length) { + userConfig.enabled = readUserConfig[0].enabled || false; + // await userConfig.config.merge({ settings: readUserConfig.config }); + // userConfig.config.setSaved(); + // userConfig.config = userConfig.config.clone({ settings: readUserConfig.config }); + userConfig.config = readUserConfig[0].config; + userConfig.data = readUserConfig[0].data || {}; + } } catch (err) { /*We don't care if this fails it either means that user config doesn't exist or there's something wrong with it so we revert to default config*/ Logger.info(this.moduleName, `Failed reading config for ${this.contentType} ${readConfig.info.name} in ${dirName}`); Logger.err(this.moduleName, err); diff --git a/client/src/modules/database.js b/client/src/modules/database.js index 87786c9e..9b6e475d 100644 --- a/client/src/modules/database.js +++ b/client/src/modules/database.js @@ -12,6 +12,10 @@ import { ClientIPC } from 'bdipc'; export default class { + static async init() { + return true; + } + static async insertOrUpdate(args, data) { try { return ClientIPC.send('bd-dba', { action: 'update', args, data }); From ce0d9e826cb49374ee187a3ac797a34b90c9d7cc Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 7 Mar 2018 10:35:49 +0200 Subject: [PATCH 4/5] Remove other db candidates --- package.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/package.json b/package.json index a14d9fb0..55e0f401 100644 --- a/package.json +++ b/package.json @@ -34,10 +34,8 @@ "gulp-plumber": "^1.2.0", "gulp-watch": "^5.0.0", "jquery": "^3.2.1", - "level": "^3.0.0", "lodash": "^4.17.4", "nedb": "^1.8.0", - "nedb-core": "^3.0.6", "node-gyp": "^3.6.2", "node-sass": "^4.7.2", "pump": "^2.0.0", From bca83da8c9665f300b5922297fbf30067e18ac5e Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 7 Mar 2018 10:36:18 +0200 Subject: [PATCH 5/5] Correct error response for ipc --- core/src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main.js b/core/src/main.js index 9d69928f..3e1c4ceb 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -96,7 +96,7 @@ class Comms { const ret = await dbInstance.exec(o.args); o.reply(ret); } catch (err) { - o.reply(err); + o.reply({err}); } })(); });