From acf5dd5351658d5abf4556d1a6059f792bee1952 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Thu, 1 Jun 2017 21:54:46 +0300 Subject: [PATCH] Do not store unnecessary information in user objects --- client/js/lounge.js | 2 +- client/views/user.tpl | 2 +- src/models/chan.js | 4 ++-- src/models/user.js | 14 +++++++++++--- src/plugins/irc-events/kick.js | 2 +- src/plugins/irc-events/mode.js | 2 +- src/plugins/irc-events/names.js | 7 ++++++- src/plugins/irc-events/nick.js | 4 ++-- src/plugins/irc-events/part.js | 2 +- src/plugins/irc-events/quit.js | 2 +- test/models/chan.js | 4 +--- 11 files changed, 28 insertions(+), 17 deletions(-) diff --git a/client/js/lounge.js b/client/js/lounge.js index e2a3cd13..4ee762d1 100644 --- a/client/js/lounge.js +++ b/client/js/lounge.js @@ -417,7 +417,7 @@ $(function() { nicks = []; for (i in data.users) { - nicks.push(data.users[i].name); + nicks.push(data.users[i].nick); } nicks = nicks.sort(function(a, b) { diff --git a/client/views/user.tpl b/client/views/user.tpl index b0af2a0c..ff940d44 100644 --- a/client/views/user.tpl +++ b/client/views/user.tpl @@ -6,6 +6,6 @@ {{/unless}}
{{/diff}} - {{mode}}{{name}} + {{mode}}{{nick}} {{/each}}
diff --git a/src/models/chan.js b/src/models/chan.js index 1448be06..3ac014ae 100644 --- a/src/models/chan.js +++ b/src/models/chan.js @@ -77,7 +77,7 @@ Chan.prototype.sortUsers = function(irc) { this.users = this.users.sort(function(a, b) { if (a.mode === b.mode) { - return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1; + return a.nick.toLowerCase() < b.nick.toLowerCase() ? -1 : 1; } return userModeSortPriority[a.mode] - userModeSortPriority[b.mode]; @@ -85,7 +85,7 @@ Chan.prototype.sortUsers = function(irc) { }; Chan.prototype.getMode = function(name) { - var user = _.find(this.users, {name: name}); + var user = _.find(this.users, {nick: name}); if (user) { return user.mode; } diff --git a/src/models/user.js b/src/models/user.js index 1585e541..ce91a38d 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -7,13 +7,21 @@ module.exports = User; function User(attr, prefixLookup) { _.defaults(this, attr, { modes: [], + mode: "", nick: "" }); // irc-framework sets character mode, but lounge works with symbols this.modes = this.modes.map(mode => prefixLookup[mode]); - // TODO: Remove this - this.name = this.nick; - this.mode = (this.modes && this.modes[0]) || ""; + if (this.modes[0]) { + this.mode = this.modes[0]; + } } + +User.prototype.toJSON = function() { + return { + nick: this.nick, + mode: this.mode, + }; +}; diff --git a/src/plugins/irc-events/kick.js b/src/plugins/irc-events/kick.js index a02c0363..cbe525a3 100644 --- a/src/plugins/irc-events/kick.js +++ b/src/plugins/irc-events/kick.js @@ -14,7 +14,7 @@ module.exports = function(irc, network) { if (data.kicked === irc.user.nick) { chan.users = []; } else { - chan.users = _.without(chan.users, _.find(chan.users, {name: data.kicked})); + chan.users = _.without(chan.users, _.find(chan.users, {nick: data.kicked})); } client.emit("users", { diff --git a/src/plugins/irc-events/mode.js b/src/plugins/irc-events/mode.js index 44db8a73..f673f16b 100644 --- a/src/plugins/irc-events/mode.js +++ b/src/plugins/irc-events/mode.js @@ -81,7 +81,7 @@ module.exports = function(irc, network) { return; } - const user = _.find(targetChan.users, {name: mode.param}); + const user = _.find(targetChan.users, {nick: mode.param}); if (!user) { return; } diff --git a/src/plugins/irc-events/names.js b/src/plugins/irc-events/names.js index daae14b3..6384b44d 100644 --- a/src/plugins/irc-events/names.js +++ b/src/plugins/irc-events/names.js @@ -10,7 +10,12 @@ module.exports = function(irc, network) { return; } - chan.users = data.users.map(u => new User(u, network.prefixLookup)); + chan.users = data.users.map(user => { + return new User({ + nick: user.nick, + modes: user.modes, + }, network.prefixLookup); + }); chan.sortUsers(irc); diff --git a/src/plugins/irc-events/nick.js b/src/plugins/irc-events/nick.js index c8519530..4baf7178 100644 --- a/src/plugins/irc-events/nick.js +++ b/src/plugins/irc-events/nick.js @@ -25,11 +25,11 @@ module.exports = function(irc, network) { } network.channels.forEach(chan => { - var user = _.find(chan.users, {name: data.nick}); + var user = _.find(chan.users, {nick: data.nick}); if (typeof user === "undefined") { return; } - user.name = data.new_nick; + user.nick = data.new_nick; chan.sortUsers(irc); client.emit("users", { chan: chan.id diff --git a/src/plugins/irc-events/part.js b/src/plugins/irc-events/part.js index 6711aced..001bbc71 100644 --- a/src/plugins/irc-events/part.js +++ b/src/plugins/irc-events/part.js @@ -18,7 +18,7 @@ module.exports = function(irc, network) { chan: chan.id }); } else { - var user = _.find(chan.users, {name: from}); + var user = _.find(chan.users, {nick: from}); chan.users = _.without(chan.users, user); client.emit("users", { chan: chan.id diff --git a/src/plugins/irc-events/quit.js b/src/plugins/irc-events/quit.js index 1684f132..dbcf0082 100644 --- a/src/plugins/irc-events/quit.js +++ b/src/plugins/irc-events/quit.js @@ -8,7 +8,7 @@ module.exports = function(irc, network) { irc.on("quit", function(data) { network.channels.forEach(chan => { var from = data.nick; - var user = _.find(chan.users, {name: from}); + var user = _.find(chan.users, {nick: from}); if (typeof user === "undefined") { return; } diff --git a/test/models/chan.js b/test/models/chan.js index 1a051182..fc0068e9 100644 --- a/test/models/chan.js +++ b/test/models/chan.js @@ -32,9 +32,7 @@ describe("Chan", function() { }; var getUserNames = function(chan) { - return chan.users.map(function(u) { - return u.name; - }); + return chan.users.map(u => u.nick); }; it("should sort a simple user list", function() {