Ghost/core/server/utils/gravatar.js
Hannah Wolfe c301510cd1 Refactor gravatarLookup, remove request dependency
no issue

- request is quite a heavy dependency
- we were only using request in 3 places: a test, storing contrib images in the gruntfile & the gravatar lookup
- all 3 are relatively simple to do with the http/https module
- refactored all 3, removed request
2016-02-16 11:12:01 +00:00

43 lines
1.3 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';
return new Promise(function gravatarRequest(resolve) {
if (config.isPrivacyDisabled('useGravatar') || process.env.NODE_ENV.indexOf('testing') > -1) {
return resolve(userData);
}
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';
userData.image = gravatarUrl;
}
resolve(userData);
});
request.on('error', function () {
clearTimeout(timer);
// just resolve with no image url
if (!timerEnded) {
return resolve(userData);
}
});
timer = setTimeout(function () {
timerEnded = true;
request.abort();
return resolve(userData);
}, timeout || 2000);
});
};