2022-01-26 14:20:52 +03:00
|
|
|
const nql = require('@nexes/nql');
|
2021-08-05 13:51:47 +03:00
|
|
|
const {BadRequestError} = require('@tryghost/errors');
|
2021-08-06 11:21:23 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
|
|
|
|
const messages = {
|
2022-01-26 14:20:52 +03:00
|
|
|
invalidEmailRecipientFilter: 'Invalid filter in email_recipient_filter param.',
|
|
|
|
invalidVisibilityFilter: 'Invalid visibility filter.'
|
2021-08-06 11:21:23 +03:00
|
|
|
};
|
|
|
|
|
2021-08-05 13:51:47 +03:00
|
|
|
class PostsService {
|
2021-08-06 11:21:23 +03:00
|
|
|
constructor({mega, apiVersion, urlUtils, models, isSet}) {
|
2021-08-05 13:51:47 +03:00
|
|
|
this.apiVersion = apiVersion;
|
|
|
|
this.mega = mega;
|
|
|
|
this.urlUtils = urlUtils;
|
|
|
|
this.models = models;
|
2021-08-06 10:50:09 +03:00
|
|
|
this.isSet = isSet;
|
2021-08-05 13:51:47 +03:00
|
|
|
}
|
|
|
|
|
2021-08-05 14:18:29 +03:00
|
|
|
async editPost(frame) {
|
|
|
|
let model;
|
|
|
|
|
|
|
|
if (!frame.options.email_recipient_filter && frame.options.send_email_when_published) {
|
|
|
|
await this.models.Base.transaction(async (transacting) => {
|
|
|
|
const options = {
|
|
|
|
...frame.options,
|
|
|
|
transacting
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. We need to edit the post first in order to know what the visibility is.
|
|
|
|
* 2. We can only pass the email_recipient_filter when we change the status.
|
|
|
|
*
|
|
|
|
* So, we first edit the post as requested, with all information except the status,
|
|
|
|
* from there we can determine what the email_recipient_filter should be and then finish
|
|
|
|
* the edit, with the status and the email_recipient_filter option.
|
|
|
|
*/
|
|
|
|
const status = frame.data.posts[0].status;
|
|
|
|
delete frame.data.posts[0].status;
|
|
|
|
const interimModel = await this.models.Post.edit(frame.data.posts[0], options);
|
|
|
|
frame.data.posts[0].status = status;
|
|
|
|
|
|
|
|
options.email_recipient_filter = interimModel.get('visibility') === 'paid' ? 'paid' : 'all';
|
|
|
|
|
|
|
|
model = await this.models.Post.edit(frame.data.posts[0], options);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
model = await this.models.Post.edit(frame.data.posts[0], frame.options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**Handle newsletter email */
|
|
|
|
const emailRecipientFilter = model.get('email_recipient_filter');
|
|
|
|
if (emailRecipientFilter !== 'none') {
|
|
|
|
if (emailRecipientFilter !== 'all') {
|
|
|
|
// check filter is valid
|
|
|
|
try {
|
|
|
|
await this.models.Member.findPage({filter: `subscribed:true+${emailRecipientFilter}`, limit: 1});
|
|
|
|
} catch (err) {
|
|
|
|
return Promise.reject(new BadRequestError({
|
2021-08-06 12:21:28 +03:00
|
|
|
message: tpl(messages.invalidEmailRecipientFilter),
|
2021-08-05 14:18:29 +03:00
|
|
|
context: err.message
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 13:00:52 +03:00
|
|
|
const sendEmail = model.wasChanged() && this.shouldSendEmail(model.get('status'), model.previous('status'));
|
2021-08-06 10:50:09 +03:00
|
|
|
|
2021-10-05 13:00:52 +03:00
|
|
|
if (sendEmail) {
|
2021-08-05 14:18:29 +03:00
|
|
|
let postEmail = model.relations.email;
|
|
|
|
|
|
|
|
if (!postEmail) {
|
|
|
|
const email = await this.mega.addEmail(model, Object.assign({}, frame.options, {apiVersion: this.apiVersion}));
|
|
|
|
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 14:20:52 +03:00
|
|
|
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
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 13:00:52 +03: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 13:51:47 +03: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 10:48:49 +03:00
|
|
|
/**
|
|
|
|
* @param {string} apiVersion - API version to use within the service
|
|
|
|
* @returns {PostsService} instance of the PostsService
|
|
|
|
*/
|
|
|
|
const getPostServiceInstance = (apiVersion) => {
|
|
|
|
const urlUtils = require('../../../shared/url-utils');
|
|
|
|
const {mega} = require('../mega');
|
2021-08-06 10:50:09 +03:00
|
|
|
const labs = require('../../../shared/labs');
|
2021-08-06 10:48:49 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
|
|
|
|
return new PostsService({
|
|
|
|
apiVersion: apiVersion,
|
|
|
|
mega: mega,
|
|
|
|
urlUtils: urlUtils,
|
2021-08-06 10:50:09 +03:00
|
|
|
models: models,
|
|
|
|
isSet: labs.isSet.bind(labs)
|
2021-08-06 10:48:49 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = getPostServiceInstance;
|
2021-10-05 13:00:52 +03:00
|
|
|
// exposed for testing purposes only
|
|
|
|
module.exports.PostsService = PostsService;
|