From f134f11b7318abed25ce11d20406f97c309f075a Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 29 Jan 2018 12:40:09 +0100 Subject: [PATCH] new getDeletedPads rpc --- rpc.js | 40 ++++++++++++++++++++++++++++++++- www/common/outer/async-store.js | 30 +++++++++++++++---------- www/common/pinpad.js | 17 +++++++++++++- 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/rpc.js b/rpc.js index ebc6ae96d..dad806a5b 100644 --- a/rpc.js +++ b/rpc.js @@ -11,6 +11,8 @@ var Path = require("path"); var Https = require("https"); const Package = require('./package.json'); const Pinned = require('./pinned'); +const Saferphore = require("saferphore"); +const nThen = require("nthen"); var RPC = module.exports; @@ -355,6 +357,33 @@ var getMultipleFileSize = function (Env, channels, cb) { }); }; +/* accepts a list, and returns a sublist of channel or file ids which seem + to have been deleted from the server (file size 0) + + we might consider that we should only say a file is gone if fs.stat returns + ENOENT, but for now it's simplest to just rely on getFileSize... +*/ +var getDeletedPads = function (Env, channels, cb) { + if (!Array.isArray(channels)) { return cb('INVALID_LIST'); } + var L = channels.length; + + var sem = Saferphore.create(10); + var absentees = []; + nThen(function (w) { + for (var i = 0; i < L; i++) { + let channel = channels[i]; + sem.take(function (give) { + getFileSize(Env, channel, w(give(function (e, size) { + if (e) { return; } + if (size === 0) { absentees.push(channel); } + }))); + }); + } + }).nThen(function () { + cb(void 0, absentees); + }); +}; + var getTotalSize = function (Env, publicKey, cb) { var bytes = 0; return void getChannelList(Env, publicKey, function (channels) { @@ -1005,7 +1034,8 @@ var isUnauthenticatedCall = function (call) { 'GET_MULTIPLE_FILE_SIZE', 'IS_CHANNEL_PINNED', 'IS_NEW_CHANNEL', - 'GET_HISTORY_OFFSET' + 'GET_HISTORY_OFFSET', + 'GET_DELETED_PADS', ].indexOf(call) !== -1; }; @@ -1128,6 +1158,14 @@ RPC.create = function (config /*:Config_t*/, cb /*:(?Error, ?Function)=>void*/) } respond(e, [null, dict, null]); }); + case 'GET_DELETED_PADS': + return void getDeletedPads(Env, msg[1], function (e, list) { + if (e) { + WARN(e, msg[1]); + return respond(e); + } + respond(e, [null, list, null]); + }); case 'IS_CHANNEL_PINNED': return void isChannelPinned(Env, msg[1], function (isPinned) { respond(null, [null, isPinned, null]); diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index d8fb6ef3b..bf8879d6f 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -211,16 +211,6 @@ define([ }); }; - Store.getDeletedPads = function (data, cb) { - if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); } - - //var list = getCanonicalChannelList(true); - - // TODO: rpc to get the deleted pads here and send this list in the callback - - cb([]); - }; - Store.uploadComplete = function (data, cb) { if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); } store.rpc.uploadComplete(function (err, res) { @@ -337,6 +327,24 @@ define([ }); }; + Store.getDeletedPads = function (data, cb) { + if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); } + var list = getCanonicalChannelList(true); + if (!Array.isArray(list)) { + return void cb({error: 'INVALID_FILE_LIST'}); + } + + store.anon_rpc.send('GET_DELETED_PADS', list, function (e, res) { + console.log(e, res); + if (e) { return void cb({error: e}); } + if (res && res.length && Array.isArray(res[0])) { + cb(res[0]); + } else { + cb({error: 'UNEXPECTED_RESPONSE'}); + } + }); + }; + Store.initAnonRpc = function (data, cb) { require([ '/common/rpc.js', @@ -349,8 +357,6 @@ define([ }); }; - - ////////////////////////////////////////////////////////////////// /////////////////////// Store //////////////////////////////////// ////////////////////////////////////////////////////////////////// diff --git a/www/common/pinpad.js b/www/common/pinpad.js index 47eb97892..0bbaddd37 100644 --- a/www/common/pinpad.js +++ b/www/common/pinpad.js @@ -145,7 +145,22 @@ define([ if (response && response.length) { cb(void 0, response[0]); } else { - cb(); + cb('INVALID_RESPONSE'); + } + }); + }; + + exp.removeOwnedChannel = function (channel, cb) { + if (typeof(channel) !== 'string' || channel.length !== 32) { + // can't use this on files because files can't be owned... + return void cb('INVALID_ARGUMENTS'); + } + rpc.send('REMOVE_OWNED_CHANNEL', channel, function (e, response) { + if (e) { return void cb(e); } + if (response && response.length) { + cb(void 0, response[0]); // I haven't tested this... + } else { + cb('INVALID_RESPONSE'); } }); };