From de2b68f3f628f40be2321f50368aa90e6b1cd3ff Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Thu, 17 Mar 2022 10:09:43 +0100 Subject: [PATCH] Introduce personal space resource loader --- packages/web-app-files/src/services/folder.ts | 9 +- .../src/services/folder/index.ts | 3 +- .../folder/{ => legacy}/loaderPersonal.ts | 31 +++---- .../services/folder/spaces/loaderPersonal.ts | 82 +++++++++++++++++++ 4 files changed, 106 insertions(+), 19 deletions(-) rename packages/web-app-files/src/services/folder/{ => legacy}/loaderPersonal.ts (69%) create mode 100644 packages/web-app-files/src/services/folder/spaces/loaderPersonal.ts diff --git a/packages/web-app-files/src/services/folder.ts b/packages/web-app-files/src/services/folder.ts index f16d8faa983..b7206659eee 100644 --- a/packages/web-app-files/src/services/folder.ts +++ b/packages/web-app-files/src/services/folder.ts @@ -9,12 +9,13 @@ import { FolderLoaderSpacesProject, FolderLoaderSpacesShare, FolderLoaderFavorites, - FolderLoaderPersonal, + FolderLoaderLegacyPersonal, FolderLoaderPublicFiles, FolderLoaderSharedViaLink, FolderLoaderSharedWithMe, FolderLoaderSharedWithOthers, - FolderLoaderTrashbin + FolderLoaderTrashbin, + FolderLoaderSpacesPersonal } from './folder/' export * from './folder/util' @@ -38,12 +39,14 @@ export class FolderService { constructor() { this.loaders = [ + // legacy loaders + new FolderLoaderLegacyPersonal(), // spaces loaders + new FolderLoaderSpacesPersonal(), new FolderLoaderSpacesProject(), new FolderLoaderSpacesShare(), // generic loaders new FolderLoaderFavorites(), - new FolderLoaderPersonal(), new FolderLoaderPublicFiles(), new FolderLoaderSharedViaLink(), new FolderLoaderSharedWithMe(), diff --git a/packages/web-app-files/src/services/folder/index.ts b/packages/web-app-files/src/services/folder/index.ts index 5c731abc09c..8e3109723ec 100644 --- a/packages/web-app-files/src/services/folder/index.ts +++ b/packages/web-app-files/src/services/folder/index.ts @@ -1,7 +1,8 @@ +export * from './legacy/loaderPersonal' +export * from './spaces/loaderPersonal' export * from './spaces/loaderProject' export * from './spaces/loaderShare' export * from './loaderFavorites' -export * from './loaderPersonal' export * from './loaderPublicFiles' export * from './loaderSharedViaLink' export * from './loaderSharedWithMe' diff --git a/packages/web-app-files/src/services/folder/loaderPersonal.ts b/packages/web-app-files/src/services/folder/legacy/loaderPersonal.ts similarity index 69% rename from packages/web-app-files/src/services/folder/loaderPersonal.ts rename to packages/web-app-files/src/services/folder/legacy/loaderPersonal.ts index 41dbb9d653e..310bade1c6d 100644 --- a/packages/web-app-files/src/services/folder/loaderPersonal.ts +++ b/packages/web-app-files/src/services/folder/legacy/loaderPersonal.ts @@ -1,16 +1,16 @@ -import { FolderLoader, FolderLoaderTask, TaskContext } from '../folder' +import { FolderLoader, FolderLoaderTask, TaskContext } from '../../folder' import Router from 'vue-router' import { useTask } from 'vue-concurrency' import { DavProperties } from 'web-pkg/src/constants' -import { buildResource, buildWebDavFilesPath } from '../../helpers/resources' -import { isLocationSpacesActive } from '../../router' +import { buildResource, buildWebDavFilesPath } from '../../../helpers/resources' +import { isLocationSpacesActive } from '../../../router' import { Store } from 'vuex' -import { fetchResources } from './util' +import { fetchResources } from '../util' +import get from 'lodash-es/get' -export class FolderLoaderPersonal implements FolderLoader { - // eslint-disable-next-line @typescript-eslint/no-unused-vars +export class FolderLoaderLegacyPersonal implements FolderLoader { public isEnabled(store: Store): boolean { - return true + return !get(store, 'getters.capabilities.spaces.enabled', false) } public isActive(router: Router): boolean { @@ -42,16 +42,17 @@ export class FolderLoaderPersonal implements FolderLoader { files: resources }) - store.dispatch('Files/loadIndicators', { - client: client, - currentFolder: currentFolder.path - }) + // load indicators + ;(() => { + store.dispatch('Files/loadIndicators', { + client: client, + currentFolder: currentFolder.path + }) + })() - // Load quota - const promiseUser = client.users.getUser(ref.user.id) - // The semicolon is important to separate from the previous statement + // fetch user quota ;(async () => { - const user = await promiseUser + const user = await client.users.getUser(ref.user.id) store.commit('SET_QUOTA', user.quota) })() } catch (error) { diff --git a/packages/web-app-files/src/services/folder/spaces/loaderPersonal.ts b/packages/web-app-files/src/services/folder/spaces/loaderPersonal.ts new file mode 100644 index 00000000000..e8f94ae226b --- /dev/null +++ b/packages/web-app-files/src/services/folder/spaces/loaderPersonal.ts @@ -0,0 +1,82 @@ +import { FolderLoader, FolderLoaderTask, TaskContext } from '../../folder' +import Router from 'vue-router' +import { useTask } from 'vue-concurrency' +import { DavProperties } from 'web-pkg/src/constants' +import { + buildResource, + buildWebDavFilesPath, + buildWebDavSpacesPath +} from '../../../helpers/resources' +import { isLocationSpacesActive } from '../../../router' +import { Store } from 'vuex' +import { fetchResources } from '../util' +import get from 'lodash-es/get' + +export class FolderLoaderSpacesPersonal implements FolderLoader { + public isEnabled(store: Store): boolean { + return get(store, 'getters.capabilities.spaces.enabled', false) + } + + public isActive(router: Router): boolean { + return isLocationSpacesActive(router, 'files-spaces-personal-home') + } + + public getTask(context: TaskContext): FolderLoaderTask { + const { store, router, clientService } = context + + const graphClient = clientService.graphAuthenticated( + store.getters.configuration.server, + store.getters.getToken + ) + + return useTask(function* (signal1, signal2, ref, sameRoute, path = null) { + try { + store.commit('Files/CLEAR_CURRENT_FILES_LIST') + + const userResponse = yield graphClient.users.getMe() + if (!userResponse.data) { + throw new Error('graph.user.getMe() has no data') + } + + let resources = yield fetchResources( + clientService.owncloudSdk, + buildWebDavSpacesPath( + userResponse.data.id, + path || router.currentRoute.params.item || '' + ), + DavProperties.Default + ) + resources = resources.map(buildResource) + + const currentFolder = resources.shift() + + store.commit('Files/LOAD_FILES', { + currentFolder, + files: resources + }) + + // load indicators + ;(() => { + store.dispatch('Files/loadIndicators', { + client: clientService.owncloudSdk, + currentFolder: currentFolder.path + }) + })() + + // fetch user quota + ;(async () => { + const user = await clientService.owncloudSdk.users.getUser(ref.user.id) + store.commit('SET_QUOTA', user.quota) + })() + } catch (error) { + store.commit('Files/SET_CURRENT_FOLDER', null) + console.error(error) + } + + ref.refreshFileListHeaderPosition() + + ref.accessibleBreadcrumb_focusAndAnnounceBreadcrumb(sameRoute) + ref.scrollToResourceFromRoute() + }).restartable() + } +}