diff --git a/core/server/api/canary/posts.js b/core/server/api/canary/posts.js index 1ec4a99859..16c188907f 100644 --- a/core/server/api/canary/posts.js +++ b/core/server/api/canary/posts.js @@ -3,7 +3,6 @@ const {i18n} = require('../../lib/common'); const errors = require('@tryghost/errors'); const urlUtils = require('../../../shared/url-utils'); const {mega} = require('../../services/mega'); -const membersService = require('../../services/members'); const allowedIncludes = ['tags', 'authors', 'authors.roles', 'email']; const unsafeAttrs = ['status', 'authors', 'visibility']; @@ -154,11 +153,6 @@ module.exports = { unsafeAttrs: unsafeAttrs }, async query(frame) { - /**Check host limits for members when send email is true**/ - if ((frame.options.email_recipient_filter && frame.options.email_recipient_filter !== 'none') || frame.options.send_email_when_published) { - await membersService.checkHostLimit(); - } - let model; if (!frame.options.email_recipient_filter && frame.options.send_email_when_published) { await models.Base.transaction(async (transacting) => { diff --git a/core/server/api/v3/posts.js b/core/server/api/v3/posts.js index 485715cd31..95fae62314 100644 --- a/core/server/api/v3/posts.js +++ b/core/server/api/v3/posts.js @@ -3,7 +3,6 @@ const {i18n} = require('../../lib/common'); const errors = require('@tryghost/errors'); const urlUtils = require('../../../shared/url-utils'); const {mega} = require('../../services/mega'); -const membersService = require('../../services/members'); const allowedIncludes = ['tags', 'authors', 'authors.roles', 'email']; const unsafeAttrs = ['status', 'authors', 'visibility']; @@ -154,11 +153,6 @@ module.exports = { unsafeAttrs: unsafeAttrs }, async query(frame) { - /**Check host limits for members when send email is true**/ - if ((frame.options.email_recipient_filter && frame.options.email_recipient_filter !== 'none') || frame.options.send_email_when_published) { - await membersService.checkHostLimit(); - } - let model; if (!frame.options.email_recipient_filter && frame.options.send_email_when_published) { await models.Base.transaction(async (transacting) => { diff --git a/core/server/models/post.js b/core/server/models/post.js index e37db7eae4..c12f94a4c7 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -10,6 +10,7 @@ const htmlToPlaintext = require('../../shared/html-to-plaintext'); const ghostBookshelf = require('./base'); const config = require('../../shared/config'); const settingsCache = require('../services/settings/cache'); +const limitService = require('../services/limits'); const mobiledocLib = require('../lib/mobiledoc'); const relations = require('./relations'); const urlUtils = require('../../shared/url-utils'); @@ -1016,7 +1017,7 @@ Post = ghostBookshelf.Model.extend({ }, // NOTE: the `authors` extension is the parent of the post model. It also has a permissible function. - permissible: function permissible(postModel, action, context, unsafeAttrs, loadedPermissions, hasUserPermission, hasApiKeyPermission) { + permissible: async function permissible(postModel, action, context, unsafeAttrs, loadedPermissions, hasUserPermission, hasApiKeyPermission) { let isContributor; let isOwner; let isAdmin; @@ -1048,6 +1049,13 @@ Post = ghostBookshelf.Model.extend({ isAdd = (action === 'add'); isDestroy = (action === 'destroy'); + if (limitService.isLimited('members')) { + // You can't publish a post if you're over your member limit + if ((isEdit && isChanging('status') && isDraft()) || (isAdd && isPublished())) { + await limitService.errorIfIsOverLimit('members'); + } + } + if (isContributor && isEdit) { // Only allow contributor edit if status is changing, and the post is a draft post hasUserPermission = !isChanging('status') && isDraft(); diff --git a/core/server/services/mega/mega.js b/core/server/services/mega/mega.js index e9d3a186fa..b5a91052b6 100644 --- a/core/server/services/mega/mega.js +++ b/core/server/services/mega/mega.js @@ -9,6 +9,7 @@ const {events, i18n} = require('../../lib/common'); const logging = require('../../../shared/logging'); const settingsCache = require('../settings/cache'); const membersService = require('../members'); +const limitService = require('../limits'); const bulkEmailService = require('../bulk-email'); const jobsService = require('../jobs'); const db = require('../../data/db'); @@ -253,7 +254,9 @@ async function sendEmailJob({emailModel, options}) { try { // Check host limit for allowed member count and throw error if over limit // - do this even if it's a retry so that there's no way around the limit - await membersService.checkHostLimit(); + if (limitService.isLimited('members')) { + await limitService.errorIfIsOverLimit('members'); + } // Create email batch and recipient rows unless this is a retry and they already exist const existingBatchCount = await emailModel.related('emailBatches').count('id'); diff --git a/core/server/services/members/index.js b/core/server/services/members/index.js index 60084dba06..d3ae84d196 100644 --- a/core/server/services/members/index.js +++ b/core/server/services/members/index.js @@ -62,8 +62,6 @@ events.on('settings.edited', function updateSettingFromModel(settingModel) { const membersService = { contentGating: require('./content-gating'), - checkHostLimit: require('./limit'), - config: membersConfig, get api() { diff --git a/core/server/services/members/limit.js b/core/server/services/members/limit.js deleted file mode 100644 index 09d2826772..0000000000 --- a/core/server/services/members/limit.js +++ /dev/null @@ -1,29 +0,0 @@ -const config = require('../../../shared/config'); -const models = require('../../models'); -const errors = require('@tryghost/errors'); - -// Get total members direct from DB -async function getTotalMembers() { - return models.Member.count('id'); -} - -module.exports = async () => { - const membersHostLimit = config.get('hostSettings:limits:members'); - if (membersHostLimit) { - const allowedMembersLimit = membersHostLimit.max; - const hostUpgradeLink = config.get('hostSettings:limits').upgrade_url; - - const totalMembers = await getTotalMembers(); - - if (totalMembers > allowedMembersLimit) { - throw new errors.HostLimitError({ - message: `Your current plan allows you to have up to ${allowedMembersLimit} members, but you currently have ${totalMembers} members`, - help: hostUpgradeLink, - errorDetails: { - limit: allowedMembersLimit, - total: totalMembers - } - }); - } - } -};