From 06141b321296275e4855ae94754bea34ac94ed73 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 29 Mar 2018 10:16:41 +0200 Subject: [PATCH] Move methods from MUC view to model * checkForReservedNick * parseRoomFeatures * sendConfiguration Refs #1032 --- spec/chatroom.js | 5 ++- src/converse-muc-views.js | 83 ++++-------------------------------- src/converse-muc.js | 89 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 79 deletions(-) diff --git a/spec/chatroom.js b/spec/chatroom.js index 57ae97ff8..d879acafb 100644 --- a/spec/chatroom.js +++ b/spec/chatroom.js @@ -264,12 +264,12 @@ ' '+ ' '); - spyOn(chatroomview, 'sendConfiguration').and.callThrough(); + spyOn(chatroomview.model, 'sendConfiguration').and.callThrough(); _converse.connection._dataRecv(test_utils.createRequest(node.firstElementChild)); return test_utils.waitUntil(function () { - return chatroomview.sendConfiguration.calls.count() === 1; + return chatroomview.model.sendConfiguration.calls.count() === 1; }, 300).then(function () { var sent_stanza = sent_IQ_els.pop(); while (sent_stanza.getAttribute('type') !== 'set') { @@ -3203,6 +3203,7 @@ to: 'dummy@localhost', type: 'groupchat' }).c('body').t(message).tree(); + view.handleMUCMessage(msg); expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1); diff --git a/src/converse-muc-views.js b/src/converse-muc-views.js index 750b9e42b..0c6794e21 100644 --- a/src/converse-muc-views.js +++ b/src/converse-muc-views.js @@ -1200,7 +1200,8 @@ */ nick = nick ? nick : this.model.get('nick'); if (!nick) { - return this.checkForReservedNick(); + this.checkForReservedNick(); + return this; } if (this.model.get('connection_status') === converse.ROOMSTATUS.ENTERED) { // We have restored a chat room from session storage, @@ -1314,29 +1315,6 @@ ); }, - sendConfiguration(config, onSuccess, onError) { - /* Send an IQ stanza with the room configuration. - * - * Parameters: - * (Array) config: The room configuration - * (Function) onSuccess: Callback upon succesful IQ response - * The first parameter passed in is IQ containing the - * room configuration. - * The second is the response IQ from the server. - * (Function) onError: Callback upon error IQ response - * The first parameter passed in is IQ containing the - * room configuration. - * The second is the response IQ from the server. - */ - const iq = $iq({to: this.model.get('jid'), type: "set"}) - .c("query", {xmlns: Strophe.NS.MUC_OWNER}) - .c("x", {xmlns: Strophe.NS.XFORM, type: "submit"}); - _.each(config || [], function (node) { iq.cnode(node).up(); }); - onSuccess = _.isUndefined(onSuccess) ? _.noop : _.partial(onSuccess, iq.nodeTree); - onError = _.isUndefined(onError) ? _.noop : _.partial(onError, iq.nodeTree); - return _converse.connection.sendIQ(iq, onSuccess, onError); - }, - saveConfiguration (form) { /* Submit the room configuration form by sending an IQ * stanza to the server. @@ -1350,7 +1328,7 @@ return new Promise((resolve, reject) => { const inputs = form ? sizzle(':input:not([type=button]):not([type=submit])', form) : [], configArray = _.map(inputs, u.webForm2xForm); - this.sendConfiguration(configArray, resolve, reject); + this.model.sendConfiguration(configArray, resolve, reject); this.closeForm(); }); }, @@ -1394,7 +1372,7 @@ } configArray.push(field); if (!--count) { - that.sendConfiguration(configArray, resolve, reject); + that.model.sendConfiguration(configArray, resolve, reject); } }); }); @@ -1434,42 +1412,6 @@ }); }, - parseRoomFeatures (iq) { - /* See http://xmpp.org/extensions/xep-0045.html#disco-roominfo - * - * - * - * - * - * - * - * - * - * - */ - const features = { - 'features_fetched': true, - 'name': iq.querySelector('identity').getAttribute('name') - } - _.each(iq.querySelectorAll('feature'), function (field) { - const fieldname = field.getAttribute('var'); - if (!fieldname.startsWith('muc_')) { - if (fieldname === Strophe.NS.MAM) { - features.mam_enabled = true; - } - return; - } - features[fieldname.replace('muc_', '')] = true; - }); - const desc_field = iq.querySelector('field[var="muc#roominfo_description"] value'); - if (!_.isNull(desc_field)) { - features.description = desc_field.textContent; - } - this.model.save(features); - }, getRoomFeatures () { /* Fetch the room disco info, parse it and then @@ -1479,7 +1421,7 @@ _converse.connection.disco.info( this.model.get('jid'), null, - _.flow(this.parseRoomFeatures.bind(this), resolve), + _.flow(this.model.parseRoomFeatures.bind(this.model), resolve), () => { reject(new Error("Could not parse the room features")) }, 5000 ); @@ -1528,22 +1470,13 @@ checkForReservedNick () { /* User service-discovery to ask the XMPP server whether * this user has a reserved nickname for this room. - * If so, we'll use that, otherwise we render the nickname - * form. + * If so, we'll use that, otherwise we render the nickname form. */ this.showSpinner(); - _converse.connection.sendIQ( - $iq({ - 'to': this.model.get('jid'), - 'from': _converse.connection.jid, - 'type': "get" - }).c("query", { - 'xmlns': Strophe.NS.DISCO_INFO, - 'node': 'x-roomuser-item' - }), + this.model.checkForReservedNick( this.onNickNameFound.bind(this), this.onNickNameNotFound.bind(this) - ); + ) return this; }, diff --git a/src/converse-muc.js b/src/converse-muc.js index dd0db0c7c..c9c5ac91c 100644 --- a/src/converse-muc.js +++ b/src/converse-muc.js @@ -273,6 +273,90 @@ ); }, + sendConfiguration (config, callback, errback) { + /* Send an IQ stanza with the room configuration. + * + * Parameters: + * (Array) config: The room configuration + * (Function) callback: Callback upon succesful IQ response + * The first parameter passed in is IQ containing the + * room configuration. + * The second is the response IQ from the server. + * (Function) errback: Callback upon error IQ response + * The first parameter passed in is IQ containing the + * room configuration. + * The second is the response IQ from the server. + */ + const iq = $iq({to: this.get('jid'), type: "set"}) + .c("query", {xmlns: Strophe.NS.MUC_OWNER}) + .c("x", {xmlns: Strophe.NS.XFORM, type: "submit"}); + _.each(config || [], function (node) { iq.cnode(node).up(); }); + callback = _.isUndefined(callback) ? _.noop : _.partial(callback, iq.nodeTree); + errback = _.isUndefined(errback) ? _.noop : _.partial(errback, iq.nodeTree); + return _converse.connection.sendIQ(iq, callback, errback); + }, + + parseRoomFeatures (iq) { + /* Parses an IQ stanza containing the room's features. + * + * See http://xmpp.org/extensions/xep-0045.html#disco-roominfo + * + * + * + * + * + * + * + * + * + * + */ + const features = { + 'features_fetched': true, + 'name': iq.querySelector('identity').getAttribute('name') + } + _.each(iq.querySelectorAll('feature'), function (field) { + const fieldname = field.getAttribute('var'); + if (!fieldname.startsWith('muc_')) { + if (fieldname === Strophe.NS.MAM) { + features.mam_enabled = true; + } + return; + } + features[fieldname.replace('muc_', '')] = true; + }); + const desc_field = iq.querySelector('field[var="muc#roominfo_description"] value'); + if (!_.isNull(desc_field)) { + features.description = desc_field.textContent; + } + this.save(features); + }, + + checkForReservedNick (callback, errback) { + /* Use service-discovery to ask the XMPP server whether + * this user has a reserved nickname for this room. + * If so, we'll use that, otherwise we render the nickname form. + * + * Parameters: + * (Function) callback: Callback upon succesful IQ response + * (Function) errback: Callback upon error IQ response + */ + _converse.connection.sendIQ( + $iq({ + 'to': this.get('jid'), + 'from': _converse.connection.jid, + 'type': "get" + }).c("query", { + 'xmlns': Strophe.NS.DISCO_INFO, + 'node': 'x-roomuser-item' + }), + callback, errback); + return this; + }, + isUserMentioned (message) { /* Returns a boolean to indicate whether the current user * was mentioned in a message. @@ -295,11 +379,12 @@ return; // The message has no text } if (u.isNewMessage(stanza) && this.newMessageWillBeHidden()) { - this.save({'num_unread_general': this.get('num_unread_general') + 1}); + const settings = {'num_unread_general': this.get('num_unread_general') + 1}; if (this.isUserMentioned(body.textContent)) { - this.save({'num_unread': this.get('num_unread') + 1}); + settings.num_unread = this.get('num_unread') + 1; _converse.incrementMsgCounter(); } + this.save(settings); } },