2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
2020-05-22 21:22:20 +03:00
|
|
|
const {i18n} = require('../../lib/common');
|
|
|
|
const errors = require('@tryghost/errors');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
2019-11-06 14:32:43 +03:00
|
|
|
const {mega} = require('../../services/mega');
|
2019-12-17 16:54:27 +03:00
|
|
|
const membersService = require('../../services/members');
|
2019-11-06 12:30:11 +03:00
|
|
|
const allowedIncludes = ['tags', 'authors', 'authors.roles', 'email'];
|
2019-10-08 16:44:27 +03:00
|
|
|
const unsafeAttrs = ['status', 'authors', 'visibility'];
|
2019-08-09 17:11:24 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'posts',
|
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'filter',
|
|
|
|
'fields',
|
|
|
|
'formats',
|
|
|
|
'limit',
|
|
|
|
'order',
|
|
|
|
'page',
|
|
|
|
'debug',
|
|
|
|
'absolute_urls'
|
|
|
|
],
|
|
|
|
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',
|
|
|
|
'debug',
|
|
|
|
'absolute_urls',
|
|
|
|
// NOTE: only for internal context
|
|
|
|
'forUpdate',
|
|
|
|
'transacting'
|
|
|
|
],
|
|
|
|
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')
|
2019-08-09 17:11:24 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'include',
|
2020-06-18 15:59:01 +03:00
|
|
|
'formats',
|
2020-11-06 20:32:23 +03:00
|
|
|
'source'
|
2019-08-09 17:11:24 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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',
|
|
|
|
'id',
|
2020-06-18 15:59:01 +03:00
|
|
|
'formats',
|
2019-08-09 17:11:24 +03:00
|
|
|
'source',
|
2020-11-06 20:32:23 +03:00
|
|
|
'email_recipient_filter',
|
2020-06-12 20:05:57 +03:00
|
|
|
'force_rerender',
|
2019-08-09 17:11:24 +03:00
|
|
|
// NOTE: only for internal context
|
|
|
|
'forUpdate',
|
|
|
|
'transacting'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
2020-11-06 20:32:23 +03:00
|
|
|
},
|
|
|
|
email_recipient_filter: {
|
|
|
|
values: ['none', 'free', 'paid', 'all']
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
2019-12-17 16:54:27 +03:00
|
|
|
async query(frame) {
|
2020-07-23 20:30:07 +03:00
|
|
|
/**Check host limits for members when send email is true**/
|
2020-11-06 20:32:23 +03:00
|
|
|
if (frame.options.email_recipient_filter && frame.options.email_recipient_filter !== 'none') {
|
2020-07-23 20:30:07 +03:00
|
|
|
await membersService.checkHostLimit();
|
2019-12-17 16:54:27 +03:00
|
|
|
}
|
2019-11-06 14:32:43 +03:00
|
|
|
|
2019-12-17 16:54:27 +03:00
|
|
|
let model = await models.Post.edit(frame.data.posts[0], frame.options);
|
2019-11-18 17:28:54 +03:00
|
|
|
|
2019-12-17 16:54:27 +03:00
|
|
|
/**Handle newsletter email */
|
2020-11-06 20:32:23 +03:00
|
|
|
if (model.get('email_recipient_filter') !== 'none') {
|
2019-12-17 16:54:27 +03:00
|
|
|
const postPublished = model.wasChanged() && (model.get('status') === 'published') && (model.previous('status') !== 'published');
|
|
|
|
if (postPublished) {
|
|
|
|
let postEmail = model.relations.email;
|
2019-11-18 17:28:54 +03:00
|
|
|
|
2019-12-17 16:54:27 +03:00
|
|
|
if (!postEmail) {
|
|
|
|
const email = await mega.addEmail(model, frame.options);
|
|
|
|
model.set('email', email);
|
|
|
|
} else if (postEmail && postEmail.get('status') === 'failed') {
|
|
|
|
const email = await mega.retryFailedEmail(postEmail);
|
|
|
|
model.set('email', email);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
2019-12-17 16:54:27 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2019-12-17 16:54:27 +03:00
|
|
|
/**Handle cache invalidation */
|
|
|
|
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;
|
2019-08-09 17:11:24 +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)
|
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.posts.postNotFound')
|
2020-04-13 13:20:51 +03:00
|
|
|
}));
|
2019-08-09 17:11:24 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|