2018-10-10 17:34:16 +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');
|
2021-08-05 13:51:47 +03:00
|
|
|
const PostsService = require('../../services/posts/posts-service');
|
2019-02-22 06:17:14 +03:00
|
|
|
const allowedIncludes = ['tags', 'authors', 'authors.roles'];
|
2019-10-08 16:44:27 +03:00
|
|
|
const unsafeAttrs = ['status', 'authors', 'visibility'];
|
2018-10-10 17:34:16 +03:00
|
|
|
|
2021-08-05 13:51:47 +03:00
|
|
|
const postsService = new PostsService({
|
|
|
|
apiVersion: 'v2',
|
|
|
|
mega: null,
|
|
|
|
urlUtils: urlUtils,
|
|
|
|
i18n: i18n,
|
|
|
|
models: models
|
|
|
|
});
|
|
|
|
|
2018-10-10 17:34:16 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'posts',
|
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'filter',
|
|
|
|
'fields',
|
|
|
|
'formats',
|
|
|
|
'limit',
|
|
|
|
'order',
|
2018-10-18 12:05:51 +03:00
|
|
|
'page',
|
2018-11-05 20:07:45 +03:00
|
|
|
'debug',
|
|
|
|
'absolute_urls'
|
2018-10-10 17:34:16 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
formats: {
|
|
|
|
values: models.Post.allowedFormats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.findPage(frame.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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-10 17:34:16 +03:00
|
|
|
],
|
|
|
|
data: [
|
|
|
|
'id',
|
|
|
|
'slug',
|
|
|
|
'uuid'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
formats: {
|
|
|
|
values: models.Post.allowedFormats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
|
|
|
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.posts.postNotFound')
|
2018-10-10 17:34:16 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
2019-01-18 14:29:56 +03:00
|
|
|
'include',
|
|
|
|
'source'
|
2018-10-10 17:34:16 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
2019-01-18 14:29:56 +03:00
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
2018-10-10 17:34:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.add(frame.data.posts[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-01-18 14:29:56 +03:00
|
|
|
'id',
|
2019-07-31 23:34:49 +03:00
|
|
|
'source',
|
|
|
|
// NOTE: only for internal context
|
|
|
|
'forUpdate',
|
|
|
|
'transacting'
|
2018-10-10 17:34:16 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
2019-01-18 14:29:56 +03:00
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
2018-10-10 17:34:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
2021-08-05 13:13:02 +03:00
|
|
|
async query(frame) {
|
2021-08-05 13:14:58 +03:00
|
|
|
const model = await models.Post.edit(frame.data.posts[0], frame.options);
|
2018-10-10 17:34:16 +03:00
|
|
|
|
2021-08-05 13:51:47 +03:00
|
|
|
this.headers.cacheInvalidate = postsService.handleCacheInvalidation(model);
|
2021-08-05 13:13:02 +03:00
|
|
|
|
|
|
|
return model;
|
2018-10-10 17:34:16 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
frame.options.require = true;
|
|
|
|
|
|
|
|
return models.Post.destroy(frame.options)
|
2020-04-07 09:20:56 +03:00
|
|
|
.then(() => null)
|
2018-10-10 17:34:16 +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.posts.postNotFound')
|
2020-04-13 13:20:51 +03:00
|
|
|
}));
|
2018-10-10 17:34:16 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|