Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily load user list in channels on init, keep autocompletion sort on server #1194

Merged
merged 4 commits into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions client/js/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function buildChatMessage(data) {
renderPreview(preview, msg);
});

if ((type === "message" || type === "action") && chan.hasClass("channel")) {
if ((type === "message" || type === "action" || type === "notice") && chan.hasClass("channel")) {
const nicks = chan.find(".users").data("nicks");
if (nicks) {
const find = nicks.indexOf(data.msg.from);
Expand Down Expand Up @@ -143,22 +143,10 @@ function renderChannelMessages(data) {

function renderChannelUsers(data) {
const users = chat.find("#chan-" + data.id).find(".users");
let nicks = users.data("nicks") || [];
const oldSortOrder = {};

for (const i in nicks) {
oldSortOrder[nicks[i]] = i;
}

nicks = [];

for (const i in data.users) {
nicks.push(data.users[i].nick);
}

nicks = nicks.sort(function(a, b) {
return (oldSortOrder[a] || Number.MAX_VALUE) - (oldSortOrder[b] || Number.MAX_VALUE);
});
const nicks = data.users
.concat() // Make a copy of the user list, sort is applied in-place
.sort((a, b) => b.lastMessage - a.lastMessage)
.map((a) => a.nick);

const search = users
.find(".search")
Expand Down Expand Up @@ -191,7 +179,13 @@ function renderNetworks(data) {
channels: channels
})
);
channels.forEach(renderChannel);
channels.forEach((channel) => {
renderChannel(channel);

if (channel.type === "channel") {
chat.find("#chan-" + channel.id).data("needsNamesRefresh", true);
}
});

utils.confirmExit();
sorting();
Expand Down
7 changes: 6 additions & 1 deletion src/models/chan.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ Chan.prototype.sortUsers = function(irc) {
});
};

Chan.prototype.findUser = function(nick) {
return _.find(this.users, {nick: nick});
};

Chan.prototype.getMode = function(name) {
var user = _.find(this.users, {nick: name});
var user = this.findUser(name);
if (user) {
return user.mode;
}
Expand All @@ -104,6 +108,7 @@ Chan.prototype.getMode = function(name) {

Chan.prototype.toJSON = function() {
var clone = _.clone(this);
clone.users = []; // Do not send user list, the client will explicitly request it when needed
clone.messages = clone.messages.slice(-100);
return clone;
};
16 changes: 10 additions & 6 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ function User(attr, prefixLookup) {
_.defaults(this, attr, {
modes: [],
mode: "",
nick: ""
nick: "",
lastMessage: 0,
});

this.setModes(this.modes, prefixLookup);
}

User.prototype.setModes = function(modes, prefixLookup) {
// irc-framework sets character mode, but lounge works with symbols
this.modes = this.modes.map((mode) => prefixLookup[mode]);
this.modes = modes.map((mode) => prefixLookup[mode]);

if (this.modes[0]) {
this.mode = this.modes[0];
}
}
this.mode = this.modes[0] || "";
};

User.prototype.toJSON = function() {
return {
nick: this.nick,
mode: this.mode,
lastMessage: this.lastMessage,
};
};
6 changes: 4 additions & 2 deletions src/plugins/irc-events/kick.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ module.exports = function(irc, network) {
return;
}

const user = chan.findUser(data.kicked);

if (data.kicked === irc.user.nick) {
chan.users = [];
} else {
chan.users = _.without(chan.users, _.find(chan.users, {nick: data.kicked}));
chan.users = _.without(chan.users, user);
}

client.emit("users", {
Expand All @@ -24,7 +26,7 @@ module.exports = function(irc, network) {
var msg = new Msg({
type: Msg.Type.KICK,
time: data.time,
mode: chan.getMode(data.nick),
mode: user.mode,
from: data.nick,
target: data.kicked,
text: data.message || "",
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/irc-events/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ module.exports = function(irc, network) {
// Query messages (unless self) always highlight
if (chan.type === Chan.Type.QUERY) {
highlight = !self;
} else if (chan.type === Chan.Type.CHANNEL) {
const user = chan.findUser(data.nick);

if (user) {
user.lastMessage = data.time || Date.now();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/irc-events/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module.exports = function(irc, network) {
return;
}

const user = _.find(targetChan.users, {nick: mode.param});
const user = targetChan.findUser(mode.param);
if (!user) {
return;
}
Expand Down
34 changes: 27 additions & 7 deletions src/plugins/irc-events/names.js
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
3 changes: 1 addition & 2 deletions src/plugins/irc-events/nick.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

var _ = require("lodash");
var Msg = require("../../models/msg");

module.exports = function(irc, network) {
Expand All @@ -25,7 +24,7 @@ module.exports = function(irc, network) {
}

network.channels.forEach((chan) => {
var user = _.find(chan.users, {nick: data.nick});
const user = chan.findUser(data.nick);
if (typeof user === "undefined") {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/irc-events/part.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function(irc, network) {
chan: chan.id
});
} else {
var user = _.find(chan.users, {nick: from});
const user = chan.findUser(from);
chan.users = _.without(chan.users, user);
client.emit("users", {
chan: chan.id
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/irc-events/quit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ module.exports = function(irc, network) {
var client = this;
irc.on("quit", function(data) {
network.channels.forEach((chan) => {
var from = data.nick;
var user = _.find(chan.users, {nick: from});
const user = chan.findUser(data.nick);
if (typeof user === "undefined") {
return;
}
Expand All @@ -22,7 +21,7 @@ module.exports = function(irc, network) {
mode: user.mode || "",
text: data.message || "",
hostmask: data.ident + "@" + data.hostname,
from: from
from: data.nick
});
chan.pushMessage(client, msg);
});
Expand Down