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) {
|
2018-05-03 13:44:52 +02:00
|
|
|
define(["converse-core", "crypto", "strophe.vcard"], factory);
|
|
|
|
}(this, function (converse, CryptoJS) {
|
2016-03-16 12:16:32 +01:00
|
|
|
"use strict";
|
2018-05-03 18:34:28 +02:00
|
|
|
const { Backbone, Promise, Strophe, SHA1, _, b64_sha1, 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');
|
2018-05-01 14:51:18 +02:00
|
|
|
let result = {};
|
|
|
|
if (!_.isNull(vcard)) {
|
|
|
|
result = {
|
|
|
|
'stanza': iq,
|
|
|
|
'fullname': _.get(vcard.querySelector('FN'), 'textContent'),
|
2018-05-03 13:44:52 +02:00
|
|
|
'image': _.get(vcard.querySelector('PHOTO BINVAL'), 'textContent'),
|
|
|
|
'image_type': _.get(vcard.querySelector('PHOTO TYPE'), 'textContent'),
|
2018-05-01 14:51:18 +02:00
|
|
|
'url': _.get(vcard.querySelector('URL'), 'textContent')
|
|
|
|
};
|
2017-12-02 18:00:18 +01:00
|
|
|
}
|
2018-05-03 13:44:52 +02:00
|
|
|
if (result.image) {
|
|
|
|
const word_array_from_b64 = CryptoJS.enc.Base64.parse(result['image']);
|
|
|
|
result['image_type'] = CryptoJS.SHA1(word_array_from_b64).toString()
|
|
|
|
}
|
2017-12-02 18:00:18 +01:00
|
|
|
if (callback) {
|
2018-05-01 14:51:18 +02:00
|
|
|
callback(result);
|
2017-12-02 18:00:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
2018-05-04 22:50:41 +02:00
|
|
|
_converse.connection.vcard.get(
|
|
|
|
_.partial(onVCardData, _converse, jid, _, resolve),
|
|
|
|
to,
|
|
|
|
_.partial(onVCardError, _converse, jid, _, resolve)
|
|
|
|
);
|
2017-12-02 18:00:18 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-05-04 23:27:08 +02:00
|
|
|
// FIXME: After refactoring, the dependency switches, from
|
|
|
|
// converse-roster to converse-vcard
|
|
|
|
dependencies: ["converse-roster"],
|
|
|
|
|
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
|
|
|
|
2018-05-03 18:34:28 +02:00
|
|
|
_converse.VCards = Backbone.Collection.extend({
|
|
|
|
model: _converse.ModelWithDefaultAvatar,
|
|
|
|
|
|
|
|
initialize () {
|
2018-05-04 22:50:41 +02:00
|
|
|
this.on('add', (vcard) => _converse.api.vcard.update(vcard));
|
2018-05-03 18:34:28 +02: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,
|
2018-05-03 13:44:52 +02:00
|
|
|
'image_hash': vcard.image_hash,
|
2017-12-03 11:10:59 +01:00
|
|
|
'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 */
|
2018-05-03 18:34:28 +02:00
|
|
|
_converse.initVCardCollection = function () {
|
|
|
|
_converse.vcards = new _converse.VCards();
|
|
|
|
_converse.vcards.browserStorage = new Backbone.BrowserStorage.local(b64_sha1(`converse.vcards`));
|
|
|
|
_converse.vcards.fetch();
|
|
|
|
}
|
|
|
|
_converse.api.listen.on('connectionInitialized', _converse.initVCardCollection);
|
|
|
|
|
|
|
|
|
2017-07-21 12:41:16 +02:00
|
|
|
_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
|
|
|
});
|
|
|
|
|
2017-12-02 18:00:18 +01:00
|
|
|
_converse.on('initialized', () => {
|
2018-05-01 14:51:18 +02:00
|
|
|
_converse.roster.on("add", (contact) => _converse.api.vcard.update(contact));
|
2016-03-16 14:50:52 +01:00
|
|
|
});
|
|
|
|
|
2017-07-21 12:41:16 +02:00
|
|
|
_converse.on('statusInitialized', function fetchOwnVCard () {
|
2018-05-01 11:33:32 +02:00
|
|
|
_converse.api.disco.supports(Strophe.NS.VCARD, _converse.domain)
|
|
|
|
.then((result) => {
|
|
|
|
if (result.length) {
|
2018-05-01 14:51:18 +02:00
|
|
|
_converse.api.vcard.update(_converse.xmppstatus);
|
2018-05-01 11:33:32 +02:00
|
|
|
}})
|
|
|
|
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
2017-12-02 18:00:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
_.extend(_converse.api, {
|
|
|
|
'vcard': {
|
2018-05-01 11:33:32 +02:00
|
|
|
'get' (model, force) {
|
|
|
|
if (_.isString(model)) {
|
|
|
|
return getVCard(_converse, model);
|
|
|
|
} else if (!model.get('vcard_updated') || force) {
|
|
|
|
const jid = model.get('jid') || model.get('muc_jid');
|
|
|
|
if (!jid) {
|
|
|
|
throw new Error("No JID to get vcard for!");
|
|
|
|
}
|
|
|
|
return getVCard(_converse, jid);
|
|
|
|
} else {
|
2018-05-01 14:51:18 +02:00
|
|
|
return Promise.resolve({});
|
2018-05-01 11:33:32 +02:00
|
|
|
}
|
2018-05-01 14:51:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'update' (model, force) {
|
2018-05-01 18:18:02 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.get(model, force).then((vcard) => {
|
|
|
|
model.save(_.extend(
|
2018-05-03 13:44:52 +02:00
|
|
|
_.pick(vcard, ['fullname', 'url', 'image_type', 'image', 'image_hash']),
|
2018-05-01 18:18:02 +02:00
|
|
|
{'vcard_updated': moment().format()}
|
|
|
|
));
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
2017-12-02 18:00:18 +01:00
|
|
|
}
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
2017-07-21 12:41:16 +02:00
|
|
|
});
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|