@@ -364,6 +364,76 @@ const mapSupabaseMarketToApi = (m: any): ApiMarket => ({
364364
365365export const mapSupabaseMarketToMarket = ( market : any ) : Market => mapApiMarket ( mapSupabaseMarketToApi ( market ) ) ;
366366
367+ const mapSupabaseCommentToApi = ( comment : any ) : ApiComment => ( {
368+ id : comment . id ,
369+ marketId : comment . market_id ,
370+ parentId : comment . parent_id ,
371+ walletAddress : comment . wallet_address ,
372+ displayName : comment . display_name ,
373+ body : comment . body ,
374+ likedBy : comment . liked_by || [ ] ,
375+ createdAt : comment . created_at ,
376+ updatedAt : comment . updated_at ,
377+ } ) ;
378+
379+ const createCommentInSupabase = async ( input : {
380+ marketId : string ;
381+ walletAddress : string ;
382+ displayName : string ;
383+ body : string ;
384+ parentId ?: string | null ;
385+ } ) => {
386+ const { data, error } = await supabase
387+ . from ( "comments" )
388+ . insert ( {
389+ market_id : input . marketId ,
390+ wallet_address : input . walletAddress . toLowerCase ( ) ,
391+ display_name : input . displayName ,
392+ body : input . body ,
393+ parent_id : input . parentId ,
394+ } )
395+ . select ( )
396+ . single ( ) ;
397+ if ( error ) throw error ;
398+ return mapSupabaseCommentToApi ( data ) ;
399+ } ;
400+
401+ const setCommentLikeInSupabase = async ( input : {
402+ marketId : string ;
403+ commentId : string ;
404+ walletAddress : string ;
405+ liked : boolean ;
406+ } ) => {
407+ const { data : current } = await supabase . from ( "comments" ) . select ( "liked_by" ) . eq ( "id" , input . commentId ) . single ( ) ;
408+ let likedBy = current ?. liked_by || [ ] ;
409+ const walletAddress = input . walletAddress . toLowerCase ( ) ;
410+ if ( input . liked ) {
411+ if ( ! likedBy . includes ( walletAddress ) ) likedBy . push ( walletAddress ) ;
412+ } else {
413+ likedBy = likedBy . filter ( ( wallet : string ) => wallet !== walletAddress ) ;
414+ }
415+
416+ const { data, error } = await supabase
417+ . from ( "comments" )
418+ . update ( { liked_by : likedBy } )
419+ . eq ( "id" , input . commentId )
420+ . select ( )
421+ . single ( ) ;
422+ if ( error ) throw error ;
423+ return mapSupabaseCommentToApi ( data ) ;
424+ } ;
425+
426+ const addParticipantInSupabase = async ( marketId : string , walletAddress : string ) => {
427+ const { data : current } = await supabase . from ( "markets" ) . select ( "participants" ) . eq ( "market_id" , marketId ) . single ( ) ;
428+ const normalizedWallet = walletAddress . toLowerCase ( ) ;
429+ const participants = current ?. participants || [ ] ;
430+ if ( ! participants . includes ( normalizedWallet ) ) {
431+ participants . push ( normalizedWallet ) ;
432+ const { error } = await supabase . from ( "markets" ) . update ( { participants } ) . eq ( "market_id" , marketId ) ;
433+ if ( error ) throw error ;
434+ }
435+ } ;
436+
367437export const darkonnetApi = {
368438 baseUrl : apiBaseUrl ,
369439 wsNotificationsUrl ( walletAddress : string ) {
@@ -499,33 +569,45 @@ export const darkonnetApi = {
499569 body : string ;
500570 parentId ?: string | null ;
501571 } ) {
502- const { comment } = await apiRequest < { comment : ApiComment } > (
503- `/api/markets/${ encodeURIComponent ( input . marketId ) } /comments` ,
504- {
505- method : "POST" ,
506- body : input ,
507- walletAddress : input . walletAddress ,
508- } ,
509- ) ;
510- return comment ;
572+ try {
573+ const { comment } = await apiRequest < { comment : ApiComment } > (
574+ `/api/markets/${ encodeURIComponent ( input . marketId ) } /comments` ,
575+ {
576+ method : "POST" ,
577+ body : input ,
578+ walletAddress : input . walletAddress ,
579+ } ,
580+ ) ;
581+ return comment ;
582+ } catch {
583+ return createCommentInSupabase ( input ) ;
584+ }
511585 } ,
512586 async setCommentLike ( input : { marketId : string ; commentId : string ; walletAddress : string ; liked : boolean } ) {
513- const { comment } = await apiRequest < { comment : ApiComment } > (
514- `/api/markets/${ encodeURIComponent ( input . marketId ) } /comments/${ encodeURIComponent ( input . commentId ) } /like` ,
515- {
516- method : "POST" ,
517- body : { walletAddress : input . walletAddress , liked : input . liked } ,
518- walletAddress : input . walletAddress ,
519- } ,
520- ) ;
521- return comment ;
587+ try {
588+ const { comment } = await apiRequest < { comment : ApiComment } > (
589+ `/api/markets/${ encodeURIComponent ( input . marketId ) } /comments/${ encodeURIComponent ( input . commentId ) } /like` ,
590+ {
591+ method : "POST" ,
592+ body : { walletAddress : input . walletAddress , liked : input . liked } ,
593+ walletAddress : input . walletAddress ,
594+ } ,
595+ ) ;
596+ return comment ;
597+ } catch {
598+ return setCommentLikeInSupabase ( input ) ;
599+ }
522600 } ,
523601 async addParticipant ( marketId : string , walletAddress : string ) {
524- await apiRequest < { market : ApiMarket } > ( `/api/markets/${ encodeURIComponent ( marketId ) } /participants` , {
525- method : "POST" ,
526- body : { walletAddress } ,
527- walletAddress,
528- } ) ;
602+ try {
603+ await apiRequest < { market : ApiMarket } > ( `/api/markets/${ encodeURIComponent ( marketId ) } /participants` , {
604+ method : "POST" ,
605+ body : { walletAddress } ,
606+ walletAddress,
607+ } ) ;
608+ } catch {
609+ await addParticipantInSupabase ( marketId , walletAddress ) ;
610+ }
529611 } ,
530612 async listNotifications ( walletAddress : string ) {
531613 const { notifications } = await apiRequest < { notifications : ApiNotification [ ] } > (
0 commit comments