From 93f3363dc80084c64d96607935c2d52dbddf2c4a Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Thu, 24 Nov 2022 11:45:35 +0100 Subject: [PATCH] refactor: resolve space file info upon space creation --- .../src/views/spaces/DriveResolver.vue | 32 +++++++------------ .../unit/views/spaces/DriveResolver.spec.ts | 17 ++++------ packages/web-client/src/webdav/listFiles.ts | 2 +- packages/web-runtime/src/index.ts | 16 ++++++++-- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/packages/web-app-files/src/views/spaces/DriveResolver.vue b/packages/web-app-files/src/views/spaces/DriveResolver.vue index dfee4180757..0e8fd8b61b7 100644 --- a/packages/web-app-files/src/views/spaces/DriveResolver.vue +++ b/packages/web-app-files/src/views/spaces/DriveResolver.vue @@ -25,7 +25,7 @@ import { } from 'web-pkg/src/composables' import { useActiveLocation } from '../../composables' import { isLocationSpacesActive, isLocationTrashActive } from '../../router' -import { isPublicSpaceResource, PublicSpaceResource, SpaceResource } from 'web-client/src/helpers' +import { isPublicSpaceResource } from 'web-client/src/helpers' import { locationPublicUpload } from 'web-app-files/src/router/public' import { linkRoleUploaderFolder } from 'web-client/src/helpers/share' @@ -44,27 +44,17 @@ export default defineComponent({ const isTrashRoute = useActiveLocation(isLocationTrashActive, 'files-trash-generic') const resolvedDrive = useDriveResolver({ store, driveAliasAndItem }) - const getSpaceResource = async (): Promise => { + onMounted(() => { const space = unref(resolvedDrive.space) - try { - const publicSpace = (await clientService.webdav.getFileInfo(space)) as SpaceResource - return publicSpace - } catch (e) { - return space - } - } - - onMounted(async () => { - const space = unref(resolvedDrive.space) - if (space && isPublicSpaceResource(space)) { - let publicSpace = (await getSpaceResource()) as PublicSpaceResource - - if (linkRoleUploaderFolder.bitmask(false) === publicSpace.publicLinkPermission) { - router.push({ - name: locationPublicUpload.name, - params: { token: space.id.toString() } - }) - } + if ( + space && + isPublicSpaceResource(space) && + linkRoleUploaderFolder.bitmask(false) === space.publicLinkPermission + ) { + return router.push({ + name: locationPublicUpload.name, + params: { token: space.id.toString() } + }) } }) diff --git a/packages/web-app-files/tests/unit/views/spaces/DriveResolver.spec.ts b/packages/web-app-files/tests/unit/views/spaces/DriveResolver.spec.ts index 9ed6d1f5370..e737fbcb70c 100644 --- a/packages/web-app-files/tests/unit/views/spaces/DriveResolver.spec.ts +++ b/packages/web-app-files/tests/unit/views/spaces/DriveResolver.spec.ts @@ -9,10 +9,7 @@ import { useDriveResolver } from 'web-pkg/src/composables' import { spaces } from 'web-app-files/tests/__fixtures__' import { computed, ref } from '@vue/composition-api' import { defaultStubs } from 'web-test-helpers/src/mocks/defaultStubs' -import { mockDeep } from 'jest-mock-extended' -import { ClientService } from 'web-pkg/src' import { locationPublicUpload } from 'web-app-files/src/router/public' -import { PublicSpaceResource } from 'web-client/src/helpers' import { SharePermissionBit } from 'web-client/src/helpers/share' jest.mock('web-pkg/src/composables/driveResolver') @@ -34,14 +31,14 @@ describe('DriveResolver view', () => { expect(wrapper.find('generic-space-stub').exists()).toBeTruthy() }) it('redirects to the public drop page in a public context with "upload-only"-permissions', async () => { - const space = { id: '1', getDriveAliasAndItem: jest.fn(), driveType: 'public' } - const clientService = mockDeep() - clientService.webdav.getFileInfo.mockResolvedValue( - mockDeep({ publicLinkPermission: SharePermissionBit.Create }) - ) + const space = { + id: '1', + getDriveAliasAndItem: jest.fn(), + driveType: 'public', + publicLinkPermission: SharePermissionBit.Create + } const { wrapper, mocks } = getMountedWrapper({ - space, - mocks: { $clientService: clientService } + space }) await wrapper.vm.$nextTick() diff --git a/packages/web-client/src/webdav/listFiles.ts b/packages/web-client/src/webdav/listFiles.ts index 6c4025089a7..41468114286 100644 --- a/packages/web-client/src/webdav/listFiles.ts +++ b/packages/web-client/src/webdav/listFiles.ts @@ -40,7 +40,7 @@ export const ListFilesFactory = ({ sdk }: WebDavOptions) => { if (!path) { const [rootFolder, ...children] = webDavResources return { - resource: buildPublicSpaceResource(rootFolder), + resource: buildPublicSpaceResource({ ...rootFolder, ...space }), children: children.map(buildResource) } as ListFilesResult } diff --git a/packages/web-runtime/src/index.ts b/packages/web-runtime/src/index.ts index e9fc184a409..a59f9cff1f7 100644 --- a/packages/web-runtime/src/index.ts +++ b/packages/web-runtime/src/index.ts @@ -32,6 +32,7 @@ import { buildSpace, isPersonalSpaceResource, isPublicSpaceResource, + PublicSpaceResource, Resource } from 'web-client/src/helpers' import { WebDAV } from 'web-client/src/webdav' @@ -145,19 +146,30 @@ export const renderSuccess = (): void => { (state, getters) => { return getters['runtime/auth/isPublicLinkContextReady'] }, - (publicLinkContextReady) => { + async (publicLinkContextReady) => { if (!publicLinkContextReady) { return } // Create virtual space for public link const publicLinkToken = store.getters['runtime/auth/publicLinkToken'] const publicLinkPassword = store.getters['runtime/auth/publicLinkPassword'] - const space = buildPublicSpaceResource({ + let space = buildPublicSpaceResource({ id: publicLinkToken, name: instance.$gettext('Public files'), ...(publicLinkPassword && { publicLinkPassword }), serverUrl: configurationManager.serverUrl }) + try { + const { + webdav: { getFileInfo } + } = instance.$clientService + space = (await getFileInfo(space)) as PublicSpaceResource + } catch (e) { + // the most important data is there already, so we don't need to fail + // hard here, just print the error + console.error(e) + } + store.commit('runtime/spaces/ADD_SPACES', [space]) store.commit('runtime/spaces/SET_SPACES_INITIALIZED', true) },