2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
2021-04-27 16:18:04 +03:00
|
|
|
const i18n = require('../../lib/common/i18n');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
2019-08-09 17:11:24 +03:00
|
|
|
const ALLOWED_INCLUDES = ['tags', 'authors', 'authors.roles'];
|
2019-10-08 16:44:27 +03:00
|
|
|
const UNSAFE_ATTRS = ['status', 'authors', 'visibility'];
|
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({
|
|
|
|
message: i18n.t('errors.api.pages.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
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.edit(frame.data.pages[0], frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (
|
|
|
|
model.get('status') === 'published' && model.wasChanged() ||
|
|
|
|
model.get('status') === 'draft' && model.previous('status') === 'published'
|
|
|
|
) {
|
|
|
|
this.headers.cacheInvalidate = true;
|
|
|
|
} else if (
|
|
|
|
model.get('status') === 'draft' && model.previous('status') !== 'published' ||
|
|
|
|
model.get('status') === 'scheduled' && model.wasChanged()
|
|
|
|
) {
|
|
|
|
this.headers.cacheInvalidate = {
|
|
|
|
value: urlUtils.urlFor({
|
|
|
|
relativeUrl: urlUtils.urlJoin('/p', model.get('uuid'), '/')
|
|
|
|
})
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.headers.cacheInvalidate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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({
|
|
|
|
message: i18n.t('errors.api.pages.pageNotFound')
|
2020-04-13 13:20:51 +03:00
|
|
|
}));
|
2019-08-09 17:11:24 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|