From 6aabd9bacb448d24e7c83dd4acc0177ed9a06267 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Wed, 27 Nov 2019 20:20:42 +0200 Subject: [PATCH] Optimize user list updates for quit/part/kick events --- client/js/socket-events/msg.js | 30 ++++++++++++++++++++++++------ src/plugins/irc-events/kick.js | 4 ---- src/plugins/irc-events/part.js | 3 --- src/plugins/irc-events/quit.js | 3 --- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/client/js/socket-events/msg.js b/client/js/socket-events/msg.js index 855ef2ad..85846473 100644 --- a/client/js/socket-events/msg.js +++ b/client/js/socket-events/msg.js @@ -85,12 +85,8 @@ socket.on("msg", function(data) { channel.moreHistoryAvailable = true; } - if ((data.msg.type === "message" || data.msg.type === "action") && channel.type === "channel") { - const user = channel.users.find((u) => u.nick === data.msg.from.nick); - - if (user) { - user.lastMessage = new Date(data.msg.time).getTime() || Date.now(); - } + if (channel.type === "channel") { + updateUserList(channel, data.msg); } }); @@ -169,3 +165,25 @@ function notifyMessage(targetId, channel, activeChannel, msg) { } } } + +function updateUserList(channel, msg) { + if (msg.type === "message" || msg.type === "action") { + const user = channel.users.find((u) => u.nick === msg.from.nick); + + if (user) { + user.lastMessage = new Date(msg.time).getTime() || Date.now(); + } + } else if (msg.type === "quit" || msg.type === "part") { + const idx = channel.users.findIndex((u) => u.nick === msg.from.nick); + + if (idx > -1) { + channel.users.splice(idx, 1); + } + } else if (msg.type === "kick") { + const idx = channel.users.findIndex((u) => u.nick === msg.target.nick); + + if (idx > -1) { + channel.users.splice(idx, 1); + } + } +} diff --git a/src/plugins/irc-events/kick.js b/src/plugins/irc-events/kick.js index 3cd2590f..2d66b341 100644 --- a/src/plugins/irc-events/kick.js +++ b/src/plugins/irc-events/kick.js @@ -35,9 +35,5 @@ module.exports = function(irc, network) { } else { chan.removeUser(msg.target); } - - client.emit("users", { - chan: chan.id, - }); }); }; diff --git a/src/plugins/irc-events/part.js b/src/plugins/irc-events/part.js index 851f3b1e..9f803fc3 100644 --- a/src/plugins/irc-events/part.js +++ b/src/plugins/irc-events/part.js @@ -33,9 +33,6 @@ module.exports = function(irc, network) { }); } else { chan.removeUser(user); - client.emit("users", { - chan: chan.id, - }); } }); }; diff --git a/src/plugins/irc-events/quit.js b/src/plugins/irc-events/quit.js index 13ff2a8c..f4eb90d2 100644 --- a/src/plugins/irc-events/quit.js +++ b/src/plugins/irc-events/quit.js @@ -23,9 +23,6 @@ module.exports = function(irc, network) { chan.pushMessage(client, msg); chan.removeUser(user); - client.emit("users", { - chan: chan.id, - }); }); // If user with the nick we are trying to keep has quit, try to get this nick