mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
d01c1cba64
closes: https://github.com/TryGhost/Toolbox/issues/318 refs: https://github.com/TryGhost/Toolbox/issues/320 refs:2a11d5100e
- The underlying problem was caused by the removal of API versions - commit2a11d5100e
- I missed doing the same refactor as was done for the post-emailserializer here:2a11d5100e (diff-0f7477bb5e5a9fdcb4c909a9e9e61ab5bb76b640b2d63a5791a9d6c8904f2758R7)
- The removal of api versions included a change to remove circular references in the api module - Written some basic unit tests to prove that webhook serialization works - I'm not 100% happy with the output, and not sure if it's the tests or reality, needs further work - Future work tracked in https://github.com/TryGhost/Toolbox/issues/320
74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
module.exports = (event, model) => {
|
|
const _ = require('lodash');
|
|
const {sequence} = require('@tryghost/promise');
|
|
const api = require('../../api').endpoints;
|
|
const apiShared = require('../../api').shared;
|
|
|
|
const resourceName = event.match(/(\w+)\./)[1];
|
|
const docName = `${resourceName}s`;
|
|
|
|
const ops = [];
|
|
|
|
if (Object.keys(model.attributes).length) {
|
|
ops.push(() => {
|
|
let frame = {options: {previous: false, context: {user: true}}};
|
|
|
|
if (['posts', 'pages'].includes(docName)) {
|
|
frame.options.formats = ['mobiledoc', 'html', 'plaintext'];
|
|
frame.options.withRelated = ['tags', 'authors'];
|
|
}
|
|
|
|
return apiShared
|
|
.serializers
|
|
.handle
|
|
.output(model, {docName: docName, method: 'read'}, api.serializers.output, frame)
|
|
.then(() => {
|
|
return frame.response[docName][0];
|
|
});
|
|
});
|
|
} else {
|
|
ops.push(() => {
|
|
return Promise.resolve({});
|
|
});
|
|
}
|
|
|
|
if (Object.keys(model._previousAttributes).length) {
|
|
ops.push(() => {
|
|
const frame = {options: {previous: true, context: {user: true}}};
|
|
|
|
if (['posts', 'pages'].includes(docName)) {
|
|
frame.options.formats = ['mobiledoc', 'html', 'plaintext'];
|
|
frame.options.withRelated = ['tags', 'authors'];
|
|
}
|
|
|
|
return apiShared
|
|
.serializers
|
|
.handle
|
|
.output(model, {docName: docName, method: 'read'}, api.serializers.output, frame)
|
|
.then(() => {
|
|
return frame.response[docName][0];
|
|
});
|
|
});
|
|
} else {
|
|
ops.push(() => {
|
|
return Promise.resolve({});
|
|
});
|
|
}
|
|
|
|
return sequence(ops)
|
|
.then((results) => {
|
|
const current = results[0];
|
|
const previous = results[1];
|
|
|
|
const changed = model._changed ? Object.keys(model._changed) : {};
|
|
|
|
const payload = {
|
|
[docName.replace(/s$/, '')]: {
|
|
current: current,
|
|
previous: _.pick(previous, changed)
|
|
}
|
|
};
|
|
|
|
return payload;
|
|
});
|
|
};
|