From bc534e9a049b326f0c45dac702cea77a44629401 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Tue, 29 Apr 2014 15:01:30 +0200 Subject: [PATCH] Added user modes. Close #7 --- client/index.html | 2 +- lib/models/chan.js | 16 ++++++++++++++++ lib/server.js | 13 ++++++++++--- package.json | 2 +- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/client/index.html b/client/index.html index 2fe46968..4de8ce4f 100644 --- a/client/index.html +++ b/client/index.html @@ -68,7 +68,7 @@ {{#each users}} {{/each}} diff --git a/lib/models/chan.js b/lib/models/chan.js index 8abfb5f4..3f5944fc 100644 --- a/lib/models/chan.js +++ b/lib/models/chan.js @@ -11,3 +11,19 @@ function Chan(attr) { users: [], }, attr)); }; + +Chan.prototype = { + sortUsers: function() { + this.users = _.sortBy( + this.users, + function(u) { return u.name.toLowerCase(); } + ); + var modes = ["+", "@"]; + modes.forEach(function(mode) { + this.users = _.remove( + this.users, + function(u) { return u.mode == mode; } + ).concat(this.users); + }, this); + } +}; diff --git a/lib/server.js b/lib/server.js index 03d07536..fa10572f 100644 --- a/lib/server.js +++ b/lib/server.js @@ -264,6 +264,7 @@ function event(e, data) { } var users = chan.users; users.push(new User({name: data.nick})); + chan.sortUsers(); sockets.emit("users", { id: chan.id, users: users, @@ -309,6 +310,7 @@ function event(e, data) { case "mode": var chan = _.findWhere(channels, {name: data.target}); if (typeof chan !== "undefined") { + this.client.write("NAMES " + data.target); var msg = new Msg({ type: "mode", from: data.nick, @@ -377,9 +379,13 @@ function event(e, data) { break; } chan.users = []; - data.names.forEach(function(n) { - chan.users.push(new User({name: n})); - }); + for (var n in data.names) { + chan.users.push(new User({ + mode: data.names[n], + name: n + })); + } + chan.sortUsers(); sockets.emit("users", { id: chan.id, users: chan.users, @@ -405,6 +411,7 @@ function event(e, data) { return; } user.name = data["new"]; + chan.sortUsers(); sockets.emit("users", { id: chan.id, users: chan.users, diff --git a/package.json b/package.json index 59e34eb9..eb60b004 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "lodash": "~2.4.1", - "slate-irc": "~0.5.0", + "slate-irc": "https://github.com/erming/slate-irc/tarball/master", "moment": "~2.5.1", "connect": "~2.14.3", "socket.io": "~0.9.16"