Recycle existing User objects in names event

This is required to keep lastMessage correct. This will also be useful for the away tracking PR.
This commit is contained in:
Pavel Djundik 2017-07-11 17:40:43 +03:00
parent 48d367e379
commit 7d981d60d8
2 changed files with 35 additions and 13 deletions

View File

@ -12,14 +12,16 @@ function User(attr, prefixLookup) {
lastMessage: 0, lastMessage: 0,
}); });
// irc-framework sets character mode, but lounge works with symbols this.setModes(this.modes, prefixLookup);
this.modes = this.modes.map((mode) => prefixLookup[mode]);
if (this.modes[0]) {
this.mode = this.modes[0];
}
} }
User.prototype.setModes = function(modes, prefixLookup) {
// irc-framework sets character mode, but lounge works with symbols
this.modes = modes.map((mode) => prefixLookup[mode]);
this.mode = this.modes[0] || "";
};
User.prototype.toJSON = function() { User.prototype.toJSON = function() {
return { return {
nick: this.nick, nick: this.nick,

View File

@ -1,19 +1,39 @@
"use strict"; "use strict";
var User = require("../../models/user"); const User = require("../../models/user");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; const client = this;
irc.on("userlist", function(data) { irc.on("userlist", function(data) {
var chan = network.getChannel(data.channel); const chan = network.getChannel(data.channel);
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
return; return;
} }
chan.users = data.users.map((user) => new User({ // Create lookup map of current users,
// as we need to keep certain properties
// and we can recycle existing User objects
const oldUsers = new Map();
chan.users.forEach((user) => {
oldUsers.set(user.nick, user);
});
chan.users = data.users.map((user) => {
const oldUser = oldUsers.get(user.nick);
// For existing users, we only need to update mode
if (oldUser) {
oldUser.setModes(user.modes, network.prefixLookup);
return oldUser;
}
return new User({
nick: user.nick, nick: user.nick,
modes: user.modes, modes: user.modes,
}, network.prefixLookup)); }, network.prefixLookup);
});
chan.sortUsers(irc); chan.sortUsers(irc);