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-02-14 15:08:39 +01:00
|
|
|
define(["converse-core", "strophe.vcard"], factory);
|
2016-12-20 11:42:20 +01:00
|
|
|
}(this, function (converse) {
|
2016-03-16 12:16:32 +01:00
|
|
|
"use strict";
|
2016-12-20 11:42:20 +01:00
|
|
|
var Strophe = converse.env.Strophe,
|
|
|
|
$ = converse.env.jQuery,
|
|
|
|
_ = converse.env._,
|
|
|
|
moment = converse.env.moment;
|
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
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Features: {
|
|
|
|
addClientFeatures: function () {
|
2016-12-20 11:42:20 +01:00
|
|
|
var _converse = this.__super__._converse;
|
2016-08-31 12:06:17 +02:00
|
|
|
this.__super__.addClientFeatures.apply(this, arguments);
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.use_vcards) {
|
|
|
|
_converse.connection.disco.addFeature(Strophe.NS.VCARD);
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
RosterContacts: {
|
|
|
|
createRequestingContact: function (presence) {
|
2016-12-20 11:42:20 +01:00
|
|
|
var _converse = this.__super__._converse;
|
2016-03-16 12:16:32 +01:00
|
|
|
var bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from'));
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.getVCard(
|
2016-03-16 12:16:32 +01:00
|
|
|
bare_jid,
|
2016-12-20 10:30:20 +01:00
|
|
|
_.partial(_converse.createRequestingContactFromVCard, presence),
|
2016-03-16 12:16:32 +01:00
|
|
|
function (iq, jid) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log("Error while retrieving vcard for "+jid);
|
|
|
|
_converse.createRequestingContactFromVCard(presence, iq, jid);
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2016-12-20 11:42:20 +01:00
|
|
|
var _converse = this._converse;
|
2016-03-16 12:16:32 +01:00
|
|
|
this.updateSettings({
|
|
|
|
use_vcards: true,
|
|
|
|
});
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.createRequestingContactFromVCard = function (presence, iq, jid, fullname, img, img_type, url) {
|
2016-03-16 12:16:32 +01:00
|
|
|
var bare_jid = Strophe.getBareJidFromJid(jid);
|
|
|
|
var nick = $(presence).children('nick[xmlns="'+Strophe.NS.NICK+'"]').text();
|
|
|
|
var user_data = {
|
|
|
|
jid: bare_jid,
|
|
|
|
subscription: 'none',
|
|
|
|
ask: null,
|
|
|
|
requesting: true,
|
|
|
|
fullname: fullname || nick || bare_jid,
|
|
|
|
image: img,
|
|
|
|
image_type: img_type,
|
|
|
|
url: url,
|
|
|
|
vcard_updated: moment().format()
|
|
|
|
};
|
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
|
|
|
};
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.onVCardError = function (jid, iq, errback) {
|
|
|
|
var contact = _converse.roster.get(jid);
|
2016-03-16 12:16:32 +01:00
|
|
|
if (contact) {
|
|
|
|
contact.save({ 'vcard_updated': moment().format() });
|
|
|
|
}
|
|
|
|
if (errback) { errback(iq, jid); }
|
|
|
|
};
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.onVCardData = function (jid, iq, callback) {
|
2016-03-16 12:16:32 +01:00
|
|
|
var $vcard = $(iq).find('vCard'),
|
|
|
|
fullname = $vcard.find('FN').text(),
|
|
|
|
img = $vcard.find('BINVAL').text(),
|
|
|
|
img_type = $vcard.find('TYPE').text(),
|
|
|
|
url = $vcard.find('URL').text();
|
|
|
|
if (jid) {
|
2016-12-20 10:30:20 +01:00
|
|
|
var contact = _converse.roster.get(jid);
|
2016-03-16 12:16:32 +01:00
|
|
|
if (contact) {
|
|
|
|
fullname = _.isEmpty(fullname)? contact.get('fullname') || jid: fullname;
|
|
|
|
contact.save({
|
|
|
|
'fullname': fullname,
|
|
|
|
'image_type': img_type,
|
|
|
|
'image': img,
|
|
|
|
'url': url,
|
|
|
|
'vcard_updated': moment().format()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback(iq, jid, fullname, img, img_type, url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.getVCard = function (jid, callback, errback) {
|
2016-03-16 12:16:32 +01:00
|
|
|
/* Request the VCard of another user.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) jid - The Jabber ID of the user whose VCard
|
|
|
|
* is being requested.
|
|
|
|
* (Function) callback - A function to call once the VCard is
|
|
|
|
* returned.
|
|
|
|
* (Function) errback - A function to call if an error occured
|
|
|
|
* while trying to fetch the VCard.
|
|
|
|
*/
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.use_vcards) {
|
2016-03-16 12:16:32 +01:00
|
|
|
if (callback) { callback(null, jid); }
|
|
|
|
} else {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.vcard.get(
|
|
|
|
_.partial(_converse.onVCardData, jid, _, callback),
|
2016-03-16 12:16:32 +01:00
|
|
|
jid,
|
2016-12-20 10:30:20 +01:00
|
|
|
_.partial(_converse.onVCardError, jid, _, errback));
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-19 08:33:46 +01:00
|
|
|
var updateVCardForChatBox = function (chatbox) {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!_converse.use_vcards) { return; }
|
2016-03-16 12:16:32 +01:00
|
|
|
var jid = chatbox.model.get('jid'),
|
2016-12-20 10:30:20 +01:00
|
|
|
contact = _converse.roster.get(jid);
|
2016-03-16 12:16:32 +01:00
|
|
|
if ((contact) && (!contact.get('vcard_updated'))) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.getVCard(
|
2016-03-16 12:16:32 +01:00
|
|
|
jid,
|
|
|
|
function (iq, jid, fullname, image, image_type, url) {
|
|
|
|
chatbox.model.save({
|
|
|
|
'fullname' : fullname || jid,
|
|
|
|
'url': url,
|
|
|
|
'image_type': image_type,
|
|
|
|
'image': image
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log(
|
2016-03-16 12:16:32 +01:00
|
|
|
"updateVCardForChatBox: Error occured while fetching vcard"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.on('chatBoxInitialized', updateVCardForChatBox);
|
2016-03-16 12:16:32 +01:00
|
|
|
|
2016-03-16 14:50:52 +01:00
|
|
|
|
|
|
|
var onContactAdd = function (contact) {
|
|
|
|
if (!contact.get('vcard_updated')) {
|
|
|
|
// This will update the vcard, which triggers a change
|
|
|
|
// request which will rerender the roster contact.
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.getVCard(contact.get('jid'));
|
2016-03-16 14:50:52 +01:00
|
|
|
}
|
|
|
|
};
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.on('initialized', function () {
|
|
|
|
_converse.roster.on("add", onContactAdd);
|
2016-03-16 14:50:52 +01:00
|
|
|
});
|
|
|
|
|
2016-03-16 12:16:32 +01:00
|
|
|
var fetchOwnVCard = function () {
|
2016-12-20 10:30:20 +01:00
|
|
|
if (_converse.xmppstatus.get('fullname') === undefined) {
|
|
|
|
_converse.getVCard(
|
2016-03-16 12:16:32 +01:00
|
|
|
null, // No 'to' attr when getting one's own vCard
|
|
|
|
function (iq, jid, fullname) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.xmppstatus.save({'fullname': fullname});
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.on('statusInitialized', fetchOwnVCard);
|
2016-03-16 12:16:32 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|