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

update space quota on upload, delete and restore #7415

Merged
merged 13 commits into from
Aug 10, 2022
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
8 changes: 8 additions & 0 deletions changelog/unreleased/bugfix-re-fetch-quota
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Bugfix: Re-fetch quota

We've fixed a bug where uploading, deleting or restoring resources doesn't always re-fetch the quota and therefore
was falsy displayed.

https://github.com/owncloud/web/pull/7415
https://github.com/owncloud/web/issues/6930
https://github.com/owncloud/web/issues/7389
31 changes: 21 additions & 10 deletions packages/web-app-files/src/components/AppBar/CreateAndUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ import MixinFileActions, { EDITOR_MODE_CREATE } from '../../mixins/fileActions'
import { buildResource, buildWebDavFilesPath, buildWebDavSpacesPath } from '../../helpers/resources'
import { isLocationPublicActive, isLocationSpacesActive } from '../../router'
import { useActiveLocation } from '../../composables'
import { useGraphClient } from 'web-client/src/composables'

import {
useRequest,
useCapabilityShareJailEnabled,
useCapabilitySpacesEnabled,
useStore,
usePublicLinkPassword,
useUserContext
Expand All @@ -151,7 +154,6 @@ export default defineComponent({

onMounted(() => {
const filesSelectedSub = uppyService.subscribe('filesSelected', instance.onFilesSelected)
const uploadSuccessSub = uppyService.subscribe('uploadSuccess', instance.onFileSuccess)
const uploadCompletedSub = uppyService.subscribe('uploadCompleted', instance.onUploadComplete)

uppyService.useDropTarget({
Expand All @@ -161,7 +163,6 @@ export default defineComponent({

instance.$on('beforeDestroy', () => {
uppyService.unsubscribe('filesSelected', filesSelectedSub)
uppyService.unsubscribe('uploadSuccess', uploadSuccessSub)
uppyService.unsubscribe('uploadCompleted', uploadCompletedSub)
uppyService.removeDropTarget()
})
Expand All @@ -173,12 +174,14 @@ export default defineComponent({
}),
...useUploadHelpers(),
...useRequest(),
...useGraphClient(),
isPersonalLocation: useActiveLocation(isLocationSpacesActive, 'files-spaces-personal'),
isPublicLocation: useActiveLocation(isLocationPublicActive, 'files-public-files'),
isSpacesProjectsLocation: useActiveLocation(isLocationSpacesActive, 'files-spaces-projects'),
isSpacesProjectLocation: useActiveLocation(isLocationSpacesActive, 'files-spaces-project'),
isSpacesShareLocation: useActiveLocation(isLocationSpacesActive, 'files-spaces-share'),
hasShareJail: useCapabilityShareJailEnabled(),
hasSpaces: useCapabilitySpacesEnabled(),
publicLinkPassword: usePublicLinkPassword({ store }),
isUserContext: useUserContext({ store })
}
Expand Down Expand Up @@ -278,23 +281,31 @@ export default defineComponent({
'setModalInputErrorMessage',
'hideModal'
]),
...mapMutations('Files', ['UPSERT_RESOURCE']),
...mapMutations('Files', ['UPSERT_RESOURCE', 'UPDATE_SPACE_FIELD']),
...mapMutations(['SET_QUOTA']),

onFileSuccess() {
if (this.user?.quota) {
this.SET_QUOTA(this.user.quota)
}
},

onUploadComplete(result) {
async onUploadComplete(result) {
if (result.successful) {
const file = result.successful[0]

if (!file) {
return
}

if (this.isSpacesProjectLocation || this.isPersonalLocation) {
if (this.hasSpaces) {
const driveResponse = await this.graphClient.drives.getDrive(file.meta.routeStorageId)
this.UPDATE_SPACE_FIELD({
id: driveResponse.data.id,
field: 'spaceQuota',
value: driveResponse.data.quota
})
} else {
const user = await this.$client.users.getUser(this.user.id)
this.SET_QUOTA(user.quota)
}
}

let pathFileWasUploadedTo = file.meta.currentFolder
if (file.meta.relativeFolder) {
pathFileWasUploadedTo += file.meta.relativeFolder
Expand Down
4 changes: 2 additions & 2 deletions packages/web-app-files/src/components/SpaceQuota.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="space-quota">
<p class="oc-mb-s oc-mt-rm" v-text="spaceStorageDetailsLabel" />
<oc-progress
:value="parseInt(quotaUsagePercent)"
:value="quotaUsagePercent"
:max="100"
size="small"
:variation="quotaProgressVariant"
Expand Down Expand Up @@ -45,7 +45,7 @@ export default {
return filesize(this.spaceQuota.used)
},
quotaUsagePercent() {
return ((this.spaceQuota.used / this.spaceQuota.total) * 100).toFixed(1)
return parseFloat(((this.spaceQuota.used / this.spaceQuota.total) * 100).toFixed(2))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aligned to oc10 user quota view

},
quotaProgressVariant() {
switch (this.spaceQuota.state) {
Expand Down
28 changes: 27 additions & 1 deletion packages/web-app-files/src/mixins/actions/restore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mapActions, mapState } from 'vuex'
import { mapActions, mapGetters, mapMutations, mapState } from 'vuex'
import PQueue from 'p-queue'
import { isLocationTrashActive } from '../../router'
import {
Expand All @@ -7,10 +7,14 @@ import {
buildWebDavSpacesTrashPath,
buildWebDavSpacesPath
} from '../../helpers/resources'
import { clientService } from 'web-pkg/src/services'

export default {
computed: {
...mapState(['user']),
...mapState('Files', ['spaces']),
...mapGetters(['configuration', 'capabilities']),
...mapGetters('runtime/auth', ['accessToken']),

$_restore_items() {
return [
Expand Down Expand Up @@ -41,6 +45,8 @@ export default {
methods: {
...mapActions('Files', ['removeFilesFromTrashbin']),
...mapActions(['showMessage']),
...mapMutations('Files', ['UPDATE_SPACE_FIELD']),
...mapMutations(['SET_QUOTA']),

async $_restore_trigger({ resources }) {
const restoredResources = []
Expand Down Expand Up @@ -102,6 +108,26 @@ export default {
status: 'danger'
})
}

// Load quota
if (this.capabilities?.spaces?.enabled) {
const graphClient = clientService.graphAuthenticated(
this.configuration.server,
this.accessToken
)
const driveId = isLocationTrashActive(this.$router, 'files-trash-spaces-project')
? this.$route.params.storageId
: this.spaces.find((s) => s.driveType === 'personal').id
const driveResponse = await graphClient.drives.getDrive(driveId)
this.UPDATE_SPACE_FIELD({
id: driveResponse.data.id,
field: 'spaceQuota',
value: driveResponse.data.quota
})
} else {
const user = await this.$client.users.getUser(this.user.id)
this.SET_QUOTA(user.quota)
}
}
}
}
31 changes: 26 additions & 5 deletions packages/web-app-files/src/mixins/deleteResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { cloneStateObject } from '../helpers/store'
import { isSameResource } from '../helpers/resource'
import { buildWebDavFilesTrashPath, buildWebDavSpacesTrashPath } from '../helpers/resources'
import PQueue from 'p-queue'
import { isLocationTrashActive } from '../router'
import { isLocationTrashActive, isLocationSpacesActive } from '../router'
import { clientService } from 'web-pkg/src/services'

export default {
data: () => ({
Expand All @@ -14,8 +15,9 @@ export default {

computed: {
...mapGetters('Files', ['selectedFiles']),
...mapGetters(['user']),
...mapGetters(['user', 'configuration', 'capabilities']),
...mapGetters('runtime/auth', { isPublicLinkContext: 'isPublicLinkContextReady' }),
...mapGetters('runtime/auth', ['accessToken']),

$_deleteResources_isInTrashbin() {
return (
Expand Down Expand Up @@ -97,6 +99,7 @@ export default {
methods: {
...mapActions('Files', ['pushResourcesToDeleteList', 'removeFilesFromTrashbin', 'deleteFiles']),
...mapActions(['showMessage', 'toggleModalConfirmButton', 'hideModal', 'createModal']),
...mapMutations('Files', ['UPDATE_SPACE_FIELD']),
...mapMutations(['SET_QUOTA']),

$_deleteResources_trashbin_deleteOp(resource) {
Expand Down Expand Up @@ -160,9 +163,27 @@ export default {
this.toggleModalConfirmButton()

// Load quota
if (this.user?.id) {
kulmann marked this conversation as resolved.
Show resolved Hide resolved
const user = await this.$client.users.getUser(this.user.id)
this.SET_QUOTA(user.quota)
if (
isLocationSpacesActive(this.$router, 'files-spaces-project') ||
isLocationSpacesActive(this.$router, 'files-spaces-personal')
) {
if (this.capabilities?.spaces?.enabled) {
const graphClient = clientService.graphAuthenticated(
this.configuration.server,
this.accessToken
)
const driveResponse = await graphClient.drives.getDrive(
this.$_deleteResources_resources[0].storageId
)
this.UPDATE_SPACE_FIELD({
id: driveResponse.data.id,
field: 'spaceQuota',
value: driveResponse.data.quota
})
} else {
const user = await this.$client.users.getUser(this.user.id)
this.SET_QUOTA(user.quota)
}
}

let parentFolderPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ export class FolderLoaderLegacyPersonal implements FolderLoader {
currentFolder,
files: resources
})

// fetch user quota
;(async () => {
const user = await client.users.getUser(router.currentRoute.params.storageId)
store.commit('SET_QUOTA', user.quota)
})()
} catch (error) {
store.commit('Files/SET_CURRENT_FOLDER', null)
console.error(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ export class FolderLoaderSpacesPersonal implements FolderLoader {
currentFolder,
files: resources
})

// 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`SpaceQuota component renders the space storage quota label 1`] = `
<div class="space-quota">
<p class="oc-mb-s oc-mt-rm">1 B of 10 B used (10.0% used)</p>
<p class="oc-mb-s oc-mt-rm">1 B of 10 B used (10% used)</p>
<oc-progress-stub value="10" max="100" size="small" variation="primary"></oc-progress-stub>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ function getWrapper({ invalidLocation = false, resolveClearTrashBin: resolveRest
getters: {
configuration: () => ({
server: 'https://example.com'
})
}),
capabilities: () => {}
},
modules: {
user: {
Expand All @@ -119,6 +120,9 @@ function getWrapper({ invalidLocation = false, resolveClearTrashBin: resolveRest
removeFilesFromTrashbin: jest.fn()
}
}
},
mutations: {
SET_QUOTA: () => jest.fn()
}
})
})
Expand Down
23 changes: 19 additions & 4 deletions packages/web-app-files/tests/unit/mixins/deleteResources.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Vuex from 'vuex'
import { createStore } from 'vuex-extensions'
import { mount, createLocalVue } from '@vue/test-utils'
import deleteResources from '@files/src/mixins/deleteResources.js'
import { createLocationSpaces } from '../../../src/router'

const localVue = createLocalVue()
localVue.use(Vuex)
Expand All @@ -27,19 +28,21 @@ describe('deleteResources', () => {
const resourcesToDelete = [{ id: 2, path: '/' }]
const wrapper = getWrapper(resourcesToDelete)
const spyHideModalStub = jest.spyOn(wrapper.vm, 'hideModal')
const spyRouterPushStub = jest.spyOn(wrapper.vm.$router, 'push')
await wrapper.vm.$_deleteResources_filesList_delete()
await wrapper.vm.$nextTick()
expect(wrapper.vm.$router.length).toBeGreaterThanOrEqual(0)
expect(spyRouterPushStub).toHaveBeenCalledTimes(0)
expect(spyHideModalStub).toHaveBeenCalledTimes(1)
})

it('should call the delete action on the current folder', async () => {
const resourcesToDelete = [currentFolder]
const wrapper = getWrapper(resourcesToDelete)
const spyHideModalStub = jest.spyOn(wrapper.vm, 'hideModal')
const spyRouterPushStub = jest.spyOn(wrapper.vm.$router, 'push')
await wrapper.vm.$_deleteResources_filesList_delete()
await wrapper.vm.$nextTick()
expect(wrapper.vm.$router.length).toBeGreaterThanOrEqual(1)
expect(spyRouterPushStub).toHaveBeenCalledTimes(1)
expect(spyHideModalStub).toHaveBeenCalledTimes(1)
})
})
Expand All @@ -52,7 +55,15 @@ function getWrapper(resourcesToDelete) {
$route: {
name: 'files-personal'
},
$router: [],
$router: {
currentRoute: createLocationSpaces('files-spaces-personal'),
resolve: (r) => {
return {
href: r.name
}
},
push: jest.fn()
},
$client: {
users: {
getUser: jest.fn(() => user)
Expand All @@ -67,7 +78,11 @@ function getWrapper(resourcesToDelete) {
getters: {
user: () => {
return { id: 'marie' }
}
},
configuration: () => ({
server: 'https://example.com'
}),
capabilities: () => {}
},
modules: {
Files: {
Expand Down
Loading