From 65628a51692d86269696de655b275e3b33e19a5a Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 5 Nov 2020 14:02:03 +0100 Subject: [PATCH] Only use offline cache in the drive app for now --- www/common/cryptpad-common.js | 1 + www/common/outer/async-store.js | 81 ++++++++++++------------------- www/common/sframe-common-outer.js | 1 + www/drive/inner.js | 2 + www/drive/main.js | 1 + 5 files changed, 37 insertions(+), 49 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 82ea2991f..9e65dba93 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -2108,6 +2108,7 @@ define([ anonHash: LocalStore.getFSHash(), localToken: tryParsing(localStorage.getItem(Constants.tokenKey)), // TODO move this to LocalStore ? language: common.getLanguage(), + cache: rdyCfg.cache, driveEvents: true //rdyCfg.driveEvents // Boolean }; diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index a2101a7ff..ec9c36d14 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -34,6 +34,8 @@ define([ NetConfig, AppConfig, Crypto, ChainPad, CpNetflux, Listmap, nThen, Saferphore) { + var onReadyEvt = Util.mkEvent(true); + // Default settings for new users var NEW_USER_SETTINGS = { drive: { @@ -595,6 +597,7 @@ define([ pendingFriends: store.proxy.friends_pending || {}, supportPrivateKey: Util.find(store.proxy, ['mailboxes', 'supportadmin', 'keys', 'curvePrivate']), accountName: store.proxy.login_name || '', + offline: store.offline, teams: teams, plan: account.plan } @@ -2178,16 +2181,18 @@ define([ }); }; Store.addSharedFolder = function (clientId, data, cb) { - var s = getStore(data.teamId); - s.manager.addSharedFolder(data, function (id) { - if (id && typeof(id) === "object" && id.error) { - return void cb(id); - } - var send = data.teamId ? s.sendEvent : sendDriveEvent; - send('DRIVE_CHANGE', { - path: ['drive', UserObject.FILES_DATA] - }, clientId); - cb(id); + onReadyEvt.reg(function () { + var s = getStore(data.teamId); + s.manager.addSharedFolder(data, function (id) { + if (id && typeof(id) === "object" && id.error) { + return void cb(id); + } + var send = data.teamId ? s.sendEvent : sendDriveEvent; + send('DRIVE_CHANGE', { + path: ['drive', UserObject.FILES_DATA] + }, clientId); + cb(id); + }); }); }; Store.updateSharedFolderPassword = function (clientId, data, cb) { @@ -2500,49 +2505,20 @@ define([ addSharedFolderHandler(); userObject.migrate(cb); }; - var onReady = function (clientId, returned/*, cb*/) { + + // onReady: called when the drive is synced (not using the cache anymore) + // "cb" is wrapped in Util.once() and may have already been called + // if we have a local cache + var onReady = function (clientId, returned, cb) { console.error('READY'); - var proxy = store.proxy; - /* - var unpin = function (data, cb) { - if (!store.loggedIn) { return void cb(); } - Store.unpinPads(null, data, cb); - }; - var pin = function (data, cb) { - if (!store.loggedIn) { return void cb(); } - Store.pinPads(null, data, cb); - }; - if (!proxy.settings) { proxy.settings = NEW_USER_SETTINGS; } - if (!proxy.friends_pending) { proxy.friends_pending = {}; } - var manager = store.manager = ProxyManager.create(proxy.drive, { - onSync: function (cb) { onSync(null, cb); }, - edPublic: proxy.edPublic, - pin: pin, - unpin: unpin, - loadSharedFolder: loadSharedFolder, - settings: proxy.settings, - removeOwnedChannel: function (channel, cb) { Store.removeOwnedChannel('', channel, cb); }, - Store: Store - }, { - outer: true, - edPublic: store.proxy.edPublic, - loggedIn: store.loggedIn, - log: function (msg) { - // broadcast to all drive apps - sendDriveEvent("DRIVE_LOG", msg); - }, - rt: store.realtime - }); - var userObject = store.userObject = manager.user.userObject; - addSharedFolderHandler(); - */ store.ready = true; + var proxy = store.proxy; var manager = store.manager; var userObject = store.userObject; nThen(function (waitFor) { if (manager) { return; } - onCacheReady(clientId, waitFor); + onCacheReady(clientId, waitFor()); manager = store.manager; userObject = store.userObject; }).nThen(function (waitFor) { @@ -2616,9 +2592,12 @@ define([ Feedback.init(returned.feedback); // XXX send feedback and logintoken to outer... - //if (typeof(cb) === 'function') { cb(returned); } - store.offline = false; + // "cb" may have already been called by onCacheReady + if (typeof(cb) === 'function') { cb(returned); } sendDriveEvent('NETWORK_RECONNECT'); + store.offline = false; +// XXX broadcast READY event with the missing data +// XXX we can improve feedback to queue the queries and send them when coming back online if (typeof(proxy.uid) !== 'string' || proxy.uid.length !== 32) { // even anonymous users should have a persistent, unique-ish id @@ -2673,6 +2652,8 @@ define([ broadcast([], "UPDATE_TOKEN", { token: proxy[Constants.tokenKey] }); }); + onReadyEvt.fire(); + loadMailbox(); }); }; @@ -2717,8 +2698,8 @@ define([ if (!data.userHash) { returned.anonHash = Hash.getEditHashFromKeys(secret); } - if (typeof(cb) === "function") { cb(returned); } }).on('cacheready', function (info) { + if (!data.cache) { return; } store.offline = true; store.realtime = info.realtime; store.networkPromise = info.networkPromise; @@ -2753,10 +2734,12 @@ define([ rt.proxy.on('disconnect', function () { store.offline = true; sendDriveEvent('NETWORK_DISCONNECT'); + broadcast([], "UPDATE_METADATA"); }); rt.proxy.on('reconnect', function () { store.offline = false; sendDriveEvent('NETWORK_RECONNECT'); + broadcast([], "UPDATE_METADATA"); }); // Ping clients regularly to make sure one tab was not closed without sending a removeClient() diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index b7ce64d4d..b71fbc249 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -151,6 +151,7 @@ define([ Cryptpad.ready(waitFor(), { driveEvents: cfg.driveEvents, + cache: Boolean(cfg.cache), currentPad: currentPad }); diff --git a/www/drive/inner.js b/www/drive/inner.js index 9aa080dc3..364d4a9d9 100644 --- a/www/drive/inner.js +++ b/www/drive/inner.js @@ -278,6 +278,7 @@ define([ if (!proxy.drive || typeof(proxy.drive) !== 'object') { throw new Error("Corrupted drive"); } + APP.online = !privateData.offline; var drive = DriveUI.create(common, { $limit: usageBar && usageBar.$container, proxy: proxy, @@ -309,6 +310,7 @@ define([ onDisconnect(); }); sframeChan.on('EV_NETWORK_RECONNECT', function () { + console.log('here'); onReconnect(); }); common.onLogout(function () { setEditable(false); }); diff --git a/www/drive/main.js b/www/drive/main.js index 5eb9dbc67..418338d58 100644 --- a/www/drive/main.js +++ b/www/drive/main.js @@ -129,6 +129,7 @@ define([ hash: hash, href: href, afterSecrets: afterSecrets, + cache: true, noHash: true, noRealtime: true, driveEvents: true,