From 941379ddba6d2faa775d54ffcf8a504149122891 Mon Sep 17 00:00:00 2001 From: Katharina Irrgang Date: Thu, 14 Dec 2017 10:51:05 +0100 Subject: [PATCH] Use request lib in gravatar utility (#9337) refs #9178 --- core/server/utils/gravatar.js | 53 +++++++++++++---------------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/core/server/utils/gravatar.js b/core/server/utils/gravatar.js index 2739b39ef8..b5c60d4d77 100644 --- a/core/server/utils/gravatar.js +++ b/core/server/utils/gravatar.js @@ -1,44 +1,31 @@ var Promise = require('bluebird'), - config = require('../config'), crypto = require('crypto'), - https = require('https'); + config = require('../config'), + request = require('../lib/request'); 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; + '?s=250'; - return new Promise(function gravatarRequest(resolve) { - if (config.isPrivacyDisabled('useGravatar')) { - return resolve(); - } + if (config.isPrivacyDisabled('useGravatar')) { + return Promise.resolve(); + } - var request, timer, timerEnded = false; + return request('https:' + gravatarUrl + '&d=404&r=x', {timeout: timeout || 2 * 1000}) + .then(function () { + gravatarUrl += '&d=mm&r=x'; - 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}); + return { + image: gravatarUrl + }; + }) + .catch({statusCode: 404}, function () { + return { + image: undefined + }; + }) + .catch(function () { + // ignore error, just resolve with no image url }); - - 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); - }); };