1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-22 09:12:19 -05:00

Extract avatar fetching into its own module.

This commit is contained in:
Lance Stout 2013-09-19 16:57:37 -07:00
parent db0dfe92b7
commit 83eb4fb0b5
3 changed files with 58 additions and 71 deletions

View File

@ -0,0 +1,49 @@
/*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);
});
});
});
};

View File

@ -8,7 +8,7 @@ var HumanModel = require('human-model');
var Resources = require('./resources');
var Messages = require('./messages');
var Message = require('./message');
var crypto = XMPP.crypto;
var fetchAvatar = require('../helpers/fetchAvatar');
module.exports = HumanModel.define({
@ -128,42 +128,10 @@ module.exports = HumanModel.define({
},
setAvatar: function (id, type) {
var self = this;
if (!id) {
var gID = crypto.createHash('md5').update(this.jid).digest('hex');
self.avatar = 'https://gravatar.com/avatar/' + gID + '?s=30&d=mm';
return;
}
app.storage.avatars.get(id, function (err, avatar) {
if (err) {
if (!type) {
// We can't find the ID, and we don't know the type, so fallback.
var gID = crypto.createHash('md5').update(self.jid).digest('hex');
self.avatar = 'https://gravatar.com/avatar/' + gID + '?s=30&d=mm';
return;
}
app.whenConnected(function () {
client.getAvatar(self.jid, id, function (err, resp) {
if (err) return;
resp = resp.toJSON();
var avatarData = resp.pubsub.retrieve.item.avatarData;
var dataURI = 'data:' + type + ';base64,' + avatarData;
app.storage.avatars.add({id: id, uri: dataURI});
self.set({
avatar: dataURI,
avatarID: id
});
self.save();
});
});
} else {
self.set({
avatar: avatar.uri,
avatarID: avatar.id
});
self.save();
}
fetchAvatar(this.jid, id, type, function (avatar) {
self.avatarID = avatar.id;
self.avatar = avatar.uri;
self.save();
});
},
onResourceChange: function () {

View File

@ -7,6 +7,7 @@ var Contact = require('./contact');
var MUCs = require('./mucs');
var MUC = require('./muc');
var uuid = require('node-uuid');
var fetchAvatar = require('../helpers/fetchAvatar');
module.exports = HumanModel.define({
@ -44,40 +45,9 @@ module.exports = HumanModel.define({
},
setAvatar: function (id, type) {
var self = this;
if (!id) {
var gID = XMPP.crypto.createHash('md5').update(this.jid).digest('hex');
self.avatar = 'https://gravatar.com/avatar/' + gID + '?s=30&d=mm';
return;
}
app.storage.avatars.get(id, function (err, avatar) {
if (err) {
if (!type) {
// We can't find the ID, and we don't know the type, so fallback.
var gID = XMPP.crypto.createHash('md5').update(self.jid.bare).digest('hex');
self.avatar = 'https://gravatar.com/avatar/' + gID + '?s=30&d=mm';
return;
}
app.whenConnected(function () {
client.getAvatar(self.jid.bare, id, function (err, resp) {
if (err) return;
resp = resp.toJSON();
var avatarData = resp.pubsub.retrieve.item.avatarData;
var dataURI = 'data:' + type + ';base64,' + avatarData;
app.storage.avatars.add({id: id, uri: dataURI});
self.set({
avatar: dataURI,
avatarID: id
});
});
});
} else {
self.set({
avatar: avatar.uri,
avatarID: avatar.id
});
}
fetchAvatar('', id, type, function (avatar) {
self.avatarID = avatar.id;
self.avatar = avatar.uri;
});
},
getContact: function (jid, alt) {