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.
This commit is contained in:
Jérémie Astori 2016-12-07 00:50:11 -05:00
parent 303fab8519
commit b01517861d
3 changed files with 24 additions and 43 deletions

View File

@ -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
//

View File

@ -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}));
};

View File

@ -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();
}
};