11/* eslint-disable @typescript-eslint/naming-convention */
22import type { TFile } from "obsidian" ;
3- import type DiscourseGraphPlugin from "~/index" ;
4- import type {
5- DiscourseNode ,
6- DiscourseRelation ,
7- DiscourseRelationType ,
8- } from "~/types" ;
3+ import type { DiscourseNode } from "~/types" ;
94import type { SupabaseContext } from "./supabaseContext" ;
10- import type { DiscourseNodeInVault } from "./syncDgNodesToSupabase" ;
115import type { LocalConceptDataInput } from "@repo/database/inputTypes" ;
126import type { ObsidianDiscourseNodeData } from "./syncDgNodesToSupabase" ;
137import type { Json } from "@repo/database/dbTypes" ;
@@ -58,150 +52,21 @@ export const discourseNodeSchemaToLocalConcept = ({
5852 } ;
5953} ;
6054
61- const STANDARD_ROLES = [ "source" , "destination" ] ;
62-
63- export const discourseRelationTypeToLocalConcept = ( {
64- context,
65- relationType,
66- accountLocalId,
67- } : {
68- context : SupabaseContext ;
69- relationType : DiscourseRelationType ;
70- accountLocalId : string ;
71- } ) : LocalConceptDataInput => {
72- const { id, label, complement, created, modified, ...otherData } =
73- relationType ;
74- return {
75- space_id : context . spaceId ,
76- name : label ,
77- source_local_id : id ,
78- is_schema : true ,
79- author_local_id : accountLocalId ,
80- created : new Date ( created ) . toISOString ( ) ,
81- last_modified : new Date ( modified ) . toISOString ( ) ,
82- literal_content : {
83- label,
84- complement,
85- source_data : otherData ,
86- } as unknown as Json ,
87- } ;
88- } ;
89-
90- export const discourseRelationSchemaToLocalConcept = ( {
91- context,
92- relation,
93- accountLocalId,
94- nodeTypesById,
95- relationTypesById,
96- } : {
97- context : SupabaseContext ;
98- relation : DiscourseRelation ;
99- accountLocalId : string ;
100- nodeTypesById : Record < string , DiscourseNode > ;
101- relationTypesById : Record < string , DiscourseRelationType > ;
102- } ) : LocalConceptDataInput => {
103- const { id, relationshipTypeId, sourceId, destinationId, created, modified } =
104- relation ;
105- const sourceName = nodeTypesById [ sourceId ] ?. name ?? sourceId ;
106- const destinationName = nodeTypesById [ destinationId ] ?. name ?? destinationId ;
107- const relationType = relationTypesById [ relationshipTypeId ] ;
108- if ( ! relationType )
109- throw new Error ( `missing relation type ${ relationshipTypeId } ` ) ;
110- const { label, complement } = relationType ;
111-
112- return {
113- space_id : context . spaceId ,
114- name : `${ sourceName } -${ label } -> ${ destinationName } ` ,
115- source_local_id : id ,
116- is_schema : true ,
117- author_local_id : accountLocalId ,
118- created : new Date ( created ) . toISOString ( ) ,
119- last_modified : new Date ( modified ) . toISOString ( ) ,
120- literal_content : {
121- roles : STANDARD_ROLES ,
122- label,
123- complement,
124- } ,
125- local_reference_content : {
126- relation_type : relationshipTypeId ,
127- source : sourceId ,
128- destination : destinationId ,
129- } ,
130- } ;
131- } ;
132-
13355/**
13456 * Convert discourse node instance (file) to LocalConceptDataInput
13557 */
136- export const discourseNodeInstanceToLocalConcepts = ( {
137- plugin,
138- allNodesByName,
58+ export const discourseNodeInstanceToLocalConcept = ( {
13959 context,
14060 nodeData,
14161 accountLocalId,
14262} : {
143- plugin : DiscourseGraphPlugin ;
144- allNodesByName : Record < string , DiscourseNodeInVault > ;
14563 context : SupabaseContext ;
14664 nodeData : ObsidianDiscourseNodeData ;
14765 accountLocalId : string ;
148- } ) : LocalConceptDataInput [ ] => {
66+ } ) : LocalConceptDataInput => {
14967 const extraData = getNodeExtraData ( nodeData . file , accountLocalId ) ;
15068 const { nodeInstanceId, nodeTypeId, ...otherData } = nodeData . frontmatter ;
151- const response : LocalConceptDataInput [ ] = [ ] ;
152- for ( const relType of plugin . settings . relationTypes ) {
153- const rels = otherData [ relType . id ] ;
154- if ( rels ) {
155- delete otherData [ relType . id ] ;
156- const triples = plugin . settings . discourseRelations . filter (
157- ( r ) => r . relationshipTypeId === relType . id && r . sourceId === nodeTypeId ,
158- ) ;
159- if ( ! triples . length ) {
160- // we're probably the target.
161- continue ;
162- }
163- const tripleIdByDestType = Object . fromEntries (
164- triples . map ( ( rel ) => [ rel . destinationId , rel . id ] ) ,
165- ) ;
166- for ( let rel of rels as string [ ] ) {
167- if ( rel . startsWith ( "[[" ) && rel . endsWith ( "]]" ) )
168- rel = rel . substring ( 2 , rel . length - 2 ) ;
169- if ( rel . endsWith ( ".md" ) ) rel = rel . substring ( 0 , rel . length - 3 ) ;
170- const target = allNodesByName [ rel ] ;
171- if ( ! target ) {
172- console . error ( `Could not find node name ${ rel } ` ) ;
173- continue ;
174- }
175- const targetTypeId = target . frontmatter . nodeTypeId as string ;
176- const targetInstanceId = target . frontmatter . nodeInstanceId as string ;
177- const relSchemaId = tripleIdByDestType [ targetTypeId ] ;
178- if ( relSchemaId === undefined ) {
179- console . error (
180- `Found a relation of type ${ relType . id } between ${ nodeData . file . path } and ${ rel } but no relation fits` ,
181- ) ;
182- continue ;
183- }
184- const compositeInstanceId = [
185- relSchemaId ,
186- nodeInstanceId as string ,
187- targetInstanceId ,
188- ] . join ( ":" ) ;
189- response . push ( {
190- space_id : context . spaceId ,
191- name : `[[${ nodeData . file . basename } ]] -${ relType . label } -> [[${ target . file . basename } ]]` ,
192- source_local_id : compositeInstanceId ,
193- schema_represented_by_local_id : relSchemaId ,
194- is_schema : false ,
195- local_reference_content : {
196- source : nodeInstanceId as string ,
197- destination : targetInstanceId ,
198- } ,
199- ...extraData ,
200- } ) ;
201- }
202- }
203- }
204- response . push ( {
69+ return {
20570 space_id : context . spaceId ,
20671 name : nodeData . file . path ,
20772 source_local_id : nodeInstanceId as string ,
@@ -212,8 +77,7 @@ export const discourseNodeInstanceToLocalConcepts = ({
21277 source_data : otherData as unknown as Json ,
21378 } ,
21479 ...extraData ,
215- } ) ;
216- return response ;
80+ } ;
21781} ;
21882
21983export const relatedConcepts = ( concept : LocalConceptDataInput ) : string [ ] => {
0 commit comments