From 4c339afc6c408077467f2d912018216609f3ae94 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 5 Sep 2019 17:55:23 +0200 Subject: [PATCH] Refactoring part2 --- www/common/cryptpad-common.js | 46 +++++++++++++++--------- www/common/outer/async-store.js | 61 ++++++++++++++++++++------------ www/common/outer/store-rpc.js | 2 -- www/common/outer/team.js | 17 +++++++-- www/common/outer/upload.js | 15 ++++---- www/common/sframe-common-file.js | 3 ++ 6 files changed, 94 insertions(+), 50 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 6e2530d62..940f5bc2a 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -110,6 +110,14 @@ define([ data: data }, cb); }; + common.getEdPublic = function (teamId, cb) { + postMessage("GET", { + teamId: teamId, + key: ['edPublic'] + }, function (obj) { + cb(obj); + }); + }; // Settings and ready common.mergeAnonDrive = function (cb) { var data = { @@ -259,11 +267,8 @@ define([ postMessage("CLEAR_OWNED_CHANNEL", channel, cb); }; // "force" allows you to delete your drive ID - common.removeOwnedChannel = function (channel, cb, force) { - postMessage("REMOVE_OWNED_CHANNEL", { - channel: channel, - force: force - }, cb); + common.removeOwnedChannel = function (data, cb) { + postMessage("REMOVE_OWNED_CHANNEL", data, cb); }; common.getDeletedPads = function (data, cb) { @@ -273,29 +278,29 @@ define([ }); }; - common.uploadComplete = function (id, owned, cb) { - postMessage("UPLOAD_COMPLETE", {id: id, owned: owned}, function (obj) { + common.uploadComplete = function (teamId, id, owned, cb) { + postMessage("UPLOAD_COMPLETE", {teamId: teamId, id: id, owned: owned}, function (obj) { if (obj && obj.error) { return void cb(obj.error); } cb(null, obj); }); }; - common.uploadStatus = function (size, cb) { - postMessage("UPLOAD_STATUS", {size: size}, function (obj) { + common.uploadStatus = function (teamId, size, cb) { + postMessage("UPLOAD_STATUS", {teamId: teamId, size: size}, function (obj) { if (obj && obj.error) { return void cb(obj.error); } cb(null, obj); }); }; - common.uploadCancel = function (size, cb) { - postMessage("UPLOAD_CANCEL", {size: size}, function (obj) { + common.uploadCancel = function (teamId, size, cb) { + postMessage("UPLOAD_CANCEL", {teamId: teamId, size: size}, function (obj) { if (obj && obj.error) { return void cb(obj.error); } cb(null, obj); }); }; - common.uploadChunk = function (data, cb) { - postMessage("UPLOAD_CHUNK", {chunk: data}, function (obj) { + common.uploadChunk = function (teamId, data, cb) { + postMessage("UPLOAD_CHUNK", {teamId: teamId, chunk: data}, function (obj) { if (obj && obj.error) { return void cb(obj.error); } cb(null, obj); }); @@ -496,6 +501,7 @@ define([ Cryptput(hash, data.toSave, function (e) { if (e) { throw new Error(e); } postMessage("ADD_PAD", { + teamId: data.teamId, href: href, title: data.title, path: ['template'] @@ -784,6 +790,7 @@ define([ }); }; + // XXX Teams: change the password of a pad owned by the team common.changePadPassword = function (Crypt, href, newPassword, edPublic, cb) { if (!href) { return void cb({ error: 'EINVAL_HREF' }); } var parsed = Hash.parsePadUrl(href); @@ -850,7 +857,10 @@ define([ }, waitFor()); pad.onDisconnectEvent.fire(true); }).nThen(function (waitFor) { - common.removeOwnedChannel(oldChannel, waitFor(function (obj) { + common.removeOwnedChannel({ + channel: oldChannel, + teamId: null // TODO + }, waitFor(function (obj) { if (obj && obj.error) { waitFor.abort(); return void cb(obj); @@ -1027,7 +1037,11 @@ define([ }).nThen(function (waitFor) { if (oldIsOwned) { console.log('removing old drive'); - common.removeOwnedChannel(secret.channel, waitFor(function (obj) { + common.removeOwnedChannel({ + channel: secret.channel, + teamId: null, + force: true + }, waitFor(function (obj) { if (obj && obj.error) { // Deal with it as if it was not owned oldIsOwned = false; @@ -1036,7 +1050,7 @@ define([ common.logoutFromAll(waitFor(function () { postMessage("DISCONNECT"); })); - }), true); + })); } }).nThen(function (waitFor) { if (!oldIsOwned) { diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 7ab65449a..f091c62f9 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -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) { diff --git a/www/common/outer/store-rpc.js b/www/common/outer/store-rpc.js index cc5125053..36ec9541c 100644 --- a/www/common/outer/store-rpc.js +++ b/www/common/outer/store-rpc.js @@ -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, diff --git a/www/common/outer/team.js b/www/common/outer/team.js index fa04ed30b..83527bbe8 100644 --- a/www/common/outer/team.js +++ b/www/common/outer/team.js @@ -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 () { diff --git a/www/common/outer/upload.js b/www/common/outer/upload.js index 9cb19f18b..a443a6d05 100644 --- a/www/common/outer/upload.js +++ b/www/common/outer/upload.js @@ -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); } diff --git a/www/common/sframe-common-file.js b/www/common/sframe-common-file.js index 868279717..573182c7f 100644 --- a/www/common/sframe-common-file.js +++ b/www/common/sframe-common-file.js @@ -33,6 +33,8 @@ define([ var File = {}; var origin = common.getMetadataMgr().getPrivateData().origin; + var teamId = config.teamId; // XXX Teams file upload as a team + var queue = File.queue = { queue: [], inProgress: false @@ -165,6 +167,7 @@ define([ file.noStore = config.noStore; try { file.blob = Nacl.util.encodeBase64(u8); + file.teamId = teamId; common.uploadFile(file, function () { console.log('Upload started...'); });