@@ -32,6 +32,8 @@ const TRUSTED_DIDS_CACHE_TTL = 300;
3232const VALID_FILTERS = [ 'follows' , 'discover' , 'likedByTrusted' ] as const ;
3333
3434export type AnnotatedEncryptedPost = EncryptedPost & {
35+ likeCount : number ;
36+ replyCount : number ;
3537 viewer ?: {
3638 like : boolean ;
3739 } ;
@@ -426,10 +428,15 @@ export class PrivatePostsService {
426428
427429 // Load reply posts
428430 // Load parent post
431+ type PostWithCounts = EncryptedPost & {
432+ _count : { reactions : number ; replies : number } ;
433+ reactions ?: { id : string } [ ] ;
434+ } ;
435+
429436 const promises : [
430- Promise < EncryptedPost & { _count : { reactions : number } } > ,
431- Promise < ( EncryptedPost & { _count : { reactions : number } } ) [ ] > ,
432- Promise < ( EncryptedPost & { _count : { reactions : number } } ) [ ] > ,
437+ Promise < PostWithCounts > ,
438+ Promise < PostWithCounts [ ] > ,
439+ Promise < PostWithCounts [ ] > ,
433440 ] = [
434441 prisma . encryptedPost
435442 . findFirst ( {
@@ -497,7 +504,7 @@ export class PrivatePostsService {
497504
498505 prisma
499506 . $queryRaw <
500- Array < EncryptedPost & { reaction_count : number ; depth : number } >
507+ Array < EncryptedPost & { reaction_count : number ; like_count : bigint ; reply_count : bigint ; depth : number } >
501508 > (
502509 Prisma . sql `
503510 WITH RECURSIVE post_chain AS (
@@ -508,6 +515,8 @@ export class PrivatePostsService {
508515 SELECT 1 FROM reactions r
509516 WHERE r.uri = ep.uri AND r."userDid" = ${ recipientDid }
510517 ))::int as reaction_count,
518+ (SELECT COUNT(*) FROM reactions r WHERE r.uri = ep.uri) as like_count,
519+ (SELECT COUNT(*) FROM encrypted_posts child WHERE child."replyUri" = ep.uri) as reply_count,
511520 1 as depth
512521 FROM encrypted_posts ep
513522 WHERE ep.uri = ${ canonicalUri }
@@ -526,6 +535,8 @@ export class PrivatePostsService {
526535 SELECT 1 FROM reactions r
527536 WHERE r.uri = parent.uri AND r."userDid" = ${ recipientDid }
528537 ))::int as reaction_count,
538+ (SELECT COUNT(*) FROM reactions r WHERE r.uri = parent.uri) as like_count,
539+ (SELECT COUNT(*) FROM encrypted_posts child WHERE child."replyUri" = parent.uri) as reply_count,
529540 pc.depth + 1
530541 FROM encrypted_posts parent
531542 INNER JOIN post_chain pc ON parent.uri = pc."replyUri"
@@ -546,7 +557,8 @@ export class PrivatePostsService {
546557 . then ( ( posts ) =>
547558 posts . map ( ( post ) => ( {
548559 ...post ,
549- _count : { reactions : post . reaction_count } ,
560+ _count : { reactions : Number ( post . like_count ) , replies : Number ( post . reply_count ) } ,
561+ reactions : post . reaction_count > 0 ? [ { id : 'raw' } ] : [ ] ,
550562 } ) ) ,
551563 ) ,
552564 ] ;
@@ -678,7 +690,7 @@ export class PrivatePostsService {
678690async function loadPrivatePost (
679691 cannonicalUri : string ,
680692 recipientDid : string ,
681- ) : Promise < ( EncryptedPost & { _count : { reactions : number } } ) | null > {
693+ ) : Promise < ( EncryptedPost & { _count : { reactions : number ; replies : number } ; reactions : { id : string } [ ] } ) | null > {
682694 return prisma . encryptedPost . findFirst ( {
683695 where : {
684696 session : {
@@ -787,15 +799,18 @@ export function getDIDFromUri(uri: string) {
787799
788800function annotatePost (
789801 post : EncryptedPost & {
790- _count : { reactions : number } ;
802+ _count : { reactions : number ; replies : number } ;
803+ reactions ?: { id : string } [ ] ;
791804 parent ?: { sessionId : string } | null ;
792805 root ?: { sessionId : string } | null ;
793806 } ,
794807) {
795808 return {
796809 ...post ,
810+ likeCount : post . _count . reactions ,
811+ replyCount : post . _count . replies ,
797812 viewer : {
798- like : post . _count . reactions > 0 ,
813+ like : post . reactions ? post . reactions . length > 0 : post . _count . reactions > 0 ,
799814 } ,
800815 } ;
801816}
@@ -811,7 +826,15 @@ function annotatePost(
811826function postIncludeForViewer ( recipientDid : string ) {
812827 return {
813828 _count : {
814- select : { reactions : { where : { userDid : recipientDid } } } ,
829+ select : {
830+ reactions : true ,
831+ replies : true ,
832+ } ,
833+ } ,
834+ reactions : {
835+ where : { userDid : recipientDid } ,
836+ select : { id : true } ,
837+ take : 1 ,
815838 } ,
816839 session : {
817840 select : {
0 commit comments