2013-11-02 09:56:20 +01:00
|
|
|
(function (root, factory) {
|
2018-01-04 12:41:03 +01:00
|
|
|
define(['converse', 'es6-promise', 'mock', 'wait-until-promise'], factory);
|
|
|
|
}(this, function (converse_api, Promise, mock, waitUntilPromise) {
|
2017-04-05 11:01:31 +02:00
|
|
|
var _ = converse_api.env._;
|
2017-05-03 09:06:28 +02:00
|
|
|
var $msg = converse_api.env.$msg;
|
2016-07-20 09:20:56 +02:00
|
|
|
var $pres = converse_api.env.$pres;
|
|
|
|
var $iq = converse_api.env.$iq;
|
2015-02-01 16:15:34 +01:00
|
|
|
var Strophe = converse_api.env.Strophe;
|
2018-01-03 20:02:05 +01:00
|
|
|
var u = converse_api.env.utils;
|
2013-11-02 09:56:20 +01:00
|
|
|
var utils = {};
|
2014-10-24 18:58:42 +02:00
|
|
|
|
2017-04-05 11:01:31 +02:00
|
|
|
if (typeof window.Promise === 'undefined') {
|
|
|
|
waitUntilPromise.setPromiseImplementation(Promise);
|
|
|
|
}
|
2017-12-03 12:21:57 +01:00
|
|
|
utils.waitUntil = waitUntilPromise.default;
|
|
|
|
|
2018-03-29 20:22:05 +02:00
|
|
|
utils.waitUntilDiscoConfirmed = function (_converse, entity_jid, identities, features, items, type) {
|
|
|
|
if (_.isNil(type)) {
|
|
|
|
type = 'info';
|
|
|
|
}
|
2017-12-03 12:21:57 +01:00
|
|
|
var IQ_disco, stanza;
|
|
|
|
return utils.waitUntil(function () {
|
|
|
|
IQ_disco = _.filter(_converse.connection.IQ_stanzas, function (iq) {
|
2018-03-29 20:22:05 +02:00
|
|
|
return iq.nodeTree.querySelector('query[xmlns="http://jabber.org/protocol/disco#'+type+'"]') &&
|
2018-02-07 17:30:44 +01:00
|
|
|
iq.nodeTree.getAttribute('to') === entity_jid;
|
2017-12-03 12:21:57 +01:00
|
|
|
}).pop();
|
|
|
|
return !_.isUndefined(IQ_disco);
|
|
|
|
}, 300).then(function () {
|
|
|
|
var info_IQ_id = IQ_disco.nodeTree.getAttribute('id');
|
2018-02-08 09:49:05 +01:00
|
|
|
var stanza = $iq({
|
2017-12-03 12:21:57 +01:00
|
|
|
'type': 'result',
|
2018-02-08 09:49:05 +01:00
|
|
|
'from': entity_jid,
|
2017-12-03 12:21:57 +01:00
|
|
|
'to': 'dummy@localhost/resource',
|
|
|
|
'id': info_IQ_id
|
2018-03-29 20:22:05 +02:00
|
|
|
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#'+type});
|
2018-02-08 09:49:05 +01:00
|
|
|
|
|
|
|
_.forEach(identities, function (identity) {
|
2018-03-29 20:22:05 +02:00
|
|
|
stanza.c('identity', {'category': identity.category, 'type': identity.type}).up()
|
2018-02-08 09:49:05 +01:00
|
|
|
});
|
|
|
|
_.forEach(features, function (feature) {
|
|
|
|
stanza.c('feature', {'var': feature}).up();
|
|
|
|
});
|
2018-03-29 20:22:05 +02:00
|
|
|
_.forEach(items, function (item) {
|
|
|
|
stanza.c('item', {'jid': item}).up();
|
|
|
|
});
|
2017-12-03 12:21:57 +01:00
|
|
|
_converse.connection._dataRecv(utils.createRequest(stanza));
|
|
|
|
});
|
|
|
|
}
|
2017-04-05 11:01:31 +02:00
|
|
|
|
2014-10-24 18:58:42 +02:00
|
|
|
utils.createRequest = function (iq) {
|
|
|
|
iq = typeof iq.tree == "function" ? iq.tree() : iq;
|
|
|
|
var req = new Strophe.Request(iq, function() {});
|
2016-04-28 16:52:27 +02:00
|
|
|
req.getResponse = function () {
|
2014-10-24 18:58:42 +02:00
|
|
|
var env = new Strophe.Builder('env', {type: 'mock'}).tree();
|
|
|
|
env.appendChild(iq);
|
|
|
|
return env;
|
|
|
|
};
|
|
|
|
return req;
|
|
|
|
};
|
2015-01-09 09:02:35 +01:00
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
utils.closeAllChatBoxes = function (converse) {
|
2013-11-03 11:02:25 +01:00
|
|
|
var i, chatbox;
|
|
|
|
for (i=converse.chatboxes.models.length-1; i>-1; i--) {
|
2013-11-02 09:56:20 +01:00
|
|
|
chatbox = converse.chatboxes.models[i];
|
2014-05-27 18:34:22 +02:00
|
|
|
converse.chatboxviews.get(chatbox.get('id')).close();
|
2013-11-02 09:56:20 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.openControlBox = function () {
|
2018-01-03 20:02:05 +01:00
|
|
|
var toggle = document.querySelector(".toggle-controlbox");
|
|
|
|
if (!u.isVisible(document.querySelector("#controlbox"))) {
|
|
|
|
if (!u.isVisible(toggle)) {
|
|
|
|
u.removeClass('hidden', toggle);
|
2014-02-28 03:04:52 +01:00
|
|
|
}
|
2018-01-03 20:02:05 +01:00
|
|
|
toggle.click();
|
2013-11-02 09:56:20 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-11-03 10:38:48 +01:00
|
|
|
utils.closeControlBox = function () {
|
2018-01-03 20:02:05 +01:00
|
|
|
var controlbox = document.querySelector("#controlbox");
|
|
|
|
if (u.isVisible(controlbox)) {
|
2018-02-07 13:29:54 +01:00
|
|
|
var button = controlbox.querySelector(".close-chatbox-button");
|
|
|
|
if (!_.isNull(button)) {
|
|
|
|
button.click();
|
|
|
|
}
|
2013-11-03 10:38:48 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
utils.openChatBoxes = function (converse, amount) {
|
2014-03-04 12:22:49 +01:00
|
|
|
var i = 0, jid, views = [];
|
2013-11-03 10:38:48 +01:00
|
|
|
for (i; i<amount; i++) {
|
2014-06-30 20:26:45 +02:00
|
|
|
jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
|
2014-08-03 23:02:25 +02:00
|
|
|
views[i] = converse.roster.get(jid).trigger("open");
|
2013-11-03 10:38:48 +01:00
|
|
|
}
|
2014-03-04 12:22:49 +01:00
|
|
|
return views;
|
2013-11-03 10:38:48 +01:00
|
|
|
};
|
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
utils.openChatBoxFor = function (converse, jid) {
|
2014-08-03 23:02:25 +02:00
|
|
|
return converse.roster.get(jid).trigger("open");
|
2014-08-01 20:09:32 +02:00
|
|
|
};
|
|
|
|
|
2018-03-11 13:56:53 +01:00
|
|
|
utils.openChatRoomViaModal = function (_converse, jid, nick) {
|
2017-02-27 22:09:17 +01:00
|
|
|
// Opens a new chatroom
|
2018-03-11 13:56:53 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
utils.openControlBox(_converse);
|
|
|
|
var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
|
|
|
|
roomspanel.el.querySelector('.trigger-add-chatrooms-modal').click();
|
|
|
|
utils.closeControlBox(_converse);
|
|
|
|
const modal = roomspanel.add_room_modal;
|
|
|
|
utils.waitUntil(function () {
|
|
|
|
return u.isVisible(modal.el);
|
|
|
|
}, 1000).then(function () {
|
|
|
|
modal.el.querySelector('input[name="chatroom"]').value = jid;
|
|
|
|
modal.el.querySelector('form input[type="submit"]').click();
|
|
|
|
resolve();
|
|
|
|
}).catch(_.partial(console.error, _));
|
|
|
|
}).catch(_.partial(console.error, _));
|
|
|
|
};
|
|
|
|
|
|
|
|
utils.openChatRoom = function (_converse, room, server, nick) {
|
|
|
|
_converse.api.rooms.open(`${room}@${server}`);
|
2014-09-06 12:25:37 +02:00
|
|
|
};
|
|
|
|
|
2018-03-11 13:56:53 +01:00
|
|
|
utils.openAndEnterChatRoom = function (_converse, room, server, nick) {
|
2018-04-30 15:23:33 +02:00
|
|
|
let last_stanza;
|
|
|
|
|
2017-07-12 09:55:43 +02:00
|
|
|
return new Promise(function (resolve, reject) {
|
2018-03-11 13:56:53 +01:00
|
|
|
_converse.api.rooms.open(`${room}@${server}`);
|
2018-04-30 15:23:33 +02:00
|
|
|
const view = _converse.chatboxviews.get((room+'@'+server).toLowerCase());
|
2017-07-12 09:55:43 +02:00
|
|
|
// We pretend this is a new room, so no disco info is returned.
|
2018-04-30 15:23:33 +02:00
|
|
|
last_stanza = _.last(_converse.connection.IQ_stanzas).nodeTree;
|
|
|
|
const IQ_id = last_stanza.getAttribute('id');
|
|
|
|
const features_stanza = $iq({
|
2017-12-22 22:24:22 +01:00
|
|
|
'from': room+'@'+server,
|
2017-07-12 09:55:43 +02:00
|
|
|
'id': IQ_id,
|
2018-04-30 15:23:33 +02:00
|
|
|
'to': nick+'@'+server,
|
2017-07-12 09:55:43 +02:00
|
|
|
'type': 'error'
|
|
|
|
}).c('error', {'type': 'cancel'})
|
|
|
|
.c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
|
2018-03-11 13:56:53 +01:00
|
|
|
_converse.connection._dataRecv(utils.createRequest(features_stanza));
|
2016-12-05 07:03:44 +01:00
|
|
|
|
2018-04-30 15:23:33 +02:00
|
|
|
utils.waitUntil(() => {
|
|
|
|
return _.filter(
|
|
|
|
_converse.connection.IQ_stanzas, (node) => node.nodeTree.querySelector('query').getAttribute('node') === 'x-roomuser-item'
|
|
|
|
).length
|
2017-07-12 09:55:43 +02:00
|
|
|
}).then(function () {
|
2018-04-30 15:23:33 +02:00
|
|
|
const last_stanza = _.filter(
|
|
|
|
_converse.connection.IQ_stanzas, (node) => node.nodeTree.querySelector('query').getAttribute('node') === 'x-roomuser-item'
|
|
|
|
).pop().nodeTree;
|
|
|
|
|
2017-07-12 09:55:43 +02:00
|
|
|
// The XMPP server returns the reserved nick for this user.
|
2018-04-30 15:23:33 +02:00
|
|
|
const IQ_id = last_stanza.getAttribute('id');
|
|
|
|
const stanza = $iq({
|
2017-07-12 09:55:43 +02:00
|
|
|
'type': 'result',
|
|
|
|
'id': IQ_id,
|
|
|
|
'from': view.model.get('jid'),
|
2018-03-11 13:56:53 +01:00
|
|
|
'to': _converse.connection.jid
|
2017-07-12 09:55:43 +02:00
|
|
|
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
|
|
|
|
.c('identity', {'category': 'conference', 'name': nick, 'type': 'text'});
|
2018-03-11 13:56:53 +01:00
|
|
|
_converse.connection._dataRecv(utils.createRequest(stanza));
|
2017-07-12 09:55:43 +02:00
|
|
|
// The user has just entered the room (because join was called)
|
|
|
|
// and receives their own presence from the server.
|
|
|
|
// See example 24: http://xmpp.org/extensions/xep-0045.html#enter-pres
|
|
|
|
var presence = $pres({
|
2018-03-11 13:56:53 +01:00
|
|
|
to: _converse.connection.jid,
|
2017-07-12 09:55:43 +02:00
|
|
|
from: room+'@'+server+'/'+nick,
|
|
|
|
id: 'DC352437-C019-40EC-B590-AF29E879AF97'
|
|
|
|
}).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
|
|
|
|
.c('item').attrs({
|
|
|
|
affiliation: 'member',
|
2018-03-11 13:56:53 +01:00
|
|
|
jid: _converse.bare_jid,
|
2017-12-22 13:02:24 +01:00
|
|
|
role: 'participant'
|
2017-07-12 09:55:43 +02:00
|
|
|
}).up()
|
|
|
|
.c('status').attrs({code:'110'});
|
2018-03-11 13:56:53 +01:00
|
|
|
_converse.connection._dataRecv(utils.createRequest(presence));
|
2017-07-12 09:55:43 +02:00
|
|
|
resolve();
|
2018-04-30 15:23:33 +02:00
|
|
|
});
|
|
|
|
});
|
2016-07-20 09:20:56 +02:00
|
|
|
};
|
|
|
|
|
2014-06-30 19:55:26 +02:00
|
|
|
utils.clearBrowserStorage = function () {
|
|
|
|
window.localStorage.clear();
|
|
|
|
window.sessionStorage.clear();
|
2014-07-04 20:37:37 +02:00
|
|
|
return this;
|
2014-06-30 19:55:26 +02:00
|
|
|
};
|
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
utils.clearChatBoxMessages = function (converse, jid) {
|
2014-03-04 14:54:36 +01:00
|
|
|
var view = converse.chatboxviews.get(jid);
|
2018-01-04 12:41:03 +01:00
|
|
|
view.el.querySelector('.chat-content').innerHTML = '';
|
2014-04-19 05:12:24 +02:00
|
|
|
view.model.messages.reset();
|
2014-06-30 19:21:16 +02:00
|
|
|
view.model.messages.browserStorage._clear();
|
2013-11-03 12:24:18 +01:00
|
|
|
};
|
|
|
|
|
2016-11-02 21:19:17 +01:00
|
|
|
utils.createContacts = function (converse, type, length) {
|
2014-08-18 20:37:38 +02:00
|
|
|
/* Create current (as opposed to requesting or pending) contacts
|
|
|
|
* for the user's roster.
|
|
|
|
*
|
|
|
|
* These contacts are not grouped. See below.
|
|
|
|
*/
|
2017-04-05 11:01:31 +02:00
|
|
|
var names, jid, subscription, requesting, ask;
|
2014-07-04 20:37:37 +02:00
|
|
|
if (type === 'requesting') {
|
|
|
|
names = mock.req_names;
|
|
|
|
subscription = 'none';
|
|
|
|
requesting = true;
|
|
|
|
ask = null;
|
|
|
|
} else if (type === 'pending') {
|
|
|
|
names = mock.pend_names;
|
|
|
|
subscription = 'none';
|
|
|
|
requesting = false;
|
|
|
|
ask = 'subscribe';
|
2014-08-18 20:37:38 +02:00
|
|
|
} else if (type === 'current') {
|
2014-07-04 20:37:37 +02:00
|
|
|
names = mock.cur_names;
|
|
|
|
subscription = 'both';
|
|
|
|
requesting = false;
|
|
|
|
ask = null;
|
2014-08-18 20:37:38 +02:00
|
|
|
} else if (type === 'all') {
|
2016-11-03 11:01:09 +01:00
|
|
|
this.createContacts(converse, 'current')
|
|
|
|
.createContacts(converse, 'requesting')
|
|
|
|
.createContacts(converse, 'pending');
|
2014-08-18 20:37:38 +02:00
|
|
|
return this;
|
|
|
|
} else {
|
2017-04-05 11:01:31 +02:00
|
|
|
throw Error("Need to specify the type of contact to create");
|
2014-07-04 20:37:37 +02:00
|
|
|
}
|
2014-08-18 20:37:38 +02:00
|
|
|
|
|
|
|
if (typeof length === 'undefined') {
|
|
|
|
length = names.length;
|
|
|
|
}
|
2017-04-05 11:01:31 +02:00
|
|
|
for (var i=0; i<length; i++) {
|
2016-05-30 18:20:23 +02:00
|
|
|
jid = names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
|
|
|
|
if (!converse.roster.get(jid)) {
|
|
|
|
converse.roster.create({
|
|
|
|
'ask': ask,
|
|
|
|
'fullname': names[i],
|
|
|
|
'jid': jid,
|
|
|
|
'requesting': requesting,
|
|
|
|
'subscription': subscription
|
|
|
|
});
|
|
|
|
}
|
2013-11-02 09:56:20 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2014-03-05 03:29:10 +01:00
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
utils.createGroupedContacts = function (converse) {
|
2014-08-18 20:37:38 +02:00
|
|
|
/* Create grouped contacts
|
|
|
|
*/
|
|
|
|
var i=0, j=0;
|
2017-12-22 14:38:23 +01:00
|
|
|
_.each(_.keys(mock.groups), function (name) {
|
2014-08-18 20:37:38 +02:00
|
|
|
j = i;
|
|
|
|
for (i=j; i<j+mock.groups[name]; i++) {
|
|
|
|
converse.roster.create({
|
|
|
|
jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
|
|
|
|
subscription: 'both',
|
|
|
|
ask: null,
|
|
|
|
groups: name === 'ungrouped'? [] : [name],
|
|
|
|
fullname: mock.cur_names[i]
|
|
|
|
});
|
|
|
|
}
|
2017-12-22 14:38:23 +01:00
|
|
|
});
|
2014-08-18 20:37:38 +02:00
|
|
|
};
|
|
|
|
|
2017-05-03 09:06:28 +02:00
|
|
|
utils.createChatMessage = function (_converse, sender_jid, message) {
|
|
|
|
return $msg({
|
|
|
|
from: sender_jid,
|
|
|
|
to: _converse.connection.jid,
|
|
|
|
type: 'chat',
|
|
|
|
id: (new Date()).getTime()
|
|
|
|
})
|
|
|
|
.c('body').t(message).up()
|
|
|
|
.c('active', {'xmlns': Strophe.NS.CHATSTATES}).tree();
|
|
|
|
}
|
|
|
|
|
2014-03-05 03:29:10 +01:00
|
|
|
utils.sendMessage = function (chatboxview, message) {
|
2018-01-03 20:02:05 +01:00
|
|
|
chatboxview.el.querySelector('.chat-textarea').value = message;
|
|
|
|
chatboxview.keyPressed({
|
|
|
|
target: chatboxview.el.querySelector('textarea.chat-textarea'),
|
|
|
|
preventDefault: _.noop,
|
|
|
|
keyCode: 13
|
|
|
|
});
|
2014-03-05 03:29:10 +01:00
|
|
|
};
|
2013-11-02 09:56:20 +01:00
|
|
|
return utils;
|
|
|
|
}));
|