🎨 do not run model listeners on import (#8720)

no issue

- if you upload a huge import file, parallel operations can throw errors e.g. lock wait exceeds
- this can happen if multiple transactions run in parallel
- there is no need to run:
  1. the removal of active tokens on import, because imported users have no active session
  2. rescheduling logic on timezone, because importing scheduled posts works out of the box via the model layer (if a published date is detected and it's in the future, the post get's scheduled)
This commit is contained in:
Katharina Irrgang 2017-07-21 10:58:58 +02:00 committed by Kevin Ansfield
parent 60558a776f
commit d6aaf2dbc7
3 changed files with 30 additions and 19 deletions

View File

@ -26,8 +26,13 @@ events.on('token.added', function (tokenModel) {
* - if an active user get's deleted, we have to access the previous attributes, because this is how bookshelf works
* if you delete a user.
*/
events.on('user.deactivated', function (userModel) {
var options = {id: userModel.id || userModel.previousAttributes().id};
events.on('user.deactivated', function (userModel, options) {
options = options || {};
options = _.merge({}, options, {id: userModel.id || userModel.previousAttributes().id});
if (options.importing) {
return;
}
models.Accesstoken.destroyByUser(options)
.then(function () {
@ -46,17 +51,23 @@ events.on('user.deactivated', function (userModel) {
* - reschedule all scheduled posts
* - draft scheduled posts, when the published_at would be in the past
*/
events.on('settings.active_timezone.edited', function (settingModel) {
events.on('settings.active_timezone.edited', function (settingModel, options) {
options = options || {};
options = _.merge({}, options, {context: {internal: true}});
var newTimezone = settingModel.attributes.value,
previousTimezone = settingModel._updatedAttributes.value,
timezoneOffsetDiff = moment.tz(previousTimezone).utcOffset() - moment.tz(newTimezone).utcOffset(),
options = {context: {internal: true}};
timezoneOffsetDiff = moment.tz(previousTimezone).utcOffset() - moment.tz(newTimezone).utcOffset();
// CASE: TZ was updated, but did not change
if (previousTimezone === newTimezone) {
return;
}
if (options.importing) {
return;
}
/**
* CASE:
* `Post.findAll` and the Post.edit` must run in one single transaction.

View File

@ -57,23 +57,23 @@ Settings = ghostBookshelf.Model.extend({
};
},
emitChange: function emitChange(event) {
events.emit('settings' + '.' + event, this);
emitChange: function emitChange(event, options) {
events.emit('settings' + '.' + event, this, options);
},
onDestroyed: function onDestroyed(model) {
onDestroyed: function onDestroyed(model, response, options) {
model.emitChange('deleted');
model.emitChange(model.attributes.key + '.' + 'deleted');
model.emitChange(model.attributes.key + '.' + 'deleted', options);
},
onCreated: function onCreated(model) {
onCreated: function onCreated(model, response, options) {
model.emitChange('added');
model.emitChange(model.attributes.key + '.' + 'added');
model.emitChange(model.attributes.key + '.' + 'added', options);
},
onUpdated: function onUpdated(model) {
onUpdated: function onUpdated(model, response, options) {
model.emitChange('edited');
model.emitChange(model.attributes.key + '.' + 'edited');
model.emitChange(model.attributes.key + '.' + 'edited', options);
},
onValidate: function onValidate() {

View File

@ -52,13 +52,13 @@ User = ghostBookshelf.Model.extend({
}, baseDefaults);
},
emitChange: function emitChange(event) {
events.emit('user' + '.' + event, this);
emitChange: function emitChange(event, options) {
events.emit('user' + '.' + event, this, options);
},
onDestroyed: function onDestroyed(model) {
onDestroyed: function onDestroyed(model, response, options) {
if (_.includes(activeStates, model.previous('status'))) {
model.emitChange('deactivated');
model.emitChange('deactivated', options);
}
model.emitChange('deleted');
@ -73,12 +73,12 @@ User = ghostBookshelf.Model.extend({
}
},
onUpdated: function onUpdated(model) {
onUpdated: function onUpdated(model, response, options) {
model.statusChanging = model.get('status') !== model.updated('status');
model.isActive = _.includes(activeStates, model.get('status'));
if (model.statusChanging) {
model.emitChange(model.isActive ? 'activated' : 'deactivated');
model.emitChange(model.isActive ? 'activated' : 'deactivated', options);
} else {
if (model.isActive) {
model.emitChange('activated.edited');