Serverside metadata update

pull/1/head
yflory 5 years ago
parent 113a362724
commit 28b2341f2c

@ -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))]);

@ -124,3 +124,5 @@ Meta.createLineHandler = function (ref, errorHandler) {
});
};
};
Meta.commands = Object.keys(commands);

@ -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);
}

@ -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);
});
};

Loading…
Cancel
Save