centralize historykeeper-rpc interaction in rpc methods

This commit is contained in:
ansuz
2020-02-07 12:53:12 -05:00
parent 7d0dbe5d09
commit 802034616c
5 changed files with 96 additions and 90 deletions

View File

@@ -644,48 +644,6 @@ module.exports.create = function (cfg, cb) {
});
};
/* onChannelCleared
* broadcasts to all clients in a channel if that channel is deleted
*/
const onChannelCleared = function (Server, channel) {
Server.channelBroadcast(channel, {
error: 'ECLEARED',
channel: channel
}, HISTORY_KEEPER_ID);
};
// When a channel is removed from datastore, broadcast a message to all its connected users
const onChannelDeleted = function (Server, channel) {
store.closeChannel(channel, function () {
Server.channelBroadcast(channel, {
error: 'EDELETED',
channel: channel
}, HISTORY_KEEPER_ID);
});
delete channel_cache[channel];
Server.clearChannel(channel);
delete metadata_cache[channel];
};
// Check if the selected channel is expired
// If it is, remove it from memory and broadcast a message to its members
const onChannelMetadataChanged = function (Server, channel, metadata) {
if (!(channel && metadata_cache[channel] && typeof (metadata) === "object")) { return; }
Log.silly('SET_METADATA_CACHE', {
channel: channel,
metadata: JSON.stringify(metadata),
});
metadata_cache[channel] = metadata;
if (channel_cache[channel] && channel_cache[channel].index) {
channel_cache[channel].index.metadata = metadata;
}
Server.channelBroadcast(channel, metadata, HISTORY_KEEPER_ID);
};
const handleGetHistory = function (Server, seq, userId, parsed) {
// parsed[1] is the channel id
// parsed[2] is a validation key or an object containing metadata (optionnal)
@@ -892,37 +850,6 @@ module.exports.create = function (cfg, cb) {
Server.send(userId, [0, HISTORY_KEEPER_ID, 'MSG', userId, JSON.stringify([parsed[0], 'ERROR', err])]);
return;
}
var msg = rpc_call[0].slice();
if (msg[3] === 'REMOVE_OWNED_CHANNEL') {
onChannelDeleted(Server, msg[4]);
}
if (msg[3] === 'CLEAR_OWNED_CHANNEL') {
onChannelCleared(Server, msg[4]);
}
if (msg[3] === 'SET_METADATA') { // or whatever we call the RPC????
// 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(Server, msg[4].channel, output[1]);
}
// unauthenticated RPC calls have a different message format
if (msg[0] === "WRITE_PRIVATE_MESSAGE" && output && output.channel) {
// clients don't validate messages sent by the historyKeeper
// so this broadcast needs to come from a different id
// we pass 'null' to indicate that it's not coming from a real user
// to ensure that they know not to trust this message
Server.getChannelUserList(output.channel).forEach(function (userId) {
Server.send(userId, output.message);
});
// rpc and anonRpc expect their responses to be of a certain length
// and we've already used the output of the rpc call, so overwrite it
output = [null, null, null];
}
// finally, send a response to the client that sent the RPC
Server.send(userId, [0, HISTORY_KEEPER_ID, 'MSG', userId, JSON.stringify([parsed[0]].concat(output))]);
});
} catch (e) {
@@ -967,6 +894,9 @@ module.exports.create = function (cfg, cb) {
};
cfg.historyKeeper = {
metadata_cache: metadata_cache,
channel_cache: channel_cache,
id: HISTORY_KEEPER_ID,
channelMessage: function (Server, channel, msgStruct) {