centralize historykeeper-rpc interaction in rpc methods
This commit is contained in:
@@ -6,7 +6,7 @@ const nThen = require("nthen");
|
||||
const Core = require("./core");
|
||||
const Metadata = require("./metadata");
|
||||
|
||||
Channel.clearOwnedChannel = function (Env, safeKey, channelId, cb) {
|
||||
Channel.clearOwnedChannel = function (Env, safeKey, channelId, cb, Server) {
|
||||
if (typeof(channelId) !== 'string' || channelId.length !== 32) {
|
||||
return cb('INVALID_ARGUMENTS');
|
||||
}
|
||||
@@ -20,19 +20,46 @@ Channel.clearOwnedChannel = function (Env, safeKey, channelId, cb) {
|
||||
return void cb('INSUFFICIENT_PERMISSIONS');
|
||||
}
|
||||
return void Env.msgStore.clearChannel(channelId, function (e) {
|
||||
cb(e);
|
||||
if (e) { return void cb(e); }
|
||||
cb();
|
||||
|
||||
const channel_cache = Env.historyKeeper.channel_cache;
|
||||
|
||||
const clear = function () {
|
||||
// delete the channel cache because it will have been invalidated
|
||||
delete channel_cache[channelId];
|
||||
};
|
||||
|
||||
nThen(function (w) {
|
||||
Server.getChannelUserList(channelId).forEach(function (userId) {
|
||||
Server.send(userId, [
|
||||
0,
|
||||
Env.historyKeeper.id,
|
||||
'MSG',
|
||||
userId,
|
||||
JSON.stringify({
|
||||
error: 'ECLEARED',
|
||||
channel: channelId
|
||||
})
|
||||
], w());
|
||||
});
|
||||
}).nThen(function () {
|
||||
clear();
|
||||
}).orTimeout(function () {
|
||||
Env.Log.warn("ON_CHANNEL_CLEARED_TIMEOUT", channelId);
|
||||
clear();
|
||||
}, 30000);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Channel.removeOwnedChannel = function (Env, safeKey, channelId, cb) {
|
||||
Channel.removeOwnedChannel = function (Env, safeKey, channelId, cb, Server) {
|
||||
if (typeof(channelId) !== 'string' || !Core.isValidId(channelId)) {
|
||||
return cb('INVALID_ARGUMENTS');
|
||||
}
|
||||
var unsafeKey = Util.unescapeKeyCharacters(safeKey);
|
||||
|
||||
if (Env.blobStore.isFileId(channelId)) {
|
||||
//var safeKey = Util.escapeKeyCharacters(unsafeKey);
|
||||
var blobId = channelId;
|
||||
|
||||
return void nThen(function (w) {
|
||||
@@ -89,6 +116,45 @@ Channel.removeOwnedChannel = function (Env, safeKey, channelId, cb) {
|
||||
return void cb(e);
|
||||
}
|
||||
cb(void 0, 'OK');
|
||||
|
||||
const channel_cache = Env.historyKeeper.channel_cache;
|
||||
const metadata_cache = Env.historyKeeper.metadata_cache;
|
||||
|
||||
const clear = function () {
|
||||
delete channel_cache[channelId];
|
||||
Server.clearChannel(channelId);
|
||||
delete metadata_cache[channelId];
|
||||
};
|
||||
|
||||
// an owner of a channel deleted it
|
||||
nThen(function (w) {
|
||||
// close the channel in the store
|
||||
Env.msgStore.closeChannel(channelId, w());
|
||||
}).nThen(function (w) {
|
||||
// Server.channelBroadcast would be better
|
||||
// but we can't trust it to track even one callback,
|
||||
// let alone many in parallel.
|
||||
// so we simulate it on this side to avoid race conditions
|
||||
Server.getChannelUserList(channelId).forEach(function (userId) {
|
||||
Server.send(userId, [
|
||||
0,
|
||||
Env.historyKeeper.id,
|
||||
"MSG",
|
||||
userId,
|
||||
JSON.stringify({
|
||||
error: 'EDELETED',
|
||||
channel: channelId,
|
||||
})
|
||||
], w());
|
||||
});
|
||||
}).nThen(function () {
|
||||
// clear the channel's data from memory
|
||||
// once you've sent everyone a notice that the channel has been deleted
|
||||
clear();
|
||||
}).orTimeout(function () {
|
||||
Env.Log.warn('ON_CHANNEL_DELETED_TIMEOUT', channelId);
|
||||
clear();
|
||||
}, 30000);
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -121,6 +187,8 @@ Channel.trimHistory = function (Env, safeKey, data, cb) {
|
||||
// clear historyKeeper's cache for this channel
|
||||
Env.historyKeeper.channelClose(channelId);
|
||||
cb(void 0, 'OK');
|
||||
delete Env.historyKeeper.channel_cache[channelId];
|
||||
delete Env.historyKeeper.metadata_cache[channelId];
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -160,7 +228,7 @@ Channel.isNewChannel = function (Env, channel, cb) {
|
||||
|
||||
Otherwise behaves the same as sending to a channel
|
||||
*/
|
||||
Channel.writePrivateMessage = function (Env, args, cb, Server) { // XXX odd signature
|
||||
Channel.writePrivateMessage = function (Env, args, cb, Server) {
|
||||
var channelId = args[0];
|
||||
var msg = args[1];
|
||||
|
||||
@@ -197,11 +265,10 @@ Channel.writePrivateMessage = function (Env, args, cb, Server) { // XXX odd sign
|
||||
// if the message isn't valid it won't be stored.
|
||||
Env.historyKeeper.channelMessage(Server, channelStruct, fullMessage);
|
||||
|
||||
// call back with the message and the target channel.
|
||||
// historyKeeper will take care of broadcasting it if anyone is in the channel
|
||||
cb(void 0, {
|
||||
channel: channelId,
|
||||
message: fullMessage
|
||||
Server.getChannelUserList(channelId).forEach(function (userId) {
|
||||
Server.send(userId, fullMessage);
|
||||
});
|
||||
|
||||
cb();
|
||||
};
|
||||
|
||||
|
||||
@@ -12,8 +12,7 @@ Data.getMetadata = function (Env, channel, cb/* , Server */) {
|
||||
if (!Core.isValidId(channel)) { return void cb('INVALID_CHAN'); }
|
||||
if (channel.length !== 32) { return cb("INVALID_CHAN_LENGTH"); }
|
||||
|
||||
// XXX get metadata from the server cache if it is available
|
||||
// Server isn't always passed, though...
|
||||
// FIXME get metadata from the server cache if it is available
|
||||
batchMetadata(channel, cb, function (done) {
|
||||
var ref = {};
|
||||
var lineHandler = Meta.createLineHandler(ref, Env.Log.error);
|
||||
@@ -37,7 +36,7 @@ Data.getMetadata = function (Env, channel, cb/* , Server */) {
|
||||
}
|
||||
*/
|
||||
var queueMetadata = WriteQueue();
|
||||
Data.setMetadata = function (Env, safeKey, data, cb) {
|
||||
Data.setMetadata = function (Env, safeKey, data, cb, Server) {
|
||||
var unsafeKey = Util.unescapeKeyCharacters(safeKey);
|
||||
|
||||
var channel = data.channel;
|
||||
@@ -108,8 +107,19 @@ Data.setMetadata = function (Env, safeKey, data, cb) {
|
||||
cb(e);
|
||||
return void next();
|
||||
}
|
||||
|
||||
cb(void 0, metadata);
|
||||
next();
|
||||
|
||||
const metadata_cache = Env.historyKeeper.metadata_cache;
|
||||
const channel_cache = Env.historyKeeper.channel_cache;
|
||||
|
||||
metadata_cache[channel] = metadata;
|
||||
|
||||
var index = Util.find(channel_cache, [channel, 'index']);
|
||||
if (index && typeof(index) === 'object') { index.metadata = metadata; }
|
||||
|
||||
Server.channelBroadcast(channel, JSON.stringify(metadata), Env.historyKeeper.id);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -205,7 +205,6 @@ Pinning.removePins = function (Env, safeKey, cb) {
|
||||
};
|
||||
|
||||
Pinning.trimPins = function (Env, safeKey, cb) {
|
||||
// XXX trim to latest pin checkpoint
|
||||
cb("NOT_IMPLEMENTED");
|
||||
};
|
||||
|
||||
@@ -453,10 +452,10 @@ Pinning.loadChannelPins = function (Env) {
|
||||
|
||||
Pinning.isChannelPinned = function (Env, channel, cb) {
|
||||
Env.evPinnedPadsReady.reg(() => {
|
||||
if (Env.pinnedPads[channel] && Object.keys(Env.pinnedPads[channel]).length) {
|
||||
if (Env.pinnedPads[channel] && Object.keys(Env.pinnedPads[channel]).length) { // FIXME 'Object.keys' here is overkill. We only need to know that it isn't empty
|
||||
cb(void 0, true);
|
||||
} else {
|
||||
delete Env.pinnedPads[channel]; // XXX WAT
|
||||
delete Env.pinnedPads[channel];
|
||||
cb(void 0, false);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user