2016-02-12 23:04:26 +03:00
|
|
|
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') +
|
2016-10-13 15:52:22 +03:00
|
|
|
'?s=250', image;
|
2016-02-12 23:04:26 +03:00
|
|
|
|
|
|
|
return new Promise(function gravatarRequest(resolve) {
|
2017-02-03 21:25:39 +03:00
|
|
|
if (config.isPrivacyDisabled('useGravatar')) {
|
2016-10-13 15:52:22 +03:00
|
|
|
return resolve();
|
2016-02-12 23:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var request, timer, timerEnded = false;
|
|
|
|
|
|
|
|
request = https.get('https:' + gravatarUrl + '&d=404&r=x', function (response) {
|
|
|
|
clearTimeout(timer);
|
2016-10-13 15:52:22 +03:00
|
|
|
|
2016-02-12 23:04:26 +03:00
|
|
|
if (response.statusCode !== 404 && !timerEnded) {
|
|
|
|
gravatarUrl += '&d=mm&r=x';
|
2016-10-13 15:52:22 +03:00
|
|
|
image = gravatarUrl;
|
2016-02-12 23:04:26 +03:00
|
|
|
}
|
|
|
|
|
2016-10-13 15:52:22 +03:00
|
|
|
resolve({image: image});
|
2016-02-12 23:04:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
request.on('error', function () {
|
|
|
|
clearTimeout(timer);
|
2016-10-13 15:52:22 +03:00
|
|
|
|
2016-02-12 23:04:26 +03:00
|
|
|
// just resolve with no image url
|
|
|
|
if (!timerEnded) {
|
2016-10-13 15:52:22 +03:00
|
|
|
return resolve();
|
2016-02-12 23:04:26 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
timer = setTimeout(function () {
|
|
|
|
timerEnded = true;
|
|
|
|
request.abort();
|
2016-10-13 15:52:22 +03:00
|
|
|
return resolve();
|
2016-02-12 23:04:26 +03:00
|
|
|
}, timeout || 2000);
|
|
|
|
});
|
|
|
|
};
|