Skip to content

Commit 85556ef

Browse files
author
Chris Jensen
committed
feat(private-sessions): add likeCount and replyCount to post responses
Include total like and reply counts in the EncryptedPostView returned by getPosts and getPostThread. The existing viewer.like boolean is preserved alongside the new counts. Updates notification view types to match the new _count shape.
1 parent 7225890 commit 85556ef

4 files changed

Lines changed: 46 additions & 12 deletions

File tree

services/private-sessions/src/services/notification.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class NotificationService {
6767
priority?: boolean;
6868
}): Promise<{
6969
notifications: (Notification & {
70-
post?: (EncryptedPost & { _count: { reactions: number } }) | null;
70+
post?: (EncryptedPost & { _count: { reactions: number; replies: number }; reactions: { id: string }[] }) | null;
7171
})[];
7272
cursor: string | null;
7373
}> {

services/private-sessions/src/services/privatePosts.service.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const TRUSTED_DIDS_CACHE_TTL = 300;
3232
const VALID_FILTERS = ['follows', 'discover', 'likedByTrusted'] as const;
3333

3434
export 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 {
678690
async 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

788800
function 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(
811826
function 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: {

services/private-sessions/src/views/notification.views.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type NotificationSubset = Pick<
2323
Notification,
2424
'userDid' | 'authorDid' | 'reason' | 'reasonSubject' | 'readAt' | 'createdAt' | 'notifiedAt'
2525
> & {
26-
post?: (EncryptedPost & { _count: { reactions: number } }) | null;
26+
post?: (EncryptedPost & { _count: { reactions: number; replies: number }; reactions: { id: string }[] }) | null;
2727
};
2828

2929
export function toNotificationView(
@@ -37,7 +37,14 @@ export function toNotificationView(
3737
readAt: notification.readAt?.toISOString() ?? null,
3838
createdAt: notification.createdAt.toISOString(),
3939
notifiedAt: notification.notifiedAt.toISOString(),
40-
post: notification.post ? toEncryptedPostView(notification.post) : null,
40+
post: notification.post
41+
? toEncryptedPostView({
42+
...notification.post,
43+
likeCount: notification.post._count.reactions,
44+
replyCount: notification.post._count.replies,
45+
viewer: { like: notification.post.reactions.length > 0 },
46+
})
47+
: null,
4148
};
4249
}
4350

services/private-sessions/src/views/private-posts.views.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export type EncryptedPostView = {
1414
parent: { uri: string };
1515
} | null;
1616
langs: string[];
17+
likeCount: number;
18+
replyCount: number;
1719
viewer?: {
1820
like: boolean;
1921
};
@@ -40,6 +42,8 @@ export function toEncryptedPostView(
4042
}
4143
: null,
4244
langs: post.langs,
45+
likeCount: post.likeCount,
46+
replyCount: post.replyCount,
4347
viewer: post.viewer,
4448
};
4549
}

0 commit comments

Comments
 (0)