standardize some function signatures and factor out a lot of boilerplate

This commit is contained in:
ansuz
2020-02-03 18:32:21 -05:00
parent d1c6e67d17
commit 88be40ede3
5 changed files with 95 additions and 129 deletions

View File

@@ -6,10 +6,11 @@ const nThen = require("nthen");
const Core = require("./core");
const Metadata = require("./metadata");
Channel.clearOwnedChannel = function (Env, channelId, unsafeKey, cb) {
Channel.clearOwnedChannel = function (Env, safeKey, channelId, cb) {
if (typeof(channelId) !== 'string' || channelId.length !== 32) {
return cb('INVALID_ARGUMENTS');
}
var unsafeKey = Util.unescapeKeyCharacters(safeKey);
Metadata.getMetadata(Env, channelId, function (err, metadata) {
if (err) { return void cb(err); }
@@ -24,13 +25,14 @@ Channel.clearOwnedChannel = function (Env, channelId, unsafeKey, cb) {
});
};
Channel.removeOwnedChannel = function (Env, channelId, unsafeKey, cb) {
Channel.removeOwnedChannel = function (Env, safeKey, channelId, cb) {
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 safeKey = Util.escapeKeyCharacters(unsafeKey);
var blobId = channelId;
return void nThen(function (w) {
@@ -65,7 +67,7 @@ Channel.removeOwnedChannel = function (Env, channelId, unsafeKey, cb) {
if (err) {
return void cb("E_PROOF_REMOVAL");
}
cb();
cb(void 0, 'OK');
});
});
}
@@ -83,12 +85,15 @@ Channel.removeOwnedChannel = function (Env, channelId, unsafeKey, cb) {
channelId: channelId,
status: e? String(e): 'SUCCESS',
});
cb(e);
if (e) {
return void cb(e);
}
cb(void 0, 'OK');
});
});
};
Channel.removeOwnedChannelHistory = function (Env, channelId, unsafeKey, hash, cb) {
Channel.removeOwnedChannelHistory = function (Env, channelId, unsafeKey, hash, cb) { // XXX UNSAFE
nThen(function (w) {
Metadata.getMetadata(Env, channelId, w(function (err, metadata) {
if (err) { return void cb(err); }
@@ -107,6 +112,7 @@ Channel.removeOwnedChannelHistory = function (Env, channelId, unsafeKey, hash, c
if (err) { return void cb(err); }
// clear historyKeeper's cache for this channel
Env.historyKeeper.channelClose(channelId);
cb(void 0, 'OK');
});
});
};