Commit 08222182 authored by JC Brand's avatar JC Brand

Move VCard functionality into separate plugin

parent 5651f763
......@@ -58,6 +58,7 @@ require.config({
"converse-register": "src/converse-register",
"converse-rosterview": "src/converse-rosterview",
"converse-templates": "src/converse-templates",
"converse-vcard": "src/converse-vcard",
// Off-the-record-encryption
"bigint": "src/bigint",
......@@ -230,8 +231,9 @@ if (typeof define !== 'undefined') {
// translations that you care about.
"converse-chatview", // Renders standalone chat boxes for single user chat
"converse-mam",
"converse-mam", // XEP-0313 Message Archive Management
"converse-muc", // XEP-0045 Multi-user chat
"converse-vcard", // XEP-0054 VCard-temp
"converse-otr", // Off-the-record encryption for one-on-one messages
"converse-controlbox", // The control box
"converse-register", // XEP-0077 In-band registration
......
This diff is collapsed.
......@@ -10,6 +10,7 @@
);
} (this, function ($, mock, test_utils) {
"use strict";
var Strophe = converse_api.env.Strophe;
var $iq = converse_api.env.$iq;
var $pres = converse_api.env.$pres;
// See:
......@@ -173,7 +174,9 @@
*/
expect(contact.subscribe).toHaveBeenCalled();
expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
"<presence to='contact@example.org' type='subscribe' xmlns='jabber:client'/>"
"<presence to='contact@example.org' type='subscribe' xmlns='jabber:client'>"+
"<nick xmlns='http://jabber.org/protocol/nick'>Max Mustermann</nick>"+
"</presence>"
);
/* As a result, the user's server MUST initiate a second roster
* push to all of the user's available resources that have
......@@ -503,16 +506,18 @@
runs(function () {
spyOn(converse, "emit");
/*
* <presence
* from='user@example.com'
* to='contact@example.org'
* type='subscribe'/>
*/
* <presence
* from='user@example.com'
* to='contact@example.org'
* type='subscribe'/>
*/
var stanza = $pres({
'to': converse.bare_jid,
'from': 'contact@example.org',
'type': 'subscribe'
});
}).c('nick', {
'xmlns': Strophe.NS.NICK,
}).t('Clint Contact');
this.connection._dataRecv(test_utils.createRequest(stanza));
expect(converse.emit).toHaveBeenCalledWith('contactRequest', jasmine.any(Object));
var $header = $('a:contains("Contact requests")');
......
......@@ -91,7 +91,8 @@
this.model.on('change:status', this.onStatusChanged, this);
this.model.on('showHelpMessages', this.showHelpMessages, this);
this.model.on('sendMessage', this.sendMessage, this);
this.updateVCard().render().fetchMessages().insertIntoPage().hide();
this.render().fetchMessages().insertIntoPage().hide();
converse.emit('chatBoxInitialized', this);
},
render: function () {
......@@ -802,29 +803,6 @@
this.$el.hide('fast', this.onMinimized.bind(this));
},
updateVCard: function () {
if (!this.use_vcards) { return this; }
var jid = this.model.get('jid'),
contact = converse.roster.get(jid);
if ((contact) && (!contact.get('vcard_updated'))) {
converse.getVCard(
jid,
function (iq, jid, fullname, image, image_type, url) {
this.model.save({
'fullname' : fullname || jid,
'url': url,
'image_type': image_type,
'image': image
});
}.bind(this),
function () {
converse.log("ChatBoxView.initialize: An error occured while fetching vcard");
}
);
}
return this;
},
renderToolbar: function (options) {
if (!converse.show_toolbar) {
return;
......
This diff is collapsed.
......@@ -199,7 +199,7 @@
converse.on('contactRequest', converse.handleContactRequestNotification);
converse.on('contactStatusChanged', converse.handleChatStateNotification);
converse.on('message', converse.handleMessageNotification);
converse.on('ready', converse.requestPermission);
converse.on('connected', converse.requestPermission);
}
});
}));
// Converse.js (A browser based XMPP chat client)
// http://conversejs.org
//
// Copyright (c) 2012-2016, Jan-Carel Brand <jc@opkode.com>
// Licensed under the Mozilla Public License (MPLv2)
//
/*global define */
(function (root, factory) {
define("converse-vcard", [
"converse-core",
"converse-api",
"strophe.vcard",
], factory);
}(this, function (converse, converse_api) {
"use strict";
var Strophe = converse_api.env.Strophe,
$ = converse_api.env.jQuery,
_ = converse_api.env._,
moment = converse_api.env.moment;
converse_api.plugins.add('vcard', {
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 () {
this._super.addClientFeatures.apply(this, arguments);
if (converse.use_vcards) {
converse.connection.disco.addFeature(Strophe.NS.VCARD);
}
}
},
RosterContacts: {
createRequestingContact: function (presence) {
var bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from'));
converse.getVCard(
bare_jid,
_.partial(converse.createRequestingContactFromVCard, presence),
function (iq, jid) {
converse.log("Error while retrieving vcard for "+jid);
converse.createRequestingContactFromVCard(presence, iq, jid);
}
);
}
}
},
initialize: function () {
/* The initialize function gets called as soon as the plugin is
* loaded by converse.js's plugin machinery.
*/
this.updateSettings({
use_vcards: true,
});
converse.createRequestingContactFromVCard = function (presence, iq, jid, fullname, img, img_type, url) {
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()
};
converse.roster.create(user_data);
converse.emit('contactRequest', user_data);
};
converse.onVCardError = function (jid, iq, errback) {
var contact = converse.roster.get(jid);
if (contact) {
contact.save({ 'vcard_updated': moment().format() });
}
if (errback) { errback(iq, jid); }
};
converse.onVCardData = function (jid, iq, callback) {
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) {
var contact = converse.roster.get(jid);
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);
}
};
converse.getVCard = function (jid, callback, errback) {
/* 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.
*/
if (!converse.use_vcards) {
if (callback) { callback(null, jid); }
} else {
converse.connection.vcard.get(
_.partial(converse.onVCardData, jid, _, callback),
jid,
_.partial(converse.onVCardError, jid, _, errback));
}
};
var updateVCardForChatBox = function (evt, chatbox) {
if (!converse.use_vcards) { return; }
var jid = chatbox.model.get('jid'),
contact = converse.roster.get(jid);
if ((contact) && (!contact.get('vcard_updated'))) {
converse.getVCard(
jid,
function (iq, jid, fullname, image, image_type, url) {
chatbox.model.save({
'fullname' : fullname || jid,
'url': url,
'image_type': image_type,
'image': image
});
},
function () {
converse.log(
"updateVCardForChatBox: Error occured while fetching vcard"
);
}
);
}
};
converse.on('chatBoxInitialized', updateVCardForChatBox);
var fetchOwnVCard = function () {
if (converse.xmppstatus.get('fullname') === undefined) {
converse.getVCard(
null, // No 'to' attr when getting one's own vCard
function (iq, jid, fullname) {
converse.xmppstatus.save({'fullname': fullname});
}
);
}
};
converse.on('statusInitialized', fetchOwnVCard);
}
});
}));
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