Ghost/core/server/utils/gravatar.js
Katharina Irrgang 5b9c213849 🎨 change gravatar file design (#7553)
no issue
- preperation for User model refactoring
- the rule is:
  --> when calling a unit, this unit should return something new
  --> and NOT modifying an existing object and return it (this is an unexpected behaviour, especially for utils and libs)
2016-10-13 13:52:22 +01:00

50 lines
1.5 KiB
JavaScript

var Promise = require('bluebird'),
config = require('../config'),
crypto = require('crypto'),
https = require('https');
module.exports.lookup = function lookup(userData, timeout) {
var gravatarUrl = '//www.gravatar.com/avatar/' +
crypto.createHash('md5').update(userData.email.toLowerCase().trim()).digest('hex') +
'?s=250', image;
return new Promise(function gravatarRequest(resolve) {
/**
* @TODO:
* - mock gravatar in test env globally, do not check for test env!
* - in test/utils/index.js -> mocks.gravatar.enable();
*/
if (config.isPrivacyDisabled('useGravatar') || config.get('env').indexOf('testing') > -1) {
return resolve();
}
var request, timer, timerEnded = false;
request = https.get('https:' + gravatarUrl + '&d=404&r=x', function (response) {
clearTimeout(timer);
if (response.statusCode !== 404 && !timerEnded) {
gravatarUrl += '&d=mm&r=x';
image = gravatarUrl;
}
resolve({image: image});
});
request.on('error', function () {
clearTimeout(timer);
// just resolve with no image url
if (!timerEnded) {
return resolve();
}
});
timer = setTimeout(function () {
timerEnded = true;
request.abort();
return resolve();
}, timeout || 2000);
});
};