Refactoring part2

This commit is contained in:
yflory
2019-09-05 17:55:23 +02:00
parent 7222d34dc0
commit 4c339afc6c
6 changed files with 94 additions and 50 deletions

View File

@@ -146,14 +146,12 @@ define([
onSync(data.teamId, cb);
};
// XXX Teams
Store.hasSigningKeys = function () {
if (!store.proxy) { return; }
return typeof(store.proxy.edPrivate) === 'string' &&
typeof(store.proxy.edPublic) === 'string';
};
// XXX Teams
Store.hasCurveKeys = function () {
if (!store.proxy) { return; }
return typeof(store.proxy.curvePrivate) === 'string' &&
@@ -218,7 +216,6 @@ define([
/////////////////////// RPC //////////////////////////////////////
//////////////////////////////////////////////////////////////////
// XXX Teams pin in teams
Store.pinPads = function (clientId, data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
if (typeof(cb) !== 'function') {
@@ -231,7 +228,6 @@ define([
});
};
// XXX Teams ...
Store.unpinPads = function (clientId, data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
@@ -284,6 +280,7 @@ define([
cb(account);
};
// clearOwnedChannel is only used for private chat at the moment
Store.clearOwnedChannel = function (clientId, data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
store.rpc.clearOwnedChannel(data, function (err) {
@@ -292,22 +289,26 @@ define([
};
Store.removeOwnedChannel = function (clientId, data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
// "data" used to be a string (channelID), now it can also be an object
// data.force tells us we can safely remove the drive ID
var channel = data;
var force = false;
var teamId;
if (data && typeof(data) === "object") {
channel = data.channel;
force = data.force;
teamId = data.teamId;
}
if (channel === storeChannel && !force) {
return void cb({error: 'User drive removal blocked!'});
}
store.rpc.removeOwnedChannel(channel, function (err) {
var s = getStore(teamId);
if (!s) { return void cb({ error: 'ENOTFOUND' }); }
if (!s.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
s.rpc.removeOwnedChannel(channel, function (err) {
cb({error:err});
});
};
@@ -334,40 +335,49 @@ define([
};
Store.uploadComplete = function (clientId, data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
var s = getStore(teamId);
if (!s) { return void cb({ error: 'ENOTFOUND' }); }
if (!s.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
if (data.owned) {
// Owned file
store.rpc.ownedUploadComplete(data.id, function (err, res) {
s.rpc.ownedUploadComplete(data.id, function (err, res) {
if (err) { return void cb({error:err}); }
cb(res);
});
return;
}
// Normal upload
store.rpc.uploadComplete(data.id, function (err, res) {
s.rpc.uploadComplete(data.id, function (err, res) {
if (err) { return void cb({error:err}); }
cb(res);
});
};
Store.uploadStatus = function (clientId, data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
store.rpc.uploadStatus(data.size, function (err, res) {
var s = getStore(teamId);
if (!s) { return void cb({ error: 'ENOTFOUND' }); }
if (!s.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
s.rpc.uploadStatus(data.size, function (err, res) {
if (err) { return void cb({error:err}); }
cb(res);
});
};
Store.uploadCancel = function (clientId, data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
store.rpc.uploadCancel(data.size, function (err, res) {
var s = getStore(teamId);
if (!s) { return void cb({ error: 'ENOTFOUND' }); }
if (!s.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
s.rpc.uploadCancel(data.size, function (err, res) {
if (err) { return void cb({error:err}); }
cb(res);
});
};
Store.uploadChunk = function (clientId, data, cb) {
store.rpc.send.unauthenticated('UPLOAD', data.chunk, function (e, msg) {
var s = getStore(teamId);
if (!s) { return void cb({ error: 'ENOTFOUND' }); }
if (!s.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
s.rpc.send.unauthenticated('UPLOAD', data.chunk, function (e, msg) {
cb({
error: e,
msg: msg
@@ -393,7 +403,7 @@ define([
});
};
Store.initRpc = function (clientId, data, cb) {
var initRpc = function (clientId, data, cb) {
if (!store.loggedIn) { return cb(); }
if (store.rpc) { return void cb(account); }
require(['/common/pinpad.js'], function (Pinpad) {
@@ -486,7 +496,7 @@ define([
});
};
Store.initAnonRpc = function (clientId, data, cb) {
var initAnonRpc = function (clientId, data, cb) {
if (store.anon_rpc) { return void cb(); }
require([
'/common/rpc.js',
@@ -580,12 +590,17 @@ define([
if (data.expire) { pad.expire = data.expire; }
if (data.password) { pad.password = data.password; }
if (data.channel || secret) { pad.channel = data.channel || secret.channel; }
store.manager.addPad(data.path, pad, function (e) {
var s = getStore(data.teamId);
if (!s || !s.manager) { return void cb({ error: 'ENOTFOUND' }); }
s.manager.addPad(data.path, pad, function (e) {
if (e) { return void cb({error: e}); }
sendDriveEvent('DRIVE_CHANGE', {
var send = data.teamId ? s.sendEvent : sendDriveEvent;
send('DRIVE_CHANGE', {
path: ['drive', UserObject.FILES_DATA]
}, clientId);
onSync(cb);
onSync(teamId, cb);
});
};
@@ -1849,7 +1864,7 @@ define([
settings: proxy.settings
}, {
outer: true,
removeOwnedChannel: function (data, cb) { Store.removeOwnedChannel('', data, cb); },
removeOwnedChannel: function (channel, cb) { Store.removeOwnedChannel('', channel, cb); },
edPublic: store.proxy.edPublic,
loggedIn: store.loggedIn,
log: function (msg) {
@@ -1866,8 +1881,8 @@ define([
});
userObject.migrate(waitFor());
}).nThen(function (waitFor) {
Store.initAnonRpc(null, null, waitFor());
Store.initRpc(null, null, waitFor());
initAnonRpc(null, null, waitFor());
initRpc(null, null, waitFor());
}).nThen(function (waitFor) {
loadMailbox(waitFor);
Migrate(proxy, waitFor(), function (version, progress) {

View File

@@ -14,7 +14,6 @@ define([
CREATE_README: Store.createReadme,
MIGRATE_ANON_DRIVE: Store.migrateAnonDrive,
// RPC
INIT_RPC: Store.initRpc,
UPDATE_PIN_LIMIT: Store.updatePinLimit,
GET_PIN_LIMIT: Store.getPinLimit,
CLEAR_OWNED_CHANNEL: Store.clearOwnedChannel,
@@ -30,7 +29,6 @@ define([
GET_DELETED_PADS: Store.getDeletedPads,
GET_PINNED_USAGE: Store.getPinnedUsage,
// ANON RPC
INIT_ANON_RPC: Store.initAnonRpc,
ANON_RPC_MESSAGE: Store.anonRpcMsg,
GET_FILE_SIZE: Store.getFileSize,
GET_MULTIPLE_FILE_SIZE: Store.getMultipleFileSize,

View File

@@ -28,11 +28,15 @@ define([
// TODO: pin or unpin document added to a shared folder from someone who is not a member of the team
};
var onready = function (ctx, team, id, cb) {
var onReady = function (ctx, team, id, cb) {
// XXX
// sanity check: do we have all the required keys?
// initialize team rpc with pin, unpin, ...
// team.rpc = rpc
// load manager
// load shared folders
// ~resetPins for the team?
// getPinLimit
};
var openChannel = function (ctx, team, id, cb) {
@@ -55,6 +59,12 @@ define([
var lm = Listmap.create(cfg);
lm.proxy.on('create', function () {
}).on('ready', function () {
var sendEvent = function (type, data, sender) {
// XXX emit UPDATE event to the inner iframe
// don't send the event back to the sender
// types are DRIVE_CHANGE, DRIVE_REMOVE and DRIVE_LOG
};
ctx.teams[id] = {
proxy: lm.proxy,
listmap: lm,
@@ -62,7 +72,8 @@ define([
manager: undefined, // XXX
realtime: lm.realtime,
handleSharedFolder: function (sfId, rt) { handleSharedFolder(ctx, id, sfId, rt); },
sharedFolders: {} // equivalent of store.sharedFolders in async-store
sharedFolders: {}, // equivalent of store.sharedFolders in async-store
sendEvent: sendEvent
};
onReady(ctx, team, id, function () {

View File

@@ -12,6 +12,7 @@ define([
var metadata = file.metadata;
var owned = file.owned;
var teamId = file.teamId;
// if it exists, path contains the new pad location in the drive
var path = file.path;
@@ -43,8 +44,9 @@ define([
getValidHash(waitFor());
}).nThen(function (waitFor) {
if (!owned) { return; }
common.getMetadata(waitFor(function (err, m) {
edPublic = m.priv.edPublic;
common.getEdPublic(teamId, waitFor(function (obj) {
if (obj && obj.error) { return; }
edPublic = obj;
metadata.owners = [edPublic];
}));
}).nThen(function () {
@@ -54,7 +56,7 @@ define([
var sendChunk = function (box, cb) {
var enc = Nacl.util.encodeBase64(box);
common.uploadChunk(enc, function (e, msg) {
common.uploadChunk(teamId, enc, function (e, msg) {
cb(e, msg);
});
};
@@ -79,7 +81,7 @@ define([
}
// if not box then done
common.uploadComplete(id, owned, function (e) {
common.uploadComplete(teamId, id, owned, function (e) {
if (e) { return void console.error(e); }
var uri = ['', 'blob', id.slice(0,2), id].join('/');
console.log("encrypted blob is now available as %s", uri);
@@ -90,6 +92,7 @@ define([
if (noStore) { return void onComplete(href); }
var data = {
teamId: teamId,
title: title || "",
href: href,
path: path,
@@ -107,7 +110,7 @@ define([
});
};
common.uploadStatus(estimate, function (e, pending) {
common.uploadStatus(teamId, estimate, function (e, pending) {
if (e) {
console.error(e);
onError(e);
@@ -117,7 +120,7 @@ define([
if (pending) {
return void onPending(function () {
// if the user wants to cancel the pending upload to execute that one
common.uploadCancel(estimate, function (e) {
common.uploadCancel(teamId, estimate, function (e) {
if (e) {
return void console.error(e);
}