Skip to content

Commit 6e5c9d4

Browse files
authored
ENG-1285 Base user affordance to trigger node sharing (#698)
* eng-1285 base user affordance and base publish
1 parent 1bbe43e commit 6e5c9d4

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { FrontMatterCache, TFile } from "obsidian";
2+
import type { default as DiscourseGraphPlugin } from "~/index";
3+
import { getLoggedInClient, getSupabaseContext } from "./supabaseContext";
4+
5+
export const publishNode = async ({
6+
plugin,
7+
file,
8+
frontmatter,
9+
}: {
10+
plugin: DiscourseGraphPlugin;
11+
file: TFile;
12+
frontmatter: FrontMatterCache;
13+
}): Promise<void> => {
14+
const nodeId = frontmatter.nodeInstanceId as string | undefined;
15+
if (!nodeId) throw new Error("Please sync the node first");
16+
const client = await getLoggedInClient(plugin);
17+
if (!client) throw new Error("Cannot get client");
18+
const context = await getSupabaseContext(plugin);
19+
if (!context) throw new Error("Cannot get context");
20+
const spaceId = context.spaceId;
21+
const myGroupResponse = await client
22+
.from("group_membership")
23+
.select("group_id");
24+
if (myGroupResponse.error) throw myGroupResponse.error;
25+
const myGroup = myGroupResponse.data[0]?.group_id;
26+
if (!myGroup) throw new Error("Cannot get group");
27+
const existingPublish =
28+
(frontmatter.publishedToGroups as undefined | string[]) || [];
29+
if (existingPublish.includes(myGroup)) return; // already published
30+
const publishResponse = await client.from("ResourceAccess").insert({
31+
/* eslint-disable @typescript-eslint/naming-convention */
32+
account_uid: myGroup,
33+
source_local_id: nodeId,
34+
space_id: spaceId,
35+
/* eslint-enable @typescript-eslint/naming-convention */
36+
});
37+
if (publishResponse.error && publishResponse.error.code !== "23505")
38+
// 23505 is duplicate key, which counts as a success.
39+
throw publishResponse.error;
40+
await plugin.app.fileManager.processFrontMatter(
41+
file,
42+
(fm: Record<string, unknown>) => {
43+
fm.publishedToGroups = [...existingPublish, myGroup];
44+
},
45+
);
46+
};

apps/obsidian/src/utils/registerCommands.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Editor } from "obsidian";
1+
import { Editor, MarkdownView, Notice } from "obsidian";
22
import type DiscourseGraphPlugin from "~/index";
33
import { NodeTypeModal } from "~/components/NodeTypeModal";
44
import ModifyNodeModal from "~/components/ModifyNodeModal";
@@ -7,7 +7,7 @@ import { createDiscourseNode } from "./createNode";
77
import { VIEW_TYPE_MARKDOWN, VIEW_TYPE_TLDRAW_DG_PREVIEW } from "~/constants";
88
import { createCanvas } from "~/components/canvas/utils/tldraw";
99
import { createOrUpdateDiscourseEmbedding } from "./syncDgNodesToSupabase";
10-
import { Notice } from "obsidian";
10+
import { publishNode } from "./publishNode";
1111

1212
export const registerCommands = (plugin: DiscourseGraphPlugin) => {
1313
plugin.addCommand({
@@ -156,4 +156,43 @@ export const registerCommands = (plugin: DiscourseGraphPlugin) => {
156156
return true;
157157
},
158158
});
159+
plugin.addCommand({
160+
id: "publish-discourse-node",
161+
name: "Publish current node to lab space",
162+
checkCallback: (checking: boolean) => {
163+
if (!plugin.settings.syncModeEnabled) {
164+
new Notice("Sync mode is not enabled", 3000);
165+
return false;
166+
}
167+
const activeView = plugin.app.workspace.getActiveViewOfType(MarkdownView);
168+
if (!activeView || !activeView.file) {
169+
return false;
170+
}
171+
const file = activeView.file;
172+
const cache = plugin.app.metadataCache.getFileCache(file);
173+
const frontmatter = cache?.frontmatter || {};
174+
if (!frontmatter.nodeTypeId) {
175+
return false;
176+
}
177+
if (!checking) {
178+
if (!frontmatter.nodeInstanceId) {
179+
new Notice("Please sync the node first");
180+
return true;
181+
}
182+
// TODO (in follow-up PRs):
183+
// Maybe sync the node now if unsynced
184+
// Ensure that the node schema is synced to the database, and shared
185+
// sync the assets to the database
186+
publishNode({ plugin, file, frontmatter })
187+
.then(() => {
188+
new Notice("Published");
189+
})
190+
.catch((error: Error) => {
191+
new Notice(error.message);
192+
console.error(error);
193+
});
194+
}
195+
return true;
196+
},
197+
});
159198
};

0 commit comments

Comments
 (0)