0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-01 02:41:39 -05:00

Converted Bookshelf collision plugin into async-await

no issue

- this helps avoid promise chaining and keeps the code neater
- also removes unneeded `bluebird` import after this change
This commit is contained in:
Daniel Lockyer 2021-06-14 17:06:46 +01:00
parent 7cc021c114
commit becf4c04e5
No known key found for this signature in database
GPG key ID: D21186F0B47295AD

View file

@ -1,5 +1,4 @@
const moment = require('moment-timezone');
const Promise = require('bluebird');
const _ = require('lodash');
const errors = require('@tryghost/errors');
@ -47,34 +46,32 @@ module.exports = function (Bookshelf) {
*
* HTML is always auto generated, ignore.
*/
parentSync.update = function update() {
return originalUpdateSync.apply(this, arguments)
.then((response) => {
const changed = _.omit(self._changed, [
'created_at', 'updated_at', 'author_id', 'id',
'published_by', 'updated_by', 'html', 'plaintext'
]);
parentSync.update = async function update() {
const response = await originalUpdateSync.apply(this, arguments);
const changed = _.omit(self._changed, [
'created_at', 'updated_at', 'author_id', 'id',
'published_by', 'updated_by', 'html', 'plaintext'
]);
const clientUpdatedAt = moment(self.clientData.updated_at || self.serverData.updated_at || new Date());
const serverUpdatedAt = moment(self.serverData.updated_at || clientUpdatedAt);
const clientUpdatedAt = moment(self.clientData.updated_at || self.serverData.updated_at || new Date());
const serverUpdatedAt = moment(self.serverData.updated_at || clientUpdatedAt);
if (Object.keys(changed).length) {
if (clientUpdatedAt.diff(serverUpdatedAt) !== 0) {
// @NOTE: This will rollback the update. We cannot know if relations were updated before doing the update.
return Promise.reject(new errors.UpdateCollisionError({
message: 'Saving failed! Someone else is editing this post.',
code: 'UPDATE_COLLISION',
level: 'critical',
errorDetails: {
clientUpdatedAt: self.clientData.updated_at,
serverUpdatedAt: self.serverData.updated_at
}
}));
if (Object.keys(changed).length) {
if (clientUpdatedAt.diff(serverUpdatedAt) !== 0) {
// @NOTE: This will rollback the update. We cannot know if relations were updated before doing the update.
throw new errors.UpdateCollisionError({
message: 'Saving failed! Someone else is editing this post.',
code: 'UPDATE_COLLISION',
level: 'critical',
errorDetails: {
clientUpdatedAt: self.clientData.updated_at,
serverUpdatedAt: self.serverData.updated_at
}
}
});
}
}
return response;
});
return response;
};
return parentSync;