diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index afd177577..b9d089c6a 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -83,6 +83,11 @@ define([ cb(obj); }); }; + common.getSharedFolder = function (id, cb) { + postMessage("GET_SHARED_FOLDER", id, function (obj) { + cb(obj); + }); + }; // Settings and ready common.mergeAnonDrive = function (cb) { var data = { diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 70e5ee95b..7c3ec7376 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -38,7 +38,7 @@ define([ Realtime.whenRealtimeSyncs(store.realtime, waitFor()); if (store.sharedFolders) { for (var k in store.sharedFolders) { - Realtime.whenRealtimeSync(store.sharedFolders[k].realtime, waitFor()); + Realtime.whenRealtimeSyncs(store.sharedFolders[k].realtime, waitFor()); } } }).nThen(function () { cb(); }); @@ -61,6 +61,13 @@ define([ onSync(cb); }; + Store.getSharedFolder = function (clientId, id, cb) { + if (store.manager.folders[id]) { + return void cb(store.manager.folders[id].proxy); + } + cb({}); + }; + Store.hasSigningKeys = function () { if (!store.proxy) { return; } return typeof(store.proxy.edPrivate) === 'string' && @@ -1166,7 +1173,7 @@ define([ var id; nThen(function (waitFor) { // TODO XXX get the folder data (href, title, ...) - var folderData = {}; + var folderData = data.folderData || {}; // 1. add the shared folder to our list of shared folders store.userObject.pushSharedFolder(folderData, waitFor(function (err, folderId) { if (err) { @@ -1177,6 +1184,7 @@ define([ })); }).nThen(function (waitFor) { // 2a. add the shared folder to the path in our drive + console.log('adding'); store.userObject.add(id, path); onSync(waitFor()); @@ -1189,6 +1197,23 @@ define([ cb(); }); }; + store.createSharedFolder = function () { + // XXX + var hash = Hash.createRandomHash('folder'); + var href = '/folder/#' + hash; + var secret = Hash.getSecrets('folder', hash); + Store.addSharedFolder(null, { + path: ['root'], + folderData: { + href: href, + roHref: '/folder/#' + Hash.getViewHashFromKeys(secret), + channel: secret.channel, + title: "Test", + } + }, function () { + console.log('done'); + }); + }; // Drive diff --git a/www/common/outer/store-rpc.js b/www/common/outer/store-rpc.js index 972776abe..e041533da 100644 --- a/www/common/outer/store-rpc.js +++ b/www/common/outer/store-rpc.js @@ -52,6 +52,7 @@ define([ GET_PAD_DATA: Store.getPadData, GET_STRONGER_HASH: Store.getStrongerHash, INCREMENT_TEMPLATE_USE: Store.incrementTemplateUse, + GET_SHARED_FOLDER: Store.getSharedFolder, // Messaging INVITE_FROM_USERLIST: Store.inviteFromUserlist, ADD_DIRECT_MESSAGE_HANDLERS: Store.addDirectMessageHandlers, diff --git a/www/common/outer/userObject.js b/www/common/outer/userObject.js index 94363a2d2..5a5469935 100644 --- a/www/common/outer/userObject.js +++ b/www/common/outer/userObject.js @@ -69,6 +69,23 @@ define([ }); }; + exp.pushSharedFolder = function (data, cb) { + if (typeof cb !== "function") { cb = function () {}; } + var todo = function () { + var id = Util.createRandomInteger(); + files[SHARED_FOLDERS][id] = data; + cb(null, id); + }; + if (!loggedIn || !AppConfig.enablePinning || config.testMode) { + return void cb("EAUTH"); + } + if (!pinPads) { return void cb('EAUTH'); } + pinPads([data.channel], function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + todo(); + }); + }; + // FILES DATA var spliceFileData = function (id) { delete files[FILES_DATA][id]; @@ -191,6 +208,7 @@ define([ var toRemove = []; Object.keys(data).forEach(function (id) { + id = Number(id); // Find and maybe update existing pads with the same channel id var d = data[id]; var found = false; @@ -332,9 +350,9 @@ define([ }; exp.add = function (id, path) { - // TODO WW if (!loggedIn && !config.testMode) { return; } - var data = files[FILES_DATA][id]; + id = Number(id); + var data = files[FILES_DATA][id] || files[SHARED_FOLDERS][id]; if (!data || typeof(data) !== "object") { return; } var newPath = path, parentEl; if (path && !Array.isArray(path)) { @@ -438,7 +456,6 @@ define([ }); delete files[OLD_FILES_DATA]; delete files.migrate; - console.log('done'); todo(); }; if (exp.rt) { @@ -581,7 +598,7 @@ define([ }); }; var fixFilesData = function () { - if (typeof files[FILES_DATA] !== "object") { debug("OLD_FILES_DATA was not an object"); files[FILES_DATA] = {}; } + if (typeof files[FILES_DATA] !== "object") { debug("FILES_DATA was not an object"); files[FILES_DATA] = {}; } var fd = files[FILES_DATA]; var rootFiles = exp.getFiles([ROOT, TRASH, 'hrefArray']); var root = exp.find([ROOT]); @@ -649,7 +666,8 @@ define([ secret = Hash.getSecrets(parsed.type, parsed.hash, el.password); } el.channel = secret.channel; - console.log('Adding missing channel in filesData ', el.channel); + console.log(el); + debug('Adding missing channel in filesData ', el.channel); } catch (e) { console.error(e); } diff --git a/www/common/proxy-manager.js b/www/common/proxy-manager.js index ac2feb883..2047a017a 100644 --- a/www/common/proxy-manager.js +++ b/www/common/proxy-manager.js @@ -7,17 +7,20 @@ define([ var getConfig = function (Env) { var cfg = {}; - for (var k in Env.cfg) { cfg[k] = Env[k]; } + for (var k in Env.cfg) { cfg[k] = Env.cfg[k]; } return cfg; }; // Add a shared folder to the list var addProxy = function (Env, id, proxy, leave) { - var cfg = getConfig(); + var cfg = getConfig(Env); cfg.sharedFolder = true; cfg.id = id; - var userObject = UserObject.init(proxy, Env.cfg); - userObject.fixFiles(); + var userObject = UserObject.init(proxy, cfg); + if (userObject.fixFiles) { + // Only in outer + userObject.fixFiles(); + } Env.folders[id] = { proxy: proxy, userObject: userObject, @@ -98,7 +101,9 @@ define([ } var current; var uo = Env.user.userObject; - for (var i=2; i<=path.length; i++) { + // We don't need to check the last element of the path because we only need to split it + // when the path contains an element inside the shared folder + for (var i=2; i metadata (href, title, password...) - // f.proxy => listmap - // f.id => id? - manager.addProxy(id, f.proxy); + manager.addProxy(id, f); }); var $tree = APP.$tree = $("#cp-app-drive-tree"); @@ -1166,7 +1179,10 @@ define([ var $target = $(target); var $el = findDataHolder($target); var newPath = $el.data('path'); - if ((!newPath || manager.isFile(manager.find(newPath))) + var dropEl = newPath && manager.find(newPath); + if (newPath && manager.isSharedFolder(dropEl)) { + newPath.push(manager.user.userObject.ROOT); + } else if ((!newPath || manager.isFile(dropEl)) && $target.parents('#cp-app-drive-content')) { newPath = currentPath; } @@ -2293,6 +2309,11 @@ define([ var isTags = path[0] === TAGS; var root = isVirtual ? undefined : manager.find(path); + if (manager.isSharedFolder(root)) { + path.push(manager.user.userObject.ROOT); + root = manager.find(path); + if (!root) { return; } + } if (!isVirtual && typeof(root) === "undefined") { log(Messages.fm_unknownFolderError); debug("Unable to locate the selected directory: ", path); @@ -2434,7 +2455,9 @@ define([ } updateObject(sframeChan, proxy, function () { copyObjectValue(files, proxy.drive); - _displayDirectory(path, force); + updateSharedFolders(sframeChan, files, folders, function () { + _displayDirectory(path, force); + }); }); }; @@ -3058,8 +3081,9 @@ define([ };*/ var sframeChan = common.getSframeChannel(); - updateObject(sframeChan, proxy, waitFor()); - // XXX Load shared folders + updateObject(sframeChan, proxy, waitFor(function () { + updateSharedFolders(sframeChan, proxy.drive, folders, waitFor()); + })); }).nThen(function () { var sframeChan = common.getSframeChannel(); var metadataMgr = common.getMetadataMgr(); diff --git a/www/drive/main.js b/www/drive/main.js index 79347695c..3e96f2e6f 100644 --- a/www/drive/main.js +++ b/www/drive/main.js @@ -53,6 +53,12 @@ define([ Cryptpad.userObjectCommand(data, cb); }); sframeChan.on('Q_DRIVE_GETOBJECT', function (data, cb) { + if (data && data.sharedFolder) { + Cryptpad.getSharedFolder(data.sharedFolder, function (obj) { + cb(obj); + }); + return; + } Cryptpad.getUserObject(function (obj) { cb(obj); });