From 071881a9fa8fbdbfcffc64df9593995ee11ff0b8 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Mon, 14 Mar 2016 13:44:06 +0200 Subject: [PATCH] Sort user list within a single pass, use server provided user modes --- src/models/chan.js | 25 ++++++++++++++----------- src/plugins/irc-events/join.js | 2 +- src/plugins/irc-events/names.js | 2 +- src/plugins/irc-events/nick.js | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/models/chan.js b/src/models/chan.js index a3501097..a6f232b8 100644 --- a/src/models/chan.js +++ b/src/models/chan.js @@ -23,18 +23,21 @@ function Chan(attr) { }, attr)); } -Chan.prototype.sortUsers = function() { - this.users = _.sortBy( - this.users, - function(u) { return u.name.toLowerCase(); } - ); +Chan.prototype.sortUsers = function(irc) { + var userModeSortPriority = {}; + irc.network.options.PREFIX.forEach(function(prefix, index) { + userModeSortPriority[prefix.symbol] = index; + }); - ["+", "%", "@", "&", "~"].forEach(function(mode) { - this.users = _.remove( - this.users, - function(u) { return u.mode === mode; } - ).concat(this.users); - }, this); + userModeSortPriority[""] = 99; // No mode is lowest + + this.users = this.users.sort(function(a, b) { + if (a.mode === b.mode) { + return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1; + } + + return userModeSortPriority[a.mode] - userModeSortPriority[b.mode]; + }); }; Chan.prototype.getMode = function(name) { diff --git a/src/plugins/irc-events/join.js b/src/plugins/irc-events/join.js index a9bacc80..e447f99e 100644 --- a/src/plugins/irc-events/join.js +++ b/src/plugins/irc-events/join.js @@ -19,7 +19,7 @@ module.exports = function(irc, network) { }); } chan.users.push(new User({nick: data.nick, modes: ""})); - chan.sortUsers(); + chan.sortUsers(irc); client.emit("users", { chan: chan.id }); diff --git a/src/plugins/irc-events/names.js b/src/plugins/irc-events/names.js index fb176075..a41c7541 100644 --- a/src/plugins/irc-events/names.js +++ b/src/plugins/irc-events/names.js @@ -19,7 +19,7 @@ module.exports = function(irc, network) { chan.users.push(user); }); - chan.sortUsers(); + chan.sortUsers(irc); client.emit("users", { chan: chan.id }); diff --git a/src/plugins/irc-events/nick.js b/src/plugins/irc-events/nick.js index 6a810e80..a3719cb8 100644 --- a/src/plugins/irc-events/nick.js +++ b/src/plugins/irc-events/nick.js @@ -29,7 +29,7 @@ module.exports = function(irc, network) { return; } user.name = data.newnick; - chan.sortUsers(); + chan.sortUsers(irc); client.emit("users", { chan: chan.id });