diff --git a/core/server/models/user.js b/core/server/models/user.js index 7e51d0a544..111140b098 100644 --- a/core/server/models/user.js +++ b/core/server/models/user.js @@ -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. @@ -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); @@ -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); diff --git a/core/server/utils/index.js b/core/server/utils/index.js index a24d40422f..4eda940475 100644 --- a/core/server/utils/index.js +++ b/core/server/utils/index.js @@ -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;