mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
a4a9ba7940
refs: https://github.com/TryGhost/Toolbox/issues/229 - we are getting rid of the concept of having multiple api versions in a single ghost install - removed all the code for multiple api versions & left canary wired up, but without the version in the URL - TODO: reorganise the folders so there's no canary folder when we're closer to shipping we need to minimise the pain of merging changes across from main for now
73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
module.exports = (event, model) => {
|
|
const _ = require('lodash');
|
|
const {sequence} = require('@tryghost/promise');
|
|
const api = require('../../api').endpoints;
|
|
|
|
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 api.shared
|
|
.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 api.shared
|
|
.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;
|
|
});
|
|
};
|