From f60ee4d6408d4f593921263de15bf8ffc8951cf2 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 13 Oct 2016 18:22:37 +0200 Subject: [PATCH] Refactor rooms to have separate handlers for presence and message stanzas --- spec/chatroom.js | 12 ++------- src/converse-muc.js | 64 +++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/spec/chatroom.js b/spec/chatroom.js index 42c106644..8c29dac64 100644 --- a/spec/chatroom.js +++ b/spec/chatroom.js @@ -173,9 +173,8 @@ it("shows users currently present in the room", function () { test_utils.openAndEnterChatRoom('lounge', 'localhost', 'dummy'); var name; - var view = this.chatboxviews.get('lounge@localhost'), + var view = converse.chatboxviews.get('lounge@localhost'), $occupants = view.$('.occupant-list'); - spyOn(view, 'onChatRoomPresence').andCallThrough(); var presence, role; for (var i=0; i 0; - if (!from || (this.model.get('id') !== from.split("/")[0]) || is_mam) { + if (is_mam) { return true; } - if (stanza.nodeName === "message") { - _.compose(this.onChatRoomMessage.bind(this), this.showStatusMessages.bind(this))(stanza); - } else if (stanza.nodeName === "presence") { - xquery = stanza.getElementsByTagName("x"); - if (xquery.length > 0) { - for (i = 0; i < xquery.length; i++) { - xmlns = xquery[i].getAttribute("xmlns"); - if (xmlns && xmlns.match(Strophe.NS.MUC)) { - this.onChatRoomPresence(stanza); - break; - } - } - } - } + _.compose(this.onChatRoomMessage.bind(this), this.showStatusMessages.bind(this))(stanza); return true; }, @@ -622,7 +606,35 @@ return node + "@" + domain + (nick !== null ? "/" + nick : ""); }, + registerHandlers: function () { + var room_jid = this.model.get('jid'); + this.removeHandlers(); + this.presence_handler = converse.connection.addHandler( + this.onChatRoomPresence.bind(this), + Strophe.NS.MUC, 'presence', null, null, room_jid, + {'ignoreNamespaceFragment': true, 'matchBareFromJid': true} + ); + this.message_handler = converse.connection.addHandler( + this.handleMUCMessage.bind(this), + null, 'message', null, null, room_jid, + {'matchBareFromJid': true} + ); + }, + + removeHandlers: function () { + if (this.message_handler) { + converse.connection.deleteHandler(this.message_handler); + delete this.message_handler; + } + if (this.presence_handler) { + converse.connection.deleteHandler(this.presence_handler); + delete this.presence_handler; + } + return this; + }, + join: function (nick, password) { + this.registerHandlers(); var stanza = $pres({ 'from': converse.connection.jid, 'to': this.getRoomJIDAndNick(nick) @@ -631,9 +643,6 @@ if (password) { stanza.cnode(Strophe.xmlElement("password", [], password)); } - if (!this.handler) { - this.handler = converse.connection.addHandler(this.handleMUCStanza.bind(this)); - } this.model.set('connection_status', Strophe.Status.CONNECTING); return converse.connection.send(stanza); }, @@ -649,11 +658,12 @@ if (exit_msg !== null) { presence.c("status", exit_msg); } + var that = this; converse.connection.addHandler( function () { - this.model.set('connection_status', Strophe.Status.DISCONNECTED); - }.bind(this), - null, "presence", null, presenceid); + that.model.set('connection_status', Strophe.Status.DISCONNECTED); + that.removeHandlers(); + }, null, "presence", null, presenceid); converse.connection.send(presence); }, @@ -1092,6 +1102,7 @@ } } this.occupantsview.updateOccupantsOnPresence(pres); + return true; }, setChatRoomSubject: function (sender, subject) { @@ -1687,6 +1698,9 @@ attrs = {}; } var fetcher = converse.chatboxviews.showChat.bind(converse.chatboxviews); + if (!attrs.nick) { + attrs.nick = Strophe.getNodeFromJid(converse.bare_jid); + } if (typeof jids === "undefined") { throw new TypeError('rooms.open: You need to provide at least one JID'); } else if (typeof jids === "string") {