1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-29 20:52:15 -05:00
kaiwa/clientapp/helpers/avatarHandler.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

/*global app*/
"use strict";
var crypto = require('crypto');
2015-03-20 15:36:40 -04:00
module.exports.getGravatar = function (jid) {
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
};
2015-03-20 15:36:40 -04:00
var getGravatar = module.exports.getGravatar;
2015-03-20 15:36:40 -04:00
module.exports.fetch = function (jid, id, type, source, cb) {
if (!id) {
2015-03-20 15:36:40 -04:00
return cb(getGravatar(jid));
}
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));
}
app.whenConnected(function () {
if (source == 'vcard') {
app.api.getVCard(jid, function (err, resp) {
if (err) {
2015-03-20 15:36:40 -04:00
return cb(getGravatar(jid));
}
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));
}
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);
});
}
});
});
};