Skip to content

Commit

Permalink
Reduce calls to fetch buckets and bucket permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
TimCsaky committed Feb 14, 2024
1 parent e902525 commit 6100713
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 43 deletions.
18 changes: 0 additions & 18 deletions frontend/src/components/object/ObjectList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@ const showUpload = () => {
const closeUpload = () => {
displayUpload.value = false;
};
// const updateBreadcrumb = async () => {
// try {
// const bucket = await bucketStore.getBucketInfo(props.bucketId as string);
// navStore.replace('__listObjectsDynamic', bucket?.bucketName ?? 'Unknown bucket');
// } catch (error: any) {
// toast.add({ severity: 'error', summary: 'Unable to load bucket information.', detail: error, life: 5000 });
// }
// };
onMounted(async () => {
// Removed for now
// updateBreadcrumb();
await bucketStore.fetchBuckets({ userId: getUserId.value, objectPerms: true });
// TODO: userId+bucketPerms bringing back deleted files??
await objectStore.fetchObjects({ bucketId: props.bucketId, userId: getUserId.value, bucketPerms: true });
});
</script>

<template>
Expand Down
30 changes: 18 additions & 12 deletions frontend/src/store/bucketStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,39 @@ export const useBucketStore = defineStore('bucket', () => {
appStore.endIndeterminateLoading();
}
}

/**
* function does the following in order:
* - fetches bucket permissions
* (fetchBucketPermissions() also add bucket permissions to the permission store)
* - pass bucketId's of buckets with a permission to searchBuckets()
* - add buckets to store (skipping existing matches)
* @param params search parameters
* @returns an array of matching buckets found
*/
async function fetchBuckets(params?: BucketSearchPermissionsOptions) {
try {
appStore.beginIndeterminateLoading();

// Get a unique list of bucket IDs the user has access to
const permResponse = await permissionStore.fetchBucketPermissions(params);
// if permissions found
if (permResponse) {
const uniqueIds: Array<string> = [
...new Set<string>(permResponse.map((x: { bucketId: string }) => x.bucketId))
];

let response = Array<Bucket>();
if (uniqueIds.length) {
response = (await bucketService.searchBuckets({ bucketId: uniqueIds })).data;
response = (await bucketService.searchBuckets({ bucketId: uniqueIds })).data;

// Remove old values matching search parameters
const matches = (x: Bucket) => !params?.bucketId || x.bucketId === params.bucketId;
// Remove old values matching search parameters
const matches = (x: Bucket) => !params?.bucketId || x.bucketId === params.bucketId;

const [, difference] = partition(state.buckets.value, matches);
const [, difference] = partition(state.buckets.value, matches);

// Merge and assign
state.buckets.value = difference.concat(response);
} else {
state.buckets.value = response;
}
}
// Merge and assign
state.buckets.value = difference.concat(response);
return response;
} else return [];
} catch (error: any) {
toast.error('Fetching buckets', error);
} finally {
Expand Down
23 changes: 10 additions & 13 deletions frontend/src/views/list/ListObjectsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ const { getUserId } = storeToRefs(useAuthStore());
const ready: Ref<boolean> = ref(false);
const bucket: Ref<Bucket | undefined> = ref(undefined);
// Actions
async function getBucketName() {
bucket.value = props.bucketId ? await bucketStore.findBucketById(props.bucketId) : undefined;
}
onErrorCaptured((e: Error) => {
const toast = useToast();
toast.error('Loading bucket', e.message);
Expand All @@ -42,17 +37,19 @@ onErrorCaptured((e: Error) => {
onBeforeMount(async () => {
const router = useRouter();
const permResponse = await permissionStore.fetchBucketPermissions({ userId: getUserId.value, objectPerms: true });
if (!permResponse?.some((x: BucketPermission) => x.bucketId === props.bucketId)) {
router.replace({ name: RouteNames.FORBIDDEN });
} else {
// fetch bucktes (which is already scoped by cur user's permissions) and populates bucket and permissions in store
const bucketResponse = await bucketStore.fetchBuckets({
bucketId: props.bucketId,
userId: getUserId.value,
objectPerms: true
});
if (bucketResponse?.length) {
bucket.value = bucketResponse[0];
ready.value = true;
} else {
router.replace({ name: RouteNames.FORBIDDEN });
}
});
onMounted(() => {
getBucketName();
});
</script>

<template>
Expand Down

0 comments on commit 6100713

Please sign in to comment.