Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Sync comment counts after sends and index account deletion

Anand Baburajan779d3b215086
diff --git a/server/migrations/153_space_comments.up.sql b/server/migrations/153_space_comments.up.sql
index f69e0ddd74..bb98ebd447 100644
--- a/server/migrations/153_space_comments.up.sql
+++ b/server/migrations/153_space_comments.up.sql
@@ -8,6 +8,8 @@ CREATE TABLE space_comments (
created_at BIGINT NOT NULL DEFAULT now_utc_micro_seconds()
);
CREATE INDEX space_comments_post ON space_comments (post_id, created_at, comment_id);
+CREATE INDEX space_comments_author ON space_comments (author_space_id, post_id);
+CREATE INDEX space_comments_parent ON space_comments (parent_id);
CREATE INDEX space_comments_reply_to ON space_comments (reply_to_id);
CREATE TABLE space_comment_reactions (
comment_id TEXT NOT NULL REFERENCES space_comments (comment_id) ON DELETE CASCADE,
diff --git a/web/apps/space/src/services/space.ts b/web/apps/space/src/services/space.ts
index 766eb3e437..f1cadd19b5 100644
--- a/web/apps/space/src/services/space.ts
+++ b/web/apps/space/src/services/space.ts
@@ -1743,6 +1743,15 @@ export const createCurrentComment = async (
replyToId,
normalizeSpaceMessageText(text),
);
+ try {
+ const post = await ctx.getPost(postSpaceId, BigInt(postId), viewer);
+ rememberCommentCount(viewer, postId, post.commentCount);
+ await patchCachedSpaceFeedPost(viewer, postId, {
+ commentCount: post.commentCount,
+ });
+ } catch (error) {
+ log.warn("Failed to refresh comment count after sending", error);
+ }
} finally {
releaseCurrentSpaceContext(ctx);
}
diff --git a/web/apps/space/tests/comment-counts.test.ts b/web/apps/space/tests/comment-counts.test.ts
index 9e7692122d..96e3d841ed 100644
--- a/web/apps/space/tests/comment-counts.test.ts
+++ b/web/apps/space/tests/comment-counts.test.ts
@@ -7,6 +7,7 @@ import {
import { patchCachedSpaceFeedPost } from "../src/services/feed-cache";
import { ensureCurrentSpaceContext } from "../src/services/profile";
import {
+ createCurrentComment,
loadCurrentComments,
loadCurrentFeedPage,
loadCurrentSpaceProfilePostsPage,
@@ -25,6 +26,8 @@ vi.mock("services/profile", () => ({
}));
const context = {
+ createComment: vi.fn(),
+ getPost: vi.fn(),
listComments: vi.fn(),
listFeed: vi.fn(),
listPosts: vi.fn(),
@@ -74,6 +77,96 @@ test("keeps the updated count after the viewer closes and reopens with an old po
expect(cachedCommentCount("viewer", snapshot.postId)).toBe(1);
});
+test.each([undefined, "parent"])(
+ "updates the count when a send to %s finishes after the viewer closes",
+ async (replyToId) => {
+ context.listFeed.mockResolvedValue({ items: [post(0)] });
+ const feed = await loadCurrentFeedPage("viewer");
+ const updateViewer = vi.fn();
+ const closeViewer = subscribeToCommentCounts(updateViewer);
+ let finishSend!: () => void;
+ context.createComment.mockReturnValue(
+ new Promise<void>((resolve) => {
+ finishSend = resolve;
+ }),
+ );
+ context.getPost.mockResolvedValue(post(3));
+
+ const send = createCurrentComment(
+ "viewer",
+ "friend",
+ 42,
+ "comment",
+ replyToId,
+ "Hello",
+ );
+ await vi.waitFor(() =>
+ expect(context.createComment).toHaveBeenCalled(),
+ );
+ expect(cachedCommentCount("viewer", 42)).toBe(0);
+ closeViewer();
+ finishSend();
+ await send;
+
+ expect(updateViewer).not.toHaveBeenCalled();
+ expect(feed.items[0]!.commentCount).toBe(0);
+ expect(cachedCommentCount("viewer", 42)).toBe(3);
+ expect(patchCachedSpaceFeedPost).toHaveBeenCalledWith("viewer", 42, {
+ commentCount: 3,
+ });
+ expect(context.getPost).toHaveBeenCalledWith("friend", 42n, "viewer");
+ },
+);
+
+test("keeps the count unchanged when sending fails", async () => {
+ context.listFeed.mockResolvedValue({ items: [post(2)] });
+ await loadCurrentFeedPage("viewer");
+ context.createComment.mockRejectedValue(new Error("Send failed"));
+
+ await expect(
+ createCurrentComment(
+ "viewer",
+ "friend",
+ 42,
+ "comment",
+ undefined,
+ "Hi",
+ ),
+ ).rejects.toThrow("Send failed");
+
+ expect(cachedCommentCount("viewer", 42)).toBe(2);
+ expect(patchCachedSpaceFeedPost).not.toHaveBeenCalled();
+});
+
+test.each(["post", "cache"])(
+ "does not report a saved comment as failed when refreshing the %s fails",
+ async (failure) => {
+ context.getPost.mockResolvedValue(post(1));
+ if (failure == "post") {
+ context.getPost.mockRejectedValue(new Error("Offline"));
+ } else {
+ vi.mocked(patchCachedSpaceFeedPost).mockRejectedValue(
+ new Error("Storage unavailable"),
+ );
+ }
+
+ await expect(
+ createCurrentComment(
+ "viewer",
+ "friend",
+ 42,
+ "comment",
+ undefined,
+ "Hi",
+ ),
+ ).resolves.toBeUndefined();
+ expect(context.getPost).toHaveBeenCalled();
+ if (failure == "cache") {
+ expect(cachedCommentCount("viewer", 42)).toBe(1);
+ }
+ },
+);
+
test("counts replies across pages and removes deleted comments from the total", async () => {
context.listComments
.mockResolvedValueOnce({