Skip to content

Commit

Permalink
feat: Update a page when you are the last modifier
Browse files Browse the repository at this point in the history
Check the accountId who did the last version. If it isn't your accountId then don't update the page.
  • Loading branch information
andymac4182 committed Apr 18, 2023
1 parent 29a195b commit 5c42d77
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
25 changes: 23 additions & 2 deletions packages/lib/src/Publisher.ts
Expand Up @@ -69,12 +69,14 @@ export interface ConfluenceAdfFile {
export interface ConfluenceNode {
file: ConfluenceAdfFile;
version: number;
lastUpdatedBy: string,
existingAdf: string;
parentPageId: string;
}
export interface ConfluenceTreeNode {
file: ConfluenceAdfFile;
version: number;
lastUpdatedBy: string,
existingAdf: string;
children: ConfluenceTreeNode[];
}
Expand All @@ -91,6 +93,7 @@ export class Publisher {
adaptor: LoaderAdaptor;
settings: ConfluenceSettings;
mermaidRenderer: MermaidRenderer;
myAccountId: string | undefined;

constructor(
adaptor: LoaderAdaptor,
Expand All @@ -106,6 +109,11 @@ export class Publisher {
}

async publish(publishFilter?: string) {
if(!this.myAccountId) {
const currentUser = await this.confluenceClient.users.getCurrentUser();
this.myAccountId = currentUser.accountId;
}

const parentPage = await this.confluenceClient.content.getContentById({
id: this.settings.confluenceParentId,
expand: ["body.atlas_doc_format", "space"],
Expand Down Expand Up @@ -177,14 +185,22 @@ export class Publisher {
node.parentPageId,
node.version,
node.existingAdf,
node.file
node.file,
node.lastUpdatedBy,
);

return {
node,
successfulUploadResult,
};
} catch (e: unknown) {
if (e instanceof Error) {
return {
node,
reason: e.message,
};
}

return {
node,
reason: JSON.stringify(e), // TODO: Understand why this doesn't show error message properly
Expand All @@ -196,8 +212,13 @@ export class Publisher {
parentPageId: string,
pageVersionNumber: number,
currentContents: string,
adfFile: ConfluenceAdfFile
adfFile: ConfluenceAdfFile,
lastUpdatedBy: string,
): Promise<UploadAdfFileResult> {
if (lastUpdatedBy !== this.myAccountId) {
throw new Error(`Page last updated by another user. Won't publish over their changes. MyAccountId: ${this.myAccountId}, Last Updated By: ${lastUpdatedBy}`);
}

const result: UploadAdfFileResult = {
adfFile,
contentResult: "same",
Expand Down
9 changes: 8 additions & 1 deletion packages/lib/src/TreeConfluence.ts
Expand Up @@ -15,12 +15,13 @@ function flattenTree(
parentPageId?: string
): ConfluenceNode[] {
const nodes: ConfluenceNode[] = [];
const { file, version, existingAdf, children } = node;
const { file, version, lastUpdatedBy, existingAdf, children } = node;

if (parentPageId) {
nodes.push({
file,
version,
lastUpdatedBy,
existingAdf,
parentPageId: parentPageId,
});
Expand Down Expand Up @@ -73,6 +74,7 @@ async function createFileStructureInConfluence(

let version: number;
let existingAdf: string | undefined;
let lastUpdatedBy: string | undefined;
const file: ConfluenceAdfFile = {
...node.file,
pageId: parentPageId,
Expand All @@ -92,6 +94,7 @@ async function createFileStructureInConfluence(
file.spaceKey = pageDetails.spaceKey;
version = pageDetails.version;
existingAdf = pageDetails.existingAdf;
lastUpdatedBy = pageDetails.lastUpdatedBy;
} else {
version = 0;
existingAdf = "";
Expand All @@ -114,6 +117,7 @@ async function createFileStructureInConfluence(
return {
file: file,
version,
lastUpdatedBy: lastUpdatedBy ?? "",
existingAdf: existingAdf ?? "",
children: childDetails,
};
Expand Down Expand Up @@ -141,6 +145,7 @@ async function ensurePageExists(
id: contentById.id,
title: file.pageTitle,
version: contentById?.version?.number ?? 1,
lastUpdatedBy: contentById?.version?.by?.accountId ?? "NO ACCOUNT ID",
existingAdf: contentById?.body?.atlas_doc_format?.value,
spaceKey: contentById.space.key,
};
Expand Down Expand Up @@ -175,6 +180,7 @@ async function ensurePageExists(
id: currentPage.id,
title: file.pageTitle,
version: currentPage?.version?.number ?? 1,
lastUpdatedBy: currentPage?.version?.by?.accountId ?? "NO ACCOUNT ID",
existingAdf: currentPage?.body?.atlas_doc_format?.value,
spaceKey,
};
Expand Down Expand Up @@ -204,6 +210,7 @@ async function ensurePageExists(
id: pageDetails.id,
title: file.pageTitle,
version: pageDetails?.version?.number ?? 1,
lastUpdatedBy: pageDetails?.version?.by?.accountId ?? "NO ACCOUNT ID",
existingAdf: pageDetails?.body?.atlas_doc_format?.value,
spaceKey,
};
Expand Down
3 changes: 3 additions & 0 deletions packages/lib/src/adaptors/index.ts
Expand Up @@ -34,4 +34,7 @@ export interface CustomConfluenceClient {
space: Api.Space;
contentAttachments: Api.ContentAttachments;
contentLabels: Api.ContentLabels;
users: Api.Users;
}


1 change: 1 addition & 0 deletions packages/obsidian/src/MyBaseClient.ts
Expand Up @@ -197,4 +197,5 @@ export class CustomConfluenceClient extends MyBaseClient {
space = new Api.Space(this);
contentAttachments = new Api.ContentAttachments(this);
contentLabels = new Api.ContentLabels(this);
users = new Api.Users(this);
}

0 comments on commit 5c42d77

Please sign in to comment.