From fc91cf606bd347d3bd2ec63f3f865a83890a5e65 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 16 Jul 2018 18:05:23 +0200 Subject: [PATCH] Disable moveToTrash button for pads in shared folders --- www/common/common-ui-elements.js | 28 +++++++++++++++++++--------- www/common/cryptpad-common.js | 7 +++++++ www/common/outer/async-store.js | 12 +++++++++++- www/common/outer/store-rpc.js | 1 + www/common/proxy-manager.js | 13 +++++++++---- www/common/sframe-common-outer.js | 8 +++++++- www/common/sframe-protocol.js | 4 ++++ www/common/userObject.js | 1 - www/drive/inner.js | 1 - 9 files changed, 58 insertions(+), 17 deletions(-) diff --git a/www/common/common-ui-elements.js b/www/common/common-ui-elements.js index ab7d304f3..5d2f15f40 100644 --- a/www/common/common-ui-elements.js +++ b/www/common/common-ui-elements.js @@ -679,17 +679,27 @@ define([ button .click(common.prepareFeedback(type)) .click(function() { - var msg = common.isLoggedIn() ? Messages.forgetPrompt : Messages.fm_removePermanentlyDialog; - UI.confirm(msg, function (yes) { - if (!yes) { return; } - sframeChan.query('Q_MOVE_TO_TRASH', null, function (err) { - if (err) { return void callback(err); } - var cMsg = common.isLoggedIn() ? Messages.movedToTrash : Messages.deleted; - var msg = common.fixLinks($('
').html(cMsg)); - UI.alert(msg); - callback(); + sframeChan.query('Q_IS_ONLY_IN_SHARED_FOLDER', null, function (err, res) { + if (err || res.error) { return void console.log(err || res.error); } + var msg = Messages.forgetPrompt; + if (res) { + UI.alert("WIP: This pad is only in a shared folder. You can't move it to the trash. You can use your CryptDrive if you want to delete it from the folder."); // XXX return; + } else if (!common.isLoggedIn()) { + msg = Messages.fm_removePermanentlyDialog; + } + UI.confirm(msg, function (yes) { + if (!yes) { return; } + sframeChan.query('Q_MOVE_TO_TRASH', null, function (err) { + if (err) { return void callback(err); } + var cMsg = common.isLoggedIn() ? Messages.movedToTrash : Messages.deleted; + var msg = common.fixLinks($('
').html(cMsg)); + UI.alert(msg); + callback(); + return; + }); }); + }); }); break; diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 3011618cd..11d5bda10 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -311,6 +311,13 @@ define([ }); }; + common.isOnlyInSharedFolder = function (data, cb) { + postMessage("IS_ONLY_IN_SHARED_FOLDER", data, function (obj) { + if (obj && obj.error) { return void cb(obj.error); } + cb(null, obj); + }); + }; + common.setDisplayName = function (value, cb) { postMessage("SET_DISPLAY_NAME", value, cb); }; diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index c009459ab..75e376099 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -691,8 +691,18 @@ define([ }; // Pads + Store.isOnlyInSharedFolder = function (clientId, channel, cb) { + var res = store.manager.findChannel(channel); + + // A pad is only in a shared worker if: + // 1. this pad is in at least one proxy + // 2. no proxy containing this pad is the main drive + return cb (res.length && !res.some(function (obj) { + // Main drive doesn't have an fId (folder ID) + return !obj.fId; + })); + }; Store.moveToTrash = function (clientId, data, cb) { - // XXX move a pad from a shared folder to the trash? var href = Hash.getRelativeHref(data.href); store.userObject.forget(href); sendDriveEvent('DRIVE_CHANGE', { diff --git a/www/common/outer/store-rpc.js b/www/common/outer/store-rpc.js index 96c392f2a..cbecfe684 100644 --- a/www/common/outer/store-rpc.js +++ b/www/common/outer/store-rpc.js @@ -41,6 +41,7 @@ define([ MOVE_TO_TRASH: Store.moveToTrash, RESET_DRIVE: Store.resetDrive, GET_METADATA: Store.getMetadata, + IS_ONLY_IN_SHARED_FOLDER: Store.isOnlyInSharedFolder, SET_DISPLAY_NAME: Store.setDisplayName, SET_PAD_ATTRIBUTE: Store.setPadAttribute, GET_PAD_ATTRIBUTE: Store.getPadAttribute, diff --git a/www/common/proxy-manager.js b/www/common/proxy-manager.js index d943ffc22..92ee6097a 100644 --- a/www/common/proxy-manager.js +++ b/www/common/proxy-manager.js @@ -84,6 +84,7 @@ define([ Object.keys(Env.folders).forEach(function (fId) { Env.folders[fId].userObject.findChannels([channel]).forEach(function (id) { ret.push({ + fId: fId, data: Env.folders[fId].userObject.getFileData(id), userObject: Env.folders[fId].userObject }); @@ -95,12 +96,15 @@ define([ var findHref = function (Env, href) { var ret = []; var id = Env.user.userObject.getIdFromHref(href); - ret.push({ - data: Env.user.userObject.getFileData(id), - userObject: Env.user.userObject - }); + if (id) { + ret.push({ + data: Env.user.userObject.getFileData(id), + userObject: Env.user.userObject + }); + } Object.keys(Env.folders).forEach(function (fId) { var id = Env.folders[fId].userObject.getIdFromHref(href); + if (!id) { return; } ret.push({ fId: fId, data: Env.folders[fId].userObject.getFileData(id), @@ -717,6 +721,7 @@ define([ addPad: callWithEnv(addPad), // Tools findChannel: callWithEnv(findChannel), + findHref: callWithEnv(findHref), user: Env.user, folders: Env.folders }; diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 8b30aa2e2..7dab590f2 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -266,7 +266,7 @@ define([ isDeleted: isNewFile && window.location.hash.length > 0, forceCreationScreen: forceCreationScreen, password: password, - channel: secret.channel + channel: secret.channel, }; for (var k in additionalPriv) { metaObj.priv[k] = additionalPriv[k]; } @@ -484,6 +484,12 @@ define([ cb(); }); + sframeChan.on('Q_IS_ONLY_IN_SHARED_FOLDER', function (data, cb) { + Cryptpad.isOnlyInSharedFolder(secret.channel, function (err, t) { + if (err) { return void cb({error: err}); } + cb(t); + }); + }); // Present mode URL sframeChan.on('Q_PRESENT_URL_GET_VALUE', function (data, cb) { diff --git a/www/common/sframe-protocol.js b/www/common/sframe-protocol.js index cc41dcbf3..91e5e2215 100644 --- a/www/common/sframe-protocol.js +++ b/www/common/sframe-protocol.js @@ -111,6 +111,10 @@ define({ 'Q_GET_PAD_ATTRIBUTE': true, 'Q_SET_PAD_ATTRIBUTE': true, + // Check if a pad is only in a shared folder or (also) in the main drive. + // This allows us to change the behavior of some buttons (trash icon...) + 'Q_IS_ONLY_IN_SHARED_FOLDER': true, + // Open/close the File picker (sent from the iframe to the outside) 'EV_FILE_PICKER_OPEN': true, 'EV_FILE_PICKER_CLOSE': true, diff --git a/www/common/userObject.js b/www/common/userObject.js index 9d3cc738a..6c99d4ae9 100644 --- a/www/common/userObject.js +++ b/www/common/userObject.js @@ -308,7 +308,6 @@ define([ result = id; return true; } - return; }); return result; }; diff --git a/www/drive/inner.js b/www/drive/inner.js index 4de5ef4d6..eae9f1e28 100644 --- a/www/drive/inner.js +++ b/www/drive/inner.js @@ -854,7 +854,6 @@ define([ hide.push('properties'); } // If we're not in the trash nor in a shared folder, hide "remove" - var el = manager.find(path); if (!manager.isInSharedFolder(path) && !$element.is('.cp-app-drive-element-sharedf')) { hide.push('removesf');