implement atomic pin reset rpc
This commit is contained in:
34
rpc.js
34
rpc.js
@@ -143,6 +143,17 @@ var getChannelList = function (store, publicKey, cb) {
|
|||||||
Object.keys(pins).forEach(function (pin) {
|
Object.keys(pins).forEach(function (pin) {
|
||||||
pins[pin] = false;
|
pins[pin] = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!parsed[1] || parsed[1].length) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
parsed[1].forEach(function (channel) {
|
||||||
|
pins[channel] = true;
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.error('invalid message read from store');
|
console.error('invalid message read from store');
|
||||||
@@ -236,8 +247,14 @@ var unpinChannel = function (store, publicKey, channel, cb) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var resetUserPins = function (store, publicKey, channelList, cb) {
|
var resetUserPins = function (store, publicKey, channelList, cb) {
|
||||||
// TODO make this atomic
|
store.message(publicKey, JSON.stringify(['RESET', channelList]),
|
||||||
store.message(publicKey, JSON.stringify(['RESET']), cb);
|
function (e) {
|
||||||
|
if (e) { return void cb(e); }
|
||||||
|
|
||||||
|
getHash(store, publicKey, function (e, hash) {
|
||||||
|
cb(e, hash);
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var expireSessions = function (Cookies) {
|
var expireSessions = function (Cookies) {
|
||||||
@@ -324,18 +341,11 @@ RPC.create = function (config, cb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (msg[0]) {
|
switch (msg[0]) {
|
||||||
case 'COOKIE':
|
case 'COOKIE': return void Respond(void 0);
|
||||||
return void Respond(void 0);
|
|
||||||
case 'ECHO':
|
|
||||||
return void Respond(void 0, msg);
|
|
||||||
|
|
||||||
/* TODO
|
|
||||||
reset should be atomic in case the operation is aborted */
|
|
||||||
case 'RESET':
|
case 'RESET':
|
||||||
return resetUserPins(store, safeKey, [], function (e) {
|
return resetUserPins(store, safeKey, msg[1], function (e, hash) {
|
||||||
return void Respond(e);
|
return void Respond(e, hash);
|
||||||
});
|
});
|
||||||
|
|
||||||
case 'PIN':
|
case 'PIN':
|
||||||
return pinChannel(store, safeKey, msg[1], function (e, hash) {
|
return pinChannel(store, safeKey, msg[1], function (e, hash) {
|
||||||
Respond(e, hash);
|
Respond(e, hash);
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ define([
|
|||||||
};
|
};
|
||||||
|
|
||||||
var reset = function (rpc, cb) {
|
var reset = function (rpc, cb) {
|
||||||
rpc.send('RESET', undefined, cb);
|
var list = uniqueChannelList();
|
||||||
|
rpc.send('RESET', list, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -54,7 +55,6 @@ define([
|
|||||||
UNPIN all, send all
|
UNPIN all, send all
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
// Don't use create until Cryptpad is ready
|
// Don't use create until Cryptpad is ready
|
||||||
// (use Cryptpad.ready)
|
// (use Cryptpad.ready)
|
||||||
var create = function (cb) {
|
var create = function (cb) {
|
||||||
|
|||||||
@@ -61,26 +61,9 @@ define([
|
|||||||
|
|
||||||
call.reset(function (e, response) {
|
call.reset(function (e, response) {
|
||||||
if (e) { return console.error(e); }
|
if (e) { return console.error(e); }
|
||||||
|
else {
|
||||||
var list = call.uniqueChannelList();
|
return console.log('reset pin list. new hash is [%s]', response);
|
||||||
|
|
||||||
var left = list.length;
|
|
||||||
|
|
||||||
// now start pinning...
|
|
||||||
list.forEach(function (channel) {
|
|
||||||
call.pin(channel, function (e, hash) {
|
|
||||||
left--;
|
|
||||||
|
|
||||||
if (!e) {
|
|
||||||
console.log('pinned channel [%s]. new hash is [%s]', channel, hash);
|
|
||||||
} else {
|
|
||||||
console.error(e);
|
|
||||||
}
|
}
|
||||||
if (left === 0) {
|
|
||||||
console.log('done pinning');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user