Skip to content

Commit a6034d5

Browse files
committed
define publishNewRelation
1 parent d5616b0 commit a6034d5

3 files changed

Lines changed: 78 additions & 13 deletions

File tree

apps/obsidian/src/components/ImportNodesModal.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,13 @@ const ImportNodesContent = ({ plugin, onClose }: ImportNodesModalProps) => {
4545
return;
4646
}
4747

48-
const groups = await getAvailableGroups(client);
49-
if (groups.length === 0) {
48+
const groupIds = await getAvailableGroups(client);
49+
if (groupIds.length === 0) {
5050
new Notice("You are not a member of any groups");
5151
onClose();
5252
return;
5353
}
5454

55-
const groupIds = groups.map((g) => g.group_id);
56-
5755
const publishedNodes = await getPublishedNodesForGroups({
5856
client,
5957
groupIds,

apps/obsidian/src/utils/importNodes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { spaceUriAndLocalIdToRid, ridToSpaceUriAndLocalId } from "./rid";
1111

1212
export const getAvailableGroups = async (
1313
client: DGSupabaseClient,
14-
): Promise<{ group_id: string }[]> => {
14+
): Promise<string[]> => {
1515
const { data, error } = await client
1616
.from("group_membership")
1717
.select("group_id")
@@ -22,7 +22,7 @@ export const getAvailableGroups = async (
2222
throw new Error(`Failed to fetch groups: ${error.message}`);
2323
}
2424

25-
return data || [];
25+
return (data || []).map((g) => g.group_id);
2626
};
2727

2828
export const getPublishedNodesForGroups = async ({

apps/obsidian/src/utils/publishNode.ts

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import mime from "mime-types";
66
import type { DGSupabaseClient } from "@repo/database/lib/client";
77
import {
88
getRelationsForNodeInstanceId,
9+
getFileForNodeInstanceId,
910
getFileForNodeInstanceIds,
1011
loadRelations,
1112
saveRelations,
1213
} from "./relationsStore";
14+
import type { RelationInstance } from "~/types";
15+
import { getAvailableGroups } from "./importNodes";
1316

1417
const publishSchema = async ({
1518
client,
@@ -62,6 +65,76 @@ const publishSchema = async ({
6265
}
6366
};
6467

68+
const intersection = <T>(set1: Set<T>, set2: Set<T>): Set<T> => {
69+
// @ts-ignore-error
70+
if (set1.intersection) return set1.intersection(set2);
71+
const r: Set<T> = new Set();
72+
for (const x of set1) {
73+
if (set2.has(x)) r.add(x);
74+
}
75+
return r;
76+
};
77+
78+
export const publishNewRelation = async (
79+
plugin: DiscourseGraphPlugin,
80+
relation: RelationInstance,
81+
) => {
82+
const client = await getLoggedInClient(plugin);
83+
if (!client) throw new Error("Cannot get client");
84+
const context = await getSupabaseContext(plugin);
85+
if (!context) throw new Error("Cannot get context");
86+
const sourceFile = getFileForNodeInstanceId(plugin, relation.source);
87+
const destinationFile = getFileForNodeInstanceId(
88+
plugin,
89+
relation.destination,
90+
);
91+
if (!sourceFile || !destinationFile) return;
92+
const sourceFm =
93+
plugin.app.metadataCache.getFileCache(sourceFile)?.frontmatter;
94+
const destinationFm =
95+
plugin.app.metadataCache.getFileCache(destinationFile)?.frontmatter;
96+
if (!sourceFm || !destinationFm) return;
97+
98+
const sourceGroups = sourceFm.publishedToGroups;
99+
const destinationGroups = destinationFm.publishedToGroups;
100+
if (!Array.isArray(sourceGroups) || !Array.isArray(destinationGroups)) return;
101+
const relationTriples = plugin.settings.discourseRelations ?? [];
102+
const triple = relationTriples.find(
103+
(triple) =>
104+
triple.relationshipTypeId === relation.type &&
105+
triple.sourceId === sourceFm.type &&
106+
triple.destinationId === destinationFm.type,
107+
);
108+
if (!triple) return;
109+
const resourceIds = [relation.id, relation.type, triple.id];
110+
const myGroups = await getAvailableGroups(client);
111+
const targetGroups = intersection(
112+
new Set(myGroups),
113+
intersection(
114+
new Set<string>(sourceGroups),
115+
new Set<string>(destinationGroups),
116+
),
117+
);
118+
if (!targetGroups.size) return;
119+
const entries = [];
120+
for (const group of targetGroups) {
121+
for (const id of resourceIds) {
122+
entries.push({
123+
/* eslint-disable @typescript-eslint/naming-convention */
124+
account_uid: group,
125+
source_local_id: id,
126+
space_id: context.spaceId,
127+
/* eslint-enable @typescript-eslint/naming-convention */
128+
});
129+
}
130+
}
131+
const publishResponse = await client
132+
.from("ResourceAccess")
133+
.upsert(entries, { ignoreDuplicates: true });
134+
if (publishResponse.error && publishResponse.error.code !== "23505")
135+
throw publishResponse.error;
136+
};
137+
65138
export const publishNodeRelations = async ({
66139
plugin,
67140
client,
@@ -155,13 +228,7 @@ export const publishNode = async ({
155228
}): Promise<void> => {
156229
const client = await getLoggedInClient(plugin);
157230
if (!client) throw new Error("Cannot get client");
158-
const myGroupsResponse = await client
159-
.from("group_membership")
160-
.select("group_id");
161-
if (myGroupsResponse.error) throw myGroupsResponse.error;
162-
const myGroups = new Set(
163-
myGroupsResponse.data.map(({ group_id }) => group_id),
164-
);
231+
const myGroups = new Set(await getAvailableGroups(client));
165232
if (myGroups.size === 0) throw new Error("Cannot get group");
166233
const existingPublish =
167234
(frontmatter.publishedToGroups as undefined | string[]) || [];

0 commit comments

Comments
 (0)