2013-11-02 09:56:20 +01:00
|
|
|
(function (root, factory) {
|
2019-07-11 22:50:30 +02:00
|
|
|
define(['es6-promise', 'mock'], factory);
|
|
|
|
}(this, function (Promise, mock) {
|
|
|
|
const _ = converse.env._;
|
|
|
|
const $msg = converse.env.$msg;
|
|
|
|
const $pres = converse.env.$pres;
|
|
|
|
const $iq = converse.env.$iq;
|
|
|
|
const Strophe = converse.env.Strophe;
|
|
|
|
const sizzle = converse.env.sizzle;
|
|
|
|
const u = converse.env.utils;
|
|
|
|
const utils = {};
|
2017-12-03 12:21:57 +01:00
|
|
|
|
2018-12-15 19:30:20 +01:00
|
|
|
utils.waitUntilDiscoConfirmed = async function (_converse, entity_jid, identities, features=[], items=[], type='info') {
|
2019-07-11 22:50:30 +02:00
|
|
|
const iq = await u.waitUntil(() => {
|
2018-09-13 10:51:56 +02:00
|
|
|
return _.filter(
|
|
|
|
_converse.connection.IQ_stanzas,
|
2019-05-20 12:38:33 +02:00
|
|
|
(iq) => sizzle(`iq[to="${entity_jid}"] query[xmlns="http://jabber.org/protocol/disco#${type}"]`, iq).length
|
2018-09-13 10:51:56 +02:00
|
|
|
).pop();
|
2018-11-09 11:55:34 +01:00
|
|
|
}, 300);
|
|
|
|
const stanza = $iq({
|
|
|
|
'type': 'result',
|
|
|
|
'from': entity_jid,
|
2019-06-03 07:58:51 +02:00
|
|
|
'to': 'romeo@montague.lit/orchard',
|
2019-05-20 12:38:33 +02:00
|
|
|
'id': iq.getAttribute('id'),
|
2018-11-09 11:55:34 +01:00
|
|
|
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#'+type});
|
2018-02-08 09:49:05 +01:00
|
|
|
|
2018-11-09 11:55:34 +01:00
|
|
|
_.forEach(identities, function (identity) {
|
|
|
|
stanza.c('identity', {'category': identity.category, 'type': identity.type}).up()
|
|
|
|
});
|
|
|
|
_.forEach(features, function (feature) {
|
|
|
|
stanza.c('feature', {'var': feature}).up();
|
|
|
|
});
|
|
|
|
_.forEach(items, function (item) {
|
|
|
|
stanza.c('item', {'jid': item}).up();
|
|
|
|
});
|
|
|
|
_converse.connection._dataRecv(utils.createRequest(stanza));
|
2017-12-03 12:21:57 +01:00
|
|
|
}
|
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 () {
|
2019-02-24 11:44:34 +01:00
|
|
|
const toggle = document.querySelector(".toggle-controlbox");
|
2018-01-03 20:02:05 +01:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-07-12 10:37:10 +02:00
|
|
|
utils.waitUntilBookmarksReturned = async function (_converse, bookmarks=[]) {
|
|
|
|
await utils.waitUntilDiscoConfirmed(
|
|
|
|
_converse, _converse.bare_jid,
|
|
|
|
[{'category': 'pubsub', 'type': 'pep'}],
|
|
|
|
['http://jabber.org/protocol/pubsub#publish-options']
|
|
|
|
);
|
|
|
|
const IQ_stanzas = _converse.connection.IQ_stanzas;
|
|
|
|
const sent_stanza = await u.waitUntil(
|
|
|
|
() => IQ_stanzas.filter(s => sizzle('items[node="storage:bookmarks"]', s).length).pop()
|
|
|
|
);
|
|
|
|
const stanza = $iq({
|
|
|
|
'to': _converse.connection.jid,
|
|
|
|
'type':'result',
|
|
|
|
'id':sent_stanza.getAttribute('id')
|
|
|
|
}).c('pubsub', {'xmlns': Strophe.NS.PUBSUB})
|
|
|
|
.c('items', {'node': 'storage:bookmarks'})
|
|
|
|
.c('item', {'id': 'current'})
|
|
|
|
.c('storage', {'xmlns': 'storage:bookmarks'});
|
|
|
|
bookmarks.forEach(bookmark => {
|
|
|
|
stanza.c('conference', {
|
|
|
|
'name': bookmark.name,
|
|
|
|
'autojoin': bookmark.autojoin,
|
|
|
|
'jid': bookmark.jid
|
|
|
|
}).c('nick').t(bookmark.nick).up().up()
|
|
|
|
});
|
|
|
|
_converse.connection._dataRecv(utils.createRequest(stanza));
|
|
|
|
await _converse.api.waitUntil('bookmarksInitialized');
|
|
|
|
};
|
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
utils.openChatBoxes = function (converse, amount) {
|
2019-07-12 12:41:21 +02:00
|
|
|
const views = [];
|
|
|
|
for (let i=0; i<amount; i++) {
|
|
|
|
const jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@montague.lit';
|
|
|
|
views.push(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
|
|
|
};
|
|
|
|
|
2018-07-30 18:16:32 +02:00
|
|
|
utils.openChatBoxFor = function (_converse, jid) {
|
|
|
|
_converse.roster.get(jid).trigger("open");
|
2019-07-11 22:50:30 +02:00
|
|
|
return u.waitUntil(() => _converse.chatboxviews.get(jid), 1000);
|
2014-08-01 20:09:32 +02:00
|
|
|
};
|
|
|
|
|
2018-10-22 09:59:53 +02:00
|
|
|
utils.openChatRoomViaModal = async function (_converse, jid, nick='') {
|
2017-02-27 22:09:17 +01:00
|
|
|
// Opens a new chatroom
|
2018-10-22 09:59:53 +02:00
|
|
|
utils.openControlBox(_converse);
|
|
|
|
const roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
|
|
|
|
roomspanel.el.querySelector('.show-add-muc-modal').click();
|
|
|
|
utils.closeControlBox(_converse);
|
|
|
|
const modal = roomspanel.add_room_modal;
|
2019-07-11 22:50:30 +02:00
|
|
|
await u.waitUntil(() => u.isVisible(modal.el), 1500)
|
2018-10-22 09:59:53 +02:00
|
|
|
modal.el.querySelector('input[name="chatroom"]').value = jid;
|
2019-03-26 12:16:18 +01:00
|
|
|
if (nick) {
|
|
|
|
modal.el.querySelector('input[name="nickname"]').value = nick;
|
|
|
|
}
|
2018-10-22 09:59:53 +02:00
|
|
|
modal.el.querySelector('form input[type="submit"]').click();
|
2019-07-11 22:50:30 +02:00
|
|
|
await u.waitUntil(() => _converse.chatboxviews.get(jid), 1000);
|
2018-10-23 03:41:38 +02:00
|
|
|
return _converse.chatboxviews.get(jid);
|
2018-03-11 13:56:53 +01:00
|
|
|
};
|
|
|
|
|
2019-06-11 12:16:27 +02:00
|
|
|
utils.openChatRoom = async function (_converse, room, server) {
|
|
|
|
const model = await _converse.api.rooms.open(`${room}@${server}`);
|
|
|
|
await model.messages.fetched;
|
|
|
|
return model;
|
2014-09-06 12:25:37 +02:00
|
|
|
};
|
|
|
|
|
2019-05-21 12:05:47 +02:00
|
|
|
utils.getRoomFeatures = async function (_converse, room, server, features=[]) {
|
2019-07-10 09:47:13 +02:00
|
|
|
const muc_jid = `${room}@${server}`.toLowerCase();
|
2018-11-09 11:55:34 +01:00
|
|
|
const stanzas = _converse.connection.IQ_stanzas;
|
2019-06-20 12:04:05 +02:00
|
|
|
const index = stanzas.length-1;
|
2019-07-11 22:50:30 +02:00
|
|
|
const stanza = await u.waitUntil(() => _.filter(
|
2019-06-20 12:04:05 +02:00
|
|
|
stanzas.slice(index),
|
2019-05-20 12:38:33 +02:00
|
|
|
iq => iq.querySelector(
|
2019-07-10 09:47:13 +02:00
|
|
|
`iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
|
2019-05-20 12:38:33 +02:00
|
|
|
)).pop());
|
2018-12-13 09:26:11 +01:00
|
|
|
|
|
|
|
const features_stanza = $iq({
|
2019-07-10 09:47:13 +02:00
|
|
|
'from': muc_jid,
|
2018-12-13 09:26:11 +01:00
|
|
|
'id': stanza.getAttribute('id'),
|
2019-06-03 07:58:51 +02:00
|
|
|
'to': 'romeo@montague.lit/desktop',
|
2018-12-13 09:26:11 +01:00
|
|
|
'type': 'result'
|
|
|
|
}).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
|
|
|
|
.c('identity', {
|
|
|
|
'category': 'conference',
|
|
|
|
'name': room[0].toUpperCase() + room.slice(1),
|
|
|
|
'type': 'text'
|
2018-12-13 09:28:29 +01:00
|
|
|
}).up();
|
2018-12-13 11:09:58 +01:00
|
|
|
|
|
|
|
features = features.length ? features : [
|
2018-12-13 09:28:29 +01:00
|
|
|
'http://jabber.org/protocol/muc',
|
|
|
|
'jabber:iq:register',
|
2019-02-14 11:32:45 +01:00
|
|
|
Strophe.NS.SID,
|
2019-02-19 23:08:05 +01:00
|
|
|
Strophe.NS.MAM,
|
2018-12-13 09:28:29 +01:00
|
|
|
'muc_passwordprotected',
|
|
|
|
'muc_hidden',
|
|
|
|
'muc_temporary',
|
|
|
|
'muc_open',
|
|
|
|
'muc_unmoderated',
|
2018-12-17 14:36:24 +01:00
|
|
|
'muc_anonymous']
|
2018-12-13 09:28:29 +01:00
|
|
|
features.forEach(f => features_stanza.c('feature', {'var': f}).up());
|
|
|
|
features_stanza.c('x', { 'xmlns':'jabber:x:data', 'type':'result'})
|
|
|
|
.c('field', {'var':'FORM_TYPE', 'type':'hidden'})
|
|
|
|
.c('value').t('http://jabber.org/protocol/muc#roominfo').up().up()
|
|
|
|
.c('field', {'type':'text-single', 'var':'muc#roominfo_description', 'label':'Description'})
|
|
|
|
.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);
|
2018-12-13 09:26:11 +01:00
|
|
|
_converse.connection._dataRecv(utils.createRequest(features_stanza));
|
2019-05-21 12:05:47 +02:00
|
|
|
};
|
|
|
|
|
2018-12-13 11:09:58 +01:00
|
|
|
|
2019-07-10 09:47:13 +02:00
|
|
|
utils.waitForReservedNick = async function (_converse, muc_jid, nick) {
|
|
|
|
const view = _converse.chatboxviews.get(muc_jid);
|
|
|
|
const stanzas = _converse.connection.IQ_stanzas;
|
2019-07-11 22:50:30 +02:00
|
|
|
const iq = await u.waitUntil(() => _.filter(
|
2018-12-13 09:26:11 +01:00
|
|
|
stanzas,
|
2019-07-10 09:47:13 +02:00
|
|
|
s => sizzle(`iq[to="${muc_jid.toLowerCase()}"] query[node="x-roomuser-item"]`, s).length
|
2018-12-13 09:26:11 +01:00
|
|
|
).pop());
|
|
|
|
// 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.
|
2019-05-20 12:38:33 +02:00
|
|
|
const IQ_id = iq.getAttribute('id');
|
2019-05-21 12:05:47 +02:00
|
|
|
const stanza = $iq({
|
2018-12-13 09:26:11 +01:00
|
|
|
'type': 'result',
|
|
|
|
'id': IQ_id,
|
|
|
|
'from': view.model.get('jid'),
|
|
|
|
'to': _converse.connection.jid
|
|
|
|
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
|
|
|
|
.c('identity', {'category': 'conference', 'name': nick, 'type': 'text'});
|
|
|
|
_converse.connection._dataRecv(utils.createRequest(stanza));
|
2019-07-11 22:50:30 +02:00
|
|
|
return u.waitUntil(() => view.model.get('nick'));
|
2019-07-10 09:47:13 +02:00
|
|
|
};
|
2018-12-13 09:26:11 +01:00
|
|
|
|
2019-07-01 10:44:59 +02:00
|
|
|
|
2019-07-04 14:12:12 +02:00
|
|
|
utils.returnMemberLists = async function (_converse, muc_jid, members=[], affiliations=['member', 'owner', 'admin']) {
|
2019-07-10 09:47:13 +02:00
|
|
|
const stanzas = _converse.connection.IQ_stanzas;
|
2019-07-04 14:12:12 +02:00
|
|
|
|
|
|
|
if (affiliations.includes('member')) {
|
|
|
|
const member_IQ = await u.waitUntil(() => _.filter(
|
|
|
|
stanzas,
|
|
|
|
s => sizzle(`iq[to="${muc_jid}"] query[xmlns="${Strophe.NS.MUC_ADMIN}"] item[affiliation="member"]`, s).length
|
|
|
|
).pop());
|
|
|
|
const member_list_stanza = $iq({
|
|
|
|
'from': 'coven@chat.shakespeare.lit',
|
|
|
|
'id': member_IQ.getAttribute('id'),
|
|
|
|
'to': 'romeo@montague.lit/orchard',
|
|
|
|
'type': 'result'
|
|
|
|
}).c('query', {'xmlns': Strophe.NS.MUC_ADMIN});
|
|
|
|
members.filter(m => m.affiliation === 'member').forEach(m => {
|
|
|
|
member_list_stanza.c('item', {
|
|
|
|
'affiliation': m.affiliation,
|
|
|
|
'jid': m.jid,
|
|
|
|
'nick': m.nick
|
|
|
|
});
|
2019-07-03 14:54:50 +02:00
|
|
|
});
|
2019-07-04 14:12:12 +02:00
|
|
|
_converse.connection._dataRecv(utils.createRequest(member_list_stanza));
|
|
|
|
}
|
2019-07-01 10:44:59 +02:00
|
|
|
|
2019-07-04 14:12:12 +02:00
|
|
|
if (affiliations.includes('admin')) {
|
|
|
|
const admin_IQ = await u.waitUntil(() => _.filter(
|
|
|
|
stanzas,
|
|
|
|
s => sizzle(`iq[to="${muc_jid}"] query[xmlns="${Strophe.NS.MUC_ADMIN}"] item[affiliation="admin"]`, s).length
|
|
|
|
).pop());
|
|
|
|
const admin_list_stanza = $iq({
|
|
|
|
'from': 'coven@chat.shakespeare.lit',
|
|
|
|
'id': admin_IQ.getAttribute('id'),
|
|
|
|
'to': 'romeo@montague.lit/orchard',
|
|
|
|
'type': 'result'
|
|
|
|
}).c('query', {'xmlns': Strophe.NS.MUC_ADMIN});
|
|
|
|
members.filter(m => m.affiliation === 'admin').forEach(m => {
|
|
|
|
admin_list_stanza.c('item', {
|
|
|
|
'affiliation': m.affiliation,
|
|
|
|
'jid': m.jid,
|
|
|
|
'nick': m.nick
|
|
|
|
});
|
|
|
|
});
|
|
|
|
_converse.connection._dataRecv(utils.createRequest(admin_list_stanza));
|
|
|
|
}
|
2019-07-01 10:44:59 +02:00
|
|
|
|
2019-07-04 14:12:12 +02:00
|
|
|
if (affiliations.includes('owner')) {
|
|
|
|
const owner_IQ = await u.waitUntil(() => _.filter(
|
|
|
|
stanzas,
|
|
|
|
s => sizzle(`iq[to="${muc_jid}"] query[xmlns="${Strophe.NS.MUC_ADMIN}"] item[affiliation="owner"]`, s).length
|
|
|
|
).pop());
|
|
|
|
const owner_list_stanza = $iq({
|
|
|
|
'from': 'coven@chat.shakespeare.lit',
|
|
|
|
'id': owner_IQ.getAttribute('id'),
|
|
|
|
'to': 'romeo@montague.lit/orchard',
|
|
|
|
'type': 'result'
|
|
|
|
}).c('query', {'xmlns': Strophe.NS.MUC_ADMIN});
|
|
|
|
members.filter(m => m.affiliation === 'owner').forEach(m => {
|
|
|
|
owner_list_stanza.c('item', {
|
|
|
|
'affiliation': m.affiliation,
|
|
|
|
'jid': m.jid,
|
|
|
|
'nick': m.nick
|
|
|
|
});
|
|
|
|
});
|
|
|
|
_converse.connection._dataRecv(utils.createRequest(owner_list_stanza));
|
|
|
|
}
|
2016-07-20 09:20:56 +02:00
|
|
|
};
|
|
|
|
|
2019-08-08 15:12:18 +02:00
|
|
|
utils.receiveOwnMUCPresence = function (_converse, muc_jid, nick) {
|
2019-07-10 09:47:13 +02:00
|
|
|
const presence = $pres({
|
|
|
|
to: _converse.connection.jid,
|
|
|
|
from: `${muc_jid}/${nick}`,
|
|
|
|
id: u.getUniqueId()
|
|
|
|
}).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
|
|
|
|
.c('item').attrs({
|
|
|
|
affiliation: 'owner',
|
|
|
|
jid: _converse.bare_jid,
|
|
|
|
role: 'moderator'
|
|
|
|
}).up()
|
|
|
|
.c('status').attrs({code:'110'});
|
|
|
|
_converse.connection._dataRecv(utils.createRequest(presence));
|
2019-08-08 15:12:18 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 09:47:13 +02:00
|
|
|
|
2019-08-08 15:12:18 +02:00
|
|
|
utils.openAndEnterChatRoom = async function (_converse, muc_jid, nick, features=[], members=[]) {
|
|
|
|
muc_jid = muc_jid.toLowerCase();
|
|
|
|
const room = Strophe.getNodeFromJid(muc_jid);
|
|
|
|
const server = Strophe.getDomainFromJid(muc_jid);
|
|
|
|
await _converse.api.rooms.open(muc_jid);
|
|
|
|
await utils.getRoomFeatures(_converse, room, server, features);
|
|
|
|
await utils.waitForReservedNick(_converse, muc_jid, nick);
|
|
|
|
// The user has just entered the room (because join was called)
|
|
|
|
// and receives their own presence from the server.
|
|
|
|
// See example 24: https://xmpp.org/extensions/xep-0045.html#enter-pres
|
|
|
|
utils.receiveOwnMUCPresence(_converse, muc_jid, nick);
|
2019-07-10 09:47:13 +02:00
|
|
|
const view = _converse.chatboxviews.get(muc_jid);
|
2019-07-11 22:50:30 +02:00
|
|
|
await u.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.ENTERED));
|
2019-07-26 13:32:21 +02:00
|
|
|
if (_converse.muc_fetch_members) {
|
|
|
|
await utils.returnMemberLists(_converse, muc_jid, members);
|
|
|
|
}
|
2019-07-10 09:47:13 +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++) {
|
2019-06-03 07:58:51 +02:00
|
|
|
jid = names[i].replace(/ /g,'.').toLowerCase() + '@montague.lit';
|
2016-05-30 18:20:23 +02:00
|
|
|
if (!converse.roster.get(jid)) {
|
|
|
|
converse.roster.create({
|
|
|
|
'ask': ask,
|
2019-06-03 07:58:51 +02:00
|
|
|
'name': names[i],
|
2018-11-09 11:55:34 +01:00
|
|
|
'jid': jid,
|
2016-05-30 18:20:23 +02:00
|
|
|
'requesting': requesting,
|
|
|
|
'subscription': subscription
|
|
|
|
});
|
|
|
|
}
|
2013-11-02 09:56:20 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2014-03-05 03:29:10 +01:00
|
|
|
|
2019-02-24 11:44:34 +01:00
|
|
|
utils.waitForRoster = async function (_converse, type='current', length, include_nick=true) {
|
2019-07-11 22:50:30 +02:00
|
|
|
const iq = await u.waitUntil(() =>
|
2019-02-24 11:44:34 +01:00
|
|
|
_.filter(
|
|
|
|
_converse.connection.IQ_stanzas,
|
2019-05-20 12:38:33 +02:00
|
|
|
iq => sizzle(`iq[type="get"] query[xmlns="${Strophe.NS.ROSTER}"]`, iq).length
|
2019-02-24 11:44:34 +01:00
|
|
|
).pop());
|
|
|
|
|
|
|
|
const result = $iq({
|
|
|
|
'to': _converse.connection.jid,
|
|
|
|
'type': 'result',
|
2019-05-20 12:38:33 +02:00
|
|
|
'id': iq.getAttribute('id')
|
2019-02-24 11:44:34 +01:00
|
|
|
}).c('query', {
|
|
|
|
'xmlns': 'jabber:iq:roster'
|
|
|
|
});
|
|
|
|
if (type === 'pending' || type === 'all') {
|
|
|
|
mock.pend_names.slice(0, length).map(name =>
|
|
|
|
result.c('item', {
|
2019-06-03 07:58:51 +02:00
|
|
|
jid: name.replace(/ /g,'.').toLowerCase() + '@montague.lit',
|
2019-02-24 11:44:34 +01:00
|
|
|
name: include_nick ? name : undefined,
|
|
|
|
subscription: 'to'
|
|
|
|
}).up()
|
|
|
|
);
|
|
|
|
} else if (type === 'current' || type === 'all') {
|
|
|
|
mock.cur_names.slice(0, length).map(name =>
|
|
|
|
result.c('item', {
|
2019-06-03 07:58:51 +02:00
|
|
|
jid: name.replace(/ /g,'.').toLowerCase() + '@montague.lit',
|
2019-02-24 11:44:34 +01:00
|
|
|
name: include_nick ? name : undefined,
|
|
|
|
subscription: 'both'
|
|
|
|
}).up()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_converse.connection._dataRecv(utils.createRequest(result));
|
|
|
|
await _converse.api.waitUntil('rosterContactsFetched');
|
|
|
|
};
|
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
utils.createGroupedContacts = function (converse) {
|
2014-08-18 20:37:38 +02:00
|
|
|
/* Create grouped contacts
|
|
|
|
*/
|
2019-02-24 11:44:34 +01:00
|
|
|
let 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({
|
2019-06-03 07:58:51 +02:00
|
|
|
'jid': mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@montague.lit',
|
|
|
|
'subscription': 'both',
|
|
|
|
'ask': null,
|
|
|
|
'groups': name === 'ungrouped'? [] : [name],
|
|
|
|
'name': mock.cur_names[i]
|
2014-08-18 20:37:38 +02:00
|
|
|
});
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2018-10-13 23:25:01 +02:00
|
|
|
utils.sendMessage = function (view, message) {
|
2019-07-12 10:37:10 +02:00
|
|
|
const promise = new Promise(resolve => view.once('messageInserted', resolve));
|
2018-10-13 23:25:01 +02:00
|
|
|
view.el.querySelector('.chat-textarea').value = message;
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyDown({
|
2018-10-13 23:25:01 +02:00
|
|
|
target: view.el.querySelector('textarea.chat-textarea'),
|
2018-01-03 20:02:05 +01:00
|
|
|
preventDefault: _.noop,
|
|
|
|
keyCode: 13
|
|
|
|
});
|
2018-10-13 23:25:01 +02:00
|
|
|
return promise;
|
2014-03-05 03:29:10 +01:00
|
|
|
};
|
2013-11-02 09:56:20 +01:00
|
|
|
return utils;
|
|
|
|
}));
|