Compute history size in the worker

pull/1/head
yflory 5 years ago
parent d736f783e7
commit 689ea40a92

@ -764,8 +764,6 @@ define([
if (bytes === 0) { return void cb(void 0, $d); } if (bytes === 0) { return void cb(void 0, $d); }
var formatted = UIElements.prettySize(bytes); var formatted = UIElements.prettySize(bytes);
historyBytes = Math.floor(0.2*bytes); // XXX test data
if (!owned || !historyBytes || historyBytes > bytes) { if (!owned || !historyBytes || historyBytes > bytes) {
$d.append(h('div.cp-app-prop', [Messages.upload_size, h('br'), h('span.cp-app-prop-content', formatted)])); $d.append(h('div.cp-app-prop', [Messages.upload_size, h('br'), h('span.cp-app-prop-content', formatted)]));
return void cb(void 0, $d); return void cb(void 0, $d);

@ -429,10 +429,12 @@ define([
}); });
}; };
Store.getFileSize = function (clientId, data, cb) { Store.getFileSize = function (clientId, data, _cb) {
var cb = Util.once(Util.mkAsync(_cb));
if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); } if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); }
var channelId = Hash.hrefToHexChannelId(data.href, data.password); var channelId = data.channel || Hash.hrefToHexChannelId(data.href, data.password);
store.anon_rpc.send("GET_FILE_SIZE", channelId, function (e, response) { store.anon_rpc.send("GET_FILE_SIZE", channelId, function (e, response) {
if (e) { return void cb({error: e}); } if (e) { return void cb({error: e}); }
if (response && response.length && typeof(response[0]) === 'number') { if (response && response.length && typeof(response[0]) === 'number') {
@ -1761,7 +1763,9 @@ define([
// Fetch the latest version of the metadata on the server and return it. // Fetch the latest version of the metadata on the server and return it.
// If the pad is stored in our drive, update the local values of "owners" and "expire" // If the pad is stored in our drive, update the local values of "owners" and "expire"
Store.getPadMetadata = function (clientId, data, cb) { Store.getPadMetadata = function (clientId, data, _cb) {
var cb = Util.once(Util.mkAsync(_cb));
if (!data.channel) { return void cb({ error: 'ENOTFOUND'}); } if (!data.channel) { return void cb({ error: 'ENOTFOUND'}); }
store.anon_rpc.send('GET_METADATA', data.channel, function (err, obj) { store.anon_rpc.send('GET_METADATA', data.channel, function (err, obj) {
if (err) { return void cb({error: err}); } if (err) { return void cb({error: err}); }
@ -1843,7 +1847,9 @@ define([
network.sendto(hk, JSON.stringify(['GET_FULL_HISTORY', data.channel, data.validateKey])); network.sendto(hk, JSON.stringify(['GET_FULL_HISTORY', data.channel, data.validateKey]));
}; };
Store.getHistory = function (clientId, data, cb) { Store.getHistory = function (clientId, data, _cb, full) {
var cb = Util.once(Util.mkAsync(_cb));
var network = store.network; var network = store.network;
var hk = network.historyKeeper; var hk = network.historyKeeper;

@ -52,6 +52,12 @@ define([
return channels; return channels;
}; };
var getEdPublic = function (ctx, teamId) {
if (!teamId) { return Util.find(ctx.store, ['proxy', 'edPublic']); }
var teamData = Util.find(ctx, ['store', 'proxy', 'teams', teamId]);
return Util.find(teamData, ['keys', 'drive', 'edPublic']);
};
var getRpc = function (ctx, teamId) { var getRpc = function (ctx, teamId) {
if (!teamId) { return ctx.store.rpc; } if (!teamId) { return ctx.store.rpc; }
var teams = ctx.store.modules['team']; var teams = ctx.store.modules['team'];
@ -61,14 +67,77 @@ define([
return team.rpc; return team.rpc;
}; };
var getHistoryData = function (ctx, channel, lastKnownHash, teamId, _cb) {
var cb = Util.once(Util.mkAsync(_cb));
var edPublic = getEdPublic(ctx, teamId);
var Store = ctx.Store;
var total = 0;
var history = 0;
var metadata = 0;
var hash;
nThen(function (waitFor) {
// Total size
Store.getFileSize(null, {
channel: channel
}, waitFor(function (obj) {
if (obj && obj.error) {
waitFor.abort();
return void cb(obj);
}
if (typeof(obj.size) === "undefined") {
waitFor.abort();
return void cb({error: 'ENOENT'});
}
total = obj.size;
}));
// Pad
Store.getHistory(null, {
channel: channel,
lastKnownHash: lastKnownHash
}, waitFor(function (obj) {
if (obj && obj.error) {
waitFor.abort();
return void cb(obj);
}
if (!Array.isArray(obj)) {
waitFor.abort();
return void cb({error: 'EINVAL'});
}
hash = obj[0].hash;
var messages = obj.map(function(data) {
return data.msg;
});
history = messages.join('\n').length;
}), true);
// Metadata
Store.getPadMetadata(null, {
channel: channel
}, waitFor(function (obj) {
if (obj && obj.error) { return; }
if (!obj || typeof(obj) !== "object") { return; }
metadata = JSON.stringify(obj).length;
if (!obj || !Array.isArray(obj.owners) ||
obj.owners.indexOf(edPublic) === -1) {
waitFor.abort();
return void cb({error: 'INSUFFICIENT_PERMISSIONS'});
}
}));
}).nThen(function () {
cb({
size: (total - metadata - history),
hash: hash
});
});
};
commands.GET_HISTORY_SIZE = function (ctx, data, cId, cb) { commands.GET_HISTORY_SIZE = function (ctx, data, cId, cb) {
if (!ctx.store.loggedIn || !ctx.store.rpc) { return void cb({ error: 'INSUFFICIENT_PERMISSIONS' }); } if (!ctx.store.loggedIn || !ctx.store.rpc) { return void cb({ error: 'INSUFFICIENT_PERMISSIONS' }); }
var channels = data.channels; var channels = data.channels;
if (!Array.isArray(channels)) { return void cb({ error: 'EINVAL' }); } if (!Array.isArray(channels)) { return void cb({ error: 'EINVAL' }); }
var rpc = getRpc(ctx, data.teamid);
if (!rpc) { return void cb({ error: 'ENORPC'}); }
var warning = []; var warning = [];
// If account trim history, get the correct channels here // If account trim history, get the correct channels here
@ -77,30 +146,26 @@ define([
} }
var size = 0; var size = 0;
var res = [];
nThen(function (waitFor) { nThen(function (waitFor) {
// TODO: check if owner first?
channels.forEach(function (chan) { channels.forEach(function (chan) {
size += Math.floor(Math.random()*1000) * 1024; // XXX
chan = chan; // XXX
waitFor = waitFor; // XXX
/*
var channel = chan; var channel = chan;
var lastKnownHash; var lastKnownHash;
if (typeof (chan) === "object" && chan.channel) { if (typeof (chan) === "object" && chan.channel) {
channel = chan.channel; channel = chan.channel;
lastKnownHash = chan.lastKnownHash; lastKnownHash = chan.lastKnownHash;
} }
rpc.getHistorySize({ getHistoryData(ctx, channel, lastKnownHash, data.teamId, waitFor(function (obj) {
channel: channel, if (obj && obj.error) {
lastKnownHash: lastKnownHash warning.push(obj.error);
}, waitFor(function (err, value) {
if (err) {
warning.push(err);
return; return;
} }
size += value; size += obj.size;
res.push({
channel: channel,
lastKnownHash: obj.hash
});
})); }));
*/ // XXX TODO
}); });
}).nThen(function () { }).nThen(function () {
cb({ cb({
@ -115,7 +180,7 @@ define([
var channels = data.channels; var channels = data.channels;
if (!Array.isArray(channels)) { return void cb({ error: 'EINVAL' }); } if (!Array.isArray(channels)) { return void cb({ error: 'EINVAL' }); }
var rpc = getRpc(ctx, data.teamid); var rpc = getRpc(ctx, data.teamId);
if (!rpc) { return void cb({ error: 'ENORPC'}); } if (!rpc) { return void cb({ error: 'ENORPC'}); }
var warning = []; var warning = [];

@ -1203,7 +1203,7 @@ define([
// XXX settings_trimHistoryTitle, settings_trimHistoryHint, trimHistory_button, trimHistory_error // XXX settings_trimHistoryTitle, settings_trimHistoryHint, trimHistory_button, trimHistory_error
// XXX trimHistory_success, trimHistory_confirm // XXX trimHistory_success, trimHistory_confirm
if (!privateData.isDriveOwned) { return; } // XXX //if (!privateData.isDriveOwned) { return; } // XXX
var spinner = UI.makeSpinner(); var spinner = UI.makeSpinner();
var button = h('button.btn.btn-danger-alt', { var button = h('button.btn.btn-danger-alt', {

Loading…
Cancel
Save