blob: ed6a521a687bcca87463379d8ee0bf9e5f1885f0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
'use strict';
var crypto = require('crypto'),
http = require('http');
function AvatarCache(options) {
this.core = options.core;
this.avatars = {};
this.get = this.get.bind(this);
this.add = this.add.bind(this);
}
AvatarCache.prototype.get = function(userId) {
return this.avatars[userId];
};
AvatarCache.prototype.add = function(user) {
var userId = (user.id || user._id).toString();
var url = 'http://www.gravatar.com/avatar/' + user.avatar + '?s=64';
http.get(url, function(response) {
if (response.statusCode !== 200) {
return;
}
var buffers = [];
response.on('data', function(buffer) {
buffers.push(buffer);
});
response.on('end', function() {
var buffer = Buffer.concat(buffers);
this.avatars[userId] = {
base64: buffer.toString('base64'),
sha1: crypto.createHash('sha1').update(buffer).digest('hex')
};
this.core.emit('avatar-cache:update', user);
}.bind(this));
}.bind(this)).on('error', function(){ });
};
module.exports = AvatarCache;
|