2019-08-09 19:41:24 +05:30
|
|
|
const models = require('../../models');
|
2021-10-06 08:59:21 +01:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-22 13:22:20 -05:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-08-06 11:48:49 +04:00
|
|
|
const getPostServiceInstance = require('../../services/posts/posts-service');
|
2022-05-09 11:06:59 +02:00
|
|
|
const allowedIncludes = ['tags', 'authors', 'authors.roles', 'email', 'tiers', 'newsletter'];
|
2019-10-08 15:44:27 +02:00
|
|
|
const unsafeAttrs = ['status', 'authors', 'visibility'];
|
2019-08-09 19:41:24 +05:30
|
|
|
|
2021-10-06 08:59:21 +01:00
|
|
|
const messages = {
|
|
|
|
postNotFound: 'Post not found.'
|
|
|
|
};
|
|
|
|
|
2022-04-08 12:27:43 +01:00
|
|
|
const postsService = getPostServiceInstance();
|
2021-08-05 14:51:47 +04:00
|
|
|
|
2019-08-09 19:41:24 +05:30
|
|
|
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 13:22:20 -05:00
|
|
|
throw new errors.NotFoundError({
|
2021-10-06 08:59:21 +01:00
|
|
|
message: tpl(messages.postNotFound)
|
2019-08-09 19:41:24 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'include',
|
2020-06-18 13:59:01 +01:00
|
|
|
'formats',
|
2020-11-06 17:32:23 +00:00
|
|
|
'source'
|
2019-08-09 19:41:24 +05:30
|
|
|
],
|
|
|
|
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 13:59:01 +01:00
|
|
|
'formats',
|
2019-08-09 19:41:24 +05:30
|
|
|
'source',
|
Renamed newsletter_id and email_recipient_filter options (#14798)
refs https://github.com/TryGhost/Team/issues/1596
- Renamed `newsletter_id` to `newsletter` option, the `newsletter` option expects a slug instead of an id
- Renamed `email_recipient_filter` to `email_segment` option
- Default `email_segment` to `all`. Ignored if no newsletter is set
- `email_segment` is ignored if no newsletter is set
- When reverting a post to a draft, both `newsletter` and `email_segment` are reset to their default values (null, all)
- Removed legacy mapping from old email_recipient_filter values 'paid' and 'free' (already a migration in place)
- Dropped legacy throwing errors when email_recipient_filter is paid or free in transformEmailRecipientFilter
- Reorganized transformEmailRecipientFilter parameters for the now required newsletter parameter
- Fixed an issue where the newsletter filter wasn't working because it wasn't in permittedoptions
- Fixed an issue where you could send to an archived newsletter
- Added an extra protection when scheduling to an active, and later archiving the newsletter
- Dropped support for `send_email_when_published` in API
- When importing posts we currently don't have a system in place to set the newsletter_id to map the `send_email_when_published` behaviour. Since this was already the case, I won't include a fix in this PR.
- Stripped `email_recipient_filter`/`email_segment` from Content API (https://ghost.slack.com/archives/C02G9E68C/p1652363211841359?thread_ts=1650623650.233229&cid=C02G9E68C)
- Updated `admin-api-schema` to 3.2.0, which includes the new email_segment property
- Contains a temporary fix for https://github.com/TryGhost/Team/issues/1626, where the `.related('newsletter').fetch` call fails when the newsletter relation is already loaded, because of the overridden `formatOnWrite` method.
Since the `email_recipient_filter` is no longer used without a newsletter, the `none` value is no longer used. A migration transforms all those values to `all`. This should be safe, because we only send an email now when newsletter_id is not null (scheduled posts should already have a newsletter_id, even if at the time of scheduling they didn't add the newsletter_id option, because at that time, we defaulted to the default newsletter).
Admin changes to make this work: https://github.com/TryGhost/Admin/pull/2380
2022-05-16 10:18:04 +02:00
|
|
|
'email_segment',
|
|
|
|
'newsletter',
|
2020-06-12 18:05:57 +01:00
|
|
|
'force_rerender',
|
2019-08-09 19:41:24 +05:30
|
|
|
// NOTE: only for internal context
|
|
|
|
'forUpdate',
|
|
|
|
'transacting'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
2019-12-17 19:24:27 +05:30
|
|
|
async query(frame) {
|
2021-08-05 15:18:29 +04:00
|
|
|
let model = await postsService.editPost(frame);
|
2019-08-09 19:41:24 +05:30
|
|
|
|
2021-08-05 14:51:47 +04:00
|
|
|
this.headers.cacheInvalidate = postsService.handleCacheInvalidation(model);
|
|
|
|
|
2019-12-17 19:24:27 +05:30
|
|
|
return model;
|
2019-08-09 19:41:24 +05:30
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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 07:20:56 +01:00
|
|
|
.then(() => null)
|
2019-08-09 19:41:24 +05:30
|
|
|
.catch(models.Post.NotFoundError, () => {
|
2020-05-22 13:22:20 -05:00
|
|
|
return Promise.reject(new errors.NotFoundError({
|
2021-10-06 08:59:21 +01:00
|
|
|
message: tpl(messages.postNotFound)
|
2020-04-13 11:20:51 +01:00
|
|
|
}));
|
2019-08-09 19:41:24 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|