mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
🎨 gravatar lookup in saving hook (#7561)
refs #7432 - in preparation for more User model cleanup - look for gravatar when email has changed - run onSaving tasks in parallel in User model
This commit is contained in:
parent
03e4acdb37
commit
e1ac6a53dd
2 changed files with 36 additions and 22 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue