From c3e3da49485909206bc4ca61f591c798e25d894f Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 5 Dec 2017 15:07:09 +0100 Subject: [PATCH] allow administrators to increase limits for friends --- config.example.js | 18 ++++++++++++++++++ rpc.js | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/config.example.js b/config.example.js index ce38b5c3a..3a097eb87 100644 --- a/config.example.js +++ b/config.example.js @@ -159,6 +159,24 @@ module.exports = { */ defaultStorageLimit: 50 * 1024 * 1024, + /* + * CryptPad allows administrators to give custom limits to their friends. + * add an entry for each friend, identified by their user id, + * which can be found on the settings page. Include a 'limit' (number of bytes), + * a 'plan' (string), and a 'note' (string). + * + * hint: 1GB is 1024 * 1024 * 1024 bytes + */ + customLimits: { + /* + "https://my.awesome.website/user/#/1/cryptpad-user/YZgXQxKR0Rcb6r6CmxHPdAGLVludrAF2lEnkbx1vVOo=": { + limit: 20 * 1024 * 1024 * 1024, + plan: 'insider', + note: 'storage space donated by my.awesome.website' + } + */ + }, + /* * By default, CryptPad also contacts our accounts server once a day to check for changes in * the people who have accounts. This check-in will also send the version of your CryptPad diff --git a/rpc.js b/rpc.js index b0e3a0e6d..3e28f7fed 100644 --- a/rpc.js +++ b/rpc.js @@ -428,6 +428,27 @@ var updateLimits = function (config, publicKey, cb /*:(?string, ?any[])=>void*/) "Content-Length": Buffer.byteLength(body) } }; + + // read custom limits from the config + var customLimits = (function (custom) { + var limits = {}; + Object.keys(custom).forEach(function (k) { + k.replace(/\/([^\/]+)$/, function (all, safeKey) { + var id = unescapeKeyCharacters(safeKey || ''); + limits[id] = custom[k]; + }); + }); + return limits; + }(config.customLimits || {})); + + var isLimit = function (o) { + var valid = o && typeof(o) === 'object' && + typeof(o.limit) === 'number' && + typeof(o.plan) === 'string' && + typeof(o.note) === 'string'; + return valid; + }; + var req = Https.request(options, function (response) { if (!('' + response.statusCode).match(/^2\d\d$/)) { return void cb('SERVER ERROR ' + response.statusCode); @@ -442,6 +463,11 @@ var updateLimits = function (config, publicKey, cb /*:(?string, ?any[])=>void*/) try { var json = JSON.parse(str); limits = json; + Object.keys(customLimits).forEach(function (k) { + if (!isLimit(customLimits[k])) { return; } + limits[k] = customLimits[k]; + }); + var l; if (userId) { var limit = limits[userId];