From 6df7209c4cf15284f3f8893854a2c2ba1fdf70bc Mon Sep 17 00:00:00 2001 From: Craig Carlyle Date: Mon, 2 Nov 2026 10:02:43 -0700 Subject: [PATCH 1/2] fix: stop treating unjoined public channels as private The private-channel check only knew about the channels you've joined, so a thread in a public channel you hadn't joined failed with "This thread belongs to a private channel" and needed --include-private-channels. When a channel isn't one you've joined, the check now looks for it among the workspace's public channels before failing. Joined channels work as before with no extra call, and joined private channels still need the flag. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/lib/public-channels.test.ts | 42 ++++++++++++++++++++++++++++++ src/lib/public-channels.ts | 45 +++++++++++++++++++++++++-------- 2 files changed, 76 insertions(+), 11 deletions(-) diff --git a/src/lib/public-channels.test.ts b/src/lib/public-channels.test.ts index 3222334..19b0b32 100644 --- a/src/lib/public-channels.test.ts +++ b/src/lib/public-channels.test.ts @@ -16,11 +16,15 @@ const mockGetCommsClient = vi.mocked(getCommsClient) function makeMockChannels( channels: Array<{ id: string; public: boolean }>, + workspacePublicChannels: Array<{ id: string }> = [], ): ReturnType { return Promise.resolve({ channels: { getChannels: vi.fn().mockResolvedValue(channels), }, + workspaces: { + getPublicChannels: vi.fn().mockResolvedValue(workspacePublicChannels), + }, }) as unknown as ReturnType } @@ -161,6 +165,44 @@ describe('assertChannelIsPublic', () => { await expect(assertChannelIsPublic('CH5', 100)).resolves.toBeUndefined() }) + it("allows a public channel the user hasn't joined", async () => { + mockGetCommsClient.mockImplementation(() => + makeMockChannels([{ id: 'CH5', public: true }], [{ id: 'CH5' }, { id: 'CH7' }]), + ) + + await expect(assertChannelIsPublic('CH7', 100)).resolves.toBeUndefined() + }) + + it('throws for a channel that is neither joined nor public', async () => { + mockGetCommsClient.mockImplementation(() => + makeMockChannels([{ id: 'CH5', public: true }], [{ id: 'CH5' }, { id: 'CH7' }]), + ) + + await expect(assertChannelIsPublic('CH9', 100)).rejects.toThrow('private channel') + }) + + it("only lists the workspace's public channels for a channel the user hasn't joined", async () => { + const getPublicChannels = vi.fn().mockResolvedValue([{ id: 'CH5' }, { id: 'CH7' }]) + mockGetCommsClient.mockResolvedValue({ + channels: { + getChannels: vi.fn().mockResolvedValue([ + { id: 'CH5', public: true }, + { id: 'CH6', public: false }, + ]), + }, + workspaces: { getPublicChannels }, + } as unknown as Awaited>) + + await assertChannelIsPublic('CH5', 100) + await expect(assertChannelIsPublic('CH6', 100)).rejects.toThrow('private channel') + expect(getPublicChannels).not.toHaveBeenCalled() + + await assertChannelIsPublic('CH7', 100) + await assertChannelIsPublic('CH7', 100) + expect(getPublicChannels).toHaveBeenCalledTimes(1) + expect(getPublicChannels).toHaveBeenCalledWith(100) + }) + it('allows private channels when --include-private-channels is set', async () => { process.argv = ['node', 'tdc', '--include-private-channels'] resetGlobalArgs() diff --git a/src/lib/public-channels.ts b/src/lib/public-channels.ts index f842f59..7d609a5 100644 --- a/src/lib/public-channels.ts +++ b/src/lib/public-channels.ts @@ -2,30 +2,53 @@ import { getCommsClient } from './api.js' import { CliError } from './errors.js' import { includePrivateChannels } from './global-args.js' -const publicChannelCache = new Map>() +// Channel ID to whether it's public, for the channels the user has joined. +const joinedChannelCache = new Map>() +const workspacePublicChannelCache = new Map>() -export async function getPublicChannelIds(workspaceId: number): Promise> { - const cached = publicChannelCache.get(workspaceId) +async function getJoinedChannels(workspaceId: number): Promise> { + const cached = joinedChannelCache.get(workspaceId) if (cached) return cached const client = await getCommsClient() const channels = await client.channels.getChannels({ workspaceId }) - const publicIds = new Set() - for (const ch of channels) { - if (ch.public) publicIds.add(ch.id) - } - publicChannelCache.set(workspaceId, publicIds) + const joined = new Map(channels.map((ch) => [ch.id, ch.public])) + joinedChannelCache.set(workspaceId, joined) + return joined +} + +/** IDs of the public channels the user has joined. */ +export async function getPublicChannelIds(workspaceId: number): Promise> { + const joined = await getJoinedChannels(workspaceId) + return new Set([...joined].filter(([, isPublic]) => isPublic).map(([id]) => id)) +} + +// Every public channel in the workspace, joined or not. Much bigger than the joined list (all +// members of every public channel) and empty for guests, so it's only used for unjoined channels. +async function getWorkspacePublicChannelIds(workspaceId: number): Promise> { + const cached = workspacePublicChannelCache.get(workspaceId) + if (cached) return cached + + const client = await getCommsClient() + const channels = await client.workspaces.getPublicChannels(workspaceId) + const publicIds = new Set(channels.map((ch) => ch.id)) + workspacePublicChannelCache.set(workspaceId, publicIds) return publicIds } export function clearPublicChannelCache(): void { - publicChannelCache.clear() + joinedChannelCache.clear() + workspacePublicChannelCache.clear() } export async function assertChannelIsPublic(channelId: string, workspaceId: number): Promise { if (includePrivateChannels()) return - const publicIds = await getPublicChannelIds(workspaceId) - if (!publicIds.has(channelId)) { + const joined = await getJoinedChannels(workspaceId) + const isPublic = + joined.get(channelId) ?? + // getChannels only returns joined channels, so an unjoined one could still be public. + (await getWorkspacePublicChannelIds(workspaceId)).has(channelId) + if (!isPublic) { throw new CliError('NOT_FOUND', 'This thread belongs to a private channel.', [ 'Use --include-private-channels to access it', ]) From 3d6924d7b380edf2697b63d1077669ea26451ab9 Mon Sep 17 00:00:00 2001 From: Craig Carlyle Date: Mon, 2 Nov 2026 13:42:28 -0700 Subject: [PATCH 2/2] perf: use the channel's own public flag when the caller has it channel threads and thread create already have the channel they resolved, so the private-channel check now takes its public flag instead of looking it up. For an unjoined channel resolved by name, the lookup was fetching the workspace's public channels a second time. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/commands/channel/threads.test.ts | 2 +- src/commands/channel/threads.ts | 2 +- src/commands/thread/create.ts | 2 +- src/lib/public-channels.test.ts | 14 ++++++++++++++ src/lib/public-channels.ts | 20 ++++++++++++++++---- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/commands/channel/threads.test.ts b/src/commands/channel/threads.test.ts index 6188f80..b15c0e9 100644 --- a/src/commands/channel/threads.test.ts +++ b/src/commands/channel/threads.test.ts @@ -425,7 +425,7 @@ describe('channel threads', () => { await program.parseAsync(['node', 'tdc', 'channel', 'threads', '12345', '--json']) - expect(vi.mocked(assertChannelIsPublic)).toHaveBeenCalledWith('CH100', 1) + expect(vi.mocked(assertChannelIsPublic)).toHaveBeenCalledWith('CH100', 1, true) }) it('propagates the error when the channel is private and the guard rejects', async () => { diff --git a/src/commands/channel/threads.ts b/src/commands/channel/threads.ts index 6dfdb65..37c97c1 100644 --- a/src/commands/channel/threads.ts +++ b/src/commands/channel/threads.ts @@ -74,7 +74,7 @@ export async function showChannelThreads( } const channel = await resolveChannelRef(channelRef, workspaceId) - await assertChannelIsPublic(channel.id, workspaceId) + await assertChannelIsPublic(channel.id, workspaceId, channel.public) const archived = archiveFilterToFlag(options.archiveFilter) const client = await getCommsClient() diff --git a/src/commands/thread/create.ts b/src/commands/thread/create.ts index ff5c9d8..4f2c2d2 100644 --- a/src/commands/thread/create.ts +++ b/src/commands/thread/create.ts @@ -47,7 +47,7 @@ export async function createThread( const client = await getCommsClient() const channel = await client.channels.getChannel(channelId) - await assertChannelIsPublic(channelId, channel.workspaceId) + await assertChannelIsPublic(channelId, channel.workspaceId, channel.public) let resolved: ResolvedNotify | undefined if (allIds) { diff --git a/src/lib/public-channels.test.ts b/src/lib/public-channels.test.ts index 19b0b32..8fb458b 100644 --- a/src/lib/public-channels.test.ts +++ b/src/lib/public-channels.test.ts @@ -203,6 +203,20 @@ describe('assertChannelIsPublic', () => { expect(getPublicChannels).toHaveBeenCalledWith(100) }) + it('uses the public flag a caller passes instead of looking the channel up', async () => { + const getChannels = vi.fn() + const getPublicChannels = vi.fn() + mockGetCommsClient.mockResolvedValue({ + channels: { getChannels }, + workspaces: { getPublicChannels }, + } as unknown as Awaited>) + + await expect(assertChannelIsPublic('CH7', 100, true)).resolves.toBeUndefined() + await expect(assertChannelIsPublic('CH6', 100, false)).rejects.toThrow('private channel') + expect(getChannels).not.toHaveBeenCalled() + expect(getPublicChannels).not.toHaveBeenCalled() + }) + it('allows private channels when --include-private-channels is set', async () => { process.argv = ['node', 'tdc', '--include-private-channels'] resetGlobalArgs() diff --git a/src/lib/public-channels.ts b/src/lib/public-channels.ts index 7d609a5..9a53f85 100644 --- a/src/lib/public-channels.ts +++ b/src/lib/public-channels.ts @@ -41,14 +41,26 @@ export function clearPublicChannelCache(): void { workspacePublicChannelCache.clear() } -export async function assertChannelIsPublic(channelId: string, workspaceId: number): Promise { - if (includePrivateChannels()) return +async function isPublicChannel(channelId: string, workspaceId: number): Promise { const joined = await getJoinedChannels(workspaceId) - const isPublic = + return ( joined.get(channelId) ?? // getChannels only returns joined channels, so an unjoined one could still be public. (await getWorkspacePublicChannelIds(workspaceId)).has(channelId) - if (!isPublic) { + ) +} + +/** + * Throws unless the channel is public or --include-private-channels is set. Callers that + * already have the channel pass its `public` flag, which skips the lookups. + */ +export async function assertChannelIsPublic( + channelId: string, + workspaceId: number, + isPublic?: boolean, +): Promise { + if (includePrivateChannels()) return + if (!(isPublic ?? (await isPublicChannel(channelId, workspaceId)))) { throw new CliError('NOT_FOUND', 'This thread belongs to a private channel.', [ 'Use --include-private-channels to access it', ])