From c5c86f82d780c7ccb38c1832cf1ec22ff1d74145 Mon Sep 17 00:00:00 2001 From: Jannik Stehle Date: Wed, 31 Jan 2024 10:36:34 +0100 Subject: [PATCH] refactor: remove aggregateResourceShares --- .../src/services/folder/loaderSpace.ts | 24 +- .../web-client/src/helpers/share/functions.ts | 205 +----------------- .../objects/app-files/resource/actions.ts | 5 +- 3 files changed, 15 insertions(+), 219 deletions(-) diff --git a/packages/web-app-files/src/services/folder/loaderSpace.ts b/packages/web-app-files/src/services/folder/loaderSpace.ts index 994628b67bb..33398166a0f 100644 --- a/packages/web-app-files/src/services/folder/loaderSpace.ts +++ b/packages/web-app-files/src/services/folder/loaderSpace.ts @@ -12,7 +12,7 @@ import { unref } from 'vue' import { FolderLoaderOptions } from './types' import { authService } from 'web-runtime/src/services/auth' import { useFileRouteReplace } from '@ownclouders/web-pkg' -import { aggregateResourceShares } from '@ownclouders/web-client/src/helpers/share' +import { IncomingShareResource } from '@ownclouders/web-client/src/helpers/share' import { getIndicators } from '@ownclouders/web-pkg' export class FolderLoaderSpace implements FolderLoader { @@ -32,9 +32,8 @@ export class FolderLoaderSpace implements FolderLoader { } public getTask(context: TaskContext): FolderLoaderTask { - const { spacesStore, router, clientService, configStore, capabilityStore, resourcesStore } = - context - const { owncloudSdk: client, webdav } = clientService + const { router, clientService, resourcesStore } = context + const { webdav } = clientService const { replaceInvalidFileRoute } = useFileRouteReplace({ router }) return useTask(function* ( @@ -60,16 +59,13 @@ export class FolderLoaderSpace implements FolderLoader { if (path === '/') { if (isShareSpaceResource(space)) { - const parentShare = yield client.shares.getShare(space.shareId) - const aggregatedShares = aggregateResourceShares({ - shares: [parentShare.shareInfo], - spaces: spacesStore.spaces, - allowSharePermission: capabilityStore.sharingResharing, - hasShareJail: true, - incomingShares: true, - fullShareOwnerPaths: configStore.options.routing.fullShareOwnerPaths - }) - currentFolder = aggregatedShares[0] + // FIXME: it would be cleaner to fetch the driveItem as soon as graph api is capable of it + currentFolder = { + ...currentFolder, + id: space.shareId, + syncEnabled: true, + canShare: () => false + } as IncomingShareResource } else if (!isPersonalSpaceResource(space) && !isPublicSpaceResource(space)) { // note: in the future we might want to show the space as root for personal spaces as well (to show quota and the like). Currently not needed. currentFolder = space diff --git a/packages/web-client/src/helpers/share/functions.ts b/packages/web-client/src/helpers/share/functions.ts index 44d12573c22..92ec57c19e1 100644 --- a/packages/web-client/src/helpers/share/functions.ts +++ b/packages/web-client/src/helpers/share/functions.ts @@ -1,15 +1,6 @@ -import { orderBy } from 'lodash-es' import { DateTime } from 'luxon' -import { - Resource, - buildWebDavFilesPath, - extractDomSelector, - extractExtensionFromFile, - extractStorageId -} from '../resource' +import { Resource } from '../resource' import { ShareTypes } from './type' -import path from 'path' -import { SHARE_JAIL_ID, SpaceResource, buildWebDavSpacesPath } from '../space' import { SharePermissions } from './permission' import { buildSpaceShare } from './space' import { LinkShareRoles, PeopleShareRoles } from './role' @@ -27,200 +18,6 @@ export const isIncomingShareResource = (resource: Resource): resource is Incomin return isShareResource(resource) && !resource.outgoing } -/** - * Transforms given shares into a resource format and returns only their unique occurences - * - * @deprecated - */ -export function aggregateResourceShares({ - shares, - spaces, - allowSharePermission = true, - hasShareJail = true, - incomingShares = false, - fullShareOwnerPaths = false -}: { - shares: Share[] - spaces: SpaceResource[] - allowSharePermission?: boolean - hasShareJail?: boolean - incomingShares?: boolean - fullShareOwnerPaths?: boolean -}): Resource[] { - shares.sort((a, b) => a.path.localeCompare(b.path)) - if (spaces.length) { - shares = addMatchingSpaceToShares(shares, spaces) - } - if (incomingShares) { - shares = addSharedWithToShares(shares) - return orderBy(shares, ['file_source', 'permissions'], ['asc', 'desc']).map((share) => { - const resource = buildSharedResource( - share, - incomingShares, - allowSharePermission, - hasShareJail - ) - resource.shareId = share.id - - if (fullShareOwnerPaths) { - resource.path = spaces.find( - (space) => - space.driveType === 'mountpoint' && - space.id === `${SHARE_JAIL_ID}$${SHARE_JAIL_ID}!${resource.shareId}` - )?.root.remoteItem.path - resource.shareRoot = resource.path - } - return resource - }) - } - - const resources = addSharedWithToShares(shares) - return resources.map((share) => - buildSharedResource(share, incomingShares, allowSharePermission, hasShareJail) - ) -} - -function addSharedWithToShares(shares) { - const resources = [] - let previousShare = null - for (const share of shares) { - if ( - previousShare?.storage_id === share.storage_id && - previousShare?.file_source === share.file_source - ) { - if (ShareTypes.containsAnyValue(ShareTypes.authenticated, [parseInt(share.share_type)])) { - if (share.stime > previousShare.stime) { - previousShare.stime = share.stime - } - previousShare.sharedWith.push({ - id: share.share_with, - displayName: share.share_with_displayname - }) - } else if (parseInt(share.share_type) === ShareTypes.link.value) { - previousShare.sharedWith.push({ - id: share.token, - displayName: share.name || share.token - }) - } - - continue - } - - if (ShareTypes.containsAnyValue(ShareTypes.authenticated, [parseInt(share.share_type)])) { - share.sharedWith = [ - { - id: share.share_with, - displayName: share.share_with_displayname - } - ] - } else if (parseInt(share.share_type) === ShareTypes.link.value) { - share.sharedWith = [ - { - id: share.token, - displayName: share.name || share.token - } - ] - } - - previousShare = share - resources.push(share) - } - return resources -} - -function addMatchingSpaceToShares(shares, spaces) { - const resources = [] - for (const share of shares) { - let matchingSpace - if (share.path === '/') { - const storageId = extractStorageId(share.item_source) - matchingSpace = spaces.find((s) => s.id === storageId && s.driveType === 'project') - } - resources.push({ ...share, matchingSpace }) - } - return resources -} - -/** @deprecated */ -export function buildSharedResource( - share, - incomingShares = false, - allowSharePermission = true, - hasShareJail = false -): ShareResource { - const isFolder = share.item_type === 'folder' - const isRemoteShare = parseInt(share.share_type) === ShareTypes.remote.value - let resource: ShareResource = { - id: share.id, - fileId: share.item_source, - storageId: extractStorageId(share.item_source), - parentFolderId: share.file_parent, - type: share.item_type, - mimeType: share.mimetype, - isFolder, - sdate: DateTime.fromSeconds(parseInt(share.stime)).toRFC2822(), - indicators: [], - tags: [], - path: undefined, - webDavPath: undefined, - processing: share.processing || false, - shareTypes: [parseInt(share.share_type)], - owner: { id: share.uid_owner, displayName: share.displayname_owner }, - sharedBy: { id: share.uid_owner, displayName: share.displayname_owner }, - sharedWith: share.sharedWith || [], - outgoing: !incomingShares - } - - if (incomingShares) { - ;(resource as IncomingShareResource).syncEnabled = parseInt(share.state) === 0 - ;(resource as IncomingShareResource).hidden = share.hidden === 'true' || share.hidden === true - resource.name = isRemoteShare ? share.name : path.basename(share.file_target) - if (isRemoteShare) { - resource.path = '/' - resource.webDavPath = buildWebDavSpacesPath(share.space_id, '/') - } else if (hasShareJail) { - // FIXME, HACK 1: path needs to be '/' because the share has it's own webdav endpoint (we access it's root). should ideally be removed backend side. - // FIXME, HACK 2: webDavPath points to `files//Shares/xyz` but now needs to point to a shares webdav root. should ideally be changed backend side. - resource.path = '/' - resource.webDavPath = buildWebDavSpacesPath([SHARE_JAIL_ID, resource.id].join('!'), '/') - } else { - resource.path = share.file_target - resource.webDavPath = buildWebDavFilesPath(share.share_with, share.file_target) - } - resource.canDownload = () => parseInt(share.state) === 0 - resource.canShare = () => SharePermissions.share.enabled(share.permissions) - resource.canRename = () => parseInt(share.state) === 0 - resource.canBeDeleted = () => SharePermissions.delete.enabled(share.permissions) - resource.canEditTags = () => - parseInt(share.state) === 0 && SharePermissions.update.enabled(share.permissions) - } else { - resource.name = isRemoteShare ? share.name : path.basename(share.path) - resource.path = share.path - resource.webDavPath = hasShareJail - ? buildWebDavSpacesPath(resource.storageId, share.path) - : buildWebDavFilesPath(share.uid_owner, share.path) - resource.canDownload = () => true - resource.canShare = () => true - resource.canRename = () => true - resource.canBeDeleted = () => true - resource.canEditTags = () => true - } - - resource.extension = extractExtensionFromFile(resource) - resource.isReceivedShare = () => incomingShares - resource.canUpload = () => SharePermissions.create.enabled(share.permissions) - resource.canCreate = () => SharePermissions.create.enabled(share.permissions) - resource.isMounted = () => false - resource.canDeny = () => SharePermissions.denied.enabled(share.permissions) - resource.getDomSelector = () => extractDomSelector(share.id) - - if (share.matchingSpace) { - resource = { ...resource, ...share.matchingSpace } - } - - return resource -} - export function buildShare(s, file, allowSharePermission): Share { if (parseInt(s.share_type) === ShareTypes.link.value) { return _buildLink(s) diff --git a/tests/e2e/support/objects/app-files/resource/actions.ts b/tests/e2e/support/objects/app-files/resource/actions.ts index 4cfc8856f4d..61d08b0d99a 100644 --- a/tests/e2e/support/objects/app-files/resource/actions.ts +++ b/tests/e2e/support/objects/app-files/resource/actions.ts @@ -123,7 +123,10 @@ export const clickResource = async ({ const itemId = await resource.locator(fileRow).getAttribute('data-item-id') await Promise.all([ page.waitForResponse( - (resp) => resp.url().endsWith(encodeURIComponent(name)) || resp.url().endsWith(itemId) + (resp) => + resp.url().endsWith(encodeURIComponent(name)) || + resp.url().endsWith(itemId) || + resp.url().endsWith(encodeURIComponent(itemId)) ), resource.click() ])