clean up serverside and clientside pinning logic

pull/1/head
ansuz 8 years ago
parent fdede0d1fd
commit a2f692b9a3

@ -144,16 +144,11 @@ var getChannelList = function (store, publicKey, cb) {
pins[pin] = false;
});
if (!parsed[1] || parsed[1].length) {
break;
}
else {
if (parsed[1] && parsed[1].length) {
parsed[1].forEach(function (channel) {
pins[channel] = true;
});
break;
}
break;
default:
console.error('invalid message read from store');

@ -1,69 +1,12 @@
define([
'/common/cryptpad-common.js',
'/common/rpc.js',
'/bower_components/tweetnacl/nacl-fast.min.js'
], function (Cryptpad, Rpc) {
], function (Rpc) {
var Nacl = window.nacl;
var uniqueChannelList = function (list) {
list = list || Cryptpad.getUserChannelList();
return Cryptpad.deduplicateString(list).sort();
};
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 create = function (network, proxy, cb) {
if (!network) { return void cb('INVALID_NETWORK'); }
if (!proxy) { return void cb('INVALID_PROXY'); }
var edPrivate = proxy.edPrivate;
var edPublic = proxy.edPublic;
@ -74,32 +17,52 @@ define([
if (e) { return void cb(e); }
var exp = {};
// expose the supplied publicKey as an identifier
exp.publicKey = edPublic;
// expose the RPC module's raw 'send' command
exp.send = rpc.send;
exp.uniqueChannelList = uniqueChannelList;
// you can ask the server to pin a particular channel for you
exp.pin = function (channel, cb) {
rpc.send('PIN', channel, cb);
};
exp.getFileSize = function (file, cb) {
getFileSize(rpc, file, cb);
// you can also ask to unpin a particular channel
exp.unpin = function (channel, cb) {
rpc.send('UNPIN', channel, cb);
};
exp.getFileListSize = function (cb) {
getFileListSize(rpc, 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) {
getServerHash(rpc, edPublic, cb);
rpc.send('GET_HASH', edPublic, function (e, hash) {
cb(e, hash[0]);
});
};
exp.pin = function (channel, cb) {
pinChannel(rpc, channel, cb);
};
exp.unpin = function (channel, cb) {
unpinChannel(rpc, channel, cb);
// if local and remote hashes don't match, send a reset
exp.reset = function (list, cb) {
rpc.send('RESET', list, cb);
};
exp.reset = function (cb) {
reset(rpc, 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);
};
exp.localChannelsHash = localChannelsHash;
// 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);
});

@ -10,12 +10,16 @@ define([
};
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;
call.getFileListSize(function (e, bytes) {
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) {
@ -26,7 +30,7 @@ define([
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); }
else {
return console.log('reset pin list. new hash is [%s]', response);
@ -37,7 +41,10 @@ define([
$(function () {
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); }
synchronize(call);
});

Loading…
Cancel
Save