From 425ac8ea57bb30a9c502b69494c50ef105f97f22 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 29 Jun 2018 18:16:04 +0200 Subject: [PATCH] Migration for read-only links + fix issues with read-only pads --- www/common/common-interface.js | 2 +- www/common/common-ui-elements.js | 24 ++------------ www/common/mergeDrive.js | 1 + www/common/migrate-user-object.js | 52 +++++++++++++++++++++++++++++-- www/common/outer/async-store.js | 10 +++--- www/common/outer/userObject.js | 29 +++++------------ www/common/userObject.js | 7 ----- www/drive/inner.js | 50 ++++++++++------------------- www/drive/tests.js | 8 ++--- 9 files changed, 85 insertions(+), 98 deletions(-) diff --git a/www/common/common-interface.js b/www/common/common-interface.js index 974490796..5e5c75176 100644 --- a/www/common/common-interface.js +++ b/www/common/common-interface.js @@ -665,7 +665,7 @@ define([ // Update the current state loading.driveState = data.state; data.progress = data.progress || 100; - data.msg = Messages['loading_drive_'+data.state] || ''; + data.msg = Messages['loading_drive_'+ Math.floor(data.state)] || ''; $progress.html(data.msg); if (data.progress) { $progress.append(h('div.cp-loading-progress-bar', [ diff --git a/www/common/common-ui-elements.js b/www/common/common-ui-elements.js index b35bebdfd..3d057ed04 100644 --- a/www/common/common-ui-elements.js +++ b/www/common/common-ui-elements.js @@ -74,32 +74,12 @@ define([ })); }).nThen(function (waitFor) { var base = common.getMetadataMgr().getPrivateData().origin; - /* XXX - common.getPadAttribute('href', waitFor(function (err, val) { - var base = common.getMetadataMgr().getPrivateData().origin; - - var parsed = Hash.parsePadUrl(val); - if (parsed.hashData.mode === "view") { - data.roHref = base + val; - return; - } - - // We're not in a read-only pad - data.href = base + val; - - // Get Read-only href - if (parsed.hashData.type !== "pad") { return; } - var i = data.href.indexOf('#') + 1; - var hBase = data.href.slice(0, i); - var hrefsecret = Hash.getSecrets(parsed.type, parsed.hash, data.password); - if (!hrefsecret.keys) { return; } - var viewHash = Hash.getViewHashFromKeys(hrefsecret); - data.roHref = hBase + viewHash; - }));*/ common.getPadAttribute('href', waitFor(function (err, val) { + if (!val) { return; } data.href = base + val; })); common.getPadAttribute('roHref', waitFor(function (err, val) { + if (!val) { return; } data.roHref = base + val; })); common.getPadAttribute('channel', waitFor(function (err, val) { diff --git a/www/common/mergeDrive.js b/www/common/mergeDrive.js index bcc30666e..020861481 100644 --- a/www/common/mergeDrive.js +++ b/www/common/mergeDrive.js @@ -119,6 +119,7 @@ define([ }); oldFiles.forEach(function (id) { var href = oldRecentPads[id].href || oldRecentPads[id].roHref; + var isRo = href === oldRecentPads[id].roHref; // Do not migrate a pad if we already have it, it would create a duplicate in the drive if (newHrefs.indexOf(href) !== -1) { return; } // If we have a stronger version, do not add the current href diff --git a/www/common/migrate-user-object.js b/www/common/migrate-user-object.js index fb69fb20b..2f1ea5e88 100644 --- a/www/common/migrate-user-object.js +++ b/www/common/migrate-user-object.js @@ -123,12 +123,58 @@ define([ })); }); }); - n.nThen(waitFor()); + n.nThen(waitFor(function () { + Feedback.send('Migrate-6', true); + userObject.version = version = 6; + })); }; if (version < 6) { addChannelId(); - Feedback.send('Migrate-6', true); - userObject.version = version = 6; + } + }).nThen(function (waitFor) { + var addRoHref = function () { + var data = userObject.drive.filesData; + var el, parsed; + var n = nThen(function () {}); + var padsLength = Object.keys(data).length; + Object.keys(data).forEach(function (k, i) { + n = n.nThen(function (w) { + setTimeout(w(function () { + el = data[k]; + if (!el.href || (el.roHref && false)) { + // Already migrated + return void progress(7, Math.round(100*i/padsLength)); + } + parsed = Hash.parsePadUrl(el.href); + if (parsed.hashData.type !== "pad") { + // No read-only mode for files + return void progress(7, Math.round(100*i/padsLength)); + } + if (parsed.hashData.mode === "view") { + // This is a read-only pad in our drive + el.roHref = el.href; + delete el.href; + console.log('Move href to roHref in filesData ', el.roHref); + } else { + var secret = Hash.getSecrets(parsed.type, parsed.hash, el.password); + var hash = Hash.getViewHashFromKeys(secret); + if (hash) { + // Version 0 won't have a view hash available + el.roHref = '/' + parsed.type + '/#' + hash; + console.log('Adding missing roHref in filesData ', el.href); + } + } + progress(6, Math.round(100*i/padsLength)); + })); + }); + }); + n.nThen(waitFor(function () { + Feedback.send('Migrate-7', true); + userObject.version = version = 7; + })); + }; + if (version < 7) { + addRoHref(); } /*}).nThen(function (waitFor) { // Test progress bar in the loading screen diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index 3837b338a..7981cec53 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -441,8 +441,10 @@ define([ if (!data.href && !data.roHref) { return void cb({error:'NO_HREF'}); } if (!data.roHref) { var parsed = Hash.parsePadUrl(data.href); - var secret = Hash.getSecrets(parsed.type, parsed.hash, data.password); - data.roHref = '/' + parsed.type + '/#' + Hash.getViewHashFromKeys(secret); + if (parsed.hashData.type === "pad") { + var secret = Hash.getSecrets(parsed.type, parsed.hash, data.password); + data.roHref = '/' + parsed.type + '/#' + Hash.getViewHashFromKeys(secret); + } } var pad = makePad(data.href, data.roHref, data.title); if (data.owners) { pad.owners = data.owners; } @@ -742,7 +744,7 @@ define([ // Edit > Edit (present) > View > View (present) for (var id in allPads) { var pad = allPads[id]; - if (!pad.href || !pad.roHref) { continue; } + if (!pad.href && !pad.roHref) { continue; } var p2 = Hash.parsePadUrl(pad.href || pad.roHref); var h2 = p2.hashData; @@ -1364,7 +1366,7 @@ define([ }).nThen(function (waitFor) { Migrate(proxy, waitFor(), function (version, progress) { postMessage(clientId, 'LOADING_DRIVE', { - state: 2, + state: (2 + (version / 10)), progress: progress }); }); diff --git a/www/common/outer/userObject.js b/www/common/outer/userObject.js index 630f8aae1..4bc0019c3 100644 --- a/www/common/outer/userObject.js +++ b/www/common/outer/userObject.js @@ -133,11 +133,6 @@ define([ if (!loggedIn && !config.testMode) { allFilesPaths.forEach(function (path) { var id = path[1]; - /* XXX - var el = exp.find(path); - if (!el) { return; } - var id = exp.getIdFromHref(el.href); - */ if (!id) { return; } spliceFileData(id); }); @@ -256,15 +251,6 @@ define([ }; // REPLACE - /* XXX - exp.replace = function (o, n) { - var idO = exp.getIdFromHref(o); - if (!idO || !exp.isFile(idO)) { return; } - var data = exp.getFileData(idO); - if (!data) { return; } - data.href = n; - }; - */ // If all the occurences of an href are in the trash, remove them and add the file in root. // This is use with setPadTitle when we open a stronger version of a deleted pad exp.restoreHref = function (href) { @@ -576,17 +562,18 @@ define([ } // If we have an edit link, check the view link - if (el.href) { - var fixRo = function () { + if (el.href && parsed.hashData.type === "pad") { + if (parsed.hashData.mode === "view") { + el.roHref = el.href; + delete el.href; + } else if (!el.roHref) { secret = Hash.getSecrets(parsed.type, parsed.hash, el.password); - el.roHref = '/' + parsed.type + '/#' + Hash.getViewHasFromKeys(secret); - }; - if (!el.roHref) { - fixRo(); + el.roHref = '/' + parsed.type + '/#' + Hash.getViewHashFromKeys(secret); } else { var parsed2 = Hash.parsePadUrl(el.roHref); if (!parsed2.hash || !parsed2.type) { - fixRo(); + secret = Hash.getSecrets(parsed.type, parsed.hash, el.password); + el.roHref = '/' + parsed.type + '/#' + Hash.getViewHashFromKeys(secret); } } } diff --git a/www/common/userObject.js b/www/common/userObject.js index 4d8a6a0a2..e19614363 100644 --- a/www/common/userObject.js +++ b/www/common/userObject.js @@ -79,13 +79,6 @@ define([ if (!isFile(element)) { return false; } var data = exp.getFileData(element); return Boolean(data.roHref && !data.href); - /* XXX - var parsed = Hash.parsePadUrl(data.href); - if (!parsed) { return false; } - var pHash = parsed.hashData; - if (!pHash || pHash.type !== "pad") { return; } - return pHash && pHash.mode === 'view'; - */ }; var isFolder = exp.isFolder = function (element) { diff --git a/www/drive/inner.js b/www/drive/inner.js index 9e538b1f4..fb6f56bf4 100644 --- a/www/drive/inner.js +++ b/www/drive/inner.js @@ -2691,22 +2691,6 @@ define([ return $div.html(); }; - /* XXX - var getReadOnlyUrl = APP.getRO = function (id) { - if (!filesOp.isFile(id)) { return; } - var data = filesOp.getFileData(id); - if (!data) { return; } - if (data.roHref) { return data.roHref; } - var parsed = Hash.parsePadUrl(data.href); - if (parsed.hashData.type !== "pad") { return; } - var i = data.href.indexOf('#') + 1; - var base = data.href.slice(0, i); - var hrefsecret = Hash.getSecrets(parsed.type, parsed.hash, data.password); - if (!hrefsecret.keys) { return; } - var viewHash = Hash.getViewHashFromKeys(hrefsecret); - return base + viewHash; - };*/ - // Disable middle click in the context menu to avoid opening /drive/inner.html# in new tabs $(window).click(function (e) { if (!e.target || !$(e.target).parents('.cp-dropdown-content').length) { return; } @@ -2723,20 +2707,14 @@ define([ //var ro = filesOp.isReadOnlyFile(el); var base = APP.origin; var data = JSON.parse(JSON.stringify(filesOp.getFileData(el))); - if (!data || !data.href) { return void cb('INVALID_FILE'); } - data.href = base + data.href; - data.roHref = base + data.roHref; - - /* XXX - var roUrl; - if (ro) { - data.roHref = data.href; - delete data.href; - } else { - roUrl = getReadOnlyUrl(el); - if (roUrl) { data.roHref = base + roUrl; } + if (!data || !(data.href || data.roHref)) { return void cb('INVALID_FILE'); } + + if (data.href) { + data.href = base + data.href; + } + if (data.roHref) { + data.roHref = base + data.roHref; } - */ UIElements.getProperties(common, data, cb); }; @@ -2810,11 +2788,15 @@ define([ else if ($(this).hasClass('cp-app-drive-context-openro')) { paths.forEach(function (p) { var el = filesOp.find(p.path); - if (filesOp.isPathIn(p.path, [FILES_DATA])) { el = el.href; } - if (!el || filesOp.isFolder(el)) { return; } - // var roUrl = getReadOnlyUrl(el); - openFile(el); - //, roUrl); XXX + var href; + if (filesOp.isPathIn(p.path, [FILES_DATA])) { + href = el.roHref; + } else { + if (!el || filesOp.isFolder(el)) { return; } + var data = filesOp.getFileData(el); + href = data.roHref; + } + openFile(null, href); }); } else if ($(this).hasClass('cp-app-drive-context-newfolder')) { diff --git a/www/drive/tests.js b/www/drive/tests.js index a5fec1145..67d986d86 100644 --- a/www/drive/tests.js +++ b/www/drive/tests.js @@ -237,7 +237,8 @@ define([ && typeof files.template[0] === "number" && typeof files.filesData[files.template[0]] === "object" && !files.filesData[files.template[0]].filename - && files.filesData[files.template[0]].href === href3 + && !files.filesData[files.template[0]].href + && files.filesData[files.template[0]].roHref === href3 && typeof fileId2 === "number" && typeof files.filesData[fileId2] === "object" && files.filesData[fileId2].filename === "Trash" @@ -392,11 +393,6 @@ define([ console.log("DRIVE operations: rename"); return cb(); } - fo.replace(href1, href2); - if (fo.getFileData(id1).href !== href2) { - console.log("DRIVE operations: replace"); - return cb(); - } cb(true); }, "DRIVE operations");