Ghost/core/server/models/newsletter.js
Simon Backx 6b3a657f88
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

184 lines
5.4 KiB
JavaScript

const ghostBookshelf = require('./base');
const ObjectID = require('bson-objectid');
const uuid = require('uuid');
const urlUtils = require('../../shared/url-utils');
const Newsletter = ghostBookshelf.Model.extend({
tableName: 'newsletters',
defaults: function defaults() {
return {
uuid: uuid.v4(),
sender_reply_to: 'newsletter',
status: 'active',
visibility: 'members',
subscribe_on_signup: true,
sort_order: 0,
title_font_category: 'sans_serif',
title_alignment: 'center',
show_feature_image: true,
body_font_category: 'sans_serif',
show_badge: true,
show_header_icon: true,
show_header_title: true,
show_header_name: true
};
},
members() {
return this.belongsToMany('Member', 'members_newsletters', 'newsletter_id', 'member_id')
.query((qb) => {
// avoids bookshelf adding a `DISTINCT` to the query
// we know the result set will already be unique and DISTINCT hurts query performance
qb.columns('members.*');
});
},
posts() {
return this.hasMany('Post');
},
// Force active newsletters for content API
enforcedFilters: function enforcedFilters(options) {
return (options.context && options.context.public) ? 'status:active' : null;
},
async onSaving(model, _attr, options) {
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
if (model.get('name')) {
model.set('name', model.get('name').trim());
}
if (model.hasChanged('slug') || !model.get('slug')) {
const slug = model.get('slug') || model.get('name');
if (slug) {
const cleanSlug = await ghostBookshelf.Model.generateSlug(Newsletter, slug, {
transacting: options.transacting
});
model.set({slug: cleanSlug});
}
}
},
subscribeMembersById(memberIds, unfilteredOptions = {}) {
let pivotRows = [];
for (const memberId of memberIds) {
pivotRows.push({
id: ObjectID().toHexString(),
member_id: memberId.id,
newsletter_id: this.id
});
}
const query = ghostBookshelf.knex.batchInsert('members_newsletters', pivotRows);
if (unfilteredOptions.transacting) {
query.transacting(unfilteredOptions.transacting);
}
return query;
},
formatOnWrite(attrs) {
['header_image'].forEach((attr) => {
if (attrs[attr]) {
attrs[attr] = urlUtils.toTransformReady(attrs[attr]);
}
});
return attrs;
},
parse() {
const attrs = ghostBookshelf.Model.prototype.parse.apply(this, arguments);
['header_image'].forEach((attr) => {
if (attrs[attr]) {
attrs[attr] = urlUtils.transformReadyToAbsolute(attrs[attr]);
}
});
return attrs;
}
}, {
/**
* Returns an array of keys permitted in a method's `options` hash, depending on the current method.
* @param {String} methodName The name of the method to check valid options for.
* @return {Array} Keys allowed in the `options` hash of the model's method.
*/
permittedOptions: function permittedOptions(methodName) {
let options = ghostBookshelf.Model.permittedOptions.call(this, methodName);
// allowlists for the `options` hash argument on methods, by method name.
// these are the only options that can be passed to Bookshelf / Knex.
const validOptions = {
findOne: ['filter'],
findAll: ['filter']
};
if (validOptions[methodName]) {
options = options.concat(validOptions[methodName]);
}
return options;
},
orderDefaultRaw: function () {
return 'sort_order ASC, created_at ASC, id ASC';
},
orderDefaultOptions: function orderDefaultOptions() {
return {
sort_order: 'ASC',
created_at: 'ASC',
id: 'ASC'
};
},
getDefaultNewsletter: async function getDefaultNewsletter(unfilteredOptions = {}) {
const options = {
filter: 'status:active',
order: this.orderDefaultRaw(),
limit: 1
};
if (unfilteredOptions.transacting) {
options.transacting = unfilteredOptions.transacting;
}
const newsletters = await this.findPage(options);
if (newsletters.data.length > 0) {
return newsletters.data[0];
}
return null;
},
getNextAvailableSortOrder: async function getNextAvailableSortOrder(unfilteredOptions = {}) {
const options = {
filter: 'status:active',
order: 'sort_order DESC', // there's no NQL syntax available here
limit: 1,
columns: ['sort_order']
};
if (unfilteredOptions.transacting) {
options.transacting = unfilteredOptions.transacting;
}
const lastNewsletter = await this.findPage(options);
if (lastNewsletter.data.length > 0) {
return lastNewsletter.data[0].get('sort_order') + 1;
}
return 0;
}
});
module.exports = {
Newsletter: ghostBookshelf.model('Newsletter', Newsletter)
};