Commit 2d511a5e authored by JC Brand's avatar JC Brand

Make `openAndEnterChatRoom` and async function

parent c9546109
...@@ -116,87 +116,80 @@ ...@@ -116,87 +116,80 @@
return _converse.api.rooms.open(`${room}@${server}`); return _converse.api.rooms.open(`${room}@${server}`);
}; };
utils.openAndEnterChatRoom = function (_converse, room, server, nick) { utils.openAndEnterChatRoom = async function (_converse, room, server, nick) {
let view;
const room_jid = `${room}@${server}`.toLowerCase(); const room_jid = `${room}@${server}`.toLowerCase();
const stanzas = _converse.connection.IQ_stanzas; const stanzas = _converse.connection.IQ_stanzas;
await _converse.api.rooms.open(room_jid);
const view = _converse.chatboxviews.get(room_jid);
let stanza = await utils.waitUntil(() => _.get(_.filter(
stanzas,
iq => iq.nodeTree.querySelector(
`iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
)).pop(), 'nodeTree'));
return _converse.api.rooms.open(room_jid).then(() => { const features_stanza = $iq({
view = _converse.chatboxviews.get(room_jid); 'from': room_jid,
return utils.waitUntil(() => _.get(_.filter( 'id': stanza.getAttribute('id'),
stanzas, 'to': 'dummy@localhost/desktop',
iq => iq.nodeTree.querySelector( 'type': 'result'
`iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]` }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
)).pop(), 'nodeTree')); .c('identity', {
}).then(stanza => { 'category': 'conference',
const features_stanza = $iq({ 'name': room[0].toUpperCase() + room.slice(1),
'from': room_jid, 'type': 'text'
'id': stanza.getAttribute('id'), }).up()
'to': 'dummy@localhost/desktop', .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
'type': 'result' .c('feature', {'var': 'jabber:iq:register'}).up()
}).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'}) .c('feature', {'var': 'muc_passwordprotected'}).up()
.c('identity', { .c('feature', {'var': 'muc_hidden'}).up()
'category': 'conference', .c('feature', {'var': 'muc_temporary'}).up()
'name': room[0].toUpperCase() + room.slice(1), .c('feature', {'var': 'muc_open'}).up()
'type': 'text' .c('feature', {'var': 'muc_unmoderated'}).up()
}).up() .c('feature', {'var': 'muc_nonanonymous'})
.c('feature', {'var': 'http://jabber.org/protocol/muc'}).up() .c('x', { 'xmlns':'jabber:x:data', 'type':'result'})
.c('feature', {'var': 'jabber:iq:register'}).up() .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
.c('feature', {'var': 'muc_passwordprotected'}).up() .c('value').t('http://jabber.org/protocol/muc#roominfo').up().up()
.c('feature', {'var': 'muc_hidden'}).up() .c('field', {'type':'text-single', 'var':'muc#roominfo_description', 'label':'Description'})
.c('feature', {'var': 'muc_temporary'}).up() .c('value').t('This is the description').up().up()
.c('feature', {'var': 'muc_open'}).up() .c('field', {'type':'text-single', 'var':'muc#roominfo_occupants', 'label':'Number of occupants'})
.c('feature', {'var': 'muc_unmoderated'}).up() .c('value').t(0);
.c('feature', {'var': 'muc_nonanonymous'}) _converse.connection._dataRecv(utils.createRequest(features_stanza));
.c('x', { 'xmlns':'jabber:x:data', 'type':'result'}) const iq = await utils.waitUntil(() => _.filter(
.c('field', {'var':'FORM_TYPE', 'type':'hidden'}) stanzas,
.c('value').t('http://jabber.org/protocol/muc#roominfo').up().up() s => sizzle(`iq[to="${room_jid}"] query[node="x-roomuser-item"]`, s.nodeTree).length
.c('field', {'type':'text-single', 'var':'muc#roominfo_description', 'label':'Description'}) ).pop());
.c('value').t('This is the description').up().up()
.c('field', {'type':'text-single', 'var':'muc#roominfo_occupants', 'label':'Number of occupants'})
.c('value').t(0);
_converse.connection._dataRecv(utils.createRequest(features_stanza));
return utils.waitUntil(() => _.filter(
stanzas,
s => sizzle(`iq[to="${room_jid}"] query[node="x-roomuser-item"]`, s.nodeTree).length
).pop()
);
}).then(iq => {
// We remove the stanza, otherwise we might get stale stanzas returned in our filter above.
stanzas.splice(stanzas.indexOf(iq), 1)
// The XMPP server returns the reserved nick for this user. // We remove the stanza, otherwise we might get stale stanzas returned in our filter above.
const IQ_id = iq.nodeTree.getAttribute('id'); stanzas.splice(stanzas.indexOf(iq), 1)
const stanza = $iq({
'type': 'result', // The XMPP server returns the reserved nick for this user.
'id': IQ_id, const IQ_id = iq.nodeTree.getAttribute('id');
'from': view.model.get('jid'), stanza = $iq({
'to': _converse.connection.jid 'type': 'result',
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'}) 'id': IQ_id,
.c('identity', {'category': 'conference', 'name': nick, 'type': 'text'}); 'from': view.model.get('jid'),
_converse.connection._dataRecv(utils.createRequest(stanza)); 'to': _converse.connection.jid
return utils.waitUntil(() => view.model.get('nick')); }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
}).then(() => { .c('identity', {'category': 'conference', 'name': nick, 'type': 'text'});
// The user has just entered the room (because join was called) _converse.connection._dataRecv(utils.createRequest(stanza));
// and receives their own presence from the server. await utils.waitUntil(() => view.model.get('nick'));
// See example 24: http://xmpp.org/extensions/xep-0045.html#enter-pres
var presence = $pres({ // The user has just entered the room (because join was called)
to: _converse.connection.jid, // and receives their own presence from the server.
from: `${room_jid}/${nick}`, // See example 24: http://xmpp.org/extensions/xep-0045.html#enter-pres
id: u.getUniqueId() var presence = $pres({
}).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'}) to: _converse.connection.jid,
.c('item').attrs({ from: `${room_jid}/${nick}`,
affiliation: 'owner', id: u.getUniqueId()
jid: _converse.bare_jid, }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
role: 'moderator' .c('item').attrs({
}).up() affiliation: 'owner',
.c('status').attrs({code:'110'}); jid: _converse.bare_jid,
_converse.connection._dataRecv(utils.createRequest(presence)); role: 'moderator'
return utils.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.ENTERED)); }).up()
}).catch(e => { .c('status').attrs({code:'110'});
console.error(e); _converse.connection._dataRecv(utils.createRequest(presence));
throw e; await utils.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.ENTERED));
});
}; };
utils.clearBrowserStorage = function () { utils.clearBrowserStorage = function () {
......
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