2017-07-06 01:08:19 +08:00
|
|
|
var step = require('step');
|
|
|
|
|
2015-07-11 01:10:55 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param metadataBackend
|
|
|
|
* @param options
|
|
|
|
* @constructor
|
|
|
|
* @type {UserLimitsApi}
|
|
|
|
*/
|
|
|
|
function UserLimitsApi(metadataBackend, options) {
|
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.options = options || {};
|
|
|
|
this.options.limits = this.options.limits || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = UserLimitsApi;
|
|
|
|
|
2017-07-06 01:08:19 +08:00
|
|
|
UserLimitsApi.prototype.getRenderLimits = function (username, apiKey, callback) {
|
2015-07-11 01:10:55 +08:00
|
|
|
var self = this;
|
2017-07-06 01:08:19 +08:00
|
|
|
|
|
|
|
var limits = {
|
|
|
|
cacheOnTimeout: self.options.limits.cacheOnTimeout || false,
|
|
|
|
render: self.options.limits.render || 0
|
|
|
|
};
|
|
|
|
|
2017-08-10 21:58:25 +08:00
|
|
|
self.getTimeoutRenderLimit(username, apiKey, function (err, timeoutRenderLimit) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2015-07-11 01:10:55 +08:00
|
|
|
}
|
2017-08-10 21:58:25 +08:00
|
|
|
|
|
|
|
if (timeoutRenderLimit && timeoutRenderLimit.render) {
|
|
|
|
if (Number.isFinite(timeoutRenderLimit.render)) {
|
|
|
|
limits.render = timeoutRenderLimit.render;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, limits);
|
|
|
|
});
|
2017-07-06 01:08:19 +08:00
|
|
|
};
|
2015-07-11 01:10:55 +08:00
|
|
|
|
2017-07-06 01:08:19 +08:00
|
|
|
UserLimitsApi.prototype.getTimeoutRenderLimit = function (username, apiKey, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
step(
|
|
|
|
function isAuthorized() {
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
if (!apiKey) {
|
|
|
|
return next(null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.metadataBackend.getUserMapKey(username, function (err, userApiKey) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(null, userApiKey === apiKey);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getUserTimeoutRenderLimits(err, authorized) {
|
2017-07-18 17:56:54 +08:00
|
|
|
var next = this;
|
|
|
|
|
2017-07-06 01:08:19 +08:00
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-07-18 17:56:54 +08:00
|
|
|
|
2017-08-02 01:13:55 +08:00
|
|
|
self.metadataBackend.getUserTimeoutRenderLimits(username, function (err, timeoutRenderLimit) {
|
|
|
|
if (err) {
|
2017-08-02 17:07:44 +08:00
|
|
|
return next(err);
|
2017-08-02 01:13:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
next(null, {
|
|
|
|
render: authorized ? timeoutRenderLimit.render : timeoutRenderLimit.renderPublic
|
|
|
|
});
|
|
|
|
});
|
2017-07-06 01:08:19 +08:00
|
|
|
},
|
2017-08-02 01:13:55 +08:00
|
|
|
callback
|
2017-07-06 01:08:19 +08:00
|
|
|
);
|
2015-07-11 01:10:55 +08:00
|
|
|
};
|