2021-03-03 15:18:51 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
|
2021-03-03 20:50:17 +03:00
|
|
|
// run in context allows us to change the templateSettings without causing havoc
|
|
|
|
const _ = require('lodash').runInContext();
|
2021-03-03 15:18:51 +03:00
|
|
|
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
|
|
|
|
|
|
|
|
class Limit {
|
|
|
|
constructor({name, error, helpLink, db}) {
|
|
|
|
this.name = name;
|
|
|
|
this.error = error;
|
|
|
|
this.helpLink = helpLink;
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateError() {
|
|
|
|
let errorObj = {
|
|
|
|
errorDetails: {
|
|
|
|
name: this.name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.helpLink) {
|
|
|
|
errorObj.help = this.helpLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
return errorObj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MaxLimit extends Limit {
|
2021-04-01 07:29:26 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {String} options.name - name of the limit
|
|
|
|
* @param {Object} options.config - limit configuration
|
|
|
|
* @param {Number} options.config.max - maximum limit the limit would check against
|
|
|
|
* @param {Function} options.config.currentCountQuery - query checking the state that would be compared against the limit
|
|
|
|
* @param {String} options.helpLink - URL to the resource explaining how the limit works
|
|
|
|
* @param {Object} options.db - instance of knex db connection that currentCountQuery can use to run state check through
|
|
|
|
*/
|
2021-03-03 15:18:51 +03:00
|
|
|
constructor({name, config, helpLink, db}) {
|
|
|
|
super({name, error: config.error || '', helpLink, db});
|
|
|
|
|
2021-04-01 07:20:56 +03:00
|
|
|
if (config.max === undefined) {
|
|
|
|
throw new errors.IncorrectUsageError('Attempted to setup a max limit without a limit');
|
|
|
|
}
|
|
|
|
|
2021-03-03 15:18:51 +03:00
|
|
|
if (!config.currentCountQuery) {
|
|
|
|
throw new errors.IncorrectUsageError('Attempted to setup a max limit without a current count query');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentCountQueryFn = config.currentCountQuery;
|
|
|
|
this.max = config.max;
|
|
|
|
this.fallbackMessage = `This action would exceed the ${_.lowerCase(this.name)} limit on your current plan.`;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateError(count) {
|
|
|
|
let errorObj = super.generateError();
|
|
|
|
|
|
|
|
errorObj.message = this.fallbackMessage;
|
|
|
|
|
|
|
|
if (this.error) {
|
|
|
|
try {
|
2021-03-04 21:08:25 +03:00
|
|
|
errorObj.message = _.template(this.error)(
|
|
|
|
{
|
|
|
|
max: Intl.NumberFormat().format(this.max),
|
|
|
|
count: Intl.NumberFormat().format(count)
|
|
|
|
});
|
2021-03-03 15:18:51 +03:00
|
|
|
} catch (e) {
|
|
|
|
errorObj.message = this.fallbackMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 21:08:25 +03:00
|
|
|
errorObj.errorDetails.limit = this.max;
|
2021-03-03 15:18:51 +03:00
|
|
|
errorObj.errorDetails.total = count;
|
|
|
|
|
|
|
|
return new errors.HostLimitError(errorObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
async currentCountQuery() {
|
|
|
|
return await this.currentCountQueryFn(this.db);
|
|
|
|
}
|
|
|
|
|
2021-04-01 07:59:52 +03:00
|
|
|
/**
|
|
|
|
* Throws a HostLimitError if the configured or passed max limit is ecceded by currentCountQuery
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Number} [options.max] - overrides configured default max value to perform checks against
|
|
|
|
*/
|
|
|
|
async errorIfWouldGoOverLimit({max} = {}) {
|
2021-03-03 15:18:51 +03:00
|
|
|
let currentCount = await this.currentCountQuery(this.db);
|
2021-04-01 07:59:52 +03:00
|
|
|
|
|
|
|
if ((currentCount + 1) > (max || this.max)) {
|
2021-03-03 15:18:51 +03:00
|
|
|
throw this.generateError(currentCount);
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 07:59:52 +03:00
|
|
|
|
2021-03-03 15:18:51 +03:00
|
|
|
async errorIfIsOverLimit() {
|
|
|
|
let currentCount = await this.currentCountQuery(this.db);
|
|
|
|
if (currentCount > this.max) {
|
|
|
|
throw this.generateError(currentCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FlagLimit extends Limit {
|
|
|
|
constructor({name, config, helpLink, db}) {
|
|
|
|
super({name, error: config.error || '', helpLink, db});
|
|
|
|
|
|
|
|
this.disabled = config.disabled;
|
|
|
|
this.fallbackMessage = `Your plan does not support ${_.lowerCase(this.name)}. Please upgrade to enable ${_.lowerCase(this.name)}.`;
|
|
|
|
}
|
|
|
|
|
|
|
|
generateError() {
|
|
|
|
let errorObj = super.generateError();
|
|
|
|
|
|
|
|
if (this.error) {
|
|
|
|
errorObj.message = this.error;
|
|
|
|
} else {
|
|
|
|
errorObj.message = this.fallbackMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new errors.HostLimitError(errorObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag limits are on/off so using a feature is always over the limit
|
|
|
|
*/
|
|
|
|
async errorIfWouldGoOverLimit() {
|
|
|
|
if (this.disabled) {
|
|
|
|
throw this.generateError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag limits are on/off so we can't be over the limit
|
|
|
|
*/
|
|
|
|
async errorIfIsOverLimit() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
MaxLimit,
|
|
|
|
FlagLimit
|
|
|
|
};
|