From f269ac3bee9faaea0b5134ab5407780bfdfbf6eb Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Sun, 15 Dec 2019 17:26:18 +0200 Subject: [PATCH] Update user file without reading, debounce all saves --- src/client.js | 40 ++++++++++++++-------------------------- src/clientManager.js | 26 +++++++------------------- src/server.js | 8 ++------ 3 files changed, 23 insertions(+), 51 deletions(-) diff --git a/src/client.js b/src/client.js index f7dffa1f..8a9f19a3 100644 --- a/src/client.js +++ b/src/client.js @@ -315,29 +315,23 @@ Client.prototype.updateSession = function(token, ip, request) { agent: friendlyAgent, }); - client.manager.updateUser(client.name, { - browser: client.config.browser, - sessions: client.config.sessions, - }); + client.save(); }; Client.prototype.setPassword = function(hash, callback) { const client = this; - client.manager.updateUser( - client.name, - { - password: hash, - }, - function(err) { - if (err) { - return callback(false); - } - - client.config.password = hash; - return callback(true); + const oldHash = client.config.password; + client.config.password = hash; + client.manager.saveUser(client, function(err) { + if (err) { + // If user file fails to write, reset it back + client.config.password = oldHash; + return callback(false); } - ); + + return callback(true); + }); }; Client.prototype.input = function(data) { @@ -662,9 +656,7 @@ Client.prototype.registerPushSubscription = function(session, subscription, noSa session.pushSubscription = data; if (!noSave) { - this.manager.updateUser(this.name, { - sessions: this.config.sessions, - }); + this.save(); } return data; @@ -672,9 +664,7 @@ Client.prototype.registerPushSubscription = function(session, subscription, noSa Client.prototype.unregisterPushSubscription = function(token) { this.config.sessions[token].pushSubscription = null; - this.manager.updateUser(this.name, { - sessions: this.config.sessions, - }); + this.save(); }; Client.prototype.save = _.debounce( @@ -684,9 +674,7 @@ Client.prototype.save = _.debounce( } const client = this; - const json = {}; - json.networks = this.networks.map((n) => n.export()); - client.manager.updateUser(client.name, json); + client.manager.saveUser(client); }, 1000, {maxWait: 10000} diff --git a/src/clientManager.js b/src/clientManager.js index e84ed37b..a591554b 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -179,25 +179,13 @@ ClientManager.prototype.addUser = function(name, password, enableLog) { return true; }; -ClientManager.prototype.updateUser = function(name, opts, callback) { - const user = readUserConfig(name); +ClientManager.prototype.saveUser = function(client, callback) { + const json = Object.assign({}, client.config, { + networks: client.networks.map((n) => n.export()), + }); + const newUser = JSON.stringify(json, null, "\t"); - if (!user) { - return callback ? callback(true) : false; - } - - const currentUser = JSON.stringify(user, null, "\t"); - _.assign(user, opts); - const newUser = JSON.stringify(user, null, "\t"); - - - // Do not touch the disk if object has not changed - if (currentUser === newUser) { - console.log("same"); - return callback ? callback() : true; - } - - const pathReal = Helper.getUserConfigPath(name); + const pathReal = Helper.getUserConfigPath(client.name); const pathTemp = pathReal + ".tmp"; try { @@ -208,7 +196,7 @@ ClientManager.prototype.updateUser = function(name, opts, callback) { return callback ? callback() : true; } catch (e) { - log.error(`Failed to update user ${colors.green(name)} (${e})`); + log.error(`Failed to update user ${colors.green(client.name)} (${e})`); if (callback) { callback(e); diff --git a/src/server.js b/src/server.js index bf1185e4..da3248eb 100644 --- a/src/server.js +++ b/src/server.js @@ -591,9 +591,7 @@ function initializeClient(socket, client, token, lastMessage, openChannel) { value: newSetting.value, }); - client.manager.updateUser(client.name, { - clientSettings: client.config.clientSettings, - }); + client.save(); if (newSetting.name === "highlights") { client.compileCustomHighlights(); @@ -630,9 +628,7 @@ function initializeClient(socket, client, token, lastMessage, openChannel) { delete client.config.sessions[tokenToSignOut]; - client.manager.updateUser(client.name, { - sessions: client.config.sessions, - }); + client.save(); _.map(client.attachedClients, (attachedClient, socketId) => { if (attachedClient.token !== tokenToSignOut) {