2022-03-09 13:02:17 +00:00
|
|
|
const nql = require('@tryghost/nql');
|
2021-08-05 14:51:47 +04:00
|
|
|
const {BadRequestError} = require('@tryghost/errors');
|
2021-08-06 12:21:23 +04:00
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
|
|
|
|
const messages = {
|
2022-04-13 15:27:43 +02:00
|
|
|
invalidVisibilityFilter: 'Invalid visibility filter.',
|
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
|
|
|
invalidEmailSegment: 'The email segment parameter doesn\'t contain a valid filter'
|
2021-08-06 12:21:23 +04:00
|
|
|
};
|
|
|
|
|
2021-08-05 14:51:47 +04:00
|
|
|
class PostsService {
|
2022-04-08 12:27:43 +01:00
|
|
|
constructor({mega, urlUtils, models, isSet}) {
|
2021-08-05 14:51:47 +04:00
|
|
|
this.mega = mega;
|
|
|
|
this.urlUtils = urlUtils;
|
|
|
|
this.models = models;
|
2021-08-06 11:50:09 +04:00
|
|
|
this.isSet = isSet;
|
2021-08-05 14:51:47 +04:00
|
|
|
}
|
|
|
|
|
2021-08-05 15:18:29 +04:00
|
|
|
async editPost(frame) {
|
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
|
|
|
// Make sure the newsletter is matching an active newsletter
|
|
|
|
// Note that this option is simply ignored if the post isn't published or scheduled
|
|
|
|
if (frame.options.newsletter && frame.options.email_segment) {
|
|
|
|
if (frame.options.email_segment !== 'all') {
|
2021-08-05 15:18:29 +04:00
|
|
|
// check filter is valid
|
|
|
|
try {
|
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
|
|
|
await this.models.Member.findPage({filter: frame.options.email_segment, limit: 1});
|
2021-08-05 15:18:29 +04:00
|
|
|
} catch (err) {
|
|
|
|
return Promise.reject(new BadRequestError({
|
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
|
|
|
message: tpl(messages.invalidEmailSegment),
|
2021-08-05 15:18:29 +04:00
|
|
|
context: err.message
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const model = await this.models.Post.edit(frame.data.posts[0], frame.options);
|
2021-08-05 15:18:29 +04:00
|
|
|
|
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
|
|
|
/**Handle newsletter email */
|
|
|
|
if (model.get('newsletter_id')) {
|
2021-10-05 12:00:52 +02:00
|
|
|
const sendEmail = model.wasChanged() && this.shouldSendEmail(model.get('status'), model.previous('status'));
|
2021-08-06 11:50:09 +04:00
|
|
|
|
2021-10-05 12:00:52 +02:00
|
|
|
if (sendEmail) {
|
2021-08-05 15:18:29 +04:00
|
|
|
let postEmail = model.relations.email;
|
|
|
|
|
|
|
|
if (!postEmail) {
|
2022-05-09 17:30:50 +02:00
|
|
|
const email = await this.mega.addEmail(model, frame.options);
|
2021-08-05 15:18:29 +04:00
|
|
|
model.set('email', email);
|
|
|
|
} else if (postEmail && postEmail.get('status') === 'failed') {
|
|
|
|
const email = await this.mega.retryFailedEmail(postEmail);
|
|
|
|
model.set('email', email);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:50:52 +05:30
|
|
|
async getProductsFromVisibilityFilter(visibilityFilter) {
|
|
|
|
try {
|
|
|
|
const allProducts = await this.models.Product.findAll();
|
|
|
|
const visibilityFilterJson = nql(visibilityFilter).toJSON();
|
|
|
|
const productsData = (visibilityFilterJson.product ? [visibilityFilterJson] : visibilityFilterJson.$or) || [];
|
|
|
|
const tiers = productsData
|
|
|
|
.map((data) => {
|
|
|
|
return allProducts.find((p) => {
|
|
|
|
return p.get('slug') === data.product;
|
|
|
|
});
|
|
|
|
}).filter(p => !!p).map((d) => {
|
|
|
|
return d.toJSON();
|
|
|
|
});
|
|
|
|
return tiers;
|
|
|
|
} catch (err) {
|
|
|
|
return Promise.reject(new BadRequestError({
|
|
|
|
message: tpl(messages.invalidVisibilityFilter),
|
|
|
|
context: err.message
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 09:51:11 +02:00
|
|
|
/**
|
|
|
|
* Returns the most recently `published_at` post that was published or sent
|
|
|
|
* via email
|
|
|
|
*/
|
|
|
|
async getMostRecentlyPublishedPost() {
|
|
|
|
const recentlyPublishedPost = await this.models.Post.findPage({
|
|
|
|
status: ['published', 'sent'],
|
|
|
|
order: 'published_at DESC',
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
return recentlyPublishedPost?.data[0];
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:00:52 +02:00
|
|
|
/**
|
|
|
|
* Calculates if the email should be tried to be sent out
|
|
|
|
* @private
|
|
|
|
* @param {String} currentStatus current status from the post model
|
|
|
|
* @param {String} previousStatus previous status from the post model
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
shouldSendEmail(currentStatus, previousStatus) {
|
|
|
|
return (['published', 'sent'].includes(currentStatus))
|
|
|
|
&& (!['published', 'sent'].includes(previousStatus));
|
|
|
|
}
|
|
|
|
|
2021-08-05 14:51:47 +04:00
|
|
|
handleCacheInvalidation(model) {
|
|
|
|
let cacheInvalidate;
|
|
|
|
|
|
|
|
if (
|
|
|
|
model.get('status') === 'published' && model.wasChanged() ||
|
|
|
|
model.get('status') === 'draft' && model.previous('status') === 'published'
|
|
|
|
) {
|
|
|
|
cacheInvalidate = true;
|
|
|
|
} else if (
|
|
|
|
model.get('status') === 'draft' && model.previous('status') !== 'published' ||
|
|
|
|
model.get('status') === 'scheduled' && model.wasChanged()
|
|
|
|
) {
|
|
|
|
cacheInvalidate = {
|
|
|
|
value: this.urlUtils.urlFor({
|
|
|
|
relativeUrl: this.urlUtils.urlJoin('/p', model.get('uuid'), '/')
|
|
|
|
})
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
cacheInvalidate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cacheInvalidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:48:49 +04:00
|
|
|
/**
|
|
|
|
* @returns {PostsService} instance of the PostsService
|
|
|
|
*/
|
2022-04-08 12:27:43 +01:00
|
|
|
const getPostServiceInstance = () => {
|
2021-08-06 11:48:49 +04:00
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
|
|
|
const {mega} = require('../mega');
|
2021-08-06 11:50:09 +04:00
|
|
|
const labs = require('../../../shared/labs');
|
2021-08-06 11:48:49 +04:00
|
|
|
const models = require('../../models');
|
|
|
|
|
|
|
|
return new PostsService({
|
|
|
|
mega: mega,
|
|
|
|
urlUtils: urlUtils,
|
2021-08-06 11:50:09 +04:00
|
|
|
models: models,
|
|
|
|
isSet: labs.isSet.bind(labs)
|
2021-08-06 11:48:49 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = getPostServiceInstance;
|
2021-10-05 12:00:52 +02:00
|
|
|
// exposed for testing purposes only
|
|
|
|
module.exports.PostsService = PostsService;
|