From e09fd1be361caadaa495ab3c77092f7e68629a48 Mon Sep 17 00:00:00 2001 From: Brad Seiler Date: Mon, 23 Nov 2026 16:35:12 +0000 Subject: [PATCH] fix(relay): separate viewer membership from peer rosters Represent viewer membership independently from retained peer rosters. Respect explicit denial across channel actions, navigation, unread state, agent setup, and live eligibility checks while preserving legacy roster fallback. Co-authored-by: Honor Co-authored-by: Harrington <91868d0cc0b8144564e350ce0a9e46447f654ad3fe93b23e39fe71314c911781> Signed-off-by: Brad Seiler --- src/bundled/agents/AgentShareRecipients.tsx | 3 +- src/bundled/bestie/BestieConversation.tsx | 5 +- src/bundled/bestie/PinnedBestie.test.tsx | 110 +++++++---- src/bundled/bestie/PrivateChannels.tsx | 3 +- src/bundled/bestie/chat.ts | 3 +- src/bundled/channels/ChannelCompletion.tsx | 3 +- .../channels/ChannelsPage.tabs.test.tsx | 68 ++++++- src/bundled/channels/ChannelsPage.tsx | 7 +- src/bundled/channels/canvas-panel.test.tsx | 14 ++ src/bundled/channels/canvas-panel.tsx | 3 +- .../channels/useConversationTabs.test.tsx | 10 + src/bundled/channels/useSoloShare.ts | 3 +- src/bundled/inbox/InboxPage.tsx | 5 +- .../agents/AgentConnectionSetup.test.tsx | 187 +++++++++++++++--- src/features/agents/AgentConnectionSetup.tsx | 33 ++-- src/features/channel-members/members.test.ts | 89 ++++++++- src/features/channel-members/members.ts | 10 +- .../channel-navigation/RegionChannelGroup.tsx | 3 +- .../channel-navigation/SoloRegions.test.tsx | 52 +++++ .../conversation/ConfirmedChannel.tsx | 4 +- .../direct-messages/NewMessage.test.tsx | 31 ++- .../drafts-sent/DraftPreview.test.tsx | 69 +++++++ src/features/drafts-sent/DraftPreview.tsx | 8 +- src/features/huddle/composer-owner.ts | 3 +- src/features/huddle/requests.ts | 9 +- src/features/huddle/service.ts | 3 +- src/features/messages/MessageManagement.tsx | 3 +- .../notifications/community-unread.test.ts | 43 +++- .../notifications/community-unread.ts | 5 +- src/features/notifications/messages.ts | 3 +- .../relay/channel-lifecycle-protocol.ts | 3 +- src/features/relay/channel-references.test.ts | 16 ++ src/features/relay/contracts.ts | 5 +- .../relay/discovery-permissions.test.ts | 1 + src/features/relay/discovery.ts | 1 + src/features/relay/fold.test.ts | 2 + src/features/relay/inbox-feed.ts | 3 +- src/features/relay/inbox-reactions.ts | 3 +- src/features/relay/membership.test.ts | 28 ++- src/features/relay/membership.ts | 23 ++- src/features/relay/prepared.test.ts | 1 + src/features/relay/store.ts | 11 +- .../relay/unread-invalidation.test.ts | 164 ++++++++++++++- src/features/relay/unread.ts | 27 +-- src/features/solo/labels.ts | 3 +- src/features/solo/share.ts | 5 +- 46 files changed, 933 insertions(+), 155 deletions(-) diff --git a/src/bundled/agents/AgentShareRecipients.tsx b/src/bundled/agents/AgentShareRecipients.tsx index 69886e1a5d..c1ae9b8f13 100644 --- a/src/bundled/agents/AgentShareRecipients.tsx +++ b/src/bundled/agents/AgentShareRecipients.tsx @@ -1,3 +1,4 @@ +import { viewerMember } from "../../features/relay/membership"; // Who Share sends an agent to: people by direct message, or a channel the // viewer belongs to, found with one search as Share access does. import { useSyncExternalStore, type Ref } from "react"; @@ -70,7 +71,7 @@ export function AgentShareRecipients({ !item.cached && !item.archived && ["stream", "forum"].includes(item.channelType ?? "") && - item.members?.includes(session.viewer ?? "") && + viewerMember(item, session.viewer) && !picked.has(item.id) && item.name.toLowerCase().includes(needle), ) diff --git a/src/bundled/bestie/BestieConversation.tsx b/src/bundled/bestie/BestieConversation.tsx index c69d1ecb22..137549586d 100644 --- a/src/bundled/bestie/BestieConversation.tsx +++ b/src/bundled/bestie/BestieConversation.tsx @@ -1,3 +1,4 @@ +import { viewerMember } from "../../features/relay/membership"; import { useCallback, useEffect, @@ -559,9 +560,7 @@ export function BestieConversation({ current.cached || (current.readOnly && current.viewerRole !== "guest") || - !current.members?.includes( - session.viewer ?? "", - ) || + !viewerMember(current, session.viewer) || !host.panels.snapshot().includes(panel) || !panel.channelMenu?.eligible(current, session) ) diff --git a/src/bundled/bestie/PinnedBestie.test.tsx b/src/bundled/bestie/PinnedBestie.test.tsx index 1ceeaea81c..089b6e3041 100644 --- a/src/bundled/bestie/PinnedBestie.test.tsx +++ b/src/bundled/bestie/PinnedBestie.test.tsx @@ -308,8 +308,15 @@ async function fixture({ owner.dispose(); }); const send = vi.fn(() => "d".repeat(64)); + let channelListOverride: + | ReturnType + | undefined; const session = { ...owner.session, + channels: { + ...owner.session.channels, + list: () => channelListOverride ?? owner.session.channels.list(), + }, agentChoices, messages: { ...owner.session.messages, send }, }; @@ -365,6 +372,9 @@ async function fixture({ find: () => undefined, } as unknown as Agents; return { + setChannelList(next: ReturnType) { + channelListOverride = next; + }, setPanels(next: readonly RegisteredPanel[]) { panels = next; for (const listener of panelListeners) listener(); @@ -1086,39 +1096,73 @@ it("offers the shared DM actions and channel details in Bestie", async () => { ).toBeInTheDocument(); }); -it("opens eligible plugin menu items in the Bestie workspace", async () => { - const f = await fixture({ existing: true }); - f.choose(pollen); - const user = userEvent.setup(); - f.setPanels([ - { - id: "canvas", - key: "test/canvas", - pluginId: "test", - revision: "1", - title: "Canvas", - matches: () => false, - channelMenu: { - label: "View canvas", - eligible: (channel) => channel.id === pollenDM, - }, - component: ({ channelContext }) => ( -

Canvas for {channelContext?.channelId}

- ), - } satisfies RegisteredPanel, - ]); - f.mount(); - await user.click( - await screen.findByRole("button", { name: "Channel actions" }), - ); - await user.click( - await screen.findByRole("menuitem", { name: "View canvas" }), - ); - expect(await screen.findByText(`Canvas for ${pollenDM}`)).toBeVisible(); - expect( - screen.getByRole("textbox", { name: "Message Pollen" }), - ).toBeInTheDocument(); -}); +it.each([ + "legacy roster", + "joined without peers", + "denied with retained peers", +])( + "opens eligible Bestie menu panels using viewer authority: %s", + async (membership) => { + const f = await fixture({ existing: true }); + f.choose(pollen); + const user = userEvent.setup(); + f.setPanels([ + { + id: "canvas", + key: "test/canvas", + pluginId: "test", + revision: "1", + title: "Canvas", + matches: () => false, + channelMenu: { + label: "View canvas", + eligible: (channel) => channel.id === pollenDM, + }, + component: ({ channelContext }) => ( +

Canvas for {channelContext?.channelId}

+ ), + } satisfies RegisteredPanel, + ]); + f.mount(); + await user.click( + await screen.findByRole("button", { name: "Channel actions" }), + ); + const action = await screen.findByRole("menuitem", { name: "View canvas" }); + // Change only the authoritative read at click time. The rendered menu is + // already open, so denial must be rechecked by the Bestie handler itself. + const list = f.session.channels.list(); + const current = list.channels.find((channel) => channel.id === pollenDM); + if (!current) throw new Error("Fixture needs the selected DM"); + if (membership !== "legacy roster") { + const { members: _peers, ...withoutPeers } = current; + const updated = + membership === "joined without peers" + ? { + ...withoutPeers, + viewerMembership: { pubkey: f.viewer, joined: true }, + } + : { + ...current, + viewerMembership: { pubkey: f.viewer, joined: false }, + }; + f.setChannelList({ + ...list, + channels: list.channels.map((channel) => + channel.id === pollenDM ? updated : channel, + ), + }); + } + await user.click(action); + if (membership === "denied with retained peers") { + expect(screen.queryByText(`Canvas for ${pollenDM}`)).toBeNull(); + } else { + expect(await screen.findByText(`Canvas for ${pollenDM}`)).toBeVisible(); + } + expect( + screen.getByRole("textbox", { name: "Message Pollen" }), + ).toBeInTheDocument(); + }, +); it("opens the DM an addition lands in, when Bestie's Members starts or finds another", async () => { const f = await fixture(); diff --git a/src/bundled/bestie/PrivateChannels.tsx b/src/bundled/bestie/PrivateChannels.tsx index fe7bde8433..dde55288b3 100644 --- a/src/bundled/bestie/PrivateChannels.tsx +++ b/src/bundled/bestie/PrivateChannels.tsx @@ -1,3 +1,4 @@ +import { viewerMember } from "../../features/relay/membership"; import { useEffect, useId, useLayoutEffect, useRef, useState } from "react"; import { administerMember } from "../../features/channel-members/administration"; import { canAddMembers } from "../../features/channel-members/members"; @@ -32,7 +33,7 @@ export function privateChannels( !channel.huddle && (channel.channelType === "stream" || channel.channelType === "forum") && channel.private && - channel.members?.includes(viewer), + viewerMember(channel, viewer), ) .sort(compareRecent); return { diff --git a/src/bundled/bestie/chat.ts b/src/bundled/bestie/chat.ts index f7b2baf146..d3925b542b 100644 --- a/src/bundled/bestie/chat.ts +++ b/src/bundled/bestie/chat.ts @@ -1,3 +1,4 @@ +import { viewerMember } from "../../features/relay/membership"; import type { ChannelSummary } from "../../features/relay/contracts"; /** The verified one-to-one DM with this Bestie. Solo chats stay separate. */ @@ -13,7 +14,7 @@ export function bestieChat( channel.channelType === "dm" && !channel.cached && !channel.archived && - channel.members?.includes(viewer) && + viewerMember(channel, viewer) && onlyOther(channel.members, viewer) === bestie, ) .sort( diff --git a/src/bundled/channels/ChannelCompletion.tsx b/src/bundled/channels/ChannelCompletion.tsx index f0ee7f5058..6a6efbaaa8 100644 --- a/src/bundled/channels/ChannelCompletion.tsx +++ b/src/bundled/channels/ChannelCompletion.tsx @@ -1,3 +1,4 @@ +import { viewerMember } from "../../features/relay/membership"; import { useContext, useLayoutEffect, useSyncExternalStore } from "react"; import { channelIcon } from "../../features/channels/channel-icon"; import { usePublicChannelSearch } from "../../features/channels/usePublicChannelSearch"; @@ -38,7 +39,7 @@ function joined(channel: ChannelSummary, viewer: string | undefined) { !!viewer && referable(channel) && (!channel.readOnly || channel.viewerRole === "guest" || !!channel.cached) && - !!channel.members?.includes(viewer) + viewerMember(channel, viewer) ); } diff --git a/src/bundled/channels/ChannelsPage.tabs.test.tsx b/src/bundled/channels/ChannelsPage.tabs.test.tsx index 2848328e3d..227578cb48 100644 --- a/src/bundled/channels/ChannelsPage.tabs.test.tsx +++ b/src/bundled/channels/ChannelsPage.tabs.test.tsx @@ -126,6 +126,7 @@ async function page( sidebarPreferences?: RelaySession["sidebarPreferences"], uploadAttachment?: (file: File) => Promise, linkedPanel?: Partial, + menuPanel = false, ) { const viewer = keypair(), authority = keypair(); @@ -179,6 +180,17 @@ async function page( await vi.waitFor(() => expect(owner.session.channels.list().status).toBe("ready"), ); + let channelListOverride: + | ReturnType + | undefined; + const menuSession = menuPanel + ? (Object.defineProperty(Object.create(owner.session), "channels", { + value: { + ...owner.session.channels, + list: () => channelListOverride ?? owner.session.channels.list(), + }, + }) as RelaySession) + : undefined; const snapshot = { status: "ready" as const, generation: 1, @@ -193,7 +205,7 @@ async function page( value: sidebarPreferences, }, ) as typeof owner.session) - : owner.session, + : (menuSession ?? owner.session), }; const relay: RelayData = { snapshot: () => snapshot, @@ -211,6 +223,9 @@ async function page( title: "Notes", matches: () => false, channelPlacement: "tab", + ...(menuPanel && { + channelMenu: { label: "View notes", eligible: () => true }, + }), channelLauncher: ({ context, toggle }) => (