Merge precise user config reloading

This has the problem that failing to get a modification trigger might
lead to corrupted state. The author tested it on their large instance
however and it seems to work very well.
So let's try if anyone hits any problems in the wild
This commit is contained in:
Reto Brunner 2025-09-19 06:03:51 +02:00
commit d7316a0691

View file

@ -86,39 +86,28 @@ class ClientManager {
}
autoloadUsers() {
fs.watch(
Config.getUsersPath(),
_.debounce(
() => {
const loaded = this.clients.map((c) => c.name);
const updatedUsers = this.getUsers();
fs.watch(Config.getUsersPath(), (_eventType, file) => {
if (!file.endsWith(".json")) {
return;
}
if (updatedUsers.length === 0) {
log.info(
`There are currently no users. Create one with ${colors.bold(
"thelounge add <name>"
)}.`
);
}
const name = file.slice(0, -5);
// Reload all users. Existing users will only have their passwords reloaded.
updatedUsers.forEach((name) => this.loadUser(name));
const userPath = Config.getUserConfigPath(name);
// Existing users removed since last time users were loaded
_.difference(loaded, updatedUsers).forEach((name) => {
const client = _.find(this.clients, {name});
if (fs.existsSync(userPath)) {
this.loadUser(name);
return;
}
if (client) {
client.quit(true);
this.clients = _.without(this.clients, client);
log.info(`User ${colors.bold(name)} disconnected and removed.`);
}
});
},
1000,
{maxWait: 10000}
)
);
const client = _.find(this.clients, {name});
if (client) {
client.quit(true);
this.clients = _.without(this.clients, client);
log.info(`User ${colors.bold(name)} disconnected and removed.`);
}
});
}
loadUser(name: string) {
@ -180,9 +169,11 @@ class ClientManager {
};
try {
fs.writeFileSync(userPath, JSON.stringify(user, null, "\t"), {
const tmpPath = userPath + ".tmp";
fs.writeFileSync(tmpPath, JSON.stringify(user, null, "\t"), {
mode: 0o600,
});
fs.renameSync(tmpPath, userPath);
} catch (e: any) {
log.error(`Failed to create user ${colors.green(name)} (${e})`);
throw e;