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,
});
// irc-framework sets character mode, but lounge works with symbols
this.modes = this.modes.map((mode) => prefixLookup[mode]);
if (this.modes[0]) {
this.mode = this.modes[0];
}
this.setModes(this.modes, prefixLookup);
}
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() {
return {
nick: this.nick,

View file

@ -1,19 +1,39 @@
"use strict";
var User = require("../../models/user");
const User = require("../../models/user");
module.exports = function(irc, network) {
var client = this;
const client = this;
irc.on("userlist", function(data) {
var chan = network.getChannel(data.channel);
const chan = network.getChannel(data.channel);
if (typeof chan === "undefined") {
return;
}
chan.users = data.users.map((user) => new User({
nick: user.nick,
modes: user.modes,
}, network.prefixLookup));
// 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,
modes: user.modes,
}, network.prefixLookup);
});
chan.sortUsers(irc);