cryptpad/www/common/pinpad.js

234 lines
8.5 KiB
JavaScript
Raw Normal View History

2017-03-13 09:56:08 +00:00
define([
'/common/rpc.js',
], function (Rpc) {
var create = function (network, proxy, cb) {
2017-04-12 14:02:42 +00:00
if (!network) {
2017-11-30 14:01:17 +00:00
setTimeout(function () {
2017-04-12 14:02:42 +00:00
cb('INVALID_NETWORK');
});
return;
}
if (!proxy) {
2017-11-30 14:01:17 +00:00
setTimeout(function () {
2017-04-12 14:02:42 +00:00
cb('INVALID_PROXY');
});
return;
}
2017-04-04 10:13:31 +00:00
var edPrivate = proxy.edPrivate;
var edPublic = proxy.edPublic;
2017-04-12 14:02:42 +00:00
if (!(edPrivate && edPublic)) {
2017-11-30 14:01:17 +00:00
setTimeout(function () {
2017-04-12 14:02:42 +00:00
cb('INVALID_KEYS');
});
return;
}
2017-04-04 10:13:31 +00:00
Rpc.create(network, edPrivate, edPublic, function (e, rpc) {
if (e) { return void cb(e); }
var exp = {};
// expose the supplied publicKey as an identifier
2017-04-04 10:13:31 +00:00
exp.publicKey = edPublic;
// expose the RPC module's raw 'send' command
2017-04-04 10:13:31 +00:00
exp.send = rpc.send;
// you can ask the server to pin a particular channel for you
2017-04-10 15:38:25 +00:00
exp.pin = function (channels, cb) {
2017-04-14 13:34:22 +00:00
if (!Array.isArray(channels)) {
2017-11-30 14:01:17 +00:00
setTimeout(function () {
cb('[TypeError] pin expects an array');
});
return;
}
2017-04-10 15:38:25 +00:00
rpc.send('PIN', channels, cb);
};
2017-04-05 15:28:04 +00:00
// you can also ask to unpin a particular channel
2017-04-10 15:38:25 +00:00
exp.unpin = function (channels, cb) {
2017-04-14 13:34:22 +00:00
if (!Array.isArray(channels)) {
2017-11-30 14:01:17 +00:00
setTimeout(function () {
cb('[TypeError] pin expects an array');
});
return;
}
2017-04-10 15:38:25 +00:00
rpc.send('UNPIN', channels, cb);
2017-04-04 10:13:31 +00:00
};
// ask the server what it thinks your hash is
2017-04-04 10:13:31 +00:00
exp.getServerHash = function (cb) {
rpc.send('GET_HASH', edPublic, function (e, hash) {
2017-04-10 15:38:25 +00:00
if (!(hash && hash[0])) {
return void cb('NO_HASH_RETURNED');
}
cb(e, hash[0]);
});
2017-04-04 10:13:31 +00:00
};
2017-03-13 09:56:08 +00:00
// if local and remote hashes don't match, send a reset
exp.reset = function (channels, cb) {
2017-04-14 13:34:22 +00:00
if (!Array.isArray(channels)) {
2017-11-30 14:01:17 +00:00
setTimeout(function () {
cb('[TypeError] pin expects an array');
});
return;
}
rpc.send('RESET', channels, function (e, response) {
2017-05-22 16:43:06 +00:00
if (e) {
return void cb(e);
}
if (!response.length) {
console.log(response);
return void cb('INVALID_RESPONSE');
}
cb(e, response[0]);
});
2017-04-05 15:28:04 +00:00
};
// get the total stored size of a channel's patches (in bytes)
exp.getFileSize = function (file, cb) {
rpc.send('GET_FILE_SIZE', file, function (e, response) {
if (e) { return void cb(e); }
2017-06-08 09:24:12 +00:00
if (response && response.length && typeof(response[0]) === 'number') {
return void cb(void 0, response[0]);
} else {
cb('INVALID_RESPONSE');
}
});
2017-04-21 12:51:00 +00:00
};
// 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, function (e, response) {
if (e) { return void cb(e); }
2017-06-09 13:28:53 +00:00
if (response && response.length && typeof(response[0]) === 'number') {
cb(void 0, response[0]);
} else {
cb('INVALID_RESPONSE');
}
});
};
2017-04-05 15:28:04 +00:00
2017-05-11 14:12:44 +00:00
// Update the limit value for all the users and return the limit for your publicKey
exp.updatePinLimits = function (cb) {
rpc.send('UPDATE_LIMITS', undefined, function (e, response) {
if (e) { return void cb(e); }
2017-05-15 16:03:12 +00:00
if (response && response.length && typeof(response[0]) === "number") {
cb (void 0, response[0], response[1], response[2]);
2017-05-11 14:12:44 +00:00
} else {
cb('INVALID_RESPONSE');
}
});
};
// Get the storage limit associated with your publicKey
exp.getLimit = function (cb) {
rpc.send('GET_LIMIT', undefined, function (e, response) {
if (e) { return void cb(e); }
2017-05-15 16:03:12 +00:00
if (response && response.length && typeof(response[0]) === "number") {
cb (void 0, response[0], response[1], response[2]);
2017-05-11 14:12:44 +00:00
} else {
cb('INVALID_RESPONSE');
}
});
};
2017-07-12 16:54:08 +00:00
exp.clearOwnedChannel = function (channel, cb) {
if (typeof(channel) !== 'string' || channel.length !== 32) {
return void cb('INVALID_ARGUMENTS');
}
rpc.send('CLEAR_OWNED_CHANNEL', channel, function (e, response) {
if (e) { return cb(e); }
2017-07-20 13:55:04 +00:00
if (response && response.length) {
cb(void 0, response[0]);
} else {
2018-01-29 11:40:09 +00:00
cb('INVALID_RESPONSE');
}
});
};
exp.removeOwnedChannel = function (channel, cb) {
2018-05-30 12:36:29 +00:00
if (typeof(channel) !== 'string' || [32,48].indexOf(channel.length) === -1) {
2018-01-29 11:40:09 +00:00
// 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 && response[0] === "OK") {
cb();
2018-01-29 11:40:09 +00:00
} else {
cb('INVALID_RESPONSE');
2017-07-20 13:55:04 +00:00
}
2017-07-12 16:54:08 +00:00
});
};
2018-03-21 17:27:20 +00:00
exp.removePins = function (cb) {
rpc.send('REMOVE_PINS', undefined, function (e, response) {
if (e) { return void cb(e); }
if (response && response.length && response[0] === "OK") {
cb();
} else {
cb('INVALID_RESPONSE');
}
});
};
exp.uploadComplete = function (id, cb) {
rpc.send('UPLOAD_COMPLETE', id, function (e, res) {
2017-05-18 10:36:12 +00:00
if (e) { return void cb(e); }
var id = res[0];
if (typeof(id) !== 'string') {
return void cb('INVALID_ID');
2018-05-29 17:42:20 +00:00
}
cb(void 0, id);
});
};
exp.ownedUploadComplete = function (id, cb) {
rpc.send('OWNED_UPLOAD_COMPLETE', id, function (e, res) {
if (e) { return void cb(e); }
var id = res[0];
if (typeof(id) !== 'string') {
return void cb('INVALID_ID');
2017-05-18 10:36:12 +00:00
}
cb(void 0, id);
});
};
exp.uploadStatus = function (size, cb) {
if (typeof(size) !== 'number') {
2017-11-30 14:01:17 +00:00
return void setTimeout(function () {
2017-05-18 10:36:12 +00:00
cb('INVALID_SIZE');
});
}
rpc.send('UPLOAD_STATUS', size, function (e, res) {
if (e) { return void cb(e); }
var pending = res[0];
if (typeof(pending) !== 'boolean') {
return void cb('INVALID_RESPONSE');
}
cb(void 0, pending);
});
};
exp.uploadCancel = function (size, cb) {
rpc.send('UPLOAD_CANCEL', size, function (e) {
2017-05-18 10:36:12 +00:00
if (e) { return void cb(e); }
cb();
});
};
exp.writeLoginBlock = function (data, cb) {
cb();
};
2017-04-04 10:13:31 +00:00
cb(e, exp);
});
2017-03-13 09:56:08 +00:00
};
return { create: create };
});