Skip to content

Commit

Permalink
fix(controller): remove deleted resources from the index file
Browse files Browse the repository at this point in the history
  • Loading branch information
NuttyShrimp committed Nov 18, 2024
1 parent 60bc267 commit 2313d3f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
33 changes: 30 additions & 3 deletions controller/src/classes/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,33 @@ export class Controller<T extends Record<keyof T, BaseSubject<keyof T & string>>
const configs: SubjectConfig<T>[] = Object.values(this.subjectConfigs);

const index = await this.store.getCurrentIndex();
const resourcesToSkip = index.items.filter(i => i.resource.includes(containerUrl));
const resourcesToSkip = index.items.filter(i => {
if (!i.resource.includes(containerUrl)) {
return false;
}
const strippedURI = i.resource.replace(containerUrl, "");
const slashIdx = strippedURI.indexOf("/");
if (slashIdx < 0) {
return true;
}
return !strippedURI.includes("/", slashIdx + 1)
});

const results = await Promise.allSettled(configs.map(c => c.manager.getContainerPermissionList(containerUrl, resourcesToSkip.map(i => i.resource))))
const resourcesToClean = resourcesToSkip.filter(r => r.isEnabled).map(r => r.resource);

if (results.length !== 0) {
index.items.push(...results.reduce<IndexItem<T[SubjectKey<T>]>[]>((arr, v) => {
if (v.status === "fulfilled") {
// Check if the resourceUrl is already present before pushing it into the array
v.value.forEach((r) => {
for (let i = 0; i < resourcesToClean.length; i++) {
const rtc = resourcesToClean[i];
if (r.resourceUrl.includes(rtc)) {
resourcesToClean.splice(i, 1);
i--;
}
}
// @ts-expect-error
arr.push(...r.permissionsPerSubject.map(p => ({
id: crypto.randomUUID(),
Expand All @@ -217,10 +235,19 @@ export class Controller<T extends Record<keyof T, BaseSubject<keyof T & string>>
}
return arr;
}, []));

await this.store.saveToRemoteIndex();
}

await Promise.allSettled(resourcesToClean.map(async r => {
for (let i = 0; i < index.items.length; i++) {
let entry = index.items[i];
if (!entry.resource.includes(r)) {
continue
}
index.items.splice(i, 1);
}
}));

await this.store.saveToRemoteIndex();

return index.items.reduce<ResourcePermissions<T[keyof T]>[]>((arr, v) => {
const resourcePath = v.resource.replace(containerUrl, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,18 @@ export abstract class InruptPermissionManager<T extends Record<keyof T, BaseSubj
const dataset = await getSolidDataset(containerUrl, { fetch: session.fetch });
const results = await Promise.allSettled(
getThingAll(dataset)
.filter(r => !resourceToSkip?.includes(r.url))
.map(async (resource) => ({
resourceUrl: resource.url,
permissionsPerSubject: await this.getRemotePermissions(resource.url)
}))
.map(async (resource) => {
if (resourceToSkip.includes(resource.url)) {
return {
resourceUrl: resource.url,
permissionsPerSubject: [],
}
}
return {
resourceUrl: resource.url,
permissionsPerSubject: await this.getRemotePermissions(resource.url)
}
})
)
return results.reduce<ResourcePermissions<T[keyof T]>[]>((arr, v) => {
if (v.status == "fulfilled") {
Expand Down
4 changes: 3 additions & 1 deletion controller/src/types/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ export interface IPermissionManager<T = Record<string, BaseSubject<string>>> {
deletePermissions<K extends SubjectKey<T>>(resource: string, subject: T[K]): Promise<void>
getRemotePermissions<K extends SubjectKey<T>>(resourceUrl: string): Promise<SubjectPermissions<T[K]>[]>
/**
* Retrieve the permissions of the resources in this container.
* Retrieve the permissions of the resources in this container.
* It will add the skipped resources to the returning object but without the permissions assignments.
* As this is necessary to clean-up the index
* Will probably work for a resource, but not guaranteed. Use getRemotePermissions for that
*/
getContainerPermissionList(containerUrl: string, resourceToSkip?: string[]): Promise<ResourcePermissions<T[keyof T]>[]>
Expand Down

0 comments on commit 2313d3f

Please sign in to comment.