Skip to content

Commit 825d257

Browse files
authored
feat(lemmyv1): surface read_comments_at on PostView (#43)
1 parent 77e2c47 commit 825d257

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/providers/lemmyv1/compat.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export function toPostView(v: LemmyV1.PostView): types.PostView {
156156
my_vote: toVote(v.post_actions),
157157
notifications: v.post_actions?.notifications ?? "replies_and_mentions",
158158
read: !!v.post_actions?.read_at,
159+
read_comments_at: nullToUndef(v.post_actions?.read_comments_at),
159160
saved: !!v.post_actions?.saved_at,
160161
subscribed: toFollowState(v.community_actions?.follow_state),
161162
unread_comments: toUnreadComments(v.post, v.post_actions),

src/schemas/PostView.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export const PostView = z.object({
2121
notifications: PostNotificationsMode,
2222
post: Post,
2323
read: z.boolean(),
24+
/**
25+
* When the current user last read the comments on this post (ISO 8601).
26+
* Comments published after this can be treated as unread. Only provided by
27+
* providers that track it (Lemmy v1); `undefined` otherwise.
28+
*/
29+
read_comments_at: z.optional(z.string()),
2430
saved: z.boolean(),
2531
subscribed: SubscribedType,
2632
tags: z.array(PostTag),

test/lemmyv1-compat.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,32 @@ describe("v1 compat - my_vote derivation", () => {
107107
} as any);
108108
expect(v.my_vote).toBeUndefined();
109109
});
110+
111+
it("PostView: surfaces read_comments_at from post_actions", () => {
112+
const v = toPostView({
113+
...basePostView(),
114+
post_actions: {
115+
read_comments_amount: 1,
116+
read_comments_at: NOW,
117+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
118+
} as any,
119+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
120+
} as any);
121+
expect(v.read_comments_at).toBe(NOW);
122+
});
123+
124+
it("PostView: read_comments_at null/absent → undefined", () => {
125+
const nulled = toPostView({
126+
...basePostView(),
127+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
128+
post_actions: { read_comments_at: null as any },
129+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
130+
} as any);
131+
expect(nulled.read_comments_at).toBeUndefined();
132+
133+
const absent = toPostView(basePostView() as never);
134+
expect(absent.read_comments_at).toBeUndefined();
135+
});
110136
});
111137

112138
describe("v1 compat - toSupportedNotificationView normalizes null id fields", () => {

0 commit comments

Comments
 (0)