From 464eaee49a99b1c3eca40560a5ac8adc1760bc58 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 29 Jan 2020 18:38:13 +0100 Subject: [PATCH] Restore full hash when safe hash is deleted from the drive --- www/common/cryptpad-common.js | 4 +++- www/common/outer/async-store.js | 23 +++++++++++++++++++++++ www/common/outer/team.js | 6 ++++++ www/common/proxy-manager.js | 14 +++++++++++++- www/common/sframe-common-outer.js | 11 ++++++++++- 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 1f612bc84..0aa75f561 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -845,6 +845,7 @@ define([ pad.onConnectEvent = Util.mkEvent(); pad.onErrorEvent = Util.mkEvent(); pad.onMetadataEvent = Util.mkEvent(); + pad.onChannelDeleted = Util.mkEvent(); pad.requestAccess = function (data, cb) { postMessage("REQUEST_PAD_ACCESS", data, cb); @@ -1753,6 +1754,7 @@ define([ PAD_CONNECT: common.padRpc.onConnectEvent.fire, PAD_ERROR: common.padRpc.onErrorEvent.fire, PAD_METADATA: common.padRpc.onMetadataEvent.fire, + CHANNEL_DELETED: common.padRpc.onChannelDeleted.fire, // Drive DRIVE_LOG: common.drive.onLog.fire, DRIVE_CHANGE: common.drive.onChange.fire, @@ -1896,7 +1898,7 @@ define([ anonHash: LocalStore.getFSHash(), localToken: tryParsing(localStorage.getItem(Constants.tokenKey)), // TODO move this to LocalStore ? language: common.getLanguage(), - driveEvents: rdyCfg.driveEvents // Boolean + driveEvents: true //rdyCfg.driveEvents // Boolean }; // if a pad is created from a file if (sessionStorage[Constants.newPadFileData]) { diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index a06f593e7..bca625598 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -1232,6 +1232,23 @@ define([ cb(res || viewRes || {}); }; + // Hidden hash: if a pad is deleted, we may have to switch back to full hash + // in some tabs + Store.checkDeletedPad = function (channel) { + if (!channel) { return; } + + // Check if the pad is still stored in one of our drives + Store.getPadDataFromChannel(null, { + channel: channel, + isFile: true // we don't care if it's view or edit + }, function (res) { + // If it is stored, abort + if (Object.keys(res).length) { return; } + // Otherwise, tell all the tabs that this channel was deleted and give them the hrefs + broadcast([], "CHANNEL_DELETED", channel); + }); + }; + // Messaging (manage friends from the userlist) Store.answerFriendRequest = function (clientId, obj, cb) { var value = obj.value; @@ -2127,6 +2144,12 @@ define([ } } } + if (o && !n && Array.isArray(p) && (p[0] === UserObject.FILES_DATA || + (p[0] === 'drive' && p[1] === UserObject.FILES_DATA))) { + setTimeout(function () { + Store.checkDeletedPad(o && o.channel); + }); + } sendDriveEvent('DRIVE_CHANGE', { id: fId, old: o, diff --git a/www/common/outer/team.js b/www/common/outer/team.js index 759d08817..ce79a800f 100644 --- a/www/common/outer/team.js +++ b/www/common/outer/team.js @@ -93,6 +93,12 @@ define([ } } } + if (o && !n && Array.isArray(p) && (p[0] === UserObject.FILES_DATA || + (p[0] === 'drive' && p[1] === UserObject.FILES_DATA))) { + setTimeout(function () { + ctx.Store.checkDeletedPad(o && o.channel); + }); + } team.sendEvent('DRIVE_CHANGE', { id: fId, old: o, diff --git a/www/common/proxy-manager.js b/www/common/proxy-manager.js index c8c86cd1e..67ce14816 100644 --- a/www/common/proxy-manager.js +++ b/www/common/proxy-manager.js @@ -771,6 +771,9 @@ define([ toUnpin.forEach(function (chan) { if (toKeep.indexOf(chan) === -1) { unpinList.push(chan); + + // Check if need need to restore a full hash (hidden hash deleted from drive) + Env.Store.checkDeletedPad(chan); } }); @@ -783,7 +786,16 @@ define([ }; // Empty the trash (main drive only) var _emptyTrash = function (Env, data, cb) { - Env.user.userObject.emptyTrash(cb); + Env.user.userObject.emptyTrash(function (err, toClean) { + cb(); + + // Check if need need to restore a full hash (hidden hash deleted from drive) + if (!Array.isArray(toClean)) { return; } + var toCheck = Util.deduplicateString(toClean); + toCheck.forEach(function (chan) { + Env.Store.checkDeletedPad(chan); + }); + }); }; // Rename files or folders var _rename = function (Env, data, cb) { diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 1d4233ccc..21995c006 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -1324,7 +1324,16 @@ define([ } } catch (e) {} - + // If our channel was deleted from all of our drives, sitch back to full hash + // in the address bar + Cryptpad.padRpc.onChannelDeleted.reg(function (channel) { + if (channel !== secret.channel) { return; } + var ohc = window.onhashchange; + window.onhashchange = function () {}; + window.location.href = currentPad.href; + window.onhashchange = ohc; + ohc({reset: true}); + }); // Join the netflux channel var rtStarted = false;