@@ -2,6 +2,7 @@ import type { Json } from "@repo/database/dbTypes";
22import matter from "gray-matter" ;
33import { App , Notice , TFile } from "obsidian" ;
44import type { DGSupabaseClient } from "@repo/database/lib/client" ;
5+ import { listGroupSharedNodes } from "@repo/database/lib/sharedNodes" ;
56import type DiscourseGraphPlugin from "~/index" ;
67import { getLoggedInClient , getSupabaseContext } from "./supabaseContext" ;
78import type { DiscourseNode , ImportableNode } from "~/types" ;
@@ -40,84 +41,35 @@ export const getPublishedNodesForGroups = async ({
4041 groupIds : string [ ] ;
4142 currentSpaceId : number ;
4243} ) : Promise < Array < PublishedNode > > => {
43- if ( groupIds . length === 0 ) {
44- return [ ] ;
45- }
46-
47- // Query my_contents (RLS applied); exclude current space. Get both variants so we can use
48- // the latest last_modified per node and prefer "direct" for text (title).
49- const { data, error } = await client
50- . from ( "my_contents" )
51- . select (
52- "source_local_id, space_id, text, created, last_modified, variant, metadata, author_id" ,
53- )
54- . neq ( "space_id" , currentSpaceId ) ;
55-
56- if ( error ) {
44+ const candidates = await listGroupSharedNodes ( {
45+ client,
46+ currentSpaceId,
47+ groupIds,
48+ } ) . catch ( ( error : { message ?: string } ) => {
5749 console . error ( "Error fetching published nodes:" , error ) ;
5850 throw new Error ( `Failed to fetch published nodes: ${ error . message } ` ) ;
59- }
60-
61- if ( ! data || data . length === 0 ) {
62- return [ ] ;
63- }
64-
65- type Row = {
66- source_local_id : string | null ;
67- space_id : number | null ;
68- text : string | null ;
69- created : string | null ;
70- last_modified : string | null ;
71- variant : string | null ;
72- author_id : number | null ;
73- metadata : Json ;
74- } ;
75-
76- const key = ( r : Row ) => `${ r . space_id ?? "" } \t${ r . source_local_id ?? "" } ` ;
77- const groups = new Map < string , Row [ ] > ( ) ;
78- for ( const row of data as Row [ ] ) {
79- if ( row . source_local_id == null || row . space_id == null ) continue ;
80- const k = key ( row ) ;
81- if ( ! groups . has ( k ) ) groups . set ( k , [ ] ) ;
82- groups . get ( k ) ! . push ( row ) ;
83- }
84-
85- const nodes : Array < PublishedNode > = [ ] ;
51+ } ) ;
8652
87- for ( const rows of groups . values ( ) ) {
88- const withDate = rows . filter (
89- ( r ) => r . last_modified != null && r . text != null ,
90- ) ;
91- if ( withDate . length === 0 ) continue ;
92- const latest = withDate . reduce ( ( a , b ) =>
93- ( a . last_modified ?? "" ) >= ( b . last_modified ?? "" ) ? a : b ,
94- ) ;
95- const direct = rows . find ( ( r ) => r . variant === "direct" ) ;
96- const text = direct ?. text ?? latest . text ?? "" ;
97- const createdAt = latest . created
98- ? new Date ( latest . created + "Z" ) . valueOf ( )
99- : 0 ;
100- const modifiedAt = latest . last_modified
101- ? new Date ( latest . last_modified + "Z" ) . valueOf ( )
102- : 0 ;
53+ return candidates . map ( ( candidate ) => {
54+ const metadata = candidate . directMetadata ;
10355 const filePath : string | undefined =
104- direct &&
105- typeof direct . metadata === "object" &&
106- typeof ( direct . metadata as Record < string , any > ) . filePath === "string"
107- ? ( direct . metadata as Record < string , any > ) . filePath
56+ metadata !== null &&
57+ typeof metadata === "object" &&
58+ typeof ( metadata as Record < string , any > ) . filePath === "string"
59+ ? ( metadata as Record < string , any > ) . filePath
10860 : undefined ;
109- nodes . push ( {
110- source_local_id : latest . source_local_id ! ,
111- space_id : latest . space_id ! ,
112- text,
113- createdAt,
114- modifiedAt,
61+ return {
62+ source_local_id : candidate . sourceLocalId ,
63+ space_id : candidate . spaceId ,
64+ text : candidate . title ,
65+ createdAt : candidate . created
66+ ? new Date ( candidate . created + "Z" ) . valueOf ( )
67+ : 0 ,
68+ modifiedAt : new Date ( candidate . lastModified + "Z" ) . valueOf ( ) ,
11569 filePath,
116- authorId : latest . author_id ?? undefined ,
117- } ) ;
118- }
119-
120- return nodes ;
70+ authorId : candidate . authorId ,
71+ } ;
72+ } ) ;
12173} ;
12274
12375export const getLocalNodeInstanceIds = (
0 commit comments