Commit 55fce912 authored by JC Brand's avatar JC Brand Committed by GitHub

Merge branch 'master' into async-await

parents 35b7dbe3 c583678c
# Changelog
## 4.1.0 (Unreleased)
## 4.0.4 (Unreleased)
- Use [Lerna](https://lernajs.io/) to create the @converse/headless package
- Use ES2015 modules instead of UMD.
- #1257: Prefer 'probably' over 'maybe' when evaluating audio play support.
- #1257 Prefer 'probably' over 'maybe' when evaluating audio play support.
- #1261 File upload not working
## 4.0.3 (2018-10-22)
......
This diff is collapsed.
......@@ -3178,16 +3178,13 @@
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/backbone/-/backbone-1.3.3.tgz",
"integrity": "sha1-TMgOp8sWMaxHSInOQPL4vGg7KZk=",
"dev": true,
"requires": {
"underscore": ">=1.8.3"
}
},
"backbone.browserStorage": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/backbone.browserStorage/-/backbone.browserStorage-0.0.4.tgz",
"integrity": "sha512-4LvDP2IkOu9Nt6kj6ft4moKmqKqm3c0WY3c/aie0Cf374wjFuO7vCh/afcKdu1YSuVsryM8yH90/J7yqWbaPHw==",
"dev": true,
"version": "github:jcbrand/Backbone.browserStorage#03bfa13f68b71f97be361840adc5a5064f57b47e",
"from": "github:jcbrand/Backbone.browserStorage#03bfa13f68b71f97be361840adc5a5064f57b47e",
"requires": {
"backbone": "~1.x.x",
"underscore": ">=1.4.0"
......@@ -15220,8 +15217,7 @@
"underscore": {
"version": "1.8.3",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz",
"integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=",
"dev": true
"integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI="
},
"underscore-contrib": {
"version": "0.3.0",
......
......@@ -1020,47 +1020,41 @@
it("will clear any other chat status notifications",
mock.initConverseWithPromises(
null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
function (done, _converse) {
async function (done, _converse) {
test_utils.createContacts(_converse, 'current');
_converse.emit('rosterContactsFetched');
test_utils.openControlBox();
const sender_jid = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
let view;
// See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
spyOn(_converse, 'emit');
test_utils.openChatBoxFor(_converse, sender_jid)
.then(() => {
view = _converse.chatboxviews.get(sender_jid);
expect(view.el.querySelectorAll('.chat-event').length).toBe(0);
// Insert <composing> message, to also check that
// text messages are inserted correctly with
// temporary chat events in the chat contents.
const msg = $msg({
'to': _converse.bare_jid,
'xmlns': 'jabber:client',
'from': sender_jid,
'type': 'chat'})
.c('composing', {'xmlns': Strophe.NS.CHATSTATES}).up()
.tree();
_converse.chatboxes.onMessage(msg);
return test_utils.waitUntil(() => view.model.messages.length);
}).then(() => {
expect(view.el.querySelectorAll('.chat-state-notification').length).toBe(1);
const msg = $msg({
from: sender_jid,
to: _converse.connection.jid,
type: 'chat',
id: (new Date()).getTime()
}).c('body').c('inactive', {'xmlns': Strophe.NS.CHATSTATES}).tree();
_converse.chatboxes.onMessage(msg);
return test_utils.waitUntil(() => (view.model.messages.length > 1));
}).then(() => {
expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
expect($(view.el).find('.chat-state-notification').length).toBe(0);
done();
});
await test_utils.openChatBoxFor(_converse, sender_jid);
const view = _converse.chatboxviews.get(sender_jid);
expect(view.el.querySelectorAll('.chat-event').length).toBe(0);
// Insert <composing> message, to also check that
// text messages are inserted correctly with
// temporary chat events in the chat contents.
let msg = $msg({
'to': _converse.bare_jid,
'xmlns': 'jabber:client',
'from': sender_jid,
'type': 'chat'})
.c('composing', {'xmlns': Strophe.NS.CHATSTATES}).up()
.tree();
_converse.chatboxes.onMessage(msg);
await test_utils.waitUntil(() => view.model.messages.length);
expect(view.el.querySelectorAll('.chat-state-notification').length).toBe(1);
msg = $msg({
from: sender_jid,
to: _converse.connection.jid,
type: 'chat',
id: (new Date()).getTime()
}).c('body').c('inactive', {'xmlns': Strophe.NS.CHATSTATES}).tree();
_converse.chatboxes.onMessage(msg);
await test_utils.waitUntil(() => (view.model.messages.length > 1));
expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
expect(view.el.querySelectorAll('.chat-state-notification').length).toBe(0);
done();
}));
});
......
......@@ -3737,9 +3737,9 @@
}));
it("contains a link to a modal which can list groupchats publically available on the server",
mock.initConverseWithPromises(
null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
function (done, _converse) {
mock.initConverseWithPromises(
null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
var sendIQ = _converse.connection.sendIQ;
var sent_stanza, IQ_id;
......@@ -3753,53 +3753,49 @@
roomspanel.el.querySelector('.show-list-muc-modal').click();
test_utils.closeControlBox(_converse);
const modal = roomspanel.list_rooms_modal;
test_utils.waitUntil(() => u.isVisible(modal.el), 1000)
.then(() => {
spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(() => Promise.resolve());
roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
// See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(0);
const input = modal.el.querySelector('input[name="server"]').value = 'chat.shakespear.lit';
modal.el.querySelector('input[type="submit"]').click();
return test_utils.waitUntil(() => _converse.chatboxes.length);
}).then(() => {
expect(sent_stanza.toLocaleString()).toBe(
`<iq from="dummy@localhost/resource" id="${IQ_id}" to="chat.shakespear.lit" type="get" xmlns="jabber:client">`+
`<query xmlns="http://jabber.org/protocol/disco#items"/>`+
`</iq>`
);
await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(() => Promise.resolve());
roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
// See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(0);
const input = modal.el.querySelector('input[name="server"]').value = 'chat.shakespear.lit';
modal.el.querySelector('input[type="submit"]').click();
await test_utils.waitUntil(() => _converse.chatboxes.length);
expect(sent_stanza.toLocaleString()).toBe(
`<iq from="dummy@localhost/resource" id="${IQ_id}" to="chat.shakespear.lit" type="get" xmlns="jabber:client">`+
`<query xmlns="http://jabber.org/protocol/disco#items"/>`+
`</iq>`
);
var iq = $iq({
from:'muc.localhost',
to:'dummy@localhost/pda',
id: IQ_id,
type:'result'
}).c('query')
.c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
.c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
.c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
.c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
_converse.connection._dataRecv(test_utils.createRequest(iq));
expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(5);
const rooms = modal.el.querySelectorAll('.available-chatrooms li');
expect(rooms[0].textContent.trim()).toBe("Groupchats found:");
expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
expect(rooms[3].textContent.trim()).toBe("The Palace");
expect(rooms[4].textContent.trim()).toBe("Macbeth's Castle");
rooms[4].querySelector('.open-room').click();
return test_utils.waitUntil(() => _converse.chatboxes.length > 1);
}).then(() => {
expect(sizzle('.chatroom', _converse.el).filter(u.isVisible).length).toBe(1); // There should now be an open chatroom
var view = _converse.chatboxviews.get('inverness@chat.shakespeare.lit');
expect(view.el.querySelector('.chat-head-chatroom').textContent.trim()).toBe("Macbeth's Castle");
done();
}).catch(_.partial(console.error, _));
const iq = $iq({
from:'muc.localhost',
to:'dummy@localhost/pda',
id: IQ_id,
type:'result'
}).c('query')
.c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
.c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
.c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
.c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
_converse.connection._dataRecv(test_utils.createRequest(iq));
await test_utils.waitUntil(() => modal.el.querySelectorAll('.available-chatrooms li').length === 5);
const rooms = modal.el.querySelectorAll('.available-chatrooms li');
expect(rooms[0].textContent.trim()).toBe("Groupchats found:");
expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
expect(rooms[3].textContent.trim()).toBe("The Palace");
expect(rooms[4].textContent.trim()).toBe("Macbeth's Castle");
rooms[4].querySelector('.open-room').click();
await test_utils.waitUntil(() => _converse.chatboxes.length > 1);
expect(sizzle('.chatroom', _converse.el).filter(u.isVisible).length).toBe(1); // There should now be an open chatroom
var view = _converse.chatboxviews.get('inverness@chat.shakespeare.lit');
expect(view.el.querySelector('.chat-head-chatroom').textContent.trim()).toBe("Macbeth's Castle");
done();
}));
it("shows the number of unread mentions received",
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -533,6 +533,9 @@ converse.plugins.add('converse-chatview', {
prev_msg_date = _.isNull(prev_msg_el) ? null : prev_msg_el.getAttribute('data-isodate'),
next_msg_date = next_msg_el.getAttribute('data-isodate');
if (_.isNull(prev_msg_date) && _.isNull(next_msg_date)) {
return;
}
if (_.isNull(prev_msg_date) || moment(next_msg_date).isAfter(prev_msg_date, 'day')) {
const day_date = moment(next_msg_date).startOf('day');
next_msg_el.insertAdjacentHTML('beforeBegin',
......@@ -722,8 +725,15 @@ converse.plugins.add('converse-chatview', {
*/
const view = new _converse.MessageView({'model': message});
await view.render();
this.clearChatStateNotification(message);
if (!view.el.innerHTML) {
// An "inactive" CSN message (for example) will have an
// empty body. No need to then continue.
return _converse.log(
"Not inserting a message with empty element",
Strophe.LogLevel.INFO
);
}
this.insertMessage(view);
this.insertDayIndicator(view.el);
this.setScrollPosition(view.el);
......
......@@ -60,6 +60,10 @@ converse.plugins.add('converse-message-view', {
if (this.model.isOnlyChatStateNotification()) {
this.renderChatStateNotification()
} else if (this.model.get('file') && !this.model.get('oob_url')) {
if (!this.model.file) {
_converse.log("Attempted to render a file upload message with no file data");
return this.el;
}
this.renderFileUploadProgresBar();
} else if (this.model.get('type') === 'error') {
this.renderErrorMessage();
......@@ -204,7 +208,7 @@ converse.plugins.add('converse-message-view', {
renderFileUploadProgresBar () {
const msg = u.stringToElement(tpl_file_progress(
_.extend(this.model.toJSON(), {
'filesize': filesize(this.model.get('file').size),
'filesize': filesize(this.model.file.size),
})));
this.replaceElement(msg);
this.renderAvatar();
......
......@@ -367,16 +367,14 @@ converse.plugins.add('converse-muc-views', {
updateRoomsList () {
/* Send an IQ stanza to the server asking for all groupchats
*/
_converse.connection.sendIQ(
$iq({
'to': this.model.get('muc_domain'),
'from': _converse.connection.jid,
'type': "get"
}).c("query", {xmlns: Strophe.NS.DISCO_ITEMS}),
this.onRoomsFound.bind(this),
this.informNoRoomsFound.bind(this),
5000
);
const iq = $iq({
'to': this.model.get('muc_domain'),
'from': _converse.connection.jid,
'type': "get"
}).c("query", {xmlns: Strophe.NS.DISCO_ITEMS});
_converse.api.sendIQ(iq)
.then(iq => this.onRoomsFound(iq))
.catch(iq => this.informNoRoomsFound())
},
showRooms (ev) {
......@@ -805,7 +803,7 @@ converse.plugins.add('converse-muc-views', {
const item = $build("item", {nick, role});
const iq = $iq({to: groupchat, type: "set"}).c("query", {xmlns: Strophe.NS.MUC_ADMIN}).cnode(item.node);
if (reason !== null) { iq.c("reason", reason); }
return _converse.connection.sendIQ(iq, onSuccess, onError);
return _converse.api.sendIQ(iq).then(onSuccess).catch(onError);
},
verifyRoles (roles) {
......
......@@ -68,10 +68,6 @@ converse.plugins.add('converse-chatboxes', {
this.setVCard();
if (this.get('file')) {
this.on('change:put', this.uploadFile, this);
if (!_.includes([_converse.SUCCESS, _converse.FAILURE], this.get('upload'))) {
this.getRequestSlotURL();
}
}
if (this.isOnlyChatStateNotification()) {
window.setTimeout(this.destroy.bind(this), 20000);
......@@ -132,20 +128,20 @@ converse.plugins.add('converse-chatboxes', {
*
* https://xmpp.org/extensions/xep-0363.html#request
*/
const file = this.get('file');
return new Promise((resolve, reject) => {
const iq = converse.env.$iq({
'from': _converse.jid,
'to': this.get('slot_request_url'),
'type': 'get'
}).c('request', {
'xmlns': Strophe.NS.HTTPUPLOAD,
'filename': file.name,
'size': file.size,
'content-type': file.type
})
_converse.connection.sendIQ(iq, resolve, reject);
});
if (_.isNil(this.file)) {
return Promise.reject(new Error("file is undefined"));
}
const iq = converse.env.$iq({
'from': _converse.jid,
'to': this.get('slot_request_url'),
'type': 'get'
}).c('request', {
'xmlns': Strophe.NS.HTTPUPLOAD,
'filename': this.file.name,
'size': this.file.size,
'content-type': this.file.type
})
return _converse.api.sendIQ(iq);
},
async getRequestSlotURL () {
......@@ -420,8 +416,8 @@ converse.plugins.add('converse-chatboxes', {
async sendFiles (files) {
const result = await _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain);
const item = result.pop(),
const result = await _converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain),
item = result.pop(),
data = item.dataforms.where({'FORM_TYPE': {'value': Strophe.NS.HTTPUPLOAD, 'type': "hidden"}}).pop(),
max_file_size = window.parseInt(_.get(data, 'attributes.max-file-size.value')),
slot_request_url = _.get(item, 'id');
......@@ -429,7 +425,7 @@ converse.plugins.add('converse-chatboxes', {
if (!slot_request_url) {
this.messages.create({
'message': __("Sorry, looks like file upload is not supported by your server."),
'type': 'error',
'type': 'error'
});
return;
}
......@@ -438,17 +434,20 @@ converse.plugins.add('converse-chatboxes', {
return this.messages.create({
'message': __('The size of your file, %1$s, exceeds the maximum allowed by your server, which is %2$s.',
file.name, filesize(max_file_size)),
'type': 'error',
'type': 'error'
});
} else {
this.messages.create(
const message = this.messages.create(
_.extend(
this.getOutgoingMessageAttributes(), {
'file': file,
'progress': 0,
'slot_request_url': slot_request_url
})
}), {'silent': true}
);
message.file = file;
this.messages.trigger('add', message);
message.getRequestSlotURL();
}
});
},
......
......@@ -1641,6 +1641,7 @@ _converse.api = {
*/
'send' (stanza) {
_converse.connection.send(stanza);
_converse.emit('send', stanza);
},
/**
......@@ -1650,9 +1651,10 @@ _converse.api = {
* @returns {Promise} A promise which resolves when we receive a `result` stanza
* or is rejected when we receive an `error` stanza.
*/
'sendIQ' (stanza) {
'sendIQ' (stanza, timeout) {
return new Promise((resolve, reject) => {
_converse.connection.sendIQ(stanza, resolve, reject, _converse.IQ_TIMEOUT);
_converse.connection.sendIQ(stanza, resolve, reject, timeout || _converse.IQ_TIMEOUT);
_converse.emit('send', stanza);
});
}
};
......
......@@ -99,9 +99,8 @@ function queryForArchivedMessages (_converse, options, callback, errback) {
return true;
}, Strophe.NS.MAM);
_converse.connection.sendIQ(
stanza,
function (iq) {
_converse.api.sendIQ(stanza, _converse.message_archiving_timeout)
.then(iq => {
_converse.connection.deleteHandler(message_handler);
if (_.isFunction(callback)) {
const set = iq.querySelector('set');
......@@ -112,13 +111,13 @@ function queryForArchivedMessages (_converse, options, callback, errback) {
}
callback(messages, rsm);
}
},
function () {
}).catch(e => {
_converse.connection.deleteHandler(message_handler);
if (_.isFunction(errback)) { errback.apply(this, arguments); }
},
_converse.message_archiving_timeout
);
if (_.isFunction(errback)) {
errback.apply(this, arguments);
}
return;
});
}
......@@ -333,7 +332,7 @@ converse.plugins.add('converse-mam', {
message_archiving_timeout: 8000, // Time (in milliseconds) to wait before aborting MAM request
});
_converse.onMAMError = function (model, iq) {
_converse.onMAMError = function (iq) {
if (iq.querySelectorAll('feature-not-implemented').length) {
_converse.log(
"Message Archive Management (XEP-0313) not supported by this server",
......@@ -365,17 +364,14 @@ converse.plugins.add('converse-mam', {
'xmlns':Strophe.NS.MAM,
'default':_converse.message_archiving
});
_.each(preference.children, function (child) {
stanza.cnode(child).up();
});
_converse.connection.sendIQ(stanza, _.partial(function (feature, iq) {
// XXX: Strictly speaking, the server should respond with the updated prefs
// (see example 18: https://xmpp.org/extensions/xep-0313.html#config)
// but Prosody doesn't do this, so we don't rely on it.
feature.save({'preferences': {'default':_converse.message_archiving}});
}, feature),
_converse.onMAMError
);
_.each(preference.children, child => stanza.cnode(child).up());
// XXX: Strictly speaking, the server should respond with the updated prefs
// (see example 18: https://xmpp.org/extensions/xep-0313.html#config)
// but Prosody doesn't do this, so we don't rely on it.
_converse.api.sendIQ(stanza)
.then(() => feature.save({'preferences': {'default':_converse.message_archiving}}))
.catch(_converse.onMAMError);
} else {
feature.save({'preferences': {'default':_converse.message_archiving}});
}
......@@ -388,11 +384,9 @@ converse.plugins.add('converse-mam', {
prefs['default'] !== _converse.message_archiving && // eslint-disable-line dot-notation
!_.isUndefined(_converse.message_archiving) ) {
// Ask the server for archiving preferences
_converse.connection.sendIQ(
$iq({'type': 'get'}).c('prefs', {'xmlns': Strophe.NS.MAM}),
_.partial(_converse.onMAMPreferences, feature),
_.partial(_converse.onMAMError, feature)
);
_converse.api.sendIQ($iq({'type': 'get'}).c('prefs', {'xmlns': Strophe.NS.MAM}))
.then(_.partial(_converse.onMAMPreferences, feature))
.catch(_converse.onMAMError);
}
});
......
......@@ -618,16 +618,10 @@ converse.plugins.add('converse-muc', {
* Returns a promise which resolves once the response IQ
* has been received.
*/
return new Promise((resolve, reject) => {
_converse.connection.sendIQ(
$iq({
'to': this.get('jid'),
'type': "get"
}).c("query", {xmlns: Strophe.NS.MUC_OWNER}),
resolve,
reject
);
});
return _converse.api.sendIQ(
$iq({'to': this.get('jid'), 'type': "get"})
.c("query", {xmlns: Strophe.NS.MUC_OWNER})
);
},
sendConfiguration (config, callback, errback) {
......@@ -650,7 +644,7 @@ converse.plugins.add('converse-muc', {
_.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);
return _converse.api.sendIQ(iq).then(callback).catch(errback);
},
saveAffiliationAndRole (pres) {
......@@ -683,19 +677,17 @@ converse.plugins.add('converse-muc', {
* (Object) member: Map containing the member's jid and
* optionally a reason and affiliation.
*/
return new Promise((resolve, reject) => {
const iq = $iq({to: this.get('jid'), type: "set"})
.c("query", {xmlns: Strophe.NS.MUC_ADMIN})
.c("item", {
'affiliation': member.affiliation || affiliation,
'nick': member.nick,
'jid': member.jid
});
if (!_.isUndefined(member.reason)) {
iq.c("reason", member.reason);
}
_converse.connection.sendIQ(iq, resolve, reject);
});
const iq = $iq({to: this.get('jid'), type: "set"})
.c("query", {xmlns: Strophe.NS.MUC_ADMIN})
.c("item", {
'affiliation': member.affiliation || affiliation,
'nick': member.nick,
'jid': member.jid
});
if (!_.isUndefined(member.reason)) {
iq.c("reason", member.reason);
}
return _converse.api.sendIQ(iq);
},
setAffiliations (members) {
......
......@@ -329,7 +329,7 @@ converse.plugins.add('converse-roster', {
const iq = $iq({type: 'set'})
.c('query', {xmlns: Strophe.NS.ROSTER})
.c('item', {jid: this.get('jid'), subscription: "remove"});
_converse.connection.sendIQ(iq, callback, errback);
_converse.api.sendIQ(iq).then(callback).catch(errback);
return this;
}
});
......@@ -451,7 +451,7 @@ converse.plugins.add('converse-roster', {
this.addContactToRoster(jid, name, groups, attributes).then(handler, handler);
},
sendContactAddIQ (jid, name, groups, callback, errback) {
sendContactAddIQ (jid, name, groups) {
/* Send an IQ stanza to the XMPP server to add a new roster contact.
*
* Parameters:
......@@ -462,14 +462,14 @@ converse.plugins.add('converse-roster', {
* (Function) errback - A function to call if an error occurred
*/
name = _.isEmpty(name)? jid: name;
const iq = $iq({type: 'set'})
.c('query', {xmlns: Strophe.NS.ROSTER})
const iq = $iq({'type': 'set'})
.c('query', {'xmlns': Strophe.NS.ROSTER})
.c('item', { jid, name });
_.each(groups, function (group) { iq.c('group').t(group).up(); });
_converse.connection.sendIQ(iq, callback, errback);
_.each(groups, group => iq.c('group').t(group).up());
_converse.api.sendIQ(iq);
},
addContactToRoster (jid, name, groups, attributes) {
async addContactToRoster (jid, name, groups, attributes) {
/* Adds a RosterContact instance to _converse.roster and
* registers the contact on the XMPP server.
* Returns a promise which is resolved once the XMPP server has
......@@ -481,27 +481,22 @@ converse.plugins.add('converse-roster', {
* (Array of Strings) groups - Any roster groups the user might belong to
* (Object) attributes - Any additional attributes to be stored on the user's model.
*/
return new Promise((resolve, reject) => {
groups = groups || [];
this.sendContactAddIQ(jid, name, groups,
() => {
const contact = this.create(_.assignIn({
'ask': undefined,
'nickname': name,
groups,
jid,
'requesting': false,
'subscription': 'none'
}, attributes), {sort: false});
resolve(contact);
},
function (err) {
alert(__('Sorry, there was an error while trying to add %1$s as a contact.', name));
_converse.log(err, Strophe.LogLevel.ERROR);
resolve(err);
}
);
});
groups = groups || [];
try {
await this.sendContactAddIQ(jid, name, groups);
} catch (e) {
_converse.log(e, Strophe.LogLevel.ERROR);
alert(__('Sorry, there was an error while trying to add %1$s as a contact.', name));
return e;
}
return this.create(_.assignIn({
'ask': undefined,
'nickname': name,
groups,
jid,
'requesting': false,
'subscription': 'none'
}, attributes), {'sort': false});
},
subscribeBack (bare_jid, presence) {
......@@ -570,24 +565,26 @@ converse.plugins.add('converse-roster', {
return _converse.api.disco.stream.getFeature('ver', 'urn:xmpp:features:rosterver') && this.data.get('version');
},
fetchFromServer () {
async fetchFromServer () {
/* Fetch the roster from the XMPP server */
return new Promise((resolve, reject) => {
const iq = $iq({
'type': 'get',
'id': _converse.connection.getUniqueId('roster')
}).c('query', {xmlns: Strophe.NS.ROSTER});
if (this.rosterVersioningSupported()) {
iq.attrs({'ver': this.data.get('version')});
}
const callback = _.flow(this.onReceivedFromServer.bind(this), resolve);
const errback = function (iq) {
const errmsg = "Error while trying to fetch roster from the server";
_converse.log(errmsg, Strophe.LogLevel.ERROR);
reject(new Error(errmsg));
}
return _converse.connection.sendIQ(iq, callback, errback);
});
const stanza = $iq({
'type': 'get',
'id': _converse.connection.getUniqueId('roster')
}).c('query', {xmlns: Strophe.NS.ROSTER});
if (this.rosterVersioningSupported()) {
stanza.attrs({'ver': this.data.get('version')});
}
let iq;
try {
iq = await _converse.api.sendIQ(stanza);
} catch (e) {
_converse.log(e, Strophe.LogLevel.ERROR);
return _converse.log(
"Error while trying to fetch roster from the server",
Strophe.LogLevel.ERROR
);
}
return this.onReceivedFromServer(iq);
},
onReceivedFromServer (iq) {
......@@ -646,7 +643,7 @@ converse.plugins.add('converse-roster', {
createRequestingContact (presence) {
const bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from')),
nickname = _.get(sizzle(`nick[xmlns="${Strophe.NS.NICK}"]`, presence).pop(), 'textContent', null);
nickname = _.get(sizzle(`nick[xmlns="${Strophe.NS.NICK}"]`, presence).pop(), 'textContent', null);
const user_data = {
'jid': bare_jid,
'subscription': 'none',
......
......@@ -56,7 +56,7 @@ converse.plugins.add('converse-vcard', {
});
function onVCardData (jid, iq, callback) {
async function onVCardData (jid, iq) {
const vcard = iq.querySelector('vCard');
let result = {};
if (!_.isNull(vcard)) {
......@@ -75,24 +75,10 @@ converse.plugins.add('converse-vcard', {
}
if (result.image) {
const buffer = u.base64ToArrayBuffer(result['image']);
crypto.subtle.digest('SHA-1', buffer)
.then(ab => {
result['image_hash'] = u.arrayBufferToHex(ab);
if (callback) callback(result);
});
} else {
if (callback) callback(result);
}
}
function onVCardError (jid, iq, errback) {
if (errback) {
errback({
'stanza': iq,
'jid': jid,
'vcard_error': moment().format()
});
const ab = await crypto.subtle.digest('SHA-1', buffer);
result['image_hash'] = u.arrayBufferToHex(ab);
}
return result;
}
function createStanza (type, jid, vcard_el) {
......@@ -113,7 +99,7 @@ converse.plugins.add('converse-vcard', {
return _converse.api.sendIQ(createStanza("set", jid, vcard_el));
}
function getVCard (_converse, jid) {
async function getVCard (_converse, jid) {
/* Request the VCard of another user. Returns a promise.
*
* Parameters:
......@@ -121,14 +107,17 @@ converse.plugins.add('converse-vcard', {
* is being requested.
*/
const to = Strophe.getBareJidFromJid(jid) === _converse.bare_jid ? null : jid;
return new Promise((resolve, reject) => {
_converse.connection.sendIQ(
createStanza("get", to),
_.partial(onVCardData, jid, _, resolve),
_.partial(onVCardError, jid, _, resolve),
_converse.IQ_TIMEOUT
);
});
let iq;
try {
iq = await _converse.api.sendIQ(createStanza("get", to))
} catch (iq) {
return {
'stanza': iq,
'jid': jid,
'vcard_error': moment().format()
}
}
return onVCardData(jid, iq);
}
/* Event handlers */
......
{
"name": "@converse/headless",
"version": "4.0.3",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"backbone.browserStorage": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/backbone.browserStorage/-/backbone.browserStorage-0.0.4.tgz",
"integrity": "sha512-4LvDP2IkOu9Nt6kj6ft4moKmqKqm3c0WY3c/aie0Cf374wjFuO7vCh/afcKdu1YSuVsryM8yH90/J7yqWbaPHw==",
"requires": {
"backbone": "~1.x.x",
"underscore": ">=1.4.0"
},
"dependencies": {
"backbone": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/backbone/-/backbone-1.3.3.tgz",
"integrity": "sha1-TMgOp8sWMaxHSInOQPL4vGg7KZk=",
"requires": {
"underscore": ">=1.8.3"
}
},
"underscore": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.9.1.tgz",
"integrity": "sha512-5/4etnCkd9c8gwgowi5/om/mYO5ajCaOgdzj/oW+0eQV9WxKBDZw5+ycmKmeaTXjInS/W0BzpGLo2xR2aBwZdg=="
}
}
}
}
}
......@@ -23,7 +23,7 @@
"gitHead": "9641dcdc820e029b05930479c242d2b707bbe8e2",
"dependencies": {
"backbone": "1.3.3",
"backbone.browserStorage": "0.0.4",
"backbone.browserStorage": "jcbrand/Backbone.browserStorage#03bfa13f68b71f97be361840adc5a5064f57b47e",
"es6-promise": "^4.1.0",
"filesize": "^3.6.1",
"lodash": "^4.17.10",
......
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