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 }) => (