From fb2428940ac1a9f753a2f7b02f4f62c619a2e2c8 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Wed, 1 Jan 2014 20:39:06 -0800 Subject: [PATCH] Add support for displaying legacy vcard avatars --- clientapp/helpers/fetchAvatar.js | 56 ++++++++++++++++++-------- clientapp/helpers/xmppEventHandlers.js | 5 ++- clientapp/models/contact.js | 8 +++- clientapp/models/me.js | 4 +- package.json | 2 +- public/x-manifest.cache | 2 +- 6 files changed, 54 insertions(+), 23 deletions(-) diff --git a/clientapp/helpers/fetchAvatar.js b/clientapp/helpers/fetchAvatar.js index 372b471..71c103f 100644 --- a/clientapp/helpers/fetchAvatar.js +++ b/clientapp/helpers/fetchAvatar.js @@ -11,7 +11,7 @@ function fallback(jid) { } -module.exports = function (jid, id, type, cb) { +module.exports = function (jid, id, type, source, cb) { if (!id) { return cb(fallback(jid)); } @@ -26,24 +26,48 @@ module.exports = function (jid, id, type, cb) { } app.whenConnected(function () { - app.api.getAvatar(jid, id, function (err, resp) { - if (err) { - return cb(fallback(jid)); - } + if (source == 'vcard') { + app.api.getVCard(jid, function (err, resp) { + if (err) { + return cb(fallback(jid)); + } - resp = resp.toJSON(); - var data = resp.pubsub.retrieve.item.avatarData; - var uri = 'data:' + type + ';base64,' + data; + resp = resp.toJSON(); + console.log(JSON.stringify(resp)); + type = resp.vCardTemp.photo.type || type; - avatar = { - id: id, - type: type, - uri: uri - }; + var data = resp.vCardTemp.photo.data; + var uri = 'data:' + type + ';base64,' + data; - app.storage.avatars.add(avatar); - return cb(avatar); - }); + avatar = { + id: id, + type: type, + uri: uri + }; + + app.storage.avatars.add(avatar); + return cb(avatar); + }); + } else { + app.api.getAvatar(jid, id, function (err, resp) { + if (err) { + return cb(fallback(jid)); + } + + resp = resp.toJSON(); + var data = resp.pubsub.retrieve.item.avatarData; + var uri = 'data:' + type + ';base64,' + data; + + avatar = { + id: id, + type: type, + uri: uri + }; + + app.storage.avatars.add(avatar); + return cb(avatar); + }); + } }); }); }; diff --git a/clientapp/helpers/xmppEventHandlers.js b/clientapp/helpers/xmppEventHandlers.js index d705782..7610de2 100644 --- a/clientapp/helpers/xmppEventHandlers.js +++ b/clientapp/helpers/xmppEventHandlers.js @@ -228,7 +228,10 @@ module.exports = function (client, app) { id = info.avatars[0].id; type = info.avatars[0].type || 'image/png'; } - contact.setAvatar(id, type); + + if (contact.setAvatar) { + contact.setAvatar(id, type, info.source); + } }); client.on('chatState', function (info) { diff --git a/clientapp/models/contact.js b/clientapp/models/contact.js index 37302a8..2551630 100644 --- a/clientapp/models/contact.js +++ b/clientapp/models/contact.js @@ -40,6 +40,7 @@ module.exports = HumanModel.define({ session: { activeContact: ['bool', true, false], avatar: 'string', + avatarSource: 'string', lastInteraction: 'date', lastSentMessage: 'object', lockedResource: 'string', @@ -203,11 +204,14 @@ module.exports = HumanModel.define({ logger.error('no jingle resources for this user'); } }, - setAvatar: function (id, type) { + setAvatar: function (id, type, source) { var self = this; - fetchAvatar(this.jid, id, type, function (avatar) { + console.log('setAvatar', this.jid, id, type, source); + fetchAvatar(this.jid, id, type, source, function (avatar) { + if (source == 'vcard' && self.avatarSource == 'pubsub') return; self.avatarID = avatar.id; self.avatar = avatar.uri; + self.avaarSource = source; self.save(); }); }, diff --git a/clientapp/models/me.js b/clientapp/models/me.js index f3f921c..f068806 100644 --- a/clientapp/models/me.js +++ b/clientapp/models/me.js @@ -77,9 +77,9 @@ module.exports = HumanModel.define({ } this._activeContact = jid; }, - setAvatar: function (id, type) { + setAvatar: function (id, type, source) { var self = this; - fetchAvatar('', id, type, function (avatar) { + fetchAvatar('', id, type, source, function (avatar) { self.avatarID = avatar.id; self.avatar = avatar.uri; }); diff --git a/package.json b/package.json index f1d5e0c..dff7be7 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "oembed": "0.1.0", "semi-static": "0.0.4", "sound-effect-manager": "0.0.5", - "stanza.io": "2.10.0", + "stanza.io": "2.11.0", "staydown": "legastero/staydown", "templatizer": "0.1.2", "underscore": "1.5.1", diff --git a/public/x-manifest.cache b/public/x-manifest.cache index d55aea1..25ef059 100644 --- a/public/x-manifest.cache +++ b/public/x-manifest.cache @@ -1,5 +1,5 @@ CACHE MANIFEST -# 0.0.1 1388626859519 +# 0.0.1 1388637293749 CACHE: /app.js