Commit 06141b32 authored by JC Brand's avatar JC Brand

Move methods from MUC view to model

* checkForReservedNick
* parseRoomFeatures
* sendConfiguration

Refs #1032
parent 1cec976b
...@@ -264,12 +264,12 @@ ...@@ -264,12 +264,12 @@
' </query>'+ ' </query>'+
' </iq>'); ' </iq>');
spyOn(chatroomview, 'sendConfiguration').and.callThrough(); spyOn(chatroomview.model, 'sendConfiguration').and.callThrough();
_converse.connection._dataRecv(test_utils.createRequest(node.firstElementChild)); _converse.connection._dataRecv(test_utils.createRequest(node.firstElementChild));
return test_utils.waitUntil(function () { return test_utils.waitUntil(function () {
return chatroomview.sendConfiguration.calls.count() === 1; return chatroomview.model.sendConfiguration.calls.count() === 1;
}, 300).then(function () { }, 300).then(function () {
var sent_stanza = sent_IQ_els.pop(); var sent_stanza = sent_IQ_els.pop();
while (sent_stanza.getAttribute('type') !== 'set') { while (sent_stanza.getAttribute('type') !== 'set') {
...@@ -3203,6 +3203,7 @@ ...@@ -3203,6 +3203,7 @@
to: 'dummy@localhost', to: 'dummy@localhost',
type: 'groupchat' type: 'groupchat'
}).c('body').t(message).tree(); }).c('body').t(message).tree();
view.handleMUCMessage(msg); view.handleMUCMessage(msg);
expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1); expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
......
...@@ -1200,7 +1200,8 @@ ...@@ -1200,7 +1200,8 @@
*/ */
nick = nick ? nick : this.model.get('nick'); nick = nick ? nick : this.model.get('nick');
if (!nick) { if (!nick) {
return this.checkForReservedNick(); this.checkForReservedNick();
return this;
} }
if (this.model.get('connection_status') === converse.ROOMSTATUS.ENTERED) { if (this.model.get('connection_status') === converse.ROOMSTATUS.ENTERED) {
// We have restored a chat room from session storage, // We have restored a chat room from session storage,
...@@ -1314,29 +1315,6 @@ ...@@ -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) { saveConfiguration (form) {
/* Submit the room configuration form by sending an IQ /* Submit the room configuration form by sending an IQ
* stanza to the server. * stanza to the server.
...@@ -1350,7 +1328,7 @@ ...@@ -1350,7 +1328,7 @@
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const inputs = form ? sizzle(':input:not([type=button]):not([type=submit])', form) : [], const inputs = form ? sizzle(':input:not([type=button]):not([type=submit])', form) : [],
configArray = _.map(inputs, u.webForm2xForm); configArray = _.map(inputs, u.webForm2xForm);
this.sendConfiguration(configArray, resolve, reject); this.model.sendConfiguration(configArray, resolve, reject);
this.closeForm(); this.closeForm();
}); });
}, },
...@@ -1394,7 +1372,7 @@ ...@@ -1394,7 +1372,7 @@
} }
configArray.push(field); configArray.push(field);
if (!--count) { if (!--count) {
that.sendConfiguration(configArray, resolve, reject); that.model.sendConfiguration(configArray, resolve, reject);
} }
}); });
}); });
...@@ -1434,42 +1412,6 @@ ...@@ -1434,42 +1412,6 @@
}); });
}, },
parseRoomFeatures (iq) {
/* See http://xmpp.org/extensions/xep-0045.html#disco-roominfo
*
* <identity
* category='conference'
* name='A Dark Cave'
* type='text'/>
* <feature var='http://jabber.org/protocol/muc'/>
* <feature var='muc_passwordprotected'/>
* <feature var='muc_hidden'/>
* <feature var='muc_temporary'/>
* <feature var='muc_open'/>
* <feature var='muc_unmoderated'/>
* <feature var='muc_nonanonymous'/>
* <feature var='urn:xmpp:mam:0'/>
*/
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 () { getRoomFeatures () {
/* Fetch the room disco info, parse it and then /* Fetch the room disco info, parse it and then
...@@ -1479,7 +1421,7 @@ ...@@ -1479,7 +1421,7 @@
_converse.connection.disco.info( _converse.connection.disco.info(
this.model.get('jid'), this.model.get('jid'),
null, null,
_.flow(this.parseRoomFeatures.bind(this), resolve), _.flow(this.model.parseRoomFeatures.bind(this.model), resolve),
() => { reject(new Error("Could not parse the room features")) }, () => { reject(new Error("Could not parse the room features")) },
5000 5000
); );
...@@ -1528,22 +1470,13 @@ ...@@ -1528,22 +1470,13 @@
checkForReservedNick () { checkForReservedNick () {
/* User service-discovery to ask the XMPP server whether /* User service-discovery to ask the XMPP server whether
* this user has a reserved nickname for this room. * this user has a reserved nickname for this room.
* If so, we'll use that, otherwise we render the nickname * If so, we'll use that, otherwise we render the nickname form.
* form.
*/ */
this.showSpinner(); this.showSpinner();
_converse.connection.sendIQ( this.model.checkForReservedNick(
$iq({
'to': this.model.get('jid'),
'from': _converse.connection.jid,
'type': "get"
}).c("query", {
'xmlns': Strophe.NS.DISCO_INFO,
'node': 'x-roomuser-item'
}),
this.onNickNameFound.bind(this), this.onNickNameFound.bind(this),
this.onNickNameNotFound.bind(this) this.onNickNameNotFound.bind(this)
); )
return this; return this;
}, },
......
...@@ -273,6 +273,90 @@ ...@@ -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
*
* <identity
* category='conference'
* name='A Dark Cave'
* type='text'/>
* <feature var='http://jabber.org/protocol/muc'/>
* <feature var='muc_passwordprotected'/>
* <feature var='muc_hidden'/>
* <feature var='muc_temporary'/>
* <feature var='muc_open'/>
* <feature var='muc_unmoderated'/>
* <feature var='muc_nonanonymous'/>
* <feature var='urn:xmpp:mam:0'/>
*/
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) { isUserMentioned (message) {
/* Returns a boolean to indicate whether the current user /* Returns a boolean to indicate whether the current user
* was mentioned in a message. * was mentioned in a message.
...@@ -295,11 +379,12 @@ ...@@ -295,11 +379,12 @@
return; // The message has no text return; // The message has no text
} }
if (u.isNewMessage(stanza) && this.newMessageWillBeHidden()) { 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)) { if (this.isUserMentioned(body.textContent)) {
this.save({'num_unread': this.get('num_unread') + 1}); settings.num_unread = this.get('num_unread') + 1;
_converse.incrementMsgCounter(); _converse.incrementMsgCounter();
} }
this.save(settings);
} }
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment