Skip to content

Commit

Permalink
🎨 gravatar lookup in saving hook (#7561)
Browse files Browse the repository at this point in the history
refs #7432

- in preparation for more User model cleanup
- look for gravatar when email has changed
- run onSaving tasks in parallel in User model
  • Loading branch information
kirrg001 authored and ErisDS committed Oct 14, 2016
1 parent 03e4acd commit e1ac6a5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
55 changes: 34 additions & 21 deletions core/server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,45 @@ User = ghostBookshelf.Model.extend({
model.emitChange('edited');
},

/**
* Lookup Gravatar if email changes to update image url
* Generating a slug requires a db call to look for conflicting slugs
*/
onSaving: function onSaving(newPage, attr, options) {
/*jshint unused:false*/
var self = this;
var self = this,
tasks = [];

ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);

if (this.hasChanged('slug') || !this.get('slug')) {
// Generating a slug requires a db call to look for conflicting slugs
return ghostBookshelf.Model.generateSlug(User, this.get('slug') || this.get('name'),
{status: 'all', transacting: options.transacting, shortSlug: !this.get('slug')})
.then(function then(slug) {
self.set({slug: slug});
if (self.hasChanged('email')) {
tasks.gravatar = (function lookUpGravatar() {
return gravatar.lookup({
email: self.get('email')
}).then(function (response) {
if (response && response.image) {
self.set('image', response.image);
}
});
})();
}

if (this.hasChanged('slug') || !this.get('slug')) {
tasks.slug = (function generateSlug() {
return ghostBookshelf.Model.generateSlug(
User,
self.get('slug') || self.get('name'),
{
status: 'all',
transacting: options.transacting,
shortSlug: !self.get('slug')
})
.then(function then(slug) {
self.set({slug: slug});
});
})();
}

return Promise.props(tasks);
},

// For the user model ONLY it is possible to disable validations.
Expand Down Expand Up @@ -404,11 +429,6 @@ User = ghostBookshelf.Model.extend({
return generatePasswordHash(userData.password).then(function then(hash) {
// Assign the hashed password
userData.password = hash;
return gravatar.lookup(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);
Expand Down Expand Up @@ -451,14 +471,7 @@ User = ghostBookshelf.Model.extend({
// Assign the hashed password
userData.password = hash;

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

return ghostBookshelf.Model.generateSlug.call(this, User, userData.name, options);
});
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
3 changes: 2 additions & 1 deletion core/server/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ utils = {
readThemes: require('./read-themes'),
generateAssetHash: require('./asset-hash'),
url: require('./url'),
tokens: require('./tokens')
tokens: require('./tokens'),
sequence: require('./sequence')
};

module.exports = utils;

0 comments on commit e1ac6a5

Please sign in to comment.