From f4f48712c5c6b14b517e166e9df7a087066418ae Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 6 Apr 2021 16:45:46 +1200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20custom=20count=20queries=20?= =?UTF-8?q?for=20"max"=20limits?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs https://github.com/TryGhost/Team/issues/597 - When the library is used on a client without a DB connection (e.g. frontend client running in a browser) the library needs to expose a way to override count queries. - The way these can be used is giving a count based on a HTTP request or some other data provider - Example use with max limit like "staff" would be loading the limit servcie if following way: ``` const limitService = new LimitService(); let limits = { staff: { max: 2, currentCountQuery: () => 5 } }; limitService.loadLimits({limits, errors}); await limitService.checkIsOverLimit('staff') ``` --- ghost/limit-service/lib/limit-service.js | 3 ++- .../limit-service/test/limit-service.test.js | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/ghost/limit-service/lib/limit-service.js b/ghost/limit-service/lib/limit-service.js index ecc94aa3c6..76bd3f5c91 100644 --- a/ghost/limit-service/lib/limit-service.js +++ b/ghost/limit-service/lib/limit-service.js @@ -28,7 +28,7 @@ class LimitService { if (config[name]) { /** @type LimitConfig */ - let limitConfig = _.merge({}, limits[name], config[name]); + let limitConfig = Object.assign({}, config[name], limits[name]); if (_.has(limitConfig, 'max')) { this.limits[name] = new MaxLimit({name: name, config: limitConfig, helpLink, db, errors}); @@ -97,4 +97,5 @@ module.exports = LimitService; * @prop {Number} [max] - max limit * @prop {Boolean} [disabled] - flag disabling/enabling limit * @prop {String} error - custom error to be displayed when the limit is reached + * @prop {Function} [currentCountQuery] - function returning count for the "max" type of limit */ diff --git a/ghost/limit-service/test/limit-service.test.js b/ghost/limit-service/test/limit-service.test.js index 3df84659bb..f709f76714 100644 --- a/ghost/limit-service/test/limit-service.test.js +++ b/ghost/limit-service/test/limit-service.test.js @@ -109,4 +109,29 @@ describe('Limit Service', function () { limitService.isLimited('customThemes').should.be.true(); }); }); + + describe('Custom limit count query configuration', function () { + it('can use a custom implementation of max limit query', async function () { + const limitService = new LimitService(); + + let limits = { + staff: { + max: 2, + currentCountQuery: () => 5 + }, + members: { + max: 100, + currentCountQuery: () => 100 + } + }; + + limitService.loadLimits({limits, errors}); + + (await limitService.checkIsOverLimit('staff')).should.be.true(); + (await limitService.checkWouldGoOverLimit('staff')).should.be.true(); + + (await limitService.checkIsOverLimit('members')).should.be.false(); + (await limitService.checkWouldGoOverLimit('members')).should.be.true(); + }); + }); });