Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 change gravatar file design #7553

Merged
merged 1 commit into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions core/server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,12 @@ User = ghostBookshelf.Model.extend({
return generatePasswordHash(userData.password).then(function then(hash) {
// Assign the hashed password
userData.password = hash;
// LookupGravatar
return gravatar.lookup(userData);
}).then(function then(userData) {
}).then(function then(response) {
if (response && response.image) {
userData.image = response.image;
}

// Save the user with the hashed password
return ghostBookshelf.Model.add.call(self, userData, options);
}).then(function then(addedUser) {
Expand Down Expand Up @@ -451,14 +454,16 @@ User = ghostBookshelf.Model.extend({
// Assign the hashed password
userData.password = hash;

return Promise.join(
gravatar.lookup(userData),
ghostBookshelf.Model.generateSlug.call(this, User, userData.name, options)
);
}).then(function then(results) {
userData = results[0];
userData.slug = results[1];
return gravatar.lookup(userData)

This comment was marked as abuse.

This comment was marked as abuse.

.then(function (response) {
if (response && response.image) {
userData.image = response.image;
}

return ghostBookshelf.Model.generateSlug.call(this, User, userData.name, options);
});
}).then(function then(slug) {
userData.slug = slug;
return self.edit.call(self, userData, options);
});
},
Expand Down
19 changes: 13 additions & 6 deletions core/server/utils/gravatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,44 @@ var Promise = require('bluebird'),
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';
'?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(userData);
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';
userData.image = gravatarUrl;
image = gravatarUrl;
}

resolve(userData);
resolve({image: image});
});

request.on('error', function () {
clearTimeout(timer);

// just resolve with no image url
if (!timerEnded) {
return resolve(userData);
return resolve();
}
});

timer = setTimeout(function () {
timerEnded = true;
request.abort();
return resolve(userData);
return resolve();
}, timeout || 2000);
});
};
4 changes: 1 addition & 3 deletions core/test/unit/server_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,7 @@ describe('Server Utilities', function () {
.reply(200);

gravatar.lookup({email: 'exists@example.com'}, 10).then(function (result) {
should.exist(result);
should.not.exist(result.image);

should.not.exist(result);
done();
}).catch(done);
});
Expand Down