refactor quota logic for external usage

pull/1/head
ansuz 4 years ago
parent bd6234c5bc
commit 0c5db31fbc

@ -6,6 +6,9 @@ const Quota = module.exports;
const Keys = require("../keys"); const Keys = require("../keys");
const Package = require('../../package.json'); const Package = require('../../package.json');
const Https = require("https"); const Https = require("https");
const Util = require("../common-util");
var validLimitFields = ['limit', 'plan', 'note', 'users'];
Quota.isValidLimit = function (o) { Quota.isValidLimit = function (o) {
var valid = o && typeof(o) === 'object' && var valid = o && typeof(o) === 'object' &&
@ -13,7 +16,11 @@ Quota.isValidLimit = function (o) {
typeof(o.plan) === 'string' && typeof(o.plan) === 'string' &&
typeof(o.note) === 'string' && typeof(o.note) === 'string' &&
// optionally contains a 'users' array // 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; return valid;
}; };
@ -62,16 +69,8 @@ Env = {
}; };
*/ */
Quota.queryAccountServer = function (Env, cb) { var queryAccountServer = function (Env, cb) {
cb = cb; // XXX var done = Util.once(Util.mkAsync(cb));
};
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 body = JSON.stringify({ var body = JSON.stringify({
domain: Env.myDomain, domain: Env.myDomain,
@ -104,31 +103,49 @@ Quota.updateCachedLimits = function (Env, cb) {
var json = JSON.parse(str); var json = JSON.parse(str);
// don't overwrite the limits with junk data // don't overwrite the limits with junk data
if (json && json.message === 'EINVAL') { return void cb(); } if (json && json.message === 'EINVAL') { return void cb(); }
done(void 0, json);
Env.limits = json;
Quota.applyCustomLimits(Env);
//console.log('Env.customLimits', Env.customLimits);
//console.log('Env.limits', Env.limits);
cb(void 0);
} catch (e) { } catch (e) {
cb(e); done(e);
} }
}); });
}); });
req.on('error', function (e) { req.on('error', function (e) {
Quota.applyCustomLimits(Env); Quota.applyCustomLimits(Env);
if (!Env.myDomain) { return cb(); } if (!Env.myDomain) { return done(); }
// only return an error if your server allows subscriptions // only return an error if your server allows subscriptions
cb(e); done(e);
}); });
req.end(body); 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 // 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 // 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) { Quota.updateCachedLimits(Env, function (err) {
if (err) { return void cb(err); } if (err) { return void cb(err); }

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

Loading…
Cancel
Save