Skip to content

Commit

Permalink
Use confluence ids to render permissions view
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd committed Jan 24, 2024
1 parent d44f960 commit b8ed9a9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 60 deletions.
20 changes: 10 additions & 10 deletions connectors/src/connectors/confluence/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import {
} from "@connectors/connectors/confluence/lib/confluence_api";
import type { ConfluenceSpaceType } from "@connectors/connectors/confluence/lib/confluence_client";
import {
getIdFromConfluenceInternalId,
isConfluenceInternalPageId,
makeConfluenceInternalPageId,
getIdFromConfluencePublicId,
isConfluencePublicPageId,
makeConfluencePublicPageId,
makeConfluencePublicSpaceId,
} from "@connectors/connectors/confluence/lib/internal_ids";
import {
retrieveAvailableSpaces,
Expand Down Expand Up @@ -298,9 +299,8 @@ export async function setConfluenceConnectorPermissions(

const addedSpaceIds = [];
const removedSpaceIds = [];
// Operate on Confluence native IDs instead of internal IDs because the database
// only stores a selection of user-specified spaces, identified by their Confluence IDs (spaceId).
for (const [confluenceId, permission] of Object.entries(permissions)) {
for (const [publicId, permission] of Object.entries(permissions)) {
const confluenceId = getIdFromConfluencePublicId(publicId);
if (permission === "none") {
await ConfluenceSpace.destroy({
where: {
Expand Down Expand Up @@ -355,15 +355,15 @@ export async function retrieveConfluenceObjectsTitles(
connectorId: ModelId,
internalIds: string[]
): Promise<Result<Record<string, string>, Error>> {
const ids = internalIds
.map((id) => getIdFromConfluenceInternalId(id))
.filter((id): id is number => Boolean(id));
const confluenceSpaceIds = internalIds.map((id) =>
getIdFromConfluencePublicId(id)
);

const confluenceSpaces = await ConfluenceSpace.findAll({
attributes: ["id", "spaceId", "name"],
where: {
connectorId: connectorId,
id: ids,
spaceId: confluenceSpaceIds,
},
});

Expand Down
19 changes: 8 additions & 11 deletions connectors/src/connectors/confluence/lib/internal_ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@ enum ConfluenceInternalIdPrefix {
Page = "page_",
}

export function makeConfluenceInternalSpaceId(spaceId: string) {
return `${ConfluenceInternalIdPrefix.Space}${spaceId}`;
export function makeConfluencePublicSpaceId(confluenceSpaceId: string) {
return `${ConfluenceInternalIdPrefix.Space}${confluenceSpaceId}`;
}

export function makeConfluenceInternalPageId(pageId: number) {
return `${ConfluenceInternalIdPrefix.Page}${pageId}`;
export function makeConfluencePublicPageId(confluencePageId: string) {
return `${ConfluenceInternalIdPrefix.Page}${confluencePageId}`;
}

export function getIdFromConfluenceInternalId(internalId: string) {
export function getIdFromConfluencePublicId(internalId: string) {
const prefixPattern = `^(${ConfluenceInternalIdPrefix.Space}|${ConfluenceInternalIdPrefix.Page})`;
const id = internalId.replace(new RegExp(prefixPattern), "");

const parsedInt = parseInt(id, 10);
return isNaN(parsedInt) ? null : parsedInt;
return internalId.replace(new RegExp(prefixPattern), "");
}

export function isConfluenceInternalSpaceId(
export function isConfluencePublicSpaceId(
internalId: string
): internalId is `${ConfluenceInternalIdPrefix.Space}${string}` {
return internalId.startsWith(ConfluenceInternalIdPrefix.Space);
}

export function isConfluenceInternalPageId(
export function isConfluencePublicPageId(
internalId: string
): internalId is `${ConfluenceInternalIdPrefix.Page}${string}` {
return internalId.startsWith(ConfluenceInternalIdPrefix.Page);
Expand Down
71 changes: 32 additions & 39 deletions connectors/src/connectors/confluence/lib/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { Op } from "sequelize";
import { listConfluenceSpaces } from "@connectors/connectors/confluence/lib/confluence_api";
import type { ConfluenceSpaceType } from "@connectors/connectors/confluence/lib/confluence_client";
import {
getIdFromConfluenceInternalId,
isConfluenceInternalPageId,
isConfluenceInternalSpaceId,
makeConfluenceInternalPageId,
makeConfluenceInternalSpaceId,
getIdFromConfluencePublicId,
isConfluencePublicPageId,
isConfluencePublicSpaceId,
makeConfluencePublicPageId,
makeConfluencePublicSpaceId,
} from "@connectors/connectors/confluence/lib/internal_ids";
import type { Connector } from "@connectors/lib/models";
import type { ConfluenceConfiguration } from "@connectors/lib/models/confluence";
Expand All @@ -27,17 +27,16 @@ function createConnectorResourceFromSpace(
space: ConfluenceSpace | ConfluenceSpaceType,
baseUrl: string,
permission: ConnectorPermission,
{
isExpandable,
usesInternalId,
}: { isExpandable: boolean; usesInternalId: boolean }
{ isExpandable, usePublicId }: { isExpandable: boolean; usePublicId: boolean }
): ConnectorResource {
const urlSuffix = "_links" in space ? space._links.webui : space.urlSuffix;
// This is not needed.
const spaceId = typeof space.id === "string" ? space.id : space.spaceId;

return {
provider: "confluence",
internalId: usesInternalId
? makeConfluenceInternalSpaceId(space.id.toString())
internalId: usePublicId
? makeConfluencePublicSpaceId(spaceId)
: space.id.toString(),
parentInternalId: null,
type: "folder",
Expand All @@ -51,18 +50,18 @@ function createConnectorResourceFromSpace(
}

function createConnectorResourceFromPage(
parent: { id: ModelId; type: "page" | "space" },
parent: { id: string; type: "page" | "space" },
baseUrl: string,
page: ConfluencePage,
isExpandable = false
): ConnectorResource {
return {
provider: "confluence",
internalId: makeConfluenceInternalPageId(page.id),
parentInternalId:
internalId: makeConfluencePublicPageId(page.pageId),
parentPublicId:
parent.type === "space"
? makeConfluenceInternalSpaceId(parent.id.toString())
: makeConfluenceInternalPageId(parent.id),
? makeConfluencePublicSpaceId(parent.id)
: makeConfluencePublicPageId(parent.id),
type: "file",
title: page.title,
sourceUrl: `${baseUrl}/wiki${page.externalUrl}`,
Expand All @@ -87,18 +86,15 @@ async function checkPageHasChildren(connectorId: ModelId, pageId: string) {
async function getSynchronizedSpaces(
connectorId: ModelId,
confluenceConfig: ConfluenceConfiguration,
parentInternalId: string
parentPublicId: string
): Promise<Result<ConnectorResource[], Error>> {
const internalId = getIdFromConfluenceInternalId(parentInternalId);
if (!internalId) {
return new Err(new Error("Invalid Confluence internal id."));
}
const confluenceId = getIdFromConfluencePublicId(parentPublicId);

const parentSpace = await ConfluenceSpace.findOne({
attributes: ["id", "spaceId"],
where: {
connectorId,
id: internalId,
spaceId: confluenceId,
},
});

Expand All @@ -122,7 +118,7 @@ async function getSynchronizedSpaces(
const hasChildren = await checkPageHasChildren(connectorId, page.pageId);

const res = createConnectorResourceFromPage(
{ id: parentSpace.id, type: "space" },
{ id: parentSpace.spaceId, type: "space" },
confluenceConfig.url,
page,
hasChildren
Expand All @@ -137,18 +133,15 @@ async function getSynchronizedSpaces(
async function getSynchronizedChildrenPages(
connectorId: ModelId,
confluenceConfig: ConfluenceConfiguration,
parentInternalId: string
parentPublicId: string
): Promise<Result<ConnectorResource[], Error>> {
const internalId = getIdFromConfluenceInternalId(parentInternalId);
if (!internalId) {
return new Err(new Error("Invalid Confluence internal id."));
}
const confluenceId = getIdFromConfluencePublicId(parentPublicId);

const parentPage = await ConfluencePage.findOne({
attributes: ["id", "pageId"],
where: {
connectorId,
id: internalId,
pageId: confluenceId,
},
});

Expand All @@ -169,7 +162,7 @@ async function getSynchronizedChildrenPages(
const hasChildren = await checkPageHasChildren(connectorId, page.pageId);

const res = createConnectorResourceFromPage(
{ id: parentPage.id, type: "page" },
{ id: parentPage.pageId, type: "page" },
confluenceConfig.url,
page,
hasChildren
Expand All @@ -184,28 +177,28 @@ async function getSynchronizedChildrenPages(
export async function retrieveSynchronizedData(
connector: Connector,
confluenceConfig: ConfluenceConfiguration,
parentInternalId: string | null
parentPublicId: string | null
) {
const { id: connectorId } = connector;

if (parentInternalId) {
if (isConfluenceInternalSpaceId(parentInternalId)) {
if (parentPublicId) {
if (isConfluencePublicSpaceId(parentPublicId)) {
const resources = await getSynchronizedSpaces(
connectorId,
confluenceConfig,
parentInternalId
parentPublicId
);

if (resources.isErr()) {
return new Err(resources.error);
}

return new Ok(resources.value);
} else if (isConfluenceInternalPageId(parentInternalId)) {
} else if (isConfluencePublicPageId(parentPublicId)) {
const resources = await getSynchronizedChildrenPages(
connectorId,
confluenceConfig,
parentInternalId
parentPublicId
);

if (resources.isErr()) {
Expand All @@ -214,7 +207,7 @@ export async function retrieveSynchronizedData(

return new Ok(resources.value);
} else {
return new Err(new Error("Invalid Confluence internal id."));
return new Err(new Error("Invalid resource id."));
}
}

Expand All @@ -227,7 +220,7 @@ export async function retrieveSynchronizedData(
const allSpaces = syncedSpaces.map((space) =>
createConnectorResourceFromSpace(space, confluenceConfig.url, "read", {
isExpandable: true,
usesInternalId: true,
usePublicId: true,
})
);

Expand Down Expand Up @@ -255,7 +248,7 @@ export async function retrieveAvailableSpaces(
space,
confluenceConfig.url,
isSynced ? "read" : "none",
{ isExpandable: false, usesInternalId: false }
{ isExpandable: false, usePublicId: false }
);
});
}

0 comments on commit b8ed9a9

Please sign in to comment.