From 4e4d01a471280194d6d73564ee4a5f2a9942a430 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 8 Oct 2019 17:54:52 +0200 Subject: [PATCH] Improve getStrongerHash to also look inside shared folders --- www/common/common-hash.js | 30 ------------------------------ www/common/cryptpad-common.js | 9 ++++++--- www/common/outer/async-store.js | 16 ++++++---------- www/common/proxy-manager.js | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 43 deletions(-) diff --git a/www/common/common-hash.js b/www/common/common-hash.js index f8b8d5f62..98330367f 100644 --- a/www/common/common-hash.js +++ b/www/common/common-hash.js @@ -420,36 +420,6 @@ Version 1 }; // STORAGE - Hash.findStronger = function (href, channel, recents) { - var parsed = parsePadUrl(href); - if (!parsed.hash) { return false; } - var parsedHash = parsed.hashData; - - // We can't have a stronger hash if we're already in edit mode - if (!parsedHash || parsedHash.mode === 'edit') { return; } - - // We don't have stronger/weaker versions of files or users - if (parsedHash.type !== 'pad') { return; } - - var stronger; - Object.keys(recents).some(function (id) { - var pad = recents[id]; - - // Not the same channel? reject - if (channel !== pad.channel) { return; } - - // If this pad doesn't have an edit link, it can't be stronger - // XXX encrypted href - if (!pad.href || !pad.roHref) { return; } - - // This is a pad with an EDIT href and using the same channel as our target - // ==> it is stronger - stronger = pad; - return true; - }); - return stronger; - }; - Hash.hrefToHexChannelId = function (href, password) { var parsed = Hash.parsePadUrl(href); if (!parsed || !parsed.hash) { return; } diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 7f16fa860..79dc781b3 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -1200,6 +1200,11 @@ define([ if (!parsed.type || !parsed.hashData) { return void cb('E_INVALID_HREF'); } hashes = Hash.getHashes(secret); + // If the current href is an edit one, return the existing hashes + var parsedHash = parsed.hashData; + if (!parsedHash || parsedHash.mode === 'edit') { return void cb(null, hashes); } + if (parsedHash.type !== 'pad') { return void cb(null, hashes); } + if (secret.version === 0) { // It means we're using an old hash hashes.editHash = window.location.hash.slice(1); @@ -1212,9 +1217,7 @@ define([ } postMessage("GET_STRONGER_HASH", { - href: window.location.href, - channel: secret.channel, - password: secret.password + channel: secret.channel }, function (hash) { if (hash) { hashes.editHash = hash; } cb(null, hashes); diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index f808c1205..b88463989 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -1265,21 +1265,17 @@ define([ // Get hashes for the share button // If we can find a stronger hash - Store.getStrongerHash = function (clientId, data, cb) { - var found = getAllStores().some(function (s) { - var allPads = Util.find(s.proxy, ['drive', 'filesData']) || {}; + Store.getStrongerHash = function (clientId, data, _cb) { + var cb = Util.once(_cb); - // If we have a stronger version in drive, add it and add a redirect button - var stronger = Hash.findStronger(data.href, data.channel, allPads); + var found = getAllStores().some(function (s) { + var stronger = s.manager.getEditHash(data.channel); if (stronger) { - var parsed2 = Hash.parsePadUrl(stronger.href); - cb(parsed2.hash); + cb(stronger); return true; } }); - if (!found) { - cb(); - } + if (!found) { cb(); } }; // Universal diff --git a/www/common/proxy-manager.js b/www/common/proxy-manager.js index 77c466db7..d7cc0a0d4 100644 --- a/www/common/proxy-manager.js +++ b/www/common/proxy-manager.js @@ -297,6 +297,21 @@ define([ return data; }; + var getEditHash = function (Env, channel) { + var res = findChannel(Env, channel); + var stronger; + res.some(function (obj) { + if (!obj || !obj.data || !obj.data.href) { return; } + var parsed = Hash.parsePadUrl(obj.data.href); + var parsedHash = parsed.hashData; + if (!parsedHash || parsedHash.mode === 'view') { return; } + // We've found an edit hash! + stronger = parsed.hash; + return true; + }); + return stronger; + }; + /* Drive RPC */ @@ -961,6 +976,7 @@ define([ // Tools findChannel: callWithEnv(findChannel), findHref: callWithEnv(findHref), + getEditHash: callWithEnv(getEditHash), user: Env.user, folders: Env.folders };