2018-10-05 01:50:45 +03:00
|
|
|
const models = require('../../models');
|
2021-05-03 19:29:44 +03:00
|
|
|
const i18n = require('../../../shared/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-02-22 06:17:14 +03:00
|
|
|
const ALLOWED_INCLUDES = ['tags', 'authors', 'authors.roles'];
|
2019-10-08 16:44:27 +03:00
|
|
|
const UNSAFE_ATTRS = ['status', 'authors', 'visibility'];
|
2018-10-05 01:50:45 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'pages',
|
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'filter',
|
|
|
|
'fields',
|
|
|
|
'formats',
|
|
|
|
'limit',
|
|
|
|
'order',
|
2019-02-22 06:17:14 +03:00
|
|
|
'page',
|
|
|
|
'debug',
|
|
|
|
'absolute_urls'
|
2018-10-05 01:50:45 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
2018-10-17 12:48:06 +03:00
|
|
|
values: ALLOWED_INCLUDES
|
2018-10-05 01:50:45 +03:00
|
|
|
},
|
|
|
|
formats: {
|
|
|
|
values: models.Post.allowedFormats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-02-22 06:17:14 +03:00
|
|
|
permissions: {
|
|
|
|
docName: 'posts',
|
|
|
|
unsafeAttrs: UNSAFE_ATTRS
|
|
|
|
},
|
2018-10-05 01:50:45 +03:00
|
|
|
query(frame) {
|
|
|
|
return models.Post.findPage(frame.options);
|
|
|
|
}
|
2018-10-17 12:48:06 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'fields',
|
|
|
|
'formats',
|
2018-11-05 20:07:45 +03:00
|
|
|
'debug',
|
2019-07-31 23:34:49 +03:00
|
|
|
'absolute_urls',
|
|
|
|
// NOTE: only for internal context
|
|
|
|
'forUpdate',
|
|
|
|
'transacting'
|
2018-10-17 12:48:06 +03:00
|
|
|
],
|
|
|
|
data: [
|
|
|
|
'id',
|
|
|
|
'slug',
|
|
|
|
'uuid'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
formats: {
|
|
|
|
values: models.Post.allowedFormats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-02-22 06:17:14 +03:00
|
|
|
permissions: {
|
|
|
|
docName: 'posts',
|
|
|
|
unsafeAttrs: UNSAFE_ATTRS
|
|
|
|
},
|
2018-10-17 12:48:06 +03:00
|
|
|
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')
|
2018-10-17 12:48:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
2019-02-22 06:17:14 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
2020-01-08 19:44:34 +03:00
|
|
|
'include',
|
|
|
|
'source'
|
2019-02-22 06:17:14 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
2020-01-08 19:44:34 +03:00
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
2019-02-22 06:17:14 +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',
|
2019-07-31 23:34:49 +03:00
|
|
|
'id',
|
2020-01-08 19:44:34 +03:00
|
|
|
'source',
|
2019-07-31 23:34:49 +03:00
|
|
|
// NOTE: only for internal context
|
|
|
|
'forUpdate',
|
|
|
|
'transacting'
|
2019-02-22 06:17:14 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: ALLOWED_INCLUDES
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
2020-01-08 19:44:34 +03:00
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
2019-02-22 06:17:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
docName: 'posts',
|
|
|
|
unsafeAttrs: UNSAFE_ATTRS
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.edit(frame.data.pages[0], frame.options)
|
|
|
|
.then((model) => {
|
2019-03-12 21:35:54 +03:00
|
|
|
if (
|
|
|
|
model.get('status') === 'published' && model.wasChanged() ||
|
|
|
|
model.get('status') === 'draft' && model.previous('status') === 'published'
|
|
|
|
) {
|
2019-02-22 06:17:14 +03:00
|
|
|
this.headers.cacheInvalidate = true;
|
2019-03-12 21:35:54 +03:00
|
|
|
} else if (
|
|
|
|
model.get('status') === 'draft' && model.previous('status') !== 'published' ||
|
|
|
|
model.get('status') === 'scheduled' && model.wasChanged()
|
|
|
|
) {
|
2019-02-22 06:17:14 +03:00
|
|
|
this.headers.cacheInvalidate = {
|
2019-06-18 16:13:55 +03:00
|
|
|
value: urlUtils.urlFor({
|
|
|
|
relativeUrl: urlUtils.urlJoin('/p', model.get('uuid'), '/')
|
2019-02-22 06:17:14 +03:00
|
|
|
})
|
|
|
|
};
|
|
|
|
} 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-02-22 06:17:14 +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-02-22 06:17:14 +03:00
|
|
|
});
|
|
|
|
}
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
};
|