Ghost/core/server/models/email.js

93 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-11-06 10:44:52 +03:00
const uuid = require('uuid');
2019-11-06 08:52:58 +03:00
const ghostBookshelf = require('./base');
const Email = ghostBookshelf.Model.extend({
tableName: 'emails',
2019-11-06 10:44:52 +03:00
defaults: function defaults() {
return {
uuid: uuid.v4(),
status: 'pending',
✨ Added ability to send a newsletter to members with a certain label or product (#12932) refs https://github.com/TryGhost/Team/issues/581 refs https://github.com/TryGhost/Team/issues/582 When publishing a post via the API it was possible to send it using `?email_recipient_filter=all/free/paid` which allowed you to send to members only based on their payment status which is quite limiting for some sites. This PR updates the `?email_recipient_filter` query param to support Ghost's `?filter` param syntax which enables more specific recipient lists, eg: `?email_recipient_filter=status:free` = free members only `?email_recipient_filter=status:paid` = paid members only `?email_recipient_filter=label:vip` = members that have the `vip` label attached `?email_recipient_filter=status:paid,label:vip` = paid members and members that have the `vip` label attached The older `free/paid` values are still supported by the API for backwards compatibility. - updates `Post` and `Email` models to transform legacy `free` and `paid` values to their NQL equivalents on read/write - lets us not worry about supporting legacy values elsewhere in the code - cleanup migration to transform all rows slated for 5.0 - removes schema and API `isIn` validations for recipient filters so allow free-form filters - updates posts API input serializers to transform `free` and `paid` values in the `?email_recipient_filter` param to their NQL equivalents for backwards compatibility - updates Post API controllers `edit` methods to run a query using the supplied filter to verify that it's valid - updates `mega` service to use the filter directly when selecting recipients
2021-05-07 13:56:41 +03:00
recipient_filter: 'status:-free',
track_opens: false,
delivered_count: 0,
opened_count: 0,
failed_count: 0
2019-11-06 10:44:52 +03:00
};
},
✨ Added ability to send a newsletter to members with a certain label or product (#12932) refs https://github.com/TryGhost/Team/issues/581 refs https://github.com/TryGhost/Team/issues/582 When publishing a post via the API it was possible to send it using `?email_recipient_filter=all/free/paid` which allowed you to send to members only based on their payment status which is quite limiting for some sites. This PR updates the `?email_recipient_filter` query param to support Ghost's `?filter` param syntax which enables more specific recipient lists, eg: `?email_recipient_filter=status:free` = free members only `?email_recipient_filter=status:paid` = paid members only `?email_recipient_filter=label:vip` = members that have the `vip` label attached `?email_recipient_filter=status:paid,label:vip` = paid members and members that have the `vip` label attached The older `free/paid` values are still supported by the API for backwards compatibility. - updates `Post` and `Email` models to transform legacy `free` and `paid` values to their NQL equivalents on read/write - lets us not worry about supporting legacy values elsewhere in the code - cleanup migration to transform all rows slated for 5.0 - removes schema and API `isIn` validations for recipient filters so allow free-form filters - updates posts API input serializers to transform `free` and `paid` values in the `?email_recipient_filter` param to their NQL equivalents for backwards compatibility - updates Post API controllers `edit` methods to run a query using the supplied filter to verify that it's valid - updates `mega` service to use the filter directly when selecting recipients
2021-05-07 13:56:41 +03:00
parse() {
const attrs = ghostBookshelf.Model.prototype.parse.apply(this, arguments);
// update legacy recipient_filter values to proper NQL
if (attrs.recipient_filter === 'free') {
attrs.recipient_filter = 'status:free';
}
if (attrs.recipient_filter === 'paid') {
attrs.recipient_filter = 'status:-free';
}
return attrs;
},
formatOnWrite(attrs) {
// update legacy recipient_filter values to proper NQL
if (attrs.recipient_filter === 'free') {
attrs.recipient_filter = 'status:free';
}
if (attrs.recipient_filter === 'paid') {
attrs.recipient_filter = 'status:-free';
}
return attrs;
},
Store email batch and recipient records when sending newsletters (#12195) requires https://github.com/TryGhost/Ghost/pull/12192 - added initial `EmailBatch` and `EmailRecipient` model definitions with defaults and relationships - added missing `post` relationship function to email model - fetch member list without bookshelf - bookshelf can add around 3x overhead when fetching the members list for an email - we don't need full members at this point, only having the data is fine - if we need full models later on we can push the model hydration into background jobs where recipient batches are fetched ready for an email to be sent - bookshelf model instantiation of many models blocks the event loop, using knex directly keeps concurrent requests fast - adds `getFilteredCollectionQuery` method to base model to facilitate getting a knex query based on our normal model filters along with transaction/forUpdate applied - store recipient list before sending email - chunk already-fetched members list into batches and insert records into the `email_recipients` table via knex - chunked into batches of 1000 to match the number of emails that Mailgun accepts in a single API request but this may not be the absolute fastest batch size for recipient insertion: | Batch size | Batch time | Total time | | ---------- | ---------- | ---------- | | 500 | 20ms | 4142ms | | 1000 | 50ms | 4651ms | | 5000 | 170ms | 3540ms | | 10000 | 370ms | 3684ms | - create an email_batch record before inserting recipient rows so we can effeciently fetch recipients by batch and store the overall batch status
2020-09-14 17:40:00 +03:00
post() {
return this.belongsTo('Post', 'post_id');
},
✨ Added ability to send a newsletter to members with a certain label or product (#12932) refs https://github.com/TryGhost/Team/issues/581 refs https://github.com/TryGhost/Team/issues/582 When publishing a post via the API it was possible to send it using `?email_recipient_filter=all/free/paid` which allowed you to send to members only based on their payment status which is quite limiting for some sites. This PR updates the `?email_recipient_filter` query param to support Ghost's `?filter` param syntax which enables more specific recipient lists, eg: `?email_recipient_filter=status:free` = free members only `?email_recipient_filter=status:paid` = paid members only `?email_recipient_filter=label:vip` = members that have the `vip` label attached `?email_recipient_filter=status:paid,label:vip` = paid members and members that have the `vip` label attached The older `free/paid` values are still supported by the API for backwards compatibility. - updates `Post` and `Email` models to transform legacy `free` and `paid` values to their NQL equivalents on read/write - lets us not worry about supporting legacy values elsewhere in the code - cleanup migration to transform all rows slated for 5.0 - removes schema and API `isIn` validations for recipient filters so allow free-form filters - updates posts API input serializers to transform `free` and `paid` values in the `?email_recipient_filter` param to their NQL equivalents for backwards compatibility - updates Post API controllers `edit` methods to run a query using the supplied filter to verify that it's valid - updates `mega` service to use the filter directly when selecting recipients
2021-05-07 13:56:41 +03:00
Store email batch and recipient records when sending newsletters (#12195) requires https://github.com/TryGhost/Ghost/pull/12192 - added initial `EmailBatch` and `EmailRecipient` model definitions with defaults and relationships - added missing `post` relationship function to email model - fetch member list without bookshelf - bookshelf can add around 3x overhead when fetching the members list for an email - we don't need full members at this point, only having the data is fine - if we need full models later on we can push the model hydration into background jobs where recipient batches are fetched ready for an email to be sent - bookshelf model instantiation of many models blocks the event loop, using knex directly keeps concurrent requests fast - adds `getFilteredCollectionQuery` method to base model to facilitate getting a knex query based on our normal model filters along with transaction/forUpdate applied - store recipient list before sending email - chunk already-fetched members list into batches and insert records into the `email_recipients` table via knex - chunked into batches of 1000 to match the number of emails that Mailgun accepts in a single API request but this may not be the absolute fastest batch size for recipient insertion: | Batch size | Batch time | Total time | | ---------- | ---------- | ---------- | | 500 | 20ms | 4142ms | | 1000 | 50ms | 4651ms | | 5000 | 170ms | 3540ms | | 10000 | 370ms | 3684ms | - create an email_batch record before inserting recipient rows so we can effeciently fetch recipients by batch and store the overall batch status
2020-09-14 17:40:00 +03:00
emailBatches() {
return this.hasMany('EmailBatch', 'email_id');
},
✨ Added ability to send a newsletter to members with a certain label or product (#12932) refs https://github.com/TryGhost/Team/issues/581 refs https://github.com/TryGhost/Team/issues/582 When publishing a post via the API it was possible to send it using `?email_recipient_filter=all/free/paid` which allowed you to send to members only based on their payment status which is quite limiting for some sites. This PR updates the `?email_recipient_filter` query param to support Ghost's `?filter` param syntax which enables more specific recipient lists, eg: `?email_recipient_filter=status:free` = free members only `?email_recipient_filter=status:paid` = paid members only `?email_recipient_filter=label:vip` = members that have the `vip` label attached `?email_recipient_filter=status:paid,label:vip` = paid members and members that have the `vip` label attached The older `free/paid` values are still supported by the API for backwards compatibility. - updates `Post` and `Email` models to transform legacy `free` and `paid` values to their NQL equivalents on read/write - lets us not worry about supporting legacy values elsewhere in the code - cleanup migration to transform all rows slated for 5.0 - removes schema and API `isIn` validations for recipient filters so allow free-form filters - updates posts API input serializers to transform `free` and `paid` values in the `?email_recipient_filter` param to their NQL equivalents for backwards compatibility - updates Post API controllers `edit` methods to run a query using the supplied filter to verify that it's valid - updates `mega` service to use the filter directly when selecting recipients
2021-05-07 13:56:41 +03:00
Store email batch and recipient records when sending newsletters (#12195) requires https://github.com/TryGhost/Ghost/pull/12192 - added initial `EmailBatch` and `EmailRecipient` model definitions with defaults and relationships - added missing `post` relationship function to email model - fetch member list without bookshelf - bookshelf can add around 3x overhead when fetching the members list for an email - we don't need full members at this point, only having the data is fine - if we need full models later on we can push the model hydration into background jobs where recipient batches are fetched ready for an email to be sent - bookshelf model instantiation of many models blocks the event loop, using knex directly keeps concurrent requests fast - adds `getFilteredCollectionQuery` method to base model to facilitate getting a knex query based on our normal model filters along with transaction/forUpdate applied - store recipient list before sending email - chunk already-fetched members list into batches and insert records into the `email_recipients` table via knex - chunked into batches of 1000 to match the number of emails that Mailgun accepts in a single API request but this may not be the absolute fastest batch size for recipient insertion: | Batch size | Batch time | Total time | | ---------- | ---------- | ---------- | | 500 | 20ms | 4142ms | | 1000 | 50ms | 4651ms | | 5000 | 170ms | 3540ms | | 10000 | 370ms | 3684ms | - create an email_batch record before inserting recipient rows so we can effeciently fetch recipients by batch and store the overall batch status
2020-09-14 17:40:00 +03:00
recipients() {
return this.hasMany('EmailRecipient', 'email_id');
},
2019-11-06 08:52:58 +03:00
emitChange: function emitChange(event, options) {
const eventToTrigger = 'email' + '.' + event;
ghostBookshelf.Model.prototype.emitChange.bind(this)(this, eventToTrigger, options);
},
onCreated: function onCreated(model, attrs, options) {
ghostBookshelf.Model.prototype.onCreated.apply(this, arguments);
model.emitChange('added', options);
},
onUpdated: function onUpdated(model, attrs, options) {
ghostBookshelf.Model.prototype.onUpdated.apply(this, arguments);
model.emitChange('edited', options);
},
onDestroyed: function onDestroyed(model, options) {
ghostBookshelf.Model.prototype.onDestroyed.apply(this, arguments);
model.emitChange('deleted', options);
}
}, {
post() {
return this.belongsTo('Post');
}
2019-11-06 08:52:58 +03:00
});
const Emails = ghostBookshelf.Collection.extend({
model: Email
});
module.exports = {
Email: ghostBookshelf.model('Email', Email),
Emails: ghostBookshelf.collection('Emails', Emails)
};