From b01517861d5af76df93848f4a4916ed457c20165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Wed, 7 Dec 2016 00:50:11 -0500 Subject: [PATCH 1/2] Remove autoload option and always autoload users Since @xPaw provided a really nice way to watch user config files, there is now no need to be cheap about it (it used to be run every second, possibly why it could be disabled via settings?). This commit also improves the function a little bit by making use of ES6 syntax. A warning gets displayed on the server console when the `autoload` option is still present in the config file. --- defaults/config.js | 11 ---------- src/clientManager.js | 49 ++++++++++++++++++-------------------------- src/server.js | 7 ++++--- 3 files changed, 24 insertions(+), 43 deletions(-) diff --git a/defaults/config.js b/defaults/config.js index 22312601..b392b762 100644 --- a/defaults/config.js +++ b/defaults/config.js @@ -55,17 +55,6 @@ module.exports = { // theme: "themes/example.css", - // - // Autoload users - // - // When this setting is enabled, your 'users/' folder will be monitored. This is useful - // if you want to add/remove users while the server is running. - // - // @type boolean - // @default true - // - autoload: true, - // // Prefetch URLs // diff --git a/src/clientManager.js b/src/clientManager.js index 8dd6af70..e4350b87 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -26,11 +26,26 @@ ClientManager.prototype.findClient = function(name, token) { return false; }; -ClientManager.prototype.loadUsers = function() { - var users = this.getUsers(); - for (var i in users) { - this.loadUser(users[i]); - } +ClientManager.prototype.autoloadUsers = function() { + this.getUsers().forEach(name => this.loadUser(name)); + + fs.watch(Helper.USERS_PATH, _.debounce(() => { + const loaded = this.clients.map(c => c.name); + const updatedUsers = this.getUsers(); + + // New users created since last time users were loaded + _.difference(updatedUsers, loaded).forEach(name => this.loadUser(name)); + + // Existing users removed since last time users were loaded + _.difference(loaded, updatedUsers).forEach(name => { + const client = _.find(this.clients, {name: name}); + if (client) { + client.quit(); + this.clients = _.without(this.clients, client); + log.info("User '" + name + "' disconnected"); + } + }); + }, 1000, {maxWait: 10000})); }; ClientManager.prototype.loadUser = function(name) { @@ -145,27 +160,3 @@ ClientManager.prototype.removeUser = function(name) { } return true; }; - -ClientManager.prototype.autoload = function() { - var self = this; - - fs.watch(Helper.USERS_PATH, _.debounce(() => { - var loaded = self.clients.map(c => c.name); - var added = _.difference(self.getUsers(), loaded); - added.forEach(name => self.loadUser(name)); - - var removed = _.difference(loaded, self.getUsers()); - removed.forEach(name => { - var client = _.find( - self.clients, { - name: name - } - ); - if (client) { - client.quit(); - self.clients = _.without(self.clients, client); - log.info("User '" + name + "' disconnected"); - } - }); - }, 1000, {maxWait: 10000})); -}; diff --git a/src/server.js b/src/server.js index 62cea3a4..57aaae3c 100644 --- a/src/server.js +++ b/src/server.js @@ -88,10 +88,11 @@ in ${config.public ? "public" : "private"} mode`); log.info(`Press Ctrl-C to stop\n`); if (!config.public) { - manager.loadUsers(); - if (config.autoload) { - manager.autoload(); + if ("autoload" in config) { + log.warn(`Autoloading users is now always enabled. Please remove the ${colors.yellow("autoload")} option from your configuration file.`); } + + manager.autoloadUsers(); } }; From bc01d6ccd100b6c8df274dc365ee8869439bd55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sun, 11 Dec 2016 03:29:09 -0500 Subject: [PATCH 2/2] Improve message and style of loading/unloading console logs, use ES6 template literals --- src/client.js | 3 ++- src/clientManager.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/client.js b/src/client.js index 88efe83c..16df18e7 100644 --- a/src/client.js +++ b/src/client.js @@ -1,6 +1,7 @@ "use strict"; var _ = require("lodash"); +var colors = require("colors/safe"); var pkg = require("../package.json"); var Chan = require("./models/chan"); var crypto = require("crypto"); @@ -89,7 +90,7 @@ function Client(manager, name, config) { }); if (client.name) { - log.info("User '" + client.name + "' loaded"); + log.info(`User ${colors.bold(client.name)} loaded`); } } diff --git a/src/clientManager.js b/src/clientManager.js index e4350b87..fd5aefd0 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -1,6 +1,7 @@ "use strict"; var _ = require("lodash"); +var colors = require("colors/safe"); var fs = require("fs"); var Client = require("./client"); var Helper = require("./helper"); @@ -42,7 +43,7 @@ ClientManager.prototype.autoloadUsers = function() { if (client) { client.quit(); this.clients = _.without(this.clients, client); - log.info("User '" + name + "' disconnected"); + log.info(`User ${colors.bold(name)} disconnected and removed`); } }); }, 1000, {maxWait: 10000}));