Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): preserve live thread membership across stale sidebar reads (#1138)

Gustavo Ye1436390b26d5
diff --git a/src/web/src/hooks/community/community-ws/live-sidebar-race.dom.test.ts b/src/web/src/hooks/community/community-ws/live-sidebar-race.dom.test.ts
new file mode 100644
index 000000000..19d3b4177
--- /dev/null
+++ b/src/web/src/hooks/community/community-ws/live-sidebar-race.dom.test.ts
@@ -0,0 +1,66 @@
+import { forumSidebarEnvelopeFixture, messageResourceFixture } from "@/test/community-resource-fixtures"
+import { capturedOnMessage, capturedQueryClient, cleanupCommunityWsHarness, getCommunityApiFetchMock, hasCanonicalChannelNotify,
+ mountCanonicalHook, mountHook, resetCommunityWsHarness, seedCanonicalForumSidebar } from "./test-harness"
+import { act } from "@/test/react-dom-harness"
+import { afterEach, beforeEach, expect, it } from "vitest"
+import { BUSINESS_SCHEMA_VERSION, communityBrowserEventBatchType, deriveCommunityDeliveryOperationId, encodeCommunityBrowserEventBatchForContract, prepareCommunityDeliveryEvents, type CommunityWsEvent } from "@alook/shared"
+import { patchCanonicalCommunityChannel } from "@/lib/community-db/sync"
+import { useForumSidebarThreads, type SidebarThreadEnvelope } from "../use-forum-sidebar-threads"
+import { communityKeys } from "@/lib/query-keys"
+
+beforeEach(resetCommunityWsHarness)
+afterEach(cleanupCommunityWsHarness)
+const serverId = "live-server"
+function envelope(ids: string[]): SidebarThreadEnvelope {
+ return forumSidebarEnvelopeFixture(serverId, ids, "u_me")
+}
+
+async function frame(events: CommunityWsEvent[], id: string) {
+ const prepared = await prepareCommunityDeliveryEvents(events)
+ if (!prepared.ok) throw new Error("invalid live window fixture")
+ const encoded = await encodeCommunityBrowserEventBatchForContract({ operationId: await deriveCommunityDeliveryOperationId(id), prepared: prepared.prepared, contract: BUSINESS_SCHEMA_VERSION })
+ if (!encoded.ok) throw new Error("invalid live window wire")
+ expect(encoded.batch.type).toBe(communityBrowserEventBatchType())
+ return encoded.batch
+}
+
+it.each(["forum", "text"] as const)("retains a promoted %s participant when an older bounded response settles after live activity", async (parentType) => {
+ await mountHook({ viewerUserId: "u_me" })
+ const visible = ["post_7", "post_6", "post_5", "post_4", "post_3"]
+ seedCanonicalForumSidebar(serverId, [...visible, "post_old"])
+ act(() => { patchCanonicalCommunityChannel(capturedQueryClient, "post_old", row => ({ ...row, lastMessageAt: new Date(Date.now() - 60_000).toISOString() })) })
+ const api = getCommunityApiFetchMock()
+ const original = api.getMockImplementation()!
+ let reads = 0
+ let release!: (value: SidebarThreadEnvelope) => void
+ const stale = envelope(visible)
+ stale.resources.channels[0]!.type = parentType
+ act(() => { patchCanonicalCommunityChannel(capturedQueryClient, "forum_1", row => ({ ...row, type: parentType })) })
+ api.mockImplementation((...args: unknown[]) => {
+ if (String(args[0]).startsWith(`/api/community/servers/${serverId}/channels?`)) {
+ reads++
+ return reads === 1 ? Promise.resolve(stale) : new Promise<SidebarThreadEnvelope>(resolve => { release = resolve })
+ }
+ return original(...args)
+ })
+ const sidebar = await mountCanonicalHook(() => useForumSidebarThreads(serverId, null))
+ await expect.poll(() => sidebar.result.current.sidebar.status).toBe("ready")
+ expect(hasCanonicalChannelNotify("post_old", "u_me")).toBe(true)
+ expect(sidebar.result.current.threads.some(row => row.id === "post_old")).toBe(false)
+ let refetch!: Promise<unknown>
+ act(() => { refetch = capturedQueryClient.invalidateQueries({ queryKey: communityKeys.forumSidebarThreads(serverId), exact: true }) })
+ await expect.poll(() => reads).toBe(2)
+ const now = new Date(Date.now() + 1000).toISOString()
+ const event: CommunityWsEvent = { profiles: [{ id: "u_me", name: "Me", discriminator: "0042", avatar: null, avatarVersion: 0 }], type: "community:message.create", serverId, channelId: "post_old", parentChannelId: "forum_1",
+ message: messageResourceFixture({ id: "promoting-reply", channelId: "post_old", type: "chat", authorId: "u_me", authorName: "Me", authorAvatarVersion: 0, content: "Live activity lifts the oldest thread", seq: 2, createdAt: now }) }
+ await act(async () => { await capturedOnMessage!(await frame([event], "promotion-race")) })
+ await expect.poll(() => sidebar.result.current.threads[0]?.id).toBe("post_old")
+ await act(async () => { release(stale); await refetch })
+ expect(hasCanonicalChannelNotify("post_old", "u_me")).toBe(true)
+ expect(sidebar.result.current.threads[0]?.id).toBe("post_old")
+ const removal: CommunityWsEvent = { type: "community:channel.membership.change", relation: "notify", present: false,
+ serverId, channelId: "post_old", userId: "u_me" }
+ await act(async () => { await capturedOnMessage!(await frame([removal], `remove-${parentType}`)) })
+ expect(hasCanonicalChannelNotify("post_old", "u_me")).toBe(false)
+ await expect.poll(() => sidebar.result.current.threads.some(row => row.id === "post_old")).toBe(false)
+})
diff --git a/src/web/src/hooks/community/use-forum-sidebar-threads.ts b/src/web/src/hooks/community/use-forum-sidebar-threads.ts
index a039b401f..da5d3de94 100644
--- a/src/web/src/hooks/community/use-forum-sidebar-threads.ts
+++ b/src/web/src/hooks/community/use-forum-sidebar-threads.ts
@@ -29,6 +29,7 @@ patchCanonicalCommunityMessage,
publishCommunityForumSidebar,
removeCanonicalCommunityChannelMembership,
setCanonicalCommunityChannelMember,
+isCommunityChannelSnapshotCurrent,
setCanonicalCommunityChannelMembership,
type CommunityFreshQueryProof,
type CommunityForumSidebarChannel,
@@ -384,6 +385,7 @@ function reconcileForumSidebarNotifyMembershipsFromBase(
if (
channel.id === protectedRetainId
|| baseIds.has(channel.id)
+ || !isCommunityChannelSnapshotCurrent(queryClient, channel.id, proof)
|| !channel.parentChannelId
) continue
const activityAt = forumSidebarActivityAt(channel)
diff --git a/src/web/src/lib/community-db/sync.ts b/src/web/src/lib/community-db/sync.ts
index 53c9f608c..7eea7cbc0 100644
--- a/src/web/src/lib/community-db/sync.ts
+++ b/src/web/src/lib/community-db/sync.ts
@@ -1860,6 +1860,11 @@ export function patchCanonicalCommunityChannel(
))
}
+export function isCommunityChannelSnapshotCurrent(queryClient: QueryClient, channelId: string, proof: CommunityFreshQueryProof) {
+ assertCommunityLiveSnapshotTokenCurrent(queryClient, proof.token, proof.signal)
+ return (canonicalRevisionState(queryClient).entityRevisions.get(canonicalEntityKey("channels", channelId)) ?? 0) <= proof.token.canonicalRevision
+}
+
export function publishCommunityChannelPatch(
queryClient: QueryClient,
channelId: string,