From c872dd1578549ec32b7088469ec20544f1737350 Mon Sep 17 00:00:00 2001 From: JoaoSRaposo Date: Mon, 9 Dec 2024 21:46:53 +0000 Subject: [PATCH] Feature/#1058 unhide (#94) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Hide action to not hiden files, unhide action to hidden files Unhide action implemented Change UI of hidden files +- * Fixing mistake * The perfect solution would be: - After hide or unhide action - reload view similiar when we toggle the show_hiden files option on config. Another solution would be: - After hide or unhide action - reload if we are with show_hiden folder file config on. What I could do: - After hide, I don't reload the screen, assuming that we are with the show_hiden folder config off - After unhide action - I reload the screen, assuming that we are with the show_hiden folder config on * Update lib/Sabre/PostPlugin.php Co-authored-by: Henry Bergström --------- Co-authored-by: Henry Bergström --- lib/Sabre/PostPlugin.php | 19 ++++++ src/fileactions.js | 123 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 134 insertions(+), 8 deletions(-) diff --git a/lib/Sabre/PostPlugin.php b/lib/Sabre/PostPlugin.php index dc0e9a5..5a4b9c3 100644 --- a/lib/Sabre/PostPlugin.php +++ b/lib/Sabre/PostPlugin.php @@ -53,6 +53,25 @@ public function httpPost(RequestInterface $request, ResponseInterface $response) return false; } + else if ($action === 'unhide') { + $path = $request->getPath(); + $node = $this->server->tree->getNodeForPath($path); + if (($node instanceof \OCA\DAV\Connector\Sabre\File)) { + $storageid = $node->getFileInfo()->getStorage()->getCache()->getNumericStorageId(); + $filename = $node->getFileInfo()->getinternalPath(); + } + if (($node instanceof \OCA\DAV\Connector\Sabre\Directory)) { + $storageid = $node->getFileInfo()->getStorage()->getCache()->getNumericStorageId(); + $filename = $node->getFileInfo()->getinternalPath(); + } + + $this->EthswarmService->setVisiblity($filename, $storageid, 1); + + $response->setStatus(200); + $response->setHeader('Content-Length', '0'); + + return false; + } // No Hejbit-action, allow other plugins to handle the request else if ($action === null) { return true; diff --git a/src/fileactions.js b/src/fileactions.js index edb5b8f..3c16929 100644 --- a/src/fileactions.js +++ b/src/fileactions.js @@ -36,6 +36,7 @@ Batch option const queue = new PQueue({ concurrency: 5 }); import { emit,subscribe } from '@nextcloud/event-bus'; import { FileAction, registerDavProperty, registerFileAction, FileType } from "@nextcloud/files"; import HideSource from "@material-design-icons/svg/filled/hide_source.svg"; +import UnhideSource from "@material-design-icons/svg/filled/settings_backup_restore.svg"; import SwarmSvg from "../img/swarm-logo.svg"; import HejBitSvg from "../img/hejbit-logo.svg"; import axios from '@nextcloud/axios'; @@ -278,11 +279,14 @@ const actionDataHideFile ={ if (files.length !== 1) // We don't support batch actions return false; const attrs = files[0].attributes["ethswarm-node"]; + const hidden = files[0].attributes.hidden; if (attrs === undefined) return false; else if (attrs === "") return false; + if (hidden) + return false; return attrs; }, @@ -290,7 +294,7 @@ const actionDataHideFile ={ let message = ''; if (node.type === FileType.File) { message = t('files_external_ethswarm', 'The file will be set to hide on the folder view. The file will continue to exist on the Swarm network.'); - }else if (node.type === FileType.Folder) { + } else if (node.type === FileType.Folder) { message = t('files_external_ethswarm', 'The folder will be set to hide on the folder view. All the files inside the folder will continue to exist on the Swarm network.'); } alert(message); @@ -303,19 +307,21 @@ const actionDataHideFile ={ } }); - // Let's delete even if it's moved to the trashbin - // since it has been removed from the current view - // and changing the view will trigger a reload anyway. - emit('files:node:deleted', node); - return true; + // The right event is emit('files:node:updated', node); + // it triggers the file:list:update, but unfortunately that doesn't + // a reevaluation of the enable funtion of the fileactions. + // To improve UX we should reload only if show_hidden is true + emit('files:node:deleted', node); + // window.location.reload(); + return true; + } catch (error) { - console.log('Error while deleting a file', { error, source: node.source, node }); + console.log('Error while hidding a file', { error, source: node.source, node }); // TODO: update to this? logger.error('Error while deleting a file', { error, source: node.source, node }); return false; } }, /* TODO: Batch option - async execBatch(nodes, view, dir) { // Map each node to a promise that resolves with the result of exec(node) const promises = nodes.map(node => { // Create a promise that resolves with the result of exec(node) @@ -339,6 +345,107 @@ const AddHideAction = new FileAction(actionDataHideFile); registerFileAction(AddHideAction); + + +const actionDataUnhideFile ={ + id: 'unhideFile', + displayName(nodes, view) { + /** + * If we're only selecting files, use proper wording + */ + if (isAllFiles(nodes)) { + if (nodes.length === 1) { + return t('files_external_ethswarm', 'Unhide'); + } + return t('files_external_ethswarm', 'Unhide'); + } + /** + * If we're only selecting folders, use proper wording + */ + if (isAllFolders(nodes)) { + if (nodes.length === 1) { + return t('files_external_ethswarm', 'Unhide'); + } + return t('files_external_ethswarm', 'Unhide'); + } + return t('files_external_ethswarm', 'Unhide'); + }, + iconSvgInline: (nodes) => { + return Buffer.from(UnhideSource.split(",")[1], 'base64'); + }, + inline(file, view) { + return true; + }, + + enabled(files, view) { + if (files.length !== 1) // We don't support batch actions + return false; + const attrs = files[0].attributes["ethswarm-node"]; + const hidden = files[0].attributes.hidden; + + if (attrs === undefined) + return false; + else if (attrs === "") + return false; + if (!hidden) + return false; + + return attrs; + }, + async exec(node, view, dir) { + try { + await axios({ + method: 'post', + url: node.encodedSource, + headers: { + 'Hejbit-Action': 'unhide' + } + }); + + + // the right event is emit('files:node:updated', node); + // it triggers the file:list:update, but unfortunately that doesn't + // a reevaluation of the enable funtion of the fileactions + emit('files:node:deleted', node); + window.location.reload(); + + + return true; + } + catch (error) { + console.log('Error while unhidding a file', { error, source: node.source, node }); + // TODO: update to this? logger.error('Error while deleting a file', { error, source: node.source, node }); + return false; + } + }, /* TODO: Batch option + async execBatch(nodes, view, dir) { + // Map each node to a promise that resolves with the result of exec(node) + const promises = nodes.map(node => { + // Create a promise that resolves with the result of exec(node) + const promise = new Promise(resolve => { + queue.add(async () => { + const result = await this.exec(node, view, dir); + resolve(result !== null ? result : false); + }); + }); + return promise; + }); + return Promise.all(promises); + }, */ + + execBatch(nodes, view) { + return Promise.all(nodes.map(node => this.exec(node, view))); + }, + order: 150, +}; + +const AddUnhideAction = new FileAction(actionDataUnhideFile); + +registerFileAction(AddUnhideAction); + + + + let previousPathHasSwarm = false; subscribe('files:list:updated', (data) => {