Fetch vCard once a roster contact is added. Updates #38

This will happen in the background and in the meantime roster contacts will
appear under their JIDs.
This commit is contained in:
JC Brand 2013-09-26 11:44:50 +02:00
parent aad8de02d0
commit b36e6bbce1

View File

@ -1875,7 +1875,7 @@
this.$el.html(this.request_template(item.toJSON())); this.$el.html(this.request_template(item.toJSON()));
converse.showControlBox(); converse.showControlBox();
} else if (subscription === 'both' || subscription === 'to') { } else if (subscription === 'both' || subscription === 'to') {
_.each(['pending-xmpp-contact', 'requesting-xmpp-contact'], _.each(['pending-xmpp-contact', 'requesting-xmpp-contact'],
function (cls) { function (cls) {
if (this.el.className.indexOf(cls) !== -1) { if (this.el.className.indexOf(cls) !== -1) {
this.$el.removeClass(cls); this.$el.removeClass(cls);
@ -1896,35 +1896,46 @@
} }
return this; return this;
}, },
initialize: function () {
this.options.model.on('change', function (item, changed) {
if (_.has(item.changed, 'chat_status')) {
this.$el.attr('class', item.changed.chat_status);
}
}, this);
}
}); });
this.getVCard = function (jid, callback, errback) { this.getVCard = function (jid, callback, errback) {
converse.connection.vcard.get($.proxy(function (iq) { converse.connection.vcard.get(
$vcard = $(iq).find('vCard'); $.proxy(function (iq) {
var fullname = $vcard.find('FN').text(), // Successful callback
img = $vcard.find('BINVAL').text(), $vcard = $(iq).find('vCard');
img_type = $vcard.find('TYPE').text(), var fullname = $vcard.find('FN').text(),
url = $vcard.find('URL').text(); img = $vcard.find('BINVAL').text(),
var rosteritem = converse.roster.get(jid); img_type = $vcard.find('TYPE').text(),
if (rosteritem) { url = $vcard.find('URL').text();
rosteritem.save({ if (jid) {
'fullname': fullname || jid, var rosteritem = converse.roster.get(jid);
'image_type': img_type, if (rosteritem) {
'image': img, rosteritem.save({
'url': url, 'fullname': fullname || jid,
'vcard_updated': converse.toISOString(new Date()) 'image_type': img_type,
}); 'image': img,
} 'url': url,
callback(jid, fullname, img, img_type, url); 'vcard_updated': converse.toISOString(new Date())
}, this), jid, errback); });
}
}
if (callback) {
callback(jid, fullname, img, img_type, url);
}
}, this),
jid,
function (iq) {
// Error callback
var rosteritem = converse.roster.get(jid);
if (rosteritem) {
rosteritem.save({
'vcard_updated': converse.toISOString(new Date())
});
}
if (errback) {
errback(iq);
}
});
}; };
this.RosterItems = Backbone.Collection.extend({ this.RosterItems = Backbone.Collection.extend({
@ -2199,6 +2210,11 @@
var view = new converse.RosterItemView({model: item}); var view = new converse.RosterItemView({model: item});
this.rosteritemviews[item.id] = view; this.rosteritemviews[item.id] = view;
this.render(item); this.render(item);
if (!item.get('vcard_updated')) {
// This will update the vcard, which triggers a change
// request which will rerender the roster item.
converse.getVCard(item.get('jid'));
}
}, this); }, this);
this.model.on('change', function (item, changed) { this.model.on('change', function (item, changed) {
@ -2276,7 +2292,7 @@
if (changed_presence) { if (changed_presence) {
this.sortRoster(changed_presence); this.sortRoster(changed_presence);
sorted = true; sorted = true;
} }
if (item.get('is_last')) { if (item.get('is_last')) {
if (!sorted) { if (!sorted) {
this.sortRoster(item.get('chat_status')); this.sortRoster(item.get('chat_status'));