diff options
Diffstat (limited to 'apps/files')
-rw-r--r-- | apps/files/src/actions/deleteAction.spec.ts | 3 | ||||
-rw-r--r-- | apps/files/src/actions/downloadAction.spec.ts | 1 | ||||
-rw-r--r-- | apps/files/src/actions/editLocallyAction.spec.ts | 163 | ||||
-rw-r--r-- | apps/files/src/actions/editLocallyAction.ts | 74 | ||||
-rw-r--r-- | apps/files/src/actions/favoriteAction.spec.ts | 391 | ||||
-rw-r--r-- | apps/files/src/actions/favoriteAction.ts | 99 | ||||
-rw-r--r-- | apps/files/src/actions/renameAction.spec.ts | 111 | ||||
-rw-r--r-- | apps/files/src/actions/renameAction.ts | 51 | ||||
-rw-r--r-- | apps/files/src/actions/sidebarAction.spec.ts | 2 | ||||
-rw-r--r-- | apps/files/src/services/FileAction.ts | 2 |
10 files changed, 894 insertions, 3 deletions
diff --git a/apps/files/src/actions/deleteAction.spec.ts b/apps/files/src/actions/deleteAction.spec.ts index 1046fa6dad7..af7722008b6 100644 --- a/apps/files/src/actions/deleteAction.spec.ts +++ b/apps/files/src/actions/deleteAction.spec.ts @@ -25,8 +25,8 @@ import { File, Folder, Permission } from '@nextcloud/files' import { FileAction } from '../services/FileAction' import * as eventBus from '@nextcloud/event-bus' import axios from '@nextcloud/axios' -import type { Navigation } from '../services/Navigation' import logger from '../logger' +import type { Navigation } from '../services/Navigation' const view = { id: 'files', @@ -44,6 +44,7 @@ describe('Delete action conditions tests', () => { expect(action.id).toBe('delete') expect(action.displayName([], view)).toBe('Delete') expect(action.iconSvgInline([], view)).toBe('SvgMock') + expect(action.default).toBe(false) expect(action.order).toBe(100) }) diff --git a/apps/files/src/actions/downloadAction.spec.ts b/apps/files/src/actions/downloadAction.spec.ts index 5d75754c50c..a9b51b39510 100644 --- a/apps/files/src/actions/downloadAction.spec.ts +++ b/apps/files/src/actions/downloadAction.spec.ts @@ -39,6 +39,7 @@ describe('Download action conditions tests', () => { expect(action.id).toBe('download') expect(action.displayName([], view)).toBe('Download') expect(action.iconSvgInline([], view)).toBe('SvgMock') + expect(action.default).toBe(false) expect(action.order).toBe(30) }) }) diff --git a/apps/files/src/actions/editLocallyAction.spec.ts b/apps/files/src/actions/editLocallyAction.spec.ts new file mode 100644 index 00000000000..93315c5a016 --- /dev/null +++ b/apps/files/src/actions/editLocallyAction.spec.ts @@ -0,0 +1,163 @@ +/** + * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +import { action } from './editLocallyAction' +import { expect } from '@jest/globals' +import { File, Folder, Permission } from '@nextcloud/files' +import { FileAction } from '../services/FileAction' +import axios from '@nextcloud/axios' +import type { Navigation } from '../services/Navigation' +import ncDialogs from '@nextcloud/dialogs' + +const view = { + id: 'files', + name: 'Files', +} as Navigation + +describe('Edit locally action conditions tests', () => { + test('Default values', () => { + expect(action).toBeInstanceOf(FileAction) + expect(action.id).toBe('edit-locally') + expect(action.displayName([], view)).toBe('Edit locally') + expect(action.iconSvgInline([], view)).toBe('SvgMock') + expect(action.default).toBe(true) + expect(action.order).toBe(25) + }) +}) + +describe('Edit locally action enabled tests', () => { + test('Enabled for file with UPDATE permission', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(true) + }) + + test('Disabled for non-dav ressources', () => { + const file = new File({ + id: 1, + source: 'https://domain.com/data/foobar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(false) + }) + + test('Disabled if more than one node', () => { + const file1 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + }) + const file2 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file1, file2], view)).toBe(false) + }) + + test('Disabled for files', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(false) + }) + + test('Disabled without UPDATE permissions', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.READ, + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(false) + }) +}) + +describe('Edit locally action execute tests', () => { + test('Edit locally opens proper URL', async () => { + jest.spyOn(axios, 'post').mockImplementation(async () => ({ data: { ocs: { data: { token: 'foobar' } } } })) + jest.spyOn(ncDialogs, 'showError') + + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.UPDATE, + }) + + const exec = await action.exec(file, view, '/') + + // Silent action + expect(exec).toBe(null) + expect(axios.post).toBeCalledTimes(1) + expect(axios.post).toBeCalledWith('http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' }) + expect(ncDialogs.showError).toBeCalledTimes(0) + expect(window.location.href).toBe('nc://open/test@localhost/foobar.txt?token=foobar') + }) + + test('Edit locally fails and show error', async () => { + jest.spyOn(axios, 'post').mockImplementation(async () => ({})) + jest.spyOn(ncDialogs, 'showError') + + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.UPDATE, + }) + + const exec = await action.exec(file, view, '/') + + // Silent action + expect(exec).toBe(null) + expect(axios.post).toBeCalledTimes(1) + expect(axios.post).toBeCalledWith('http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' }) + expect(ncDialogs.showError).toBeCalledTimes(1) + expect(ncDialogs.showError).toBeCalledWith('Failed to redirect to client') + expect(window.location.href).toBe('http://localhost/') + }) +}) diff --git a/apps/files/src/actions/editLocallyAction.ts b/apps/files/src/actions/editLocallyAction.ts new file mode 100644 index 00000000000..ad7e805ec2e --- /dev/null +++ b/apps/files/src/actions/editLocallyAction.ts @@ -0,0 +1,74 @@ +/** + * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +import { encodePath } from '@nextcloud/paths' +import { Permission, type Node } from '@nextcloud/files' +import { translate as t } from '@nextcloud/l10n' +import axios from '@nextcloud/axios' +import DevicesSvg from '@mdi/svg/svg/devices.svg?raw' + +import { generateOcsUrl } from '@nextcloud/router' +import { getCurrentUser } from '@nextcloud/auth' +import { registerFileAction, FileAction } from '../services/FileAction' +import { showError } from '@nextcloud/dialogs' + +const openLocalClient = async function(path: string) { + const link = generateOcsUrl('apps/files/api/v1') + '/openlocaleditor?format=json' + + try { + const result = await axios.post(link, { path }) + const uid = getCurrentUser()?.uid + let url = `nc://open/${uid}@` + window.location.host + encodePath(path) + url += '?token=' + result.data.ocs.data.token + + window.location.href = url + } catch (error) { + showError(t('files', 'Failed to redirect to client')) + } +} + +export const action = new FileAction({ + id: 'edit-locally', + displayName: () => t('files', 'Edit locally'), + iconSvgInline: () => DevicesSvg, + + // Only works on single files + enabled(nodes: Node[]) { + // Only works on single node + if (nodes.length !== 1) { + return false + } + + return (nodes[0].permissions & Permission.UPDATE) !== 0 + }, + + async exec(node: Node) { + openLocalClient(node.path) + return null + }, + + default: true, + order: 25, +}) + +if (!/Android|iPhone|iPad|iPod/i.test(navigator.userAgent)) { + registerFileAction(action) +} diff --git a/apps/files/src/actions/favoriteAction.spec.ts b/apps/files/src/actions/favoriteAction.spec.ts new file mode 100644 index 00000000000..144c3a51dc8 --- /dev/null +++ b/apps/files/src/actions/favoriteAction.spec.ts @@ -0,0 +1,391 @@ +/** + * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +import * as favoriteAction from './favoriteAction' +import { action } from './favoriteAction' +import { expect } from '@jest/globals' +import { File, Folder, Permission } from '@nextcloud/files' +import { FileAction } from '../services/FileAction' +import * as eventBus from '@nextcloud/event-bus' +import axios from '@nextcloud/axios' +import type { Navigation } from '../services/Navigation' +import logger from '../logger' + +const view = { + id: 'files', + name: 'Files', +} as Navigation + +const favoriteView = { + id: 'favorites', + name: 'Favorites', +} as Navigation + +global.window.OC = { + TAG_FAVORITE: '_$!<Favorite>!$_', +} + +describe('Favorite action conditions tests', () => { + test('Default values', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action).toBeInstanceOf(FileAction) + expect(action.id).toBe('favorite') + expect(action.displayName([file], view)).toBe('Add to favorites') + expect(action.iconSvgInline([], view)).toBe('SvgMock') + expect(action.default).toBe(false) + expect(action.order).toBe(-50) + }) + + test('Display name is Remove from favorites if already in favorites', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + attributes: { + favorite: 1, + }, + }) + + expect(action.displayName([file], view)).toBe('Remove from favorites') + }) + + test('Display name for multiple state files', () => { + const file1 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + attributes: { + favorite: 1, + }, + }) + const file2 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + attributes: { + favorite: 0, + }, + }) + const file3 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + attributes: { + favorite: 1, + }, + }) + + expect(action.displayName([file1, file2, file3], view)).toBe('Add to favorites') + expect(action.displayName([file1, file2], view)).toBe('Add to favorites') + expect(action.displayName([file2, file3], view)).toBe('Add to favorites') + expect(action.displayName([file1, file3], view)).toBe('Remove from favorites') + }) +}) + +describe('Favorite action enabled tests', () => { + test('Enabled for dav file', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(true) + }) + + test('Disabled for non-dav ressources', () => { + const file = new File({ + id: 1, + source: 'https://domain.com/data/foobar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(false) + }) +}) + +describe('Favorite action execute tests', () => { + afterEach(() => { + jest.spyOn(axios, 'post').mockRestore() + }) + + test('Favorite triggers tag addition', async () => { + jest.spyOn(axios, 'post') + jest.spyOn(eventBus, 'emit') + + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + const exec = await action.exec(file, view, '/') + + expect(exec).toBe(true) + + // Check POST request + expect(axios.post).toBeCalledTimes(1) + expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: ['_$!<Favorite>!$_'] }) + + // Check node change propagation + expect(file.attributes.favorite).toBe(1) + expect(eventBus.emit).toBeCalledTimes(1) + expect(eventBus.emit).toBeCalledWith('files:favorites:added', file) + }) + + test('Favorite triggers tag removal', async () => { + jest.spyOn(axios, 'post') + jest.spyOn(eventBus, 'emit') + + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + attributes: { + favorite: 1, + }, + }) + + const exec = await action.exec(file, view, '/') + + expect(exec).toBe(true) + + // Check POST request + expect(axios.post).toBeCalledTimes(1) + expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] }) + + // Check node change propagation + expect(file.attributes.favorite).toBe(0) + expect(eventBus.emit).toBeCalledTimes(1) + expect(eventBus.emit).toBeCalledWith('files:favorites:removed', file) + }) + + test('Favorite triggers node removal if favorite view and root dir', async () => { + jest.spyOn(axios, 'post') + jest.spyOn(eventBus, 'emit') + + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + attributes: { + favorite: 1, + }, + }) + + const exec = await action.exec(file, favoriteView, '/') + + expect(exec).toBe(true) + + // Check POST request + expect(axios.post).toBeCalledTimes(1) + expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] }) + + // Check node change propagation + expect(file.attributes.favorite).toBe(0) + expect(eventBus.emit).toBeCalledTimes(2) + expect(eventBus.emit).toHaveBeenNthCalledWith(1, 'files:node:deleted', file) + expect(eventBus.emit).toHaveBeenNthCalledWith(2, 'files:favorites:removed', file) + }) + + test('Favorite does NOT triggers node removal if favorite view but NOT root dir', async () => { + jest.spyOn(axios, 'post') + jest.spyOn(eventBus, 'emit') + + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/Foo/Bar/foobar.txt', + root: '/files/admin', + owner: 'admin', + mime: 'text/plain', + attributes: { + favorite: 1, + }, + }) + + const exec = await action.exec(file, favoriteView, '/') + + expect(exec).toBe(true) + + // Check POST request + expect(axios.post).toBeCalledTimes(1) + expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/Foo/Bar/foobar.txt', { tags: [] }) + + // Check node change propagation + expect(file.attributes.favorite).toBe(0) + expect(eventBus.emit).toBeCalledTimes(1) + expect(eventBus.emit).toBeCalledWith('files:favorites:removed', file) + }) + + test('Favorite fails and show error', async () => { + const error = new Error('Mock error') + jest.spyOn(axios, 'post').mockImplementation(() => { throw new Error('Mock error') }) + jest.spyOn(logger, 'error').mockImplementation(() => jest.fn()) + + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + attributes: { + favorite: 0, + }, + }) + + const exec = await action.exec(file, view, '/') + + expect(exec).toBe(false) + + // Check POST request + expect(axios.post).toBeCalledTimes(1) + expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: ['_$!<Favorite>!$_'] }) + + // Check node change propagation + expect(logger.error).toBeCalledTimes(1) + expect(logger.error).toBeCalledWith('Error while adding a file to favourites', { error, source: file.source, node: file }) + expect(file.attributes.favorite).toBe(0) + expect(eventBus.emit).toBeCalledTimes(0) + }) + + test('Removing from favorites fails and show error', async () => { + const error = new Error('Mock error') + jest.spyOn(axios, 'post').mockImplementation(() => { throw error }) + jest.spyOn(logger, 'error').mockImplementation(() => jest.fn()) + + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + attributes: { + favorite: 1, + }, + }) + + const exec = await action.exec(file, view, '/') + + expect(exec).toBe(false) + + // Check POST request + expect(axios.post).toBeCalledTimes(1) + expect(axios.post).toBeCalledWith('/index.php/apps/files/api/v1/files/foobar.txt', { tags: [] }) + + // Check node change propagation + expect(logger.error).toBeCalledTimes(1) + expect(logger.error).toBeCalledWith('Error while removing a file from favourites', { error, source: file.source, node: file }) + expect(file.attributes.favorite).toBe(1) + expect(eventBus.emit).toBeCalledTimes(0) + }) +}) + +describe('Favorite action batch execute tests', () => { + test('Favorite action batch execute with mixed files', async () => { + jest.spyOn(favoriteAction, 'favoriteNode') + jest.spyOn(axios, 'post') + + const file1 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + attributes: { + favorite: 1, + }, + }) + const file2 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + attributes: { + favorite: 0, + }, + }) + + // Mixed states triggers favorite action + const exec = await action.execBatch!([file1, file2], view, '/') + expect(exec).toStrictEqual([true, true]) + expect([file1, file2].every(file => file.attributes.favorite === 1)).toBe(true) + + expect(favoriteAction.favoriteNode).toBeCalledTimes(2) + expect(axios.post).toBeCalledTimes(2) + expect(axios.post).toHaveBeenNthCalledWith(1, '/index.php/apps/files/api/v1/files/foo.txt', { tags: ['_$!<Favorite>!$_'] }) + expect(axios.post).toHaveBeenNthCalledWith(2, '/index.php/apps/files/api/v1/files/bar.txt', { tags: ['_$!<Favorite>!$_'] }) + }) + + test('Remove from favorite action batch execute with favorites only files', async () => { + jest.spyOn(favoriteAction, 'favoriteNode') + jest.spyOn(axios, 'post') + + const file1 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + attributes: { + favorite: 1, + }, + }) + const file2 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + attributes: { + favorite: 1, + }, + }) + + // Mixed states triggers favorite action + const exec = await action.execBatch!([file1, file2], view, '/') + expect(exec).toStrictEqual([true, true]) + expect([file1, file2].every(file => file.attributes.favorite === 0)).toBe(true) + + expect(favoriteAction.favoriteNode).toBeCalledTimes(2) + expect(axios.post).toBeCalledTimes(2) + expect(axios.post).toHaveBeenNthCalledWith(1, '/index.php/apps/files/api/v1/files/foo.txt', { tags: [] }) + expect(axios.post).toHaveBeenNthCalledWith(2, '/index.php/apps/files/api/v1/files/bar.txt', { tags: [] }) + }) +}) diff --git a/apps/files/src/actions/favoriteAction.ts b/apps/files/src/actions/favoriteAction.ts new file mode 100644 index 00000000000..5b9ba7f95e6 --- /dev/null +++ b/apps/files/src/actions/favoriteAction.ts @@ -0,0 +1,99 @@ +/** + * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +import { emit } from '@nextcloud/event-bus' +import { translate as t } from '@nextcloud/l10n' +import axios from '@nextcloud/axios' +import Star from '@mdi/svg/svg/star.svg?raw' +import type { Node } from '@nextcloud/files' + +import { generateUrl } from '@nextcloud/router' +import { registerFileAction, FileAction } from '../services/FileAction' +import logger from '../logger.js' +import type { Navigation } from '../services/Navigation' + +// If any of the nodes is not favorited, we display the favorite action. +const shouldFavorite = (nodes: Node[]): boolean => { + return nodes.some(node => node.attributes.favorite !== 1) +} + +export const favoriteNode = async (node: Node, view: Navigation, willFavorite: boolean): Promise<boolean> => { + try { + // TODO: migrate to webdav tags plugin + const url = generateUrl('/apps/files/api/v1/files') + node.path + await axios.post(url, { + tags: willFavorite + ? [window.OC.TAG_FAVORITE] + : [], + }) + + // Let's delete if we are in the favourites view + // AND if it is removed from the user favorites + // AND it's in the root of the favorites view + if (view.id === 'favorites' && !willFavorite && node.dirname === '/') { + emit('files:node:deleted', node) + } + + // Update the node webdav attribute + node.attributes.favorite = willFavorite ? 1 : 0 + + // Dispatch event to whoever is interested + if (willFavorite) { + emit('files:favorites:added', node) + } else { + emit('files:favorites:removed', node) + } + + return true + } catch (error) { + const action = willFavorite ? 'adding a file to favourites' : 'removing a file from favourites' + logger.error('Error while ' + action, { error, source: node.source, node }) + return false + } +} + +export const action = new FileAction({ + id: 'favorite', + displayName(nodes: Node[]) { + return shouldFavorite(nodes) + ? t('files', 'Add to favorites') + : t('files', 'Remove from favorites') + }, + iconSvgInline: () => Star, + + enabled(nodes: Node[]) { + // We can only favorite nodes within files + return !nodes.some(node => !node.root?.startsWith?.('/files')) + }, + + async exec(node: Node, view: Navigation) { + const willFavorite = shouldFavorite([node]) + return await favoriteNode(node, view, willFavorite) + }, + async execBatch(nodes: Node[], view: Navigation) { + const willFavorite = shouldFavorite(nodes) + return Promise.all(nodes.map(async node => await favoriteNode(node, view, willFavorite))) + }, + + order: -50, +}) + +registerFileAction(action) diff --git a/apps/files/src/actions/renameAction.spec.ts b/apps/files/src/actions/renameAction.spec.ts new file mode 100644 index 00000000000..ae2cfcec7eb --- /dev/null +++ b/apps/files/src/actions/renameAction.spec.ts @@ -0,0 +1,111 @@ +/** + * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +import { action } from './renameAction' +import { expect } from '@jest/globals' +import { File, Permission } from '@nextcloud/files' +import { FileAction } from '../services/FileAction' +import * as eventBus from '@nextcloud/event-bus' +import type { Navigation } from '../services/Navigation' + +const view = { + id: 'files', + name: 'Files', +} as Navigation + +describe('Rename action conditions tests', () => { + test('Default values', () => { + expect(action).toBeInstanceOf(FileAction) + expect(action.id).toBe('rename') + expect(action.displayName([], view)).toBe('Rename') + expect(action.iconSvgInline([], view)).toBe('SvgMock') + expect(action.default).toBe(false) + expect(action.order).toBe(10) + }) +}) + +describe('Rename action enabled tests', () => { + test('Enabled for node with UPDATE permission', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.UPDATE, + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(true) + }) + + test('Disabled for node without UPDATE permission', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.READ, + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file], view)).toBe(false) + }) + + test('Disabled if more than one node', () => { + window.OCA = { Files: { Sidebar: {} } } + + const file1 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt', + owner: 'admin', + mime: 'text/plain', + }) + const file2 = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + expect(action.enabled).toBeDefined() + expect(action.enabled!([file1, file2], view)).toBe(false) + }) +}) + +describe('Rename action exec tests', () => { + test('Rename', async () => { + jest.spyOn(eventBus, 'emit') + + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + }) + + const exec = await action.exec(file, view, '/') + + // Silent action + expect(exec).toBe(null) + expect(eventBus.emit).toBeCalledTimes(1) + expect(eventBus.emit).toHaveBeenCalledWith('files:node:rename', file) + }) +}) diff --git a/apps/files/src/actions/renameAction.ts b/apps/files/src/actions/renameAction.ts new file mode 100644 index 00000000000..02aaaa6d3b4 --- /dev/null +++ b/apps/files/src/actions/renameAction.ts @@ -0,0 +1,51 @@ +/** + * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +import { Permission, type Node } from '@nextcloud/files' +import { translate as t } from '@nextcloud/l10n' +import PencilSvg from '@mdi/svg/svg/pencil.svg?raw' + +import { emit } from '@nextcloud/event-bus' +import { registerFileAction, FileAction } from '../services/FileAction' + +export const ACTION_DETAILS = 'details' + +export const action = new FileAction({ + id: 'rename', + displayName: () => t('files', 'Rename'), + iconSvgInline: () => PencilSvg, + + enabled: (nodes: Node[]) => { + return nodes.length > 0 && nodes + .map(node => node.permissions) + .every(permission => (permission & Permission.UPDATE) !== 0) + }, + + async exec(node: Node) { + // Renaming is a built-in feature of the files app + emit('files:node:rename', node) + return null + }, + + order: 10, +}) + +registerFileAction(action) diff --git a/apps/files/src/actions/sidebarAction.spec.ts b/apps/files/src/actions/sidebarAction.spec.ts index cdf878459db..f6594090c53 100644 --- a/apps/files/src/actions/sidebarAction.spec.ts +++ b/apps/files/src/actions/sidebarAction.spec.ts @@ -42,7 +42,7 @@ describe('Open sidebar action conditions tests', () => { }) }) -describe('Open folder action enabled tests', () => { +describe('Open sidebar action enabled tests', () => { test('Enabled for ressources within user root folder', () => { window.OCA = { Files: { Sidebar: {} } } diff --git a/apps/files/src/services/FileAction.ts b/apps/files/src/services/FileAction.ts index 6b2e3750a24..03cc957e1e5 100644 --- a/apps/files/src/services/FileAction.ts +++ b/apps/files/src/services/FileAction.ts @@ -110,7 +110,7 @@ export class FileAction { } get default() { - return this._action.default + return this._action.default === true } get inline() { |