clean up serverside and clientside pinning logic
This commit is contained in:
7
rpc.js
7
rpc.js
@@ -144,16 +144,11 @@ var getChannelList = function (store, publicKey, cb) {
|
|||||||
pins[pin] = false;
|
pins[pin] = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!parsed[1] || parsed[1].length) {
|
if (parsed[1] && parsed[1].length) {
|
||||||
break;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
parsed[1].forEach(function (channel) {
|
parsed[1].forEach(function (channel) {
|
||||||
pins[channel] = true;
|
pins[channel] = true;
|
||||||
});
|
});
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.error('invalid message read from store');
|
console.error('invalid message read from store');
|
||||||
|
|||||||
@@ -1,69 +1,12 @@
|
|||||||
define([
|
define([
|
||||||
'/common/cryptpad-common.js',
|
|
||||||
'/common/rpc.js',
|
'/common/rpc.js',
|
||||||
|
|
||||||
'/bower_components/tweetnacl/nacl-fast.min.js'
|
'/bower_components/tweetnacl/nacl-fast.min.js'
|
||||||
], function (Cryptpad, Rpc) {
|
], function (Rpc) {
|
||||||
var Nacl = window.nacl;
|
var Nacl = window.nacl;
|
||||||
|
|
||||||
var uniqueChannelList = function (list) {
|
var create = function (network, proxy, cb) {
|
||||||
list = list || Cryptpad.getUserChannelList();
|
if (!network) { return void cb('INVALID_NETWORK'); }
|
||||||
return Cryptpad.deduplicateString(list).sort();
|
if (!proxy) { return void cb('INVALID_PROXY'); }
|
||||||
};
|
|
||||||
|
|
||||||
var localChannelsHash = function (fileList) {
|
|
||||||
var uniqueList = uniqueChannelList(fileList);
|
|
||||||
var hash = Nacl.util.encodeBase64(Nacl
|
|
||||||
.hash(Nacl.util.decodeUTF8( JSON.stringify(uniqueList) )));
|
|
||||||
return hash;
|
|
||||||
};
|
|
||||||
|
|
||||||
var getServerHash = function (rpc, edPublic, cb) {
|
|
||||||
rpc.send('GET_HASH', edPublic, function (e, hash) {
|
|
||||||
cb(e, hash[0]);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var getFileSize = function (rpc, file, cb) {
|
|
||||||
rpc.send('GET_FILE_SIZE', file, cb);
|
|
||||||
};
|
|
||||||
|
|
||||||
var getFileListSize = function (rpc, cb) {
|
|
||||||
return rpc.send('GET_TOTAL_SIZE', undefined, cb);
|
|
||||||
};
|
|
||||||
|
|
||||||
var pinChannel = function (rpc, channel, cb) {
|
|
||||||
rpc.send('PIN', channel, cb);
|
|
||||||
};
|
|
||||||
|
|
||||||
var unpinChannel = function (rpc, channel, cb) {
|
|
||||||
rpc.send('UNPIN', channel, cb);
|
|
||||||
};
|
|
||||||
|
|
||||||
var reset = function (rpc, cb) {
|
|
||||||
var list = uniqueChannelList();
|
|
||||||
rpc.send('RESET', list, cb);
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
1. every time you want to pin or unpid a pad you send a message to the server
|
|
||||||
2. the server sends back a hash of the sorted list of your pinned pads
|
|
||||||
3. you hash your sorted list of pinned pads that you should have according to your drive
|
|
||||||
4. compare them, if same
|
|
||||||
AWESOME
|
|
||||||
if they are not
|
|
||||||
UNPIN all, send all
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Don't use create until Cryptpad is ready
|
|
||||||
// (use Cryptpad.ready)
|
|
||||||
var create = function (cb) {
|
|
||||||
// you will need to communicate with the server
|
|
||||||
// use an already established
|
|
||||||
var network = Cryptpad.getNetwork();
|
|
||||||
|
|
||||||
// your user proxy contains credentials you will need to make RPC calls
|
|
||||||
var proxy = Cryptpad.getStore().getProxy().proxy;
|
|
||||||
|
|
||||||
var edPrivate = proxy.edPrivate;
|
var edPrivate = proxy.edPrivate;
|
||||||
var edPublic = proxy.edPublic;
|
var edPublic = proxy.edPublic;
|
||||||
@@ -74,32 +17,52 @@ define([
|
|||||||
if (e) { return void cb(e); }
|
if (e) { return void cb(e); }
|
||||||
|
|
||||||
var exp = {};
|
var exp = {};
|
||||||
|
|
||||||
|
// expose the supplied publicKey as an identifier
|
||||||
exp.publicKey = edPublic;
|
exp.publicKey = edPublic;
|
||||||
|
|
||||||
|
// expose the RPC module's raw 'send' command
|
||||||
exp.send = rpc.send;
|
exp.send = rpc.send;
|
||||||
|
|
||||||
exp.uniqueChannelList = uniqueChannelList;
|
// you can ask the server to pin a particular channel for you
|
||||||
|
|
||||||
exp.getFileSize = function (file, cb) {
|
|
||||||
getFileSize(rpc, file, cb);
|
|
||||||
};
|
|
||||||
exp.getFileListSize = function (cb) {
|
|
||||||
getFileListSize(rpc, cb);
|
|
||||||
};
|
|
||||||
exp.getServerHash = function (cb) {
|
|
||||||
getServerHash(rpc, edPublic, cb);
|
|
||||||
};
|
|
||||||
|
|
||||||
exp.pin = function (channel, cb) {
|
exp.pin = function (channel, cb) {
|
||||||
pinChannel(rpc, channel, cb);
|
rpc.send('PIN', channel, cb);
|
||||||
};
|
|
||||||
exp.unpin = function (channel, cb) {
|
|
||||||
unpinChannel(rpc, channel, cb);
|
|
||||||
};
|
|
||||||
exp.reset = function (cb) {
|
|
||||||
reset(rpc, cb);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exp.localChannelsHash = localChannelsHash;
|
// you can also ask to unpin a particular channel
|
||||||
|
exp.unpin = function (channel, cb) {
|
||||||
|
rpc.send('UNPIN', channel, cb);
|
||||||
|
};
|
||||||
|
|
||||||
|
// This implementation must match that on the server
|
||||||
|
// it's used for a checksum
|
||||||
|
exp.hashChannelList = function (list) {
|
||||||
|
return Nacl.util.encodeBase64(Nacl.hash(Nacl.util
|
||||||
|
.decodeUTF8(JSON.stringify(list))));
|
||||||
|
};
|
||||||
|
|
||||||
|
// ask the server what it thinks your hash is
|
||||||
|
exp.getServerHash = function (cb) {
|
||||||
|
rpc.send('GET_HASH', edPublic, function (e, hash) {
|
||||||
|
cb(e, hash[0]);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// if local and remote hashes don't match, send a reset
|
||||||
|
exp.reset = function (list, cb) {
|
||||||
|
rpc.send('RESET', list, cb);
|
||||||
|
};
|
||||||
|
|
||||||
|
// get the total stored size of a channel's patches (in bytes)
|
||||||
|
exp.getFileSize = function (file, cb) {
|
||||||
|
rpc.send('GET_FILE_SIZE', file, cb);
|
||||||
|
};
|
||||||
|
|
||||||
|
// get the combined size of all channels (in bytes) for all the
|
||||||
|
// channels which the server has pinned for your publicKey
|
||||||
|
exp.getFileListSize = function (cb) {
|
||||||
|
rpc.send('GET_TOTAL_SIZE', undefined, cb);
|
||||||
|
};
|
||||||
|
|
||||||
cb(e, exp);
|
cb(e, exp);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,12 +10,16 @@ define([
|
|||||||
};
|
};
|
||||||
|
|
||||||
var synchronize = function (call) {
|
var synchronize = function (call) {
|
||||||
var localHash = call.localChannelsHash();
|
// provide a sorted list of unique channels
|
||||||
|
var list = Cryptpad.deduplicateString(Cryptpad.getUserChannelList())
|
||||||
|
.sort();
|
||||||
|
|
||||||
|
var localHash = call.hashChannelList(list);
|
||||||
var serverHash;
|
var serverHash;
|
||||||
|
|
||||||
call.getFileListSize(function (e, bytes) {
|
call.getFileListSize(function (e, bytes) {
|
||||||
if (e) { return void console.error(e); }
|
if (e) { return void console.error(e); }
|
||||||
console.log("%s total bytes used", bytes);
|
console.log("total %sK bytes used", bytes / 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
call.getServerHash(function (e, hash) {
|
call.getServerHash(function (e, hash) {
|
||||||
@@ -26,7 +30,7 @@ define([
|
|||||||
return console.log("all your pads are pinned. There is nothing to do");
|
return console.log("all your pads are pinned. There is nothing to do");
|
||||||
}
|
}
|
||||||
|
|
||||||
call.reset(function (e, response) {
|
call.reset(list, function (e, response) {
|
||||||
if (e) { return console.error(e); }
|
if (e) { return console.error(e); }
|
||||||
else {
|
else {
|
||||||
return console.log('reset pin list. new hash is [%s]', response);
|
return console.log('reset pin list. new hash is [%s]', response);
|
||||||
@@ -37,7 +41,10 @@ define([
|
|||||||
|
|
||||||
$(function () {
|
$(function () {
|
||||||
Cryptpad.ready(function (err, env) {
|
Cryptpad.ready(function (err, env) {
|
||||||
Pinpad.create(function (e, call) {
|
var network = Cryptpad.getNetwork();
|
||||||
|
var proxy = Cryptpad.getStore().getProxy().proxy;
|
||||||
|
|
||||||
|
Pinpad.create(network, proxy, function (e, call) {
|
||||||
if (e) { return void console.error(e); }
|
if (e) { return void console.error(e); }
|
||||||
synchronize(call);
|
synchronize(call);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user