From e87992c7ec9a64b17262ad1cca69d1d9b5ec0778 Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 7 Apr 2017 11:37:19 +0200 Subject: [PATCH] implement atomic pin reset rpc --- rpc.js | 34 ++++++++++++++++++++++------------ www/common/pinpad.js | 4 ++-- www/examples/pin/main.js | 23 +++-------------------- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/rpc.js b/rpc.js index 45ede9b49..7ad81acb3 100644 --- a/rpc.js +++ b/rpc.js @@ -143,6 +143,17 @@ var getChannelList = function (store, publicKey, cb) { Object.keys(pins).forEach(function (pin) { pins[pin] = false; }); + + if (!parsed[1] || parsed[1].length) { + break; + } + else { + parsed[1].forEach(function (channel) { + pins[channel] = true; + }); + break; + } + break; default: 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) { - // TODO make this atomic - store.message(publicKey, JSON.stringify(['RESET']), cb); + store.message(publicKey, JSON.stringify(['RESET', channelList]), + function (e) { + if (e) { return void cb(e); } + + getHash(store, publicKey, function (e, hash) { + cb(e, hash); + }); + }); }; var expireSessions = function (Cookies) { @@ -324,18 +341,11 @@ RPC.create = function (config, cb) { } switch (msg[0]) { - case 'COOKIE': - 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 'COOKIE': return void Respond(void 0); case 'RESET': - return resetUserPins(store, safeKey, [], function (e) { - return void Respond(e); + return resetUserPins(store, safeKey, msg[1], function (e, hash) { + return void Respond(e, hash); }); - case 'PIN': return pinChannel(store, safeKey, msg[1], function (e, hash) { Respond(e, hash); diff --git a/www/common/pinpad.js b/www/common/pinpad.js index 92721c11d..e40270311 100644 --- a/www/common/pinpad.js +++ b/www/common/pinpad.js @@ -41,7 +41,8 @@ define([ }; 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 */ - // Don't use create until Cryptpad is ready // (use Cryptpad.ready) var create = function (cb) { diff --git a/www/examples/pin/main.js b/www/examples/pin/main.js index 1eb0388e0..903435d60 100644 --- a/www/examples/pin/main.js +++ b/www/examples/pin/main.js @@ -61,26 +61,9 @@ define([ call.reset(function (e, response) { if (e) { return console.error(e); } - - var list = call.uniqueChannelList(); - - 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'); - } - }); - }); + else { + return console.log('reset pin list. new hash is [%s]', response); + } }); /*