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 3222334..8fb458b 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,58 @@ 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('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 f842f59..9a53f85 100644 --- a/src/lib/public-channels.ts +++ b/src/lib/public-channels.ts @@ -2,30 +2,65 @@ 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() +} + +async function isPublicChannel(channelId: string, workspaceId: number): Promise { + const joined = await getJoinedChannels(workspaceId) + return ( + joined.get(channelId) ?? + // getChannels only returns joined channels, so an unjoined one could still be public. + (await getWorkspacePublicChannelIds(workspaceId)).has(channelId) + ) } -export async function assertChannelIsPublic(channelId: string, workspaceId: number): Promise { +/** + * 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 - const publicIds = await getPublicChannelIds(workspaceId) - if (!publicIds.has(channelId)) { + 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', ])