Repository navigation
fix: stop treating unjoined public channels as private #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,30 +2,65 @@ import { getCommsClient } from './api.js' | |
| import { CliError } from './errors.js' | ||
| import { includePrivateChannels } from './global-args.js' | ||
|
|
||
| const publicChannelCache = new Map<number, Set<string>>() | ||
| // Channel ID to whether it's public, for the channels the user has joined. | ||
| const joinedChannelCache = new Map<number, Map<string, boolean>>() | ||
| const workspacePublicChannelCache = new Map<number, Set<string>>() | ||
|
|
||
| export async function getPublicChannelIds(workspaceId: number): Promise<Set<string>> { | ||
| const cached = publicChannelCache.get(workspaceId) | ||
| async function getJoinedChannels(workspaceId: number): Promise<Map<string, boolean>> { | ||
| const cached = joinedChannelCache.get(workspaceId) | ||
| if (cached) return cached | ||
|
|
||
| const client = await getCommsClient() | ||
| const channels = await client.channels.getChannels({ workspaceId }) | ||
| const publicIds = new Set<string>() | ||
| 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<Set<string>> { | ||
| 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<Set<string>> { | ||
| 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<boolean> { | ||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 3d6924d, and in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not changing this one. Thread view's channel lookup is the rate-limited call, and #94 is making a 429 there show the thread without the channel name instead of failing. If the private-channel check relied on that lookup, those views would fail again. The list call only runs for channels you haven't joined, about 0.3s there. Once #94 is in, the lookup could feed the check, with the list as the fallback on a 429. |
||
| ) | ||
| } | ||
|
|
||
| export async function assertChannelIsPublic(channelId: string, workspaceId: number): Promise<void> { | ||
| /** | ||
| * 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<void> { | ||
| 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', | ||
| ]) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the workspace's public channels and not
getChannelfor just this one:channels/getoneis the call with the per-user rate limit (see #94), and thread view already makes one for the channel name. The public channel list has no limit but is much bigger, since it carries every public channel's members: about 0.3s extra on an unjoined channel in a quick timing (1.31s against 0.98s for a joined one). It's only fetched when the channel isn't one you've joined, once per run. It also comes back empty for guests, so it can only be a fallback, not a replacement for the joined list.