Skip to content

Commit

Permalink
feat(list-folders)
Browse files Browse the repository at this point in the history
- skip broken entities if they are broken due to wrong privacy state
  • Loading branch information
thiagocarvalhodev committed Jul 22, 2024
1 parent 7b6ec3d commit 1212f9f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
29 changes: 20 additions & 9 deletions src/arfs/arfsdao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1528,16 +1528,27 @@ export class ArFSDAO extends ArFSDAOAnonymous {
const { edges } = transactions;
hasNextPage = transactions.pageInfo.hasNextPage;

const folders: Promise<ArFSPrivateFolder>[] = edges.map(async (edge: GQLEdgeInterface) => {
cursor = edge.cursor;
const { node } = edge;
const folderBuilder = ArFSPrivateFolderBuilder.fromArweaveNode(node, this.gatewayApi, driveKey);
// Build the folder so that we don't add something invalid to the cache
const folder = await folderBuilder.build(node);
const cacheKey = { folderId: folder.entityId, owner, driveKey };
return this.caches.privateFolderCache.put(cacheKey, Promise.resolve(folder));
const folderPromises: Promise<ArFSPrivateFolder | null>[] = edges.map(async (edge: GQLEdgeInterface) => {
try {
cursor = edge.cursor;
const { node } = edge;
const folderBuilder = ArFSPrivateFolderBuilder.fromArweaveNode(node, this.gatewayApi, driveKey);
// Build the folder so that we don't add something invalid to the cache
const folder = await folderBuilder.build(node);
const cacheKey = { folderId: folder.entityId, owner, driveKey };
return this.caches.privateFolderCache.put(cacheKey, Promise.resolve(folder));
} catch (e) {
// If the folder is broken, skip it
console.error(`Error building folder: ${e}`);
return null;
}
});
allFolders.push(...(await Promise.all(folders)));

const folders = await Promise.all(folderPromises);

// Filter out null values
const validFolders = folders.filter((f) => f !== null) as ArFSPrivateFolder[];
allFolders.push(...(await Promise.all(validFolders)));
}

return latestRevisionsOnly ? allFolders.filter(latestRevisionFilter) : allFolders;
Expand Down
28 changes: 22 additions & 6 deletions src/arfs/arfsdao_anonymous.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prettier/prettier */
/* eslint-disable no-console */
import Arweave from 'arweave';
import {
Expand Down Expand Up @@ -304,15 +305,28 @@ export class ArFSDAOAnonymous extends ArFSDAOType {
const { edges } = transactions;

hasNextPage = transactions.pageInfo.hasNextPage;
const folders: Promise<ArFSPublicFolder>[] = edges.map(async (edge: GQLEdgeInterface) => {
const folderPromises = edges.map(async (edge: GQLEdgeInterface) => {
const { node } = edge;
cursor = edge.cursor;
const folderBuilder = ArFSPublicFolderBuilder.fromArweaveNode(node, this.gatewayApi);
const folder = await folderBuilder.build(node);
const cacheKey = { folderId: folder.entityId, owner };
return this.caches.publicFolderCache.put(cacheKey, Promise.resolve(folder));
try {
const folderBuilder = ArFSPublicFolderBuilder.fromArweaveNode(node, this.gatewayApi);
const folder = await folderBuilder.build(node);
const cacheKey = { folderId: folder.entityId, owner };
await this.caches.publicFolderCache.put(cacheKey, Promise.resolve(folder));
return folder;
} catch (e) {
// If the folder is broken, skip it
console.error(`Error building folder: ${e}`);
return null;
}
});
allFolders.push(...(await Promise.all(folders)));

const folders = await Promise.all(folderPromises);

// Filter out null values
const validFolders = folders.filter(folder => folder !== null) as ArFSPublicFolder[];

allFolders.push(...validFolders);
}
return latestRevisionsOnly ? allFolders.filter(latestRevisionFilter) : allFolders;
}
Expand All @@ -338,6 +352,7 @@ export class ArFSDAOAnonymous extends ArFSDAOType {

// Fetch all of the folder entities within the drive
const driveIdOfFolder = folder.driveId;

const allFolderEntitiesOfDrive = await this.getAllFoldersOfPublicDrive({
driveId: driveIdOfFolder,
owner,
Expand Down Expand Up @@ -375,6 +390,7 @@ export class ArFSDAOAnonymous extends ArFSDAOType {
}

const entitiesWithPath = children.map((entity) => publicEntityWithPathsFactory(entity, hierarchy));

return entitiesWithPath;
}

Expand Down

0 comments on commit 1212f9f

Please sign in to comment.