Merge pull request #779 from thelounge/astorije/users-always-autoload

Make user autoload more transparent in the app
This commit is contained in:
Alistair McKinlay 2016-12-12 12:10:52 +00:00 committed by GitHub
commit 5213853524
4 changed files with 27 additions and 44 deletions

View File

@ -55,17 +55,6 @@ module.exports = {
// //
theme: "themes/example.css", 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 // Prefetch URLs
// //

View File

@ -1,6 +1,7 @@
"use strict"; "use strict";
var _ = require("lodash"); var _ = require("lodash");
var colors = require("colors/safe");
var pkg = require("../package.json"); var pkg = require("../package.json");
var Chan = require("./models/chan"); var Chan = require("./models/chan");
var crypto = require("crypto"); var crypto = require("crypto");
@ -89,7 +90,7 @@ function Client(manager, name, config) {
}); });
if (client.name) { if (client.name) {
log.info("User '" + client.name + "' loaded"); log.info(`User ${colors.bold(client.name)} loaded`);
} }
} }

View File

@ -1,6 +1,7 @@
"use strict"; "use strict";
var _ = require("lodash"); var _ = require("lodash");
var colors = require("colors/safe");
var fs = require("fs"); var fs = require("fs");
var Client = require("./client"); var Client = require("./client");
var Helper = require("./helper"); var Helper = require("./helper");
@ -26,11 +27,26 @@ ClientManager.prototype.findClient = function(name, token) {
return false; return false;
}; };
ClientManager.prototype.loadUsers = function() { ClientManager.prototype.autoloadUsers = function() {
var users = this.getUsers(); this.getUsers().forEach(name => this.loadUser(name));
for (var i in users) {
this.loadUser(users[i]); 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 ${colors.bold(name)} disconnected and removed`);
}
});
}, 1000, {maxWait: 10000}));
}; };
ClientManager.prototype.loadUser = function(name) { ClientManager.prototype.loadUser = function(name) {
@ -145,27 +161,3 @@ ClientManager.prototype.removeUser = function(name) {
} }
return true; 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`); log.info(`Press Ctrl-C to stop\n`);
if (!config.public) { if (!config.public) {
manager.loadUsers(); if ("autoload" in config) {
if (config.autoload) { log.warn(`Autoloading users is now always enabled. Please remove the ${colors.yellow("autoload")} option from your configuration file.`);
manager.autoload();
} }
manager.autoloadUsers();
} }
}; };