Skip to content

Commit

Permalink
Implement retrieveConfluenceResourceParents
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd committed Jan 24, 2024
1 parent b8ed9a9 commit ec1cada
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
68 changes: 68 additions & 0 deletions connectors/src/connectors/confluence/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type { ConfluenceSpaceType } from "@connectors/connectors/confluence/lib/
import {
getIdFromConfluencePublicId,
isConfluencePublicPageId,
isConfluencePublicSpaceId,
makeConfluencePublicPageId,
makeConfluencePublicSpaceId,
} from "@connectors/connectors/confluence/lib/internal_ids";
Expand Down Expand Up @@ -377,3 +378,70 @@ export async function retrieveConfluenceObjectsTitles(

return new Ok(titles);
}

export async function retrieveConfluenceResourceParents(
connectorId: ModelId,
publicId: string
): Promise<Result<string[], Error>> {
const confluenceId = getIdFromConfluencePublicId(publicId);

if (isConfluencePublicPageId(publicId)) {
const currentPage = await ConfluencePage.findOne({
attributes: ["pageId", "parentId", "spaceId"],
where: {
connectorId,
pageId: confluenceId,
},
});

if (!currentPage) {
return new Err(new Error("Confluence page not found."));
}

// If the page does not have a parentId, return only the spaceId.
if (!currentPage.parentId) {
return new Ok([makeConfluencePublicSpaceId(currentPage.spaceId)]);
}

// Currently opting for a best-effort strategy to reduce database queries,
// this logic may be enhanced later for important Confluence connections.
// By fetching all pages within a space, we reconstruct parent-child
// relationships in-app, minimizing database interactions.
const allPages = await ConfluencePage.findAll({
attributes: ["pageId", "parentId"],
where: {
connectorId,
spaceId: currentPage.spaceId,
},
});

// Map each pageId to its respective parentId.
const pageIdToParentIdMap = new Map(
allPages.map((page) => [page.pageId, page.parentId])
);

const parentIds = [];
let currentId = currentPage.pageId;

// Traverse the hierarchy upwards until no further parent IDs are found.
while (pageIdToParentIdMap.has(currentId)) {
const parentId = pageIdToParentIdMap.get(currentId);
if (parentId) {
parentIds.push(parentId);
// Move up the hierarchy.
currentId = parentId;
} else {
// No more parents, exit the loop.
break;
}
}

return new Ok([
// Add the space id at the beginning.
makeConfluencePublicSpaceId(currentPage.spaceId),
...parentIds.map((p) => makeConfluencePublicPageId(p)),
]);
}

return new Ok([]);
}
3 changes: 2 additions & 1 deletion connectors/src/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
resumeConfluenceConnector,
retrieveConfluenceConnectorPermissions,
retrieveConfluenceObjectsTitles,
retrieveConfluenceResourceParents,
setConfluenceConnectorPermissions,
stopConfluenceConnector,
updateConfluenceConnector,
Expand Down Expand Up @@ -252,7 +253,7 @@ export const RETRIEVE_RESOURCE_PARENTS_BY_TYPE: Record<
ConnectorProvider,
ConnectorResourceParentsRetriever
> = {
confluence: async () => new Ok([]), // Confluence is flat.
confluence: retrieveConfluenceResourceParents,
notion: retrieveNotionResourceParents,
google_drive: retrieveGoogleDriveObjectsParents,
slack: async () => new Ok([]), // Slack is flat
Expand Down

0 comments on commit ec1cada

Please sign in to comment.