always return errors as strings when sending roster messages. fix getLastCheckpointHash logic

pull/1/head
ansuz 5 years ago
parent 55f19b998f
commit 2d761388c1

@ -197,14 +197,22 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) {
} }
var members = roster.state.members; var members = roster.state.members;
var changed = false; // validate first...
args.forEach(function (curve) { args.forEach(function (curve) {
if (!isValidId(curve)) { throw new Error("INVALID_CURVE_KEY"); } if (!isValidId(curve)) { throw new Error("INVALID_CURVE_KEY"); }
// don't try to remove something that isn't there // even members can remove themselves
if (!members[curve]) { return; } if (curve === author) { return; }
// but if it concerns anyone else, validate that the author has sufficient permissions
var role = members[curve].role; var role = members[curve].role;
if (!canRemoveRole(author, role, members)) { throw new Error("INSUFFICIENT_PERMISSIONS"); } if (!canRemoveRole(author, role, members)) { throw new Error("INSUFFICIENT_PERMISSIONS"); }
});
var changed = false;
args.forEach(function (curve) {
// don't try to remove something that isn't there
if (!members[curve]) { return; }
changed = true; changed = true;
delete members[curve]; delete members[curve];
}); });
@ -352,6 +360,8 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) {
var keys = config.keys; var keys = config.keys;
var me = keys.myCurvePublic; var me = keys.myCurvePublic;
var channel = config.channel; var channel = config.channel;
var lastKnownHash = config.lastKnownHash || -1;
var ref = { var ref = {
state: { state: {
members: { }, members: { },
@ -360,7 +370,7 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) {
internal: { internal: {
initialized: false, initialized: false,
sinceLastCheckpoint: 0, sinceLastCheckpoint: 0,
lastCheckpointHash: -1 lastCheckpointHash: lastKnownHash,
}, },
}; };
var roster = {}; var roster = {};
@ -462,7 +472,7 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) {
try { try {
changed = handleCommand(parsed, author, ref); changed = handleCommand(parsed, author, ref);
} catch (err) { } catch (err) {
error = err; error = err.message;
} }
var id = getMessageId(hash); var id = getMessageId(hash);
@ -517,7 +527,7 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) {
// simulate the command before you send it // simulate the command before you send it
changed = simulate(msg, keys.myCurvePublic, ref); changed = simulate(msg, keys.myCurvePublic, ref);
} catch (err) { } catch (err) {
return void cb(err); return void cb(err.message);
} }
if (!changed) { return void cb("NO_CHANGE"); } if (!changed) { return void cb("NO_CHANGE"); }
@ -533,7 +543,7 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) {
channel, channel,
ciphertext ciphertext
], function (err) { ], function (err) {
if (err) { return response.handle(id, [err]); } if (err) { return response.handle(id, [err.message || err]); }
}); });
return id; return id;
}; };
@ -644,7 +654,6 @@ var factory = function (Util, Hash, CPNetflux, Sortify, nThen, Crypto) {
return void cb(err); return void cb(err);
} }
}).nThen(function () { }).nThen(function () {
var lastKnownHash = config.lastKnownHash || -1;
if (typeof(lastKnownHash) === 'string') { if (typeof(lastKnownHash) === 'string') {
console.log("Synchronizing from checkpoint"); console.log("Synchronizing from checkpoint");
} }

Loading…
Cancel
Save