1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-22 17:22:22 -05:00
kaiwa/clientapp/helpers/fetchAvatar.js
2013-09-19 16:57:37 -07:00

50 lines
1.1 KiB
JavaScript

/*global app, client*/
var crypto = require('crypto');
function fallback(jid) {
var gID = crypto.createHash('md5').update(jid).digest('hex');
return {
uri: 'https://gravatar.com/avatar/' + gID + '?s=80&d=mm'
};
}
module.exports = function (jid, id, type, cb) {
if (!id) {
return cb(fallback(jid));
}
app.storage.avatars.get(id, function (err, avatar) {
if (!err) {
return cb(avatar);
}
if (!type) {
return cb(fallback(jid));
}
app.whenConnected(function () {
client.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);
});
});
});
};