2013-09-26 23:19:46 -04:00
|
|
|
/*global app*/
|
|
|
|
"use strict";
|
2013-09-19 19:57:37 -04:00
|
|
|
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'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-01 23:39:06 -05:00
|
|
|
module.exports = function (jid, id, type, source, cb) {
|
2013-09-19 19:57:37 -04:00
|
|
|
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 () {
|
2014-01-01 23:39:06 -05:00
|
|
|
if (source == 'vcard') {
|
|
|
|
app.api.getVCard(jid, function (err, resp) {
|
|
|
|
if (err) {
|
|
|
|
return cb(fallback(jid));
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = resp.toJSON();
|
|
|
|
type = resp.vCardTemp.photo.type || type;
|
|
|
|
|
|
|
|
var data = resp.vCardTemp.photo.data;
|
|
|
|
var uri = 'data:' + type + ';base64,' + data;
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2013-09-19 19:57:37 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|