Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(move): Allow moving files only shared by link into shared folder #3022

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/drive/web/modules/move/MoveModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
buildMoveOrImportQuery,
buildOnlyFolderQuery
} from 'drive/web/modules/queries'
import { cancelMove } from 'drive/web/modules/move/helpers'
import {
cancelMove,
hasOneOfEntriesShared
} from 'drive/web/modules/move/helpers'
import { MoveOutsideSharedFolderModal } from 'drive/web/modules/move/MoveOutsideSharedFolderModal'
import { MoveSharedFolderInsideAnotherModal } from 'drive/web/modules/move/MoveSharedFolderInsideAnotherModal'

Expand Down Expand Up @@ -110,9 +113,10 @@ const MoveModal = ({ onClose, entries, classes }) => {
setMovingOutsideSharedFolder(true)
}
} else {
const hasOneOrMoreEntriesShared =
entries.filter(({ _id }) => byDocId[_id] !== undefined).length > 0
if (hasOneOrMoreEntriesShared && hasSharedParent(targetPath)) {
if (
hasOneOfEntriesShared(entries, byDocId) &&
hasSharedParent(targetPath)
) {
setMovingSharedFolderInsideAnother(true)
} else {
moveEntries()
Expand Down
41 changes: 38 additions & 3 deletions src/drive/web/modules/move/MoveModal.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,32 @@ describe('MoveModal component', () => {
})

describe('move shared folder inside another', () => {
it('should move the shared folder inside another if its by link', async () => {
CozyFile.getFullpath.mockImplementation((destinationFolder, name) =>
Promise.resolve(`/${destinationFolder}/${name}`)
)

setup({
sharedPaths: ['/bills/bill_201903.pdf', '/destinationFolder'],
byDocId: {
bill_201903: {
permissions: ['perm1'],
sharings: []
}
},
getSharedParentPath: () => null
})

const moveButton = await screen.findByText('Move')
fireEvent.click(moveButton)

await waitFor(() => {
expect(CozyFile.move).toHaveBeenCalled()
expect(onCloseSpy).toHaveBeenCalled()
expect(refreshSpy).toHaveBeenCalled()
})
})

it('should display an alert when move shared folder inside another', async () => {
CozyFile.getFullpath.mockImplementation((destinationFolder, name) =>
Promise.resolve(`/${destinationFolder}/${name}`)
Expand All @@ -237,7 +263,10 @@ describe('MoveModal component', () => {
setup({
sharedPaths: ['/bills/bill_201903.pdf', '/destinationFolder'],
byDocId: {
bill_201903: {}
bill_201903: {
permissions: [],
sharings: []
}
},
getSharedParentPath: () => null
})
Expand All @@ -260,7 +289,10 @@ describe('MoveModal component', () => {
setup({
sharedPaths: ['/bills/bill_201903.pdf', '/destinationFolder'],
byDocId: {
bill_201903: {}
bill_201903: {
permissions: [],
sharings: []
}
},
getSharedParentPath: () => null,
sharingContext: {
Expand Down Expand Up @@ -297,7 +329,10 @@ describe('MoveModal component', () => {
setup({
sharedPaths: ['/bills/bill_201903.pdf', '/destinationFolder'],
byDocId: {
bill_201903: {}
bill_201903: {
permissions: [],
sharings: []
}
},
getSharedParentPath: () => null,
sharingContext: {
Expand Down
49 changes: 39 additions & 10 deletions src/drive/web/modules/move/helpers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import Alerter from 'cozy-ui/transpiled/react/deprecated/Alerter'
// eslint-disable-next-line no-unused-vars
const { IOCozyFile } = require('cozy-client/dist/types')

import { CozyFile } from 'models'
import logger from 'lib/logger'

/**
* Returns whether one of the targeted folders is part of the current folder
* @param {object[]} targets List of folders
* @param {string} currentDirId The id of the current folder
* @returns {boolean} whether one of the targeted folders is part of the current folder
* @param {IOCozyFile[]} targets - List of folders
* @param {string} currentDirId - The id of the current folder
* @returns {boolean} - Whether one of the targeted folders is part of the current folder
*/
export const areTargetsInCurrentDir = (targets, currentDirId) => {
const targetsInCurrentDir = targets.filter(
Expand All @@ -19,8 +21,8 @@ export const areTargetsInCurrentDir = (targets, currentDirId) => {
/**
* Cancel file movement function
* @param {object} client - The CozyClient instance
* @param {object[]} entries - List of files moved
* @param {string[]} trashedFiles - List of ids for files moved to the trash
* @param {IOCozyFile[]} entries - List of files moved
* @param {IOCozyFile[]} trashedFiles - List of ids for files moved to the trash
* @param {Function} registerCancelable - Function to register the promise
* @param {Functione} refreshSharing - Function refresh sharing state
*/
Expand Down Expand Up @@ -70,9 +72,9 @@ export const cancelMove = async ({

/**
* Gets a name for the entry if there is only one, or a sentence with the number of elements if there are several
* @param {object[]} entries - List of files moved
* @param {Function} t translation function
* @returns {string} name for entries
* @param {IOCozyFile[]} entries - List of files moved
* @param {Function} t - Translation function
* @returns {string} - Name for entries
*/
export const getEntriesName = (entries, t) => {
return entries.length !== 1
Expand All @@ -84,8 +86,8 @@ export const getEntriesName = (entries, t) => {

/**
* Get type from the entries
* @param {object[]} entries
* @returns {string} type from the entries
* @param {IOCozyFile[]} entries - List of files moved
* @returns {string} - Type from the entries
*/
export const getEntriesType = entries => {
return entries.reduce((previous, current) => {
Expand All @@ -100,3 +102,30 @@ export const getEntriesType = entries => {
return current.type
}, null)
}

/**
* @typedef {Object} SharedDoc
* @property {string[]} permissions - List of permissions
* @property {string[]} sharings - List of sharings
*/

/**
* Returns whether one of the entries that is shared not only by link
* @param {IOCozyFile[]} entries - List of files moved
* @param {Object<string, SharedDoc>} byDocId - Object with shared files by id from cozy-sharing
* @returns {boolean} - Whether one of the entries that is shared not only by link
*/
export const hasOneOfEntriesShared = (entries, byDocId) => {
const sharedEntries = entries.filter(({ _id }) => {
const doc = byDocId[_id]
if (doc === undefined) return false

const onlySharedByLink =
doc.permissions.length > 0 && doc.sharings.length === 0

if (onlySharedByLink) return false

return true
})
return sharedEntries.length > 0
}
31 changes: 30 additions & 1 deletion src/drive/web/modules/move/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import CozyClient from 'cozy-client'

import { cancelMove, getEntriesType } from 'drive/web/modules/move/helpers'
import {
cancelMove,
getEntriesType,
hasOneOfEntriesShared
} from 'drive/web/modules/move/helpers'
import { CozyFile } from 'models'

jest.mock('cozy-doctypes')
Expand Down Expand Up @@ -119,3 +123,28 @@ describe('getEntriesType', () => {
expect(res).toBe('element')
})
})

describe('hasOneOfEntriesShared', () => {
it('should return false if entries are not shared', () => {
const entries = [{ _id: '1' }, { _id: '2' }, { _id: '3' }]
const byDocId = {}
expect(hasOneOfEntriesShared(entries, byDocId)).toBe(false)
})

it('should return false if all entries are only shared by link', () => {
const entries = [{ _id: '1' }, { _id: '2' }, { _id: '3' }]
const byDocId = {
1: { permissions: ['permission1'], sharings: [] },
2: { permissions: ['permission2'], sharings: [] }
}
expect(hasOneOfEntriesShared(entries, byDocId)).toBe(false)
})

it('should return true if at least one entry is shared', () => {
const entries = [{ _id: '1' }, { _id: '2' }, { _id: '3' }]
const byDocId = {
2: { permissions: [], sharings: ['sharingId'] }
}
expect(hasOneOfEntriesShared(entries, byDocId)).toBe(true)
})
})
Loading