2013-09-26 23:19:46 -04:00
|
|
|
/*global app*/
|
|
|
|
"use strict";
|
2013-09-19 19:57:37 -04:00
|
|
|
var crypto = require('crypto');
|
|
|
|
|
2015-03-20 15:36:40 -04:00
|
|
|
module.exports.getGravatar = function (jid) {
|
2013-09-19 19:57:37 -04:00
|
|
|
var gID = crypto.createHash('md5').update(jid).digest('hex');
|
|
|
|
return {
|
|
|
|
uri: 'https://gravatar.com/avatar/' + gID + '?s=80&d=mm'
|
|
|
|
};
|
2015-03-20 15:36:40 -04:00
|
|
|
};
|
2013-09-19 19:57:37 -04:00
|
|
|
|
2015-03-20 15:36:40 -04:00
|
|
|
var getGravatar = module.exports.getGravatar;
|
2013-09-19 19:57:37 -04:00
|
|
|
|
2015-03-20 15:36:40 -04:00
|
|
|
module.exports.fetch = function (jid, id, type, source, cb) {
|
2013-09-19 19:57:37 -04:00
|
|
|
if (!id) {
|
2015-03-20 15:36:40 -04:00
|
|
|
return cb(getGravatar(jid));
|
2013-09-19 19:57:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
app.storage.avatars.get(id, function (err, avatar) {
|
|
|
|
if (!err) {
|
|
|
|
return cb(avatar);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!type) {
|
2015-03-20 15:36:40 -04:00
|
|
|
return cb(getGravatar(jid));
|
2013-09-19 19:57:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
app.whenConnected(function () {
|
2014-01-01 23:39:06 -05:00
|
|
|
if (source == 'vcard') {
|
|
|
|
app.api.getVCard(jid, function (err, resp) {
|
|
|
|
if (err) {
|
2015-03-20 15:36:40 -04:00
|
|
|
return cb(getGravatar(jid));
|
2014-01-01 23:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-03-20 15:36:40 -04:00
|
|
|
return cb(getGravatar(jid));
|
2014-01-01 23:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|