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();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user