From 11f3d452dda272bb2cac4abeffb715bdb703b257 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 21 Sep 2014 09:46:43 -0700 Subject: [PATCH] Server-side tracking of new message count --- client/js/shout.js | 11 ++++++----- client/js/shout.templates.js | 13 +++++++++---- client/templates/chan.tpl | 2 +- src/client.js | 9 +++++++++ src/models/chan.js | 1 + src/plugins/irc-events/message.js | 9 +++++++++ src/server.js | 7 +++++++ 7 files changed, 42 insertions(+), 10 deletions(-) diff --git a/client/js/shout.js b/client/js/shout.js index 0fa7720..e960ab3 100644 --- a/client/js/shout.js +++ b/client/js/shout.js @@ -132,8 +132,8 @@ $(function() { $("body").removeClass("signed-out"); $("#sign-in").detach(); - var id = $.cookie("target"); - var target = sidebar.find("[data-target='" + id + "']").trigger("click"); + var id = data.active; + var target = sidebar.find("[data-id='" + id + "']").trigger("click"); if (target.length === 0) { var first = sidebar.find(".chan") .eq(0) @@ -371,13 +371,14 @@ $(function() { return; } - if (self.hasClass("chan")) { - $.cookie("target", target); - } chat.data( "id", self.data("id") ); + socket.emit( + "open", + self.data("id") + ); sidebar.find(".active").removeClass("active"); self.addClass("active") diff --git a/client/js/shout.templates.js b/client/js/shout.templates.js index 94a6ae3..fb2ff16 100644 --- a/client/js/shout.templates.js +++ b/client/js/shout.templates.js @@ -1,8 +1,7 @@ (function() { var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; templates['chan'] = template({"1":function(depth0,helpers,partials,data) { - var helper, functionType="function", helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression; - return "\n"; -},"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) { +},"2":function(depth0,helpers,partials,data) { + var helper, functionType="function", helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression; + return escapeExpression(((helper = (helper = helpers.unread || (depth0 != null ? depth0.unread : depth0)) != null ? helper : helperMissing),(typeof helper === functionType ? helper.call(depth0, {"name":"unread","hash":{},"data":data}) : helper))); + },"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) { var stack1, buffer = ""; stack1 = helpers.each.call(depth0, (depth0 != null ? depth0.channels : depth0), {"name":"each","hash":{},"fn":this.program(1, data),"inverse":this.noop,"data":data}); if (stack1 != null) { buffer += stack1; } diff --git a/client/templates/chan.tpl b/client/templates/chan.tpl index ce7744d..421545f 100644 --- a/client/templates/chan.tpl +++ b/client/templates/chan.tpl @@ -1,6 +1,6 @@ {{#each channels}} diff --git a/src/client.js b/src/client.js index 9b48a90..1fc6289 100644 --- a/src/client.js +++ b/src/client.js @@ -49,6 +49,7 @@ var inputs = [ function Client(sockets, name, config) { _.merge(this, { + activeChannel: -1, config: config, id: id++, name: name, @@ -236,6 +237,14 @@ Client.prototype.more = function(data) { }); }; +Client.prototype.open = function(data) { + var target = this.find(data); + if (target) { + target.chan.unread = 0; + this.activeChannel = target.chan.id; + } +}; + Client.prototype.quit = function() { this.networks.forEach(function(network) { var irc = network.irc; diff --git a/src/models/chan.js b/src/models/chan.js index 19f8f96..a8ba3ac 100644 --- a/src/models/chan.js +++ b/src/models/chan.js @@ -16,6 +16,7 @@ function Chan(attr) { messages: [], name: "", type: Chan.Type.CHANNEL, + unread: 5, users: [] }, attr)); } diff --git a/src/plugins/irc-events/message.js b/src/plugins/irc-events/message.js index 51dceff..eba6597 100644 --- a/src/plugins/irc-events/message.js +++ b/src/plugins/irc-events/message.js @@ -9,6 +9,7 @@ module.exports = function(irc, network) { if (target.toLowerCase() == irc.me.toLowerCase()) { target = data.from; } + var chan = _.findWhere(network.channels, {name: target}); if (typeof chan === "undefined") { chan = new Chan({ @@ -21,19 +22,27 @@ module.exports = function(irc, network) { chan: chan }); } + var type = ""; var text = data.message; if (text.split(" ")[0] === "\u0001ACTION") { type = Msg.Type.ACTION; text = text.replace(/\u0001|ACTION/g, ""); } + text.split(" ").forEach(function(w) { if (w.indexOf(irc.me) === 0) type += " highlight"; }); + var self = false; if (data.from.toLowerCase() == irc.me.toLowerCase()) { self = true; } + + if (chan.id != client.activeChannel) { + chan.unread++; + } + var msg = new Msg({ type: type || Msg.Type.MESSAGE, from: data.from, diff --git a/src/server.js b/src/server.js index 821ddf8..c98d729 100644 --- a/src/server.js +++ b/src/server.js @@ -80,8 +80,15 @@ function init(socket, client, token) { client.connect(data); } ); + socket.on( + "open", + function(data) { + client.open(data); + } + ) socket.join(client.id); socket.emit("init", { + active: client.activeChannel, networks: client.networks, token: token || "" });