refactor quota logic for external usage

This commit is contained in:
ansuz 2020-10-06 18:35:36 +05:30
parent bd6234c5bc
commit 0c5db31fbc
2 changed files with 39 additions and 22 deletions

View file

@ -6,6 +6,9 @@ const Quota = module.exports;
const Keys = require("../keys");
const Package = require('../../package.json');
const Https = require("https");
const Util = require("../common-util");
var validLimitFields = ['limit', 'plan', 'note', 'users'];
Quota.isValidLimit = function (o) {
var valid = o && typeof(o) === 'object' &&
@ -13,7 +16,11 @@ Quota.isValidLimit = function (o) {
typeof(o.plan) === 'string' &&
typeof(o.note) === 'string' &&
// optionally contains a 'users' array
(Array.isArray(o.users) || typeof(o.users) === 'undefined');
(Array.isArray(o.users) || typeof(o.users) === 'undefined') &&
// check that the object contains only the expected fields
!Object.keys(o).some(function (k) {
return validLimitFields.indexOf(k) === -1;
});
return valid;
};
@ -62,16 +69,8 @@ Env = {
};
*/
Quota.queryAccountServer = function (Env, cb) {
cb = cb; // XXX
};
Quota.updateCachedLimits = function (Env, cb) {
Quota.applyCustomLimits(Env);
if (Env.blockDailyCheck === true ||
(typeof(Env.blockDailyCheck) === 'undefined' && Env.adminEmail === false && Env.allowSubscriptions === false)) {
return void cb();
}
var queryAccountServer = function (Env, cb) {
var done = Util.once(Util.mkAsync(cb));
var body = JSON.stringify({
domain: Env.myDomain,
@ -104,31 +103,49 @@ Quota.updateCachedLimits = function (Env, cb) {
var json = JSON.parse(str);
// don't overwrite the limits with junk data
if (json && json.message === 'EINVAL') { return void cb(); }
Env.limits = json;
Quota.applyCustomLimits(Env);
//console.log('Env.customLimits', Env.customLimits);
//console.log('Env.limits', Env.limits);
cb(void 0);
done(void 0, json);
} catch (e) {
cb(e);
done(e);
}
});
});
req.on('error', function (e) {
Quota.applyCustomLimits(Env);
if (!Env.myDomain) { return cb(); }
if (!Env.myDomain) { return done(); }
// only return an error if your server allows subscriptions
cb(e);
done(e);
});
req.end(body);
};
Quota.queryAccountServer = function (Env, cb) {
Env.batchAccountQuery('', cb, function (done) {
queryAccountServer(Env, done);
});
};
Quota.updateCachedLimits = function (Env, cb) {
Quota.applyCustomLimits(Env);
if (Env.blockDailyCheck === true ||
(typeof(Env.blockDailyCheck) === 'undefined' && Env.adminEmail === false && Env.allowSubscriptions === false)) {
return void cb();
}
Quota.queryAccountServer(Env, function (err, json) {
if (err) { return void cb(err); }
if (!json) { return void cb(); }
Env.limits = json;
Quota.applyCustomLimits(Env);
cb();
});
};
// The limits object contains storage limits for all the publicKey that have paid
// To each key is associated an object containing the 'limit' value and a 'note' explaining that limit
Quota.getUpdatedLimit = function (Env, safeKey, cb) { // FIXME BATCH?S
Quota.getUpdatedLimit = function (Env, safeKey, cb) {
Quota.updateCachedLimits(Env, function (err) {
if (err) { return void cb(err); }

View file

@ -11,7 +11,6 @@ const Core = require("./commands/core");
const Store = require("./storage/file");
const BlobStore = require("./storage/blob");
const Workers = require("./workers/index");
//const Eviction = require("./eviction");
module.exports.create = function (config, cb) {
const Log = config.log;
@ -50,6 +49,7 @@ module.exports.create = function (config, cb) {
batchDiskUsage: BatchRead('GET_DISK_USAGE'),
batchUserPins: BatchRead('LOAD_USER_PINS'),
batchTotalSize: BatchRead('GET_TOTAL_SIZE'),
batchAccountQuery: BatchRead("QUERY_ACCOUNT_SERVER"),
//historyKeeper: config.historyKeeper,
intervals: config.intervals || {},