mirror of
https://github.com/TryGhost/Ghost.git
synced 2023-12-13 21:00:40 +01:00
🏗Added host config limits for member emails (#11439)
no issue Introduces host limits config for allowing email limits with members.
This commit is contained in:
parent
169daead1f
commit
160ef2976a
|
@ -2,8 +2,11 @@ const models = require('../../models');
|
||||||
const common = require('../../lib/common');
|
const common = require('../../lib/common');
|
||||||
const urlUtils = require('../../lib/url-utils');
|
const urlUtils = require('../../lib/url-utils');
|
||||||
const {mega} = require('../../services/mega');
|
const {mega} = require('../../services/mega');
|
||||||
|
const membersService = require('../../services/members');
|
||||||
const allowedIncludes = ['tags', 'authors', 'authors.roles', 'email'];
|
const allowedIncludes = ['tags', 'authors', 'authors.roles', 'email'];
|
||||||
const unsafeAttrs = ['status', 'authors', 'visibility'];
|
const unsafeAttrs = ['status', 'authors', 'visibility'];
|
||||||
|
const _ = require('lodash');
|
||||||
|
const config = require('../../config');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
docName: 'posts',
|
docName: 'posts',
|
||||||
|
@ -142,50 +145,63 @@ module.exports = {
|
||||||
permissions: {
|
permissions: {
|
||||||
unsafeAttrs: unsafeAttrs
|
unsafeAttrs: unsafeAttrs
|
||||||
},
|
},
|
||||||
query(frame) {
|
async query(frame) {
|
||||||
return models.Post.edit(frame.data.posts[0], frame.options)
|
/**Check host limits for members when send email is true*/
|
||||||
.then(async (model) => {
|
const membersHostLimit = config.get('host_settings:limits:members');
|
||||||
if (!model.get('send_email_when_published')) {
|
if (frame.options.send_email_when_published && membersHostLimit) {
|
||||||
return model;
|
const allowedMembersLimit = membersHostLimit.max;
|
||||||
}
|
const hostUpgradeLink = config.get('host_settings:limits').upgrade_url;
|
||||||
|
const knexOptions = _.pick(frame.options, ['transacting', 'forUpdate']);
|
||||||
const postPublished = model.wasChanged() && (model.get('status') === 'published') && (model.previous('status') !== 'published');
|
const {members} = await membersService.api.members.list(Object.assign(knexOptions, {filter: 'subscribed:true'}, {limit: 'all'}));
|
||||||
|
if (members.length > allowedMembersLimit) {
|
||||||
if (postPublished) {
|
throw new common.errors.HostLimitError({
|
||||||
let postEmail = model.relations.email;
|
message: `Your current plan allows you to send email to up to ${allowedMembersLimit} members, but you currently have ${members.length} members`,
|
||||||
|
help: hostUpgradeLink,
|
||||||
if (!postEmail) {
|
errorDetails: {
|
||||||
const email = await mega.addEmail(model, frame.options);
|
limit: allowedMembersLimit,
|
||||||
model.set('email', email);
|
total: members.length
|
||||||
} else if (postEmail && postEmail.get('status') === 'failed') {
|
|
||||||
const email = await mega.retryFailedEmail(postEmail);
|
|
||||||
model.set('email', email);
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return model;
|
let model = await models.Post.edit(frame.data.posts[0], frame.options);
|
||||||
})
|
|
||||||
.then((model) => {
|
|
||||||
if (
|
|
||||||
model.get('status') === 'published' && model.wasChanged() ||
|
|
||||||
model.get('status') === 'draft' && model.previous('status') === 'published'
|
|
||||||
) {
|
|
||||||
this.headers.cacheInvalidate = true;
|
|
||||||
} else if (
|
|
||||||
model.get('status') === 'draft' && model.previous('status') !== 'published' ||
|
|
||||||
model.get('status') === 'scheduled' && model.wasChanged()
|
|
||||||
) {
|
|
||||||
this.headers.cacheInvalidate = {
|
|
||||||
value: urlUtils.urlFor({
|
|
||||||
relativeUrl: urlUtils.urlJoin('/p', model.get('uuid'), '/')
|
|
||||||
})
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
this.headers.cacheInvalidate = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return model;
|
/**Handle newsletter email */
|
||||||
});
|
if (model.get('send_email_when_published')) {
|
||||||
|
const postPublished = model.wasChanged() && (model.get('status') === 'published') && (model.previous('status') !== 'published');
|
||||||
|
if (postPublished) {
|
||||||
|
let postEmail = model.relations.email;
|
||||||
|
|
||||||
|
if (!postEmail) {
|
||||||
|
const email = await mega.addEmail(model, frame.options);
|
||||||
|
model.set('email', email);
|
||||||
|
} else if (postEmail && postEmail.get('status') === 'failed') {
|
||||||
|
const email = await mega.retryFailedEmail(postEmail);
|
||||||
|
model.set('email', email);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**Handle cache invalidation */
|
||||||
|
if (
|
||||||
|
model.get('status') === 'published' && model.wasChanged() ||
|
||||||
|
model.get('status') === 'draft' && model.previous('status') === 'published'
|
||||||
|
) {
|
||||||
|
this.headers.cacheInvalidate = true;
|
||||||
|
} else if (
|
||||||
|
model.get('status') === 'draft' && model.previous('status') !== 'published' ||
|
||||||
|
model.get('status') === 'scheduled' && model.wasChanged()
|
||||||
|
) {
|
||||||
|
this.headers.cacheInvalidate = {
|
||||||
|
value: urlUtils.urlFor({
|
||||||
|
relativeUrl: urlUtils.urlJoin('/p', model.get('uuid'), '/')
|
||||||
|
})
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
this.headers.cacheInvalidate = false;
|
||||||
|
}
|
||||||
|
return model;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -55,6 +55,13 @@ const ghostErrors = {
|
||||||
errorType: 'UpdateCollisionError'
|
errorType: 'UpdateCollisionError'
|
||||||
}, options));
|
}, options));
|
||||||
},
|
},
|
||||||
|
HostLimitError: function HostLimitError(options) {
|
||||||
|
GhostError.call(this, merge({
|
||||||
|
errorType: 'HostLimitError',
|
||||||
|
hideStack: true,
|
||||||
|
statusCode: 403
|
||||||
|
}, options));
|
||||||
|
},
|
||||||
HelperWarning: function HelperWarning(options) {
|
HelperWarning: function HelperWarning(options) {
|
||||||
GhostError.call(this, merge({
|
GhostError.call(this, merge({
|
||||||
errorType: 'HelperWarning',
|
errorType: 'HelperWarning',
|
||||||
|
|
|
@ -464,6 +464,7 @@
|
||||||
"DatabaseVersionError": "Database version compatibility error, cannot {action}.",
|
"DatabaseVersionError": "Database version compatibility error, cannot {action}.",
|
||||||
"EmailError": "Error sending email!",
|
"EmailError": "Error sending email!",
|
||||||
"ThemeValidationError": "Theme validation error, cannot {action}.",
|
"ThemeValidationError": "Theme validation error, cannot {action}.",
|
||||||
|
"HostLimitError": "Host Limit error, cannot {action}.",
|
||||||
"DisabledFeatureError": "Theme validation error, the {{{helperName}}} helper is not available. Cannot {action}.",
|
"DisabledFeatureError": "Theme validation error, the {{{helperName}}} helper is not available. Cannot {action}.",
|
||||||
"UpdateCollisionError": "Saving failed! Someone else is editing this post."
|
"UpdateCollisionError": "Saving failed! Someone else is editing this post."
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue