2016-03-16 12:16:32 +01:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
2017-02-13 15:37:17 +01:00
|
|
|
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
|
2016-03-16 12:16:32 +01:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
|
|
|
/*global define */
|
|
|
|
|
|
|
|
(function (root, factory) {
|
2017-07-13 23:27:24 +02:00
|
|
|
define(["converse-core", "strophe.vcard"], factory);
|
|
|
|
}(this, function (converse) {
|
2016-03-16 12:16:32 +01:00
|
|
|
"use strict";
|
2017-12-02 18:00:18 +01:00
|
|
|
const { Promise, Strophe, _, moment, sizzle } = converse.env;
|
2018-02-23 22:15:04 +01:00
|
|
|
const u = converse.env.utils;
|
2017-12-02 18:00:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
function onVCardData (_converse, jid, iq, callback) {
|
2018-01-30 10:25:08 +01:00
|
|
|
const vcard = iq.querySelector('vCard');
|
|
|
|
if (_.isNull(vcard)) {
|
|
|
|
// Some servers return an empty IQ
|
|
|
|
return onVCardError(_converse, jid, iq, callback);
|
|
|
|
}
|
|
|
|
const img_type = _.get(vcard.querySelector('TYPE'), 'textContent'),
|
2017-12-02 18:00:18 +01:00
|
|
|
img = _.get(vcard.querySelector('BINVAL'), 'textContent'),
|
|
|
|
url = _.get(vcard.querySelector('URL'), 'textContent'),
|
|
|
|
fullname = _.get(vcard.querySelector('FN'), 'textContent');
|
|
|
|
|
2018-02-23 22:15:04 +01:00
|
|
|
if (!u.isSameBareJID(jid, _converse.bare_jid)) {
|
2017-12-02 18:00:18 +01:00
|
|
|
const contact = _converse.roster.get(jid);
|
|
|
|
if (contact) {
|
|
|
|
contact.save({
|
|
|
|
'fullname': fullname || _.get(contact, 'fullname', jid),
|
|
|
|
'image_type': img_type,
|
|
|
|
'image': img,
|
|
|
|
'url': url,
|
|
|
|
'vcard_updated': moment().format()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback({
|
|
|
|
'stanza': iq,
|
2018-02-23 22:15:04 +01:00
|
|
|
'fullname': fullname,
|
2017-12-02 18:00:18 +01:00
|
|
|
'image': img,
|
|
|
|
'image_type': img_type,
|
|
|
|
'url': url
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onVCardError (_converse, jid, iq, errback) {
|
|
|
|
const contact = _converse.roster.get(jid);
|
|
|
|
if (contact) {
|
|
|
|
contact.save({'vcard_updated': moment().format() });
|
|
|
|
}
|
|
|
|
if (errback) { errback({'stanza': iq, 'jid': jid}); }
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVCard (_converse, jid) {
|
|
|
|
/* Request the VCard of another user. Returns a promise.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) jid - The Jabber ID of the user whose VCard
|
|
|
|
* is being requested.
|
|
|
|
*/
|
2018-02-23 22:15:04 +01:00
|
|
|
const to = Strophe.getBareJidFromJid(jid) === _converse.bare_jid ? null : jid;
|
2017-12-02 18:00:18 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!_converse.use_vcards) {
|
|
|
|
if (resolve) { resolve({'jid': jid}); }
|
|
|
|
} else {
|
|
|
|
_converse.connection.vcard.get(
|
|
|
|
_.partial(onVCardData, _converse, jid, _, resolve),
|
2018-02-23 22:15:04 +01:00
|
|
|
to,
|
2017-12-02 18:00:18 +01:00
|
|
|
_.partial(onVCardError, _converse, jid, _, resolve)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateChatBoxFromVCard (_converse, jid) {
|
|
|
|
_converse.api.vcard.get(jid)
|
|
|
|
.then((vcard) => {
|
2018-03-07 10:31:33 +01:00
|
|
|
const chatbox = _converse.chatboxes.getChatBox(jid);
|
2017-12-02 18:00:18 +01:00
|
|
|
if (!_.isUndefined(chatbox)) {
|
|
|
|
chatbox.save(_.pick(vcard, ['fullname', 'url', 'image_type', 'image', 'vcard_updated']));
|
|
|
|
}
|
|
|
|
})
|
2018-03-07 10:31:33 +01:00
|
|
|
.catch((e) => {
|
|
|
|
_converse.log(e, Strophe.LogLevel.ERROR);
|
2017-12-02 18:00:18 +01:00
|
|
|
_converse.log(
|
|
|
|
"updateChatBoxFromVCard: Error occured while attempting to update chatbox with VCard data",
|
|
|
|
Strophe.LogLevel.ERROR
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-16 12:16:32 +01:00
|
|
|
|
2016-12-20 11:42:20 +01:00
|
|
|
converse.plugins.add('converse-vcard', {
|
2016-03-16 12:16:32 +01:00
|
|
|
|
2018-03-27 12:14:57 +02:00
|
|
|
enabled (_converse) {
|
|
|
|
_converse.api.settings.update({'use_vcards': true});
|
|
|
|
return _converse.use_vcards;
|
|
|
|
},
|
|
|
|
|
2016-03-16 12:16:32 +01:00
|
|
|
overrides: {
|
|
|
|
// Overrides mentioned here will be picked up by converse.js's
|
|
|
|
// plugin architecture they will replace existing methods on the
|
|
|
|
// relevant objects or classes.
|
|
|
|
//
|
|
|
|
// New functions which don't exist yet can also be added.
|
|
|
|
|
|
|
|
RosterContacts: {
|
2017-07-10 17:46:22 +02:00
|
|
|
createRequestingContact (presence) {
|
|
|
|
const { _converse } = this.__super__;
|
2017-06-12 20:30:58 +02:00
|
|
|
const bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from'));
|
2017-12-02 18:00:18 +01:00
|
|
|
|
|
|
|
_converse.api.vcard.get(bare_jid)
|
|
|
|
.then(_.partial(_converse.createRequestingContactFromVCard, presence))
|
|
|
|
.catch((vcard) => {
|
2017-07-05 11:33:55 +02:00
|
|
|
_converse.log(
|
2017-12-02 18:00:18 +01:00
|
|
|
`Error while retrieving vcard for ${vcard.jid}`,
|
|
|
|
Strophe.LogLevel.WARN);
|
|
|
|
_converse.createRequestingContactFromVCard(presence, vcard.stanza, vcard.jid);
|
|
|
|
});
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-03-16 12:16:32 +01:00
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this;
|
2016-03-16 12:16:32 +01:00
|
|
|
|
2017-12-02 18:00:18 +01:00
|
|
|
_converse.createRequestingContactFromVCard = function (presence, vcard) {
|
2018-03-14 12:54:22 +01:00
|
|
|
const bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from'));
|
2017-12-02 18:00:18 +01:00
|
|
|
let fullname = vcard.fullname;
|
2017-07-13 23:27:24 +02:00
|
|
|
if (!fullname) {
|
|
|
|
const nick_el = sizzle(`nick[xmlns="${Strophe.NS.NICK}"]`, presence);
|
|
|
|
fullname = nick_el.length ? nick_el[0].textContent : bare_jid;
|
|
|
|
}
|
2017-06-12 20:30:58 +02:00
|
|
|
const user_data = {
|
2017-12-03 11:10:59 +01:00
|
|
|
'jid': bare_jid,
|
|
|
|
'subscription': 'none',
|
|
|
|
'ask': null,
|
|
|
|
'requesting': true,
|
|
|
|
'fullname': fullname,
|
|
|
|
'image': vcard.image,
|
|
|
|
'image_type': vcard.image_type,
|
|
|
|
'url': vcard.url,
|
|
|
|
'vcard_updated': moment().format()
|
2016-03-16 12:16:32 +01:00
|
|
|
};
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.roster.create(user_data);
|
|
|
|
_converse.emit('contactRequest', user_data);
|
2016-03-16 12:16:32 +01:00
|
|
|
};
|
|
|
|
|
2017-07-21 12:41:16 +02:00
|
|
|
/* Event handlers */
|
|
|
|
_converse.on('addClientFeatures', () => {
|
2018-03-27 12:14:57 +02:00
|
|
|
_converse.connection.disco.addFeature(Strophe.NS.VCARD);
|
2017-07-21 12:41:16 +02:00
|
|
|
});
|
|
|
|
|
2018-02-19 22:12:50 +01:00
|
|
|
_converse.on('chatBoxInitialized', (chatbox) => {
|
2017-08-16 14:16:24 +02:00
|
|
|
if (!_converse.use_vcards || chatbox.model.get('type') === 'headline') {
|
|
|
|
return;
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
2017-08-16 14:16:24 +02:00
|
|
|
_converse.api.waitUntil('rosterInitialized').then(() => {
|
|
|
|
const jid = chatbox.model.get('jid'),
|
|
|
|
contact = _converse.roster.get(jid);
|
2017-12-03 19:05:36 +01:00
|
|
|
if (contact && !contact.get('vcard_updated') ||
|
|
|
|
_.isUndefined(contact) && _converse.allow_non_roster_messaging) {
|
|
|
|
|
2017-12-02 18:00:18 +01:00
|
|
|
updateChatBoxFromVCard(_converse, jid);
|
2017-08-16 14:16:24 +02:00
|
|
|
}
|
|
|
|
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
2017-12-03 19:05:36 +01:00
|
|
|
});
|
2016-03-16 12:16:32 +01:00
|
|
|
|
2017-12-02 18:00:18 +01:00
|
|
|
_converse.on('initialized', () => {
|
|
|
|
_converse.roster.on("add", (contact) => {
|
|
|
|
if (!contact.get('vcard_updated')) {
|
|
|
|
_converse.api.vcard.get(contact.get('jid'));
|
|
|
|
}
|
|
|
|
});
|
2016-03-16 14:50:52 +01:00
|
|
|
});
|
|
|
|
|
2017-07-21 12:41:16 +02:00
|
|
|
_converse.on('statusInitialized', function fetchOwnVCard () {
|
2018-02-23 22:15:04 +01:00
|
|
|
if (_.isNil(_converse.xmppstatus.get('vcard_updated'))) {
|
2018-02-19 22:12:50 +01:00
|
|
|
_converse.api.disco.supports(Strophe.NS.VCARD, _converse.domain)
|
|
|
|
.then((result) => {
|
2017-12-03 11:10:59 +01:00
|
|
|
if (result.supported) {
|
2018-02-19 22:12:50 +01:00
|
|
|
_converse.api.vcard.get(_converse.bare_jid)
|
2018-02-23 22:15:04 +01:00
|
|
|
.then((vcard) => _converse.xmppstatus.save(vcard));
|
2018-02-19 22:12:50 +01:00
|
|
|
}})
|
|
|
|
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
2017-12-02 18:00:18 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_.extend(_converse.api, {
|
|
|
|
'vcard': {
|
|
|
|
'get' (jid) {
|
|
|
|
return getVCard(_converse, jid);
|
|
|
|
}
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
2017-07-21 12:41:16 +02:00
|
|
|
});
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|