mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-18 07:51:55 +03:00
9349e99e54
closes #7137 Deleting the content from the database runs in a transaction. see https://github.com/TryGhost/Ghost/blob/master/core/server/api/users.js#L390 `destroyByAuthor` is one of the operations we trigger to delete all the conent, see https://github.com/TryGhost/Ghost/blob/master/core/server/models/post.js#L647 The post model has a specific hook for deleting content to delete the relations as well, see https://github.com/TryGhost/Ghost/blob/master/core/server/models/post.js#L122 This hook is part of the transaction. But the `options` are ignored. `(model/*, attr, options*/)` We use the `options` to forward the transaction reference, which we need to pass into the bookshelf queries. So `return model.load('tags').call('related', 'tags').call('detach')` does not forward the transaction and that's why it stucks when deleting the content.
83 lines
2.6 KiB
JavaScript
83 lines
2.6 KiB
JavaScript
var Promise = require('bluebird'),
|
|
ghostBookshelf = require('./index'),
|
|
errors = require('../../errors'),
|
|
i18n = require('../../i18n'),
|
|
|
|
Basetoken;
|
|
|
|
Basetoken = ghostBookshelf.Model.extend({
|
|
|
|
user: function user() {
|
|
return this.belongsTo('User');
|
|
},
|
|
|
|
client: function client() {
|
|
return this.belongsTo('Client');
|
|
},
|
|
|
|
// override for base function since we don't have
|
|
// a created_by field for sessions
|
|
creating: function creating(newObj, attr, options) {
|
|
/*jshint unused:false*/
|
|
},
|
|
|
|
// override for base function since we don't have
|
|
// a updated_by field for sessions
|
|
saving: function saving(newObj, attr, options) {
|
|
/*jshint unused:false*/
|
|
// Remove any properties which don't belong on the model
|
|
this.attributes = this.pick(this.permittedAttributes());
|
|
}
|
|
|
|
}, {
|
|
destroyAllExpired: function destroyAllExpired(options) {
|
|
options = this.filterOptions(options, 'destroyAll');
|
|
return ghostBookshelf.Collection.forge([], {model: this})
|
|
.query('where', 'expires', '<', Date.now())
|
|
.fetch(options)
|
|
.then(function then(collection) {
|
|
return collection.invokeThen('destroy', options);
|
|
});
|
|
},
|
|
/**
|
|
* ### destroyByUser
|
|
* @param {[type]} options has context and id. Context is the user doing the destroy, id is the user to destroy
|
|
*/
|
|
destroyByUser: function destroyByUser(options) {
|
|
var userId = options.id;
|
|
|
|
options = this.filterOptions(options, 'destroyByUser');
|
|
|
|
if (userId) {
|
|
return ghostBookshelf.Collection.forge([], {model: this})
|
|
.query('where', 'user_id', '=', userId)
|
|
.fetch(options)
|
|
.then(function then(collection) {
|
|
return collection.invokeThen('destroy', options);
|
|
});
|
|
}
|
|
|
|
return Promise.reject(new errors.NotFoundError(i18n.t('errors.models.base.token.noUserFound')));
|
|
},
|
|
|
|
/**
|
|
* ### destroyByToken
|
|
* @param {[type]} options has token where token is the token to destroy
|
|
*/
|
|
destroyByToken: function destroyByToken(options) {
|
|
var token = options.token;
|
|
|
|
options = this.filterOptions(options, 'destroyByUser');
|
|
options.require = true;
|
|
|
|
return this.forge()
|
|
.query('where', 'token', '=', token)
|
|
.fetch(options)
|
|
.then(function then(model) {
|
|
return model.destroy(options);
|
|
});
|
|
}
|
|
});
|
|
|
|
module.exports = Basetoken;
|