Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat(community): patch unread dot/rail badge by serverId on the bump (inbox-dot-ws-driven ②)

Gretnae6a512718a78
diff --git a/src/web/src/hooks/community/use-community-ws.test.ts b/src/web/src/hooks/community/use-community-ws.test.ts
index 3f5cf5c83..f72475558 100644
--- a/src/web/src/hooks/community/use-community-ws.test.ts
+++ b/src/web/src/hooks/community/use-community-ws.test.ts
@@ -153,8 +153,12 @@ function messageCreate(channelId: string, msgId = "m_1"): CommunityMessageCreate
}
}
-function unreadBump(channelId: string, userId: string) {
- return { type: "community:unread.bump" as const, userId, channelId }
+function unreadBump(
+ channelId: string,
+ userId: string,
+ extra?: { serverId?: string; railChannelId?: string; isMention?: boolean },
+) {
+ return { type: "community:unread.bump" as const, userId, channelId, ...extra }
}
describe("useCommunityWs — message.create", () => {
@@ -529,6 +533,76 @@ describe("useCommunityWs — message.create patches channel unread in the open s
expect(() => capturedOnMessage!(unreadBump("ch_random", "u_me"))).not.toThrow()
})
+ // ── inbox-dot-ws-driven ② : bump carries serverId / railChannelId / isMention ──
+
+ it("lights the dot on the bump's OWN serverId, even when a DIFFERENT server is open (the cross-server bug)", async () => {
+ // The core fix: previously the handler only patched the currently-open
+ // server, so a message in another server never lit its dot. With
+ // `serverId` on the bump we patch the right server's detail regardless of
+ // which one is focused.
+ await mountHook({ viewerUserId: "u_me" })
+ const { useCommunityStore } = await import("@/stores/community")
+ useCommunityStore.getState().setCurrentServerId("srv_open") // a different server is focused
+ capturedQueryClient.setQueryData(communityKeys.server("srv_other"), serverDetailFixture("ch_bg"))
+
+ capturedOnMessage!(unreadBump("ch_bg", "u_me", { serverId: "srv_other" }))
+
+ const cache = capturedQueryClient.getQueryData<{
+ categories: { channels: { id: string; unread: boolean }[] }[]
+ }>(communityKeys.server("srv_other"))
+ expect(cache?.categories[0].channels[0]).toMatchObject({ id: "ch_bg", unread: true })
+ })
+
+ it("lights the PARENT channel row for a thread bump (railChannelId), not the thread's own id", async () => {
+ await mountHook({ viewerUserId: "u_me" })
+ const { useCommunityStore } = await import("@/stores/community")
+ useCommunityStore.getState().setCurrentServerId("srv_open")
+ // The tree row is the parent channel; the thread has no independent row.
+ capturedQueryClient.setQueryData(communityKeys.server("srv_open"), serverDetailFixture("ch_parent"))
+
+ // channelId = the thread's id (true scope), railChannelId = parent row.
+ capturedOnMessage!(unreadBump("ch_thread", "u_me", { serverId: "srv_open", railChannelId: "ch_parent" }))
+
+ const cache = capturedQueryClient.getQueryData<{
+ categories: { channels: { id: string; unread: boolean }[] }[]
+ }>(communityKeys.server("srv_open"))
+ expect(cache?.categories[0].channels[0]).toMatchObject({ id: "ch_parent", unread: true })
+ })
+
+ it("suppresses the dot when the viewer is looking at the RAIL row (thread bump whose parent is open)", async () => {
+ await mountHook({ viewerUserId: "u_me" })
+ const { useCommunityStore } = await import("@/stores/community")
+ useCommunityStore.getState().setCurrentServerId("srv_open")
+ useCommunityStore.getState().subscribe({ channelId: "ch_parent" }) // parent row is open
+ refCounter = 0; stateCounter = 0; callbackCounter = 0
+ await mountHook({ viewerUserId: "u_me" })
+ capturedQueryClient.setQueryData(communityKeys.server("srv_open"), serverDetailFixture("ch_parent"))
+
+ capturedOnMessage!(unreadBump("ch_thread", "u_me", { serverId: "srv_open", railChannelId: "ch_parent" }))
+
+ const cache = capturedQueryClient.getQueryData<{
+ categories: { channels: { id: string; unread: boolean }[] }[]
+ }>(communityKeys.server("srv_open"))
+ expect(cache?.categories[0].channels[0].unread).toBe(false)
+ })
+
+ it("bumps the rail mention badge (servers() mentions +1) ONLY when isMention is set", async () => {
+ await mountHook({ viewerUserId: "u_me" })
+ capturedQueryClient.setQueryData(communityKeys.servers(), {
+ servers: [{ id: "srv_x", name: "X", initial: "X", active: false, mentions: 2, icon: null }],
+ })
+
+ // Plain unread (no isMention) → mention count unchanged.
+ capturedOnMessage!(unreadBump("ch_a", "u_me", { serverId: "srv_x" }))
+ let servers = capturedQueryClient.getQueryData<{ servers: { id: string; mentions: number }[] }>(communityKeys.servers())
+ expect(servers?.servers[0].mentions).toBe(2)
+
+ // Mention → +1.
+ capturedOnMessage!(unreadBump("ch_a", "u_me", { serverId: "srv_x", isMention: true }))
+ servers = capturedQueryClient.getQueryData<{ servers: { id: string; mentions: number }[] }>(communityKeys.servers())
+ expect(servers?.servers[0].mentions).toBe(3)
+ })
+
it("existing focused-channel message patch and debounced inbox invalidation still fire on message.create", async () => {
vi.useFakeTimers()
try {
@@ -1574,6 +1648,41 @@ describe("useCommunityWs — resyncs machines on WS reconnect", () => {
).toBe(true)
})
+ it("re-seeds the rail list + open server's detail on reconnect (inbox-dot-ws-driven ②)", async () => {
+ // Sidebar dots + rail mention badges are now driven by the live
+ // `unread.bump` patch, with no switch-refetch backing them. A bump dropped
+ // during the socket gap would leave them stale, so reconnect must re-seed
+ // both — else the cross-server dot fix silently rots after any disconnect.
+ await mountHook()
+ const { useCommunityStore } = await import("@/stores/community")
+ useCommunityStore.getState().setCurrentServerId("srv_open")
+ const spy = vi.spyOn(capturedQueryClient, "invalidateQueries")
+
+ // handleReconnect reads currentServerId via getState() at call time.
+ expect(capturedOnReconnect).not.toBeNull()
+ capturedOnReconnect!()
+
+ const invalidatedKeys = spy.mock.calls.map(
+ (c) => c[0]?.queryKey as unknown[] | undefined,
+ )
+ // Rail LIST = communityKeys.servers() = ["community","servers"] (length 2).
+ expect(
+ invalidatedKeys.some(
+ (k) => Array.isArray(k) && k.length === 2 && k[0] === "community" && k[1] === "servers",
+ ),
+ ).toBe(true)
+ // Open server's DETAIL = communityKeys.server(id) = ["community","servers",id].
+ expect(
+ invalidatedKeys.some(
+ (k) =>
+ Array.isArray(k) &&
+ k[0] === "community" &&
+ k[1] === "servers" &&
+ k[2] === "srv_open",
+ ),
+ ).toBe(true)
+ })
+
it("invalidates the focused DM's messages on reconnect, but NOT its read-state snapshot", async () => {
const { useCommunityStore } = await import("@/stores/community")
useCommunityStore.getState().subscribe({ dmConversationId: "dm_focus" })
diff --git a/src/web/src/hooks/community/use-community-ws.ts b/src/web/src/hooks/community/use-community-ws.ts
index e4c68441f..4ba490370 100644
--- a/src/web/src/hooks/community/use-community-ws.ts
+++ b/src/web/src/hooks/community/use-community-ws.ts
@@ -508,12 +508,48 @@ export function useCommunityWs(options?: UseCommunityWsOptions) {
// defensively).
case "community:unread.bump": {
const viewerId = viewerUserIdRef.current
- if (event.userId === viewerId && event.channelId !== sub.channelId) {
- const currentServerId = useCommunityStore.getState().currentServerId
- if (currentServerId) {
+ // The sidebar row to light: a thread/forum-post message has no
+ // independent tree row, so its dot lights the PARENT channel
+ // (`railChannelId`); a plain channel is its own row. Fall back to
+ // `channelId` for older frames that predate `railChannelId`.
+ const railChannelId = event.railChannelId ?? event.channelId
+ // Suppress the dot for the channel the viewer is actually looking at
+ // (its unread clears on read anyway) — compare against the ROW being
+ // lit, so a thread bump whose parent is open still suppresses.
+ if (event.userId === viewerId && railChannelId !== sub.channelId) {
+ // Channel-tree dot: patch the RIGHT server's detail. `serverId`
+ // (inbox-dot-ws-driven) lets an other-server message light its dot
+ // — previously only the open server was patched, so cross-server
+ // bumps never lit. Absent serverId (DM bump / older frame) → fall
+ // back to the currently-open server (backward-compatible).
+ const targetServerId =
+ event.serverId ?? useCommunityStore.getState().currentServerId
+ if (targetServerId) {
queryClient.setQueryData<ServerDetail | undefined>(
- communityKeys.server(currentServerId),
- (cache) => patchChannelUnread(cache, event.channelId, true),
+ communityKeys.server(targetServerId),
+ (cache) => patchChannelUnread(cache, railChannelId, true),
+ )
+ }
+ // Rail mention badge: the rail row carries only a `mentions` count
+ // (no separate unread dot — plain unread lives on the tree dot
+ // above). So bump the rail badge ONLY for a mention, on the right
+ // server row. Needs `serverId`; a DM/older frame without it can't
+ // locate a rail row, so skip (the tree dot / inbox still reflect it).
+ if (event.isMention && event.serverId) {
+ const bumpServerId = event.serverId
+ queryClient.setQueryData<ServersResponse | undefined>(
+ communityKeys.servers(),
+ (prev) =>
+ prev
+ ? {
+ ...prev,
+ servers: prev.servers.map((s) =>
+ s.id === bumpServerId
+ ? { ...s, mentions: s.mentions + 1 }
+ : s,
+ ),
+ }
+ : prev,
)
}
}
@@ -1068,6 +1104,20 @@ export function useCommunityWs(options?: UseCommunityWsOptions) {
// `message.create` events, none of which arrive while the socket is
// down.
void queryClient.invalidateQueries({ queryKey: communityKeys.inbox() })
+ // Sidebar unread dots + rail mention badges are now driven by the live
+ // `unread.bump` patch (inbox-dot-ws-driven plan) — no switch-refetch backs
+ // them anymore. A bump dropped during the socket gap would leave the dot /
+ // rail badge stale forever, so re-seed both on reconnect: the rail LIST
+ // (`servers()`, mention counts) and the open server's detail tree
+ // (`server(currentServerId)`, channel dots). `exact` on the list so it
+ // doesn't cascade-refetch every server's nested detail subtree.
+ void queryClient.invalidateQueries({ queryKey: communityKeys.servers(), exact: true })
+ const currentServerId = useCommunityStore.getState().currentServerId
+ if (currentServerId) {
+ void queryClient.invalidateQueries({
+ queryKey: communityKeys.server(currentServerId),
+ })
+ }
// Bot audit logs are WS-live-patched into the React Query cache; if
// the socket dropped, any events emitted during the gap never entered
// the store's ring and so never made it into the cache. Invalidating