@@ -6,10 +6,13 @@ import mime from "mime-types";
66import type { DGSupabaseClient } from "@repo/database/lib/client" ;
77import {
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
1417const 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+
65138export 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