From 9eb33e39e68515a41a32be2ead8ac216bca91293 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 5 Dec 2017 18:09:43 +0100 Subject: [PATCH] Enable change events in the drive --- www/common/cryptpad-common.js | 16 +++++++++++++--- www/common/outer/async-store.js | 21 +++++++++++++++++++-- www/common/sframe-common-outer.js | 3 ++- www/common/sframe-protocol.js | 3 +++ www/drive/inner.js | 20 ++++++++------------ www/drive/main.js | 23 +++++++++++++---------- www/drive/tests.js | 2 +- 7 files changed, 59 insertions(+), 29 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 555030b74..489c3c6df 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -99,7 +99,10 @@ define([ common.userObjectCommand = function (data, cb) { postMessage("DRIVE_USEROBJECT", data, cb); }; - common.onDriveLog = Util.mkEvent(); + common.drive = {}; + common.drive.onLog = Util.mkEvent(); + common.drive.onChange = Util.mkEvent(); + common.drive.onRemove = Util.mkEvent(); // Profile common.getProfileEditUrl = function (cb) { postMessage("GET", ['profile', 'edit'], function (obj) { @@ -649,7 +652,13 @@ define([ } // Drive case 'DRIVE_LOG': { - common.onDriveLog.fire(data); + common.drive.onLog.fire(data); + } + case 'DRIVE_CHANGE': { + common.drive.onChange.fire(data); + } + case 'DRIVE_REMOVE': { + common.drive.onRemove.fire(data); } } }; @@ -696,7 +705,8 @@ define([ anonHash: LocalStore.getFSHash(), localToken: tryParsing(localStorage.getItem(Constants.tokenKey)), language: common.getLanguage(), - messenger: rdyCfg.messenger + messenger: rdyCfg.messenger, + driveEvents: rdyCfg.driveEvents }; if (sessionStorage[Constants.newPadPathKey]) { cfg.initialPath = sessionStorage[Constants.newPadPathKey]; diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index c8627dbea..0748be159 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -264,7 +264,6 @@ define([ }; Store.getFileSize = function (data, cb) { - console.log(data, cb); if (!store.anon_rpc) { return void cb({error: 'ANON_RPC_NOT_READY'}); } var channelId = Hash.hrefToHexChannelId(data.href); @@ -930,7 +929,7 @@ define([ ChainPad: ChainPad, classic: true, }; - var rt = Listmap.create(listmapConfig); + var rt = window.rt = Listmap.create(listmapConfig); store.proxy = rt.proxy; store.loggedIn = typeof(data.userHash) !== "undefined"; @@ -959,6 +958,7 @@ define([ if (path[0] === 'drive' && path[1] === "migrate" && value === 1) { rt.network.disconnect(); rt.realtime.abort(); + postMessage('NETWORK_DISCONNECT'); } }); @@ -1007,6 +1007,23 @@ define([ var messagingCfg = getMessagingCfg(); Messaging.addDirectMessageHandler(messagingCfg); + // Send events whenever there is a change or a removal in the drive + if (data.driveEvents) { + store.proxy.on('change', [], function (o, n, p) { + postMessage('DRIVE_CHANGE', { + old: o, + new: n, + path: p + }) + }); + store.proxy.on('remove', [], function (o, p) { + postMessage('DRIVE_REMOVE', { + old: o, + path: p + }) + }); + } + if (data.messenger) { var messenger = store.messenger = Messenger.messenger(store); // TODO messenger.on('message', function (message) { diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index ca7d1d0e0..19e4f315c 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -86,7 +86,8 @@ define([ sframeChan = sfc; }), false, { cache: cache, localStore: localStore, language: Cryptpad.getLanguage() }); Cryptpad.ready(waitFor(), { - messenger: cfg.messaging + messenger: cfg.messaging, + driveEvents: cfg.driveEvents }); if (!cfg.newNetwork) { diff --git a/www/common/sframe-protocol.js b/www/common/sframe-protocol.js index f33d52c13..7db1a561a 100644 --- a/www/common/sframe-protocol.js +++ b/www/common/sframe-protocol.js @@ -203,6 +203,9 @@ define({ 'Q_DRIVE_GETOBJECT': true, // Store's userObject need to send log messages to inner to display them in the UI 'EV_DRIVE_LOG': true, + // Refresh the drive when the drive has changed ('change' or 'remove' events) + 'EV_DRIVE_CHANGE': true, + 'EV_DRIVE_REMOVE': true, // Notifications about connection and disconnection from the network 'EV_NETWORK_DISCONNECT': true, diff --git a/www/drive/inner.js b/www/drive/inner.js index 15a3299ea..2197ba563 100644 --- a/www/drive/inner.js +++ b/www/drive/inner.js @@ -2869,9 +2869,10 @@ define([ onRefresh.to = window.setTimeout(refresh, 500); } }; - /*proxy.on('change', [], function () { + + sframeChan.on('EV_DRIVE_CHANGE', function (data) { if (history.isHistoryMode) { return; } - var path = arguments[2]; + var path = data.path; if (path[0] !== 'drive') { return false; } path = path.slice(1); var cPath = currentPath.slice(); @@ -2884,26 +2885,21 @@ define([ } APP.resetTree(); return false; - }).on('remove', [], function () { + }); + sframeChan.on('EV_DRIVE_REMOVE', function (data) { if (history.isHistoryMode) { return; } - var path = arguments[1]; + var path = data.path; if (path[0] !== 'drive') { return false; } path = path.slice(1); var cPath = currentPath.slice(); if ((filesOp.isPathIn(cPath, ['hrefArray', TRASH]) && cPath[0] === path[0]) || (path.length >= cPath.length && filesOp.isSubpath(path, cPath))) { // Reload after a few to make sure all the change events have been received - onRefresh.to = window.setTimeout(refresh, 500); + onRefresh.refresh(); } APP.resetTree(); return false; - }).on('change', ['drive', 'migrate'], function () { - var path = arguments[2]; - var value = arguments[1]; - if (path[1] === "migrate" && value === 1) { - if (APP.onDisconnect) { APP.onDisconnect(true); } - } - });*/ + }); history.onEnterHistory = function (obj) { var files = obj.drive; diff --git a/www/drive/main.js b/www/drive/main.js index f8799d9eb..552a6a0c6 100644 --- a/www/drive/main.js +++ b/www/drive/main.js @@ -62,18 +62,21 @@ define([ Cryptpad.onNetworkReconnect.reg(function (data) { sframeChan.event('EV_NETWORK_RECONNECT', data); }); - Cryptpad.onDriveLog.reg(function (msg) { + Cryptpad.drive.onLog.reg(function (msg) { sframeChan.event('EV_DRIVE_LOG', msg); }); - // History? - }; - //Netflux.connect(NetConfig.getWebsocketURL()).then(function (network) { - SFCommonO.start({ - getSecrets: getSecrets, - //newNetwork: network, - noHash: true, - addRpc: addRpc + Cryptpad.drive.onChange.reg(function (data) { + sframeChan.event('EV_DRIVE_CHANGE', data); + }); + Cryptpad.drive.onRemove.reg(function (data) { + sframeChan.event('EV_DRIVE_REMOVE', data); }); - //}, function (err) { console.error(err); }); + }; + SFCommonO.start({ + getSecrets: getSecrets, + noHash: true, + driveEvents: true, + addRpc: addRpc + }); }); }); diff --git a/www/drive/tests.js b/www/drive/tests.js index 7132fd8a7..a5fec1145 100644 --- a/www/drive/tests.js +++ b/www/drive/tests.js @@ -362,7 +362,7 @@ define([ return cb(); } var path; - fo.addFolder(["root", "Folder2"], "subsub", function (e, o) { path = o.newPath; }); + fo.addFolder(["root", "Folder2"], "subsub", function (o) { path = o.newPath; }); if (!files.root.Folder2.subsub || path.length !== 3) { console.log("DRIVE operations: add folder"); return cb();