2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
2021-10-06 13:47:26 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-08-06 10:48:49 +03:00
|
|
|
const getPostServiceInstance = require('../../services/posts/posts-service');
|
2022-01-26 14:20:52 +03:00
|
|
|
const ALLOWED_INCLUDES = ['tags', 'authors', 'authors.roles', 'tiers'];
|
2019-10-08 16:44:27 +03:00
|
|
|
const UNSAFE_ATTRS = ['status', 'authors', 'visibility'];
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2021-10-06 13:47:26 +03:00
|
|
|
const messages = {
|
|
|
|
pageNotFound: 'Page not found.'
|
|
|
|
};
|
|
|
|
|
2021-08-06 10:48:49 +03:00
|
|
|
const postsService = getPostServiceInstance('canary');
|
2021-08-05 13:51:47 +03:00
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'pages',
|
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'filter',
|
|
|
|
'fields',
|
|
|
|
'formats',
|
|
|
|
'limit',
|
|
|
|
'order',
|
|
|
|
'page',
|
|
|
|
'debug',
|
|
|
|
'absolute_urls'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
formats: {
|
|
|
|
values: models.Post.allowedFormats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
docName: 'posts',
|
|
|
|
unsafeAttrs: UNSAFE_ATTRS
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.findPage(frame.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'fields',
|
|
|
|
'formats',
|
|
|
|
'debug',
|
|
|
|
'absolute_urls',
|
|
|
|
// NOTE: only for internal context
|
|
|
|
'forUpdate',
|
|
|
|
'transacting'
|
|
|
|
],
|
|
|
|
data: [
|
|
|
|
'id',
|
|
|
|
'slug',
|
|
|
|
'uuid'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
formats: {
|
|
|
|
values: models.Post.allowedFormats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
docName: 'posts',
|
|
|
|
unsafeAttrs: UNSAFE_ATTRS
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.findOne(frame.data, frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (!model) {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.NotFoundError({
|
2021-10-06 13:47:26 +03:00
|
|
|
message: tpl(messages.pageNotFound)
|
2019-08-09 17:11:24 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
2020-01-08 19:44:34 +03:00
|
|
|
'include',
|
2020-06-18 15:59:01 +03:00
|
|
|
'formats',
|
2020-01-08 19:44:34 +03:00
|
|
|
'source'
|
2019-08-09 17:11:24 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
2020-01-08 19:44:34 +03:00
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
docName: 'posts',
|
|
|
|
unsafeAttrs: UNSAFE_ATTRS
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.add(frame.data.pages[0], frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (model.get('status') !== 'published') {
|
|
|
|
this.headers.cacheInvalidate = false;
|
|
|
|
} else {
|
|
|
|
this.headers.cacheInvalidate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: {
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'id',
|
2020-06-18 15:59:01 +03:00
|
|
|
'formats',
|
2020-01-08 19:44:34 +03:00
|
|
|
'source',
|
2020-06-12 21:15:03 +03:00
|
|
|
'force_rerender',
|
2019-08-09 17:11:24 +03:00
|
|
|
// NOTE: only for internal context
|
|
|
|
'forUpdate',
|
|
|
|
'transacting'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
2020-01-08 19:44:34 +03:00
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
docName: 'posts',
|
|
|
|
unsafeAttrs: UNSAFE_ATTRS
|
|
|
|
},
|
2021-08-05 13:42:16 +03:00
|
|
|
async query(frame) {
|
|
|
|
const model = await models.Post.edit(frame.data.pages[0], frame.options);
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2021-08-05 13:51:47 +03:00
|
|
|
this.headers.cacheInvalidate = postsService.handleCacheInvalidation(model);
|
2021-08-05 13:42:16 +03:00
|
|
|
|
|
|
|
return model;
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
docName: 'posts',
|
|
|
|
unsafeAttrs: UNSAFE_ATTRS
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
frame.options.require = true;
|
|
|
|
|
|
|
|
return models.Post.destroy(frame.options)
|
2020-04-07 09:20:56 +03:00
|
|
|
.then(() => null)
|
2019-08-09 17:11:24 +03:00
|
|
|
.catch(models.Post.NotFoundError, () => {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
2021-10-06 13:47:26 +03:00
|
|
|
message: tpl(messages.pageNotFound)
|
2020-04-13 13:20:51 +03:00
|
|
|
}));
|
2019-08-09 17:11:24 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|