From 28b2341f2ce66e2d4bf1a48518ee7b06f658c601 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 28 Aug 2019 12:40:35 +0200 Subject: [PATCH] Serverside metadata update --- historyKeeper.js | 10 ++++++--- lib/metadata.js | 2 ++ rpc.js | 49 ++++++++++++++++++++++++++++++++++++++++++++ www/common/pinpad.js | 10 +++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/historyKeeper.js b/historyKeeper.js index 96ac43d96..b0d59a3f0 100644 --- a/historyKeeper.js +++ b/historyKeeper.js @@ -713,8 +713,12 @@ module.exports.create = function (cfg) { // Check if the selected channel is expired // If it is, remove it from memory and broadcast a message to its members - const onChannelMetadataChanged = function (ctx, channel) { - channel = channel; + const onChannelMetadataChanged = function (ctx, channel, metadata) { + Log.debug('SET_METADATA_CACHE', 'Test'); // XXX + if (channel && metadata_cache[channel]) { + Log.debug('SET_METADATA_CACHE', 'Channel '+ channel +', metadata: '+ JSON.stringify(metadata)); + metadata_cache[channel] = metadata; + } }; /* checkExpired @@ -984,7 +988,7 @@ module.exports.create = function (cfg) { // make sure we update our cache of metadata // or at least invalidate it and force other mechanisms to recompute its state // 'output' could be the new state as computed by rpc - onChannelMetadataChanged(ctx, msg[4]); + onChannelMetadataChanged(ctx, msg[4], output[1]); } sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify([parsed[0]].concat(output))]); diff --git a/lib/metadata.js b/lib/metadata.js index 419780d7b..11214931b 100644 --- a/lib/metadata.js +++ b/lib/metadata.js @@ -124,3 +124,5 @@ Meta.createLineHandler = function (ref, errorHandler) { }); }; }; + +Meta.commands = Object.keys(commands); diff --git a/rpc.js b/rpc.js index 3af44160a..c6600125e 100644 --- a/rpc.js +++ b/rpc.js @@ -332,6 +332,46 @@ var getMetadata = function (Env, channel, cb) { }); }; +/* setMetadata + - write a new line to the metadata log if a valid command is provided + - data is an object: { + channel: channelId, + command: metadataCommand (string), + value: value + } +*/ +var setMetadata = function (Env, data, unsafeKey, cb) { + var channel = data.channel; + var command = data.command; + if (!channel || !isValidId(channel)) { return void cb ('INVALID_CHAN'); } + if (!command || typeof (command) !== 'string') { return void cb ('INVALID_COMMAND'); } + if (Meta.commands.indexOf(command) === -1) { return void('UNSUPPORTED_COMMAND'); } + + // XXX should we add checks to "metadata.js" to make sure data.value is + // valid for the selected command? + + getMetadata(Env, channel, function (err, metadata) { + if (err) { return void cb(err); } + if (!(metadata && Array.isArray(metadata.owners))) { return void cb('E_NO_OWNERS'); } + // Confirm that the channel is owned by the user in question + if (metadata.owners.indexOf(unsafeKey) === -1) { + return void cb('INSUFFICIENT_PERMISSIONS'); + } + + // Add the new metadata line + var line = JSON.stringify([command, data.value]); + return void Env.msgStore.writeMetadata(channel, line, function (e) { + if (e) { + return void cb(e); + } + getMetadata(Env, channel, function (err, metadata) { + // XXX handle error here? + cb(void 0, metadata); + }); + }); + }); +}; + var getMultipleFileSize = function (Env, channels, cb) { if (!Array.isArray(channels)) { return cb('INVALID_PIN_LIST'); } if (typeof(Env.msgStore.getChannelSize) !== 'function') { @@ -1602,6 +1642,7 @@ var isAuthenticatedCall = function (call) { 'WRITE_LOGIN_BLOCK', 'REMOVE_LOGIN_BLOCK', 'ADMIN', + 'SET_METADATA' ].indexOf(call) !== -1; }; @@ -1971,6 +2012,14 @@ RPC.create = function ( } Respond(void 0, result); }); + case 'SET_METADATA': + return void setMetadata(Env, msg[1], publicKey, function (e, data) { + if (e) { + WARN(e, data); + return void Respond(e); + } + Respond(void 0, data); + }); default: return void Respond('UNSUPPORTED_RPC_CALL', msg); } diff --git a/www/common/pinpad.js b/www/common/pinpad.js index 353b2c4fb..b1334d3a1 100644 --- a/www/common/pinpad.js +++ b/www/common/pinpad.js @@ -260,6 +260,16 @@ define([ }); }; + // Get data for the admin panel + exp.setMetadata = function (obj, cb) { + rpc.send('SET_METADATA', { + channel: obj.channel, + command: obj.command, + value: obj.value + }, cb); + }; + + cb(e, exp); }); };