Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): preserve channel ref discriminator

Gus977795958a40
diff --git a/src/web/src/app/c/me/[dmId]/page.tsx b/src/web/src/app/c/me/[dmId]/page.tsx
index 0f8f6fbd9..8cec2138d 100644
--- a/src/web/src/app/c/me/[dmId]/page.tsx
+++ b/src/web/src/app/c/me/[dmId]/page.tsx
@@ -29,6 +29,7 @@ import { useDmReadStateSnapshot } from "@/hooks/community/use-dm-read-state"
import { useDmWatermark } from "@/hooks/community/use-dm-watermark"
import { useEagerDmRead } from "@/hooks/community/use-eager-dm-read"
import { useChannelRefDirectory } from "@/hooks/community/use-channel-ref-directory"
+import { toChannelRefCandidate } from "@/lib/community/channel-ref-extension"
import {
useToggleReactionApi,
useToggleMark,
@@ -130,7 +131,7 @@ function DmView() {
const channelRefCandidates = useMemo(
() =>
channelRefDirectory.flatMap((s) =>
- s.channels.map((ch) => ({ id: ch.id, name: ch.name, serverId: s.id, serverName: s.name })),
+ s.channels.map((ch) => toChannelRefCandidate(s, ch)),
),
[channelRefDirectory],
)
diff --git a/src/web/src/components/community/channels/channel-route.tsx b/src/web/src/components/community/channels/channel-route.tsx
index a8e320074..421fa44b1 100644
--- a/src/web/src/components/community/channels/channel-route.tsx
+++ b/src/web/src/components/community/channels/channel-route.tsx
@@ -16,6 +16,7 @@ import type { OpenProfile } from "@/components/community/social/profile-types"
import { canManageServer, USE_SERVER_DEFAULT } from "@alook/shared"
import { setLastChannel } from "@/lib/community/last-channel"
import { resolveChannelDisplayName } from "@/lib/community/channel-display-name"
+import { toChannelRefCandidate } from "@/lib/community/channel-ref-extension"
import {
useCurrentChannelId,
useUiHandlers,
@@ -102,14 +103,11 @@ export function ChannelRoute({ serverParam, channelId }: { serverParam: string;
// server, so no directory hook needed here (see `me/[dmId]/page.tsx` for
// the cross-server DM case).
const channelRefCandidates = useMemo(() => {
- const allChannels = currentServer?.categories?.flatMap((c) => c.channels) ?? []
- return allChannels.map((ch) => ({
- id: ch.id,
- name: ch.name,
- serverId,
- serverName: currentServer?.name ?? "",
- }))
- }, [currentServer, serverId])
+ if (!currentServer) return []
+ return currentServer.categories
+ .flatMap((category) => category.channels)
+ .map((channel) => toChannelRefCandidate(currentServer, channel))
+ }, [currentServer])
const notifs = useNotificationSettings()
const channelNotif = notifs.channel
const { mutate: setChannelNotif } = useSetChannelNotif()
diff --git a/src/web/src/components/community/channels/thread-channel-surface.test.ts b/src/web/src/components/community/channels/thread-channel-surface.test.ts
index 3becb9dd7..2352d18f3 100644
--- a/src/web/src/components/community/channels/thread-channel-surface.test.ts
+++ b/src/web/src/components/community/channels/thread-channel-surface.test.ts
@@ -176,7 +176,7 @@ function surfaceProps(overrides: Record<string, unknown> = {}) {
onBack: vi.fn(),
composerMembers: [{ id: "member_1", userId: "member_1", name: "Alice" }],
onSearchComposerMembers: vi.fn(),
- channelRefCandidates: [{ id: "parent_1", name: "general", serverId: "server_1", serverName: "Server" }],
+ channelRefCandidates: [{ id: "parent_1", name: "general", serverId: "server_1", serverName: "Server", serverDiscriminator: "0001" }],
memberPanelProps: {
members: [],
membersLoading: false,
diff --git a/src/web/src/components/community/messages/composer-suggestion-popups.test.ts b/src/web/src/components/community/messages/composer-suggestion-popups.test.ts
index acc0e00a5..6fa8e6b7e 100644
--- a/src/web/src/components/community/messages/composer-suggestion-popups.test.ts
+++ b/src/web/src/components/community/messages/composer-suggestion-popups.test.ts
@@ -181,12 +181,14 @@ describe("Composer suggestion popups", () => {
name: "general",
serverId: "server-1",
serverName: "One",
+ serverDiscriminator: "0001",
},
{
id: "channel-2",
name: "random",
serverId: "server-2",
serverName: "Two",
+ serverDiscriminator: "0002",
},
],
selectedIndex: 0,
@@ -213,6 +215,7 @@ describe("Composer suggestion popups", () => {
])
const first = renderer.root.findAllByType("button")[0]
+ expect(first.props["data-testid"]).toBe("community-channel-ref-option-channel-1")
const preventDefault = vi.fn()
await act(async () => first.props.onMouseDown({ preventDefault }))
expect(preventDefault).toHaveBeenCalledOnce()
@@ -221,6 +224,7 @@ describe("Composer suggestion popups", () => {
label: "general",
serverId: "server-1",
serverName: "One",
+ serverDiscriminator: "0001",
})
await act(async () => {
@@ -234,6 +238,7 @@ describe("Composer suggestion popups", () => {
...state.items[1],
serverId: "server-1",
serverName: "One",
+ serverDiscriminator: "0001",
},
],
},
diff --git a/src/web/src/components/community/messages/composer-suggestion-popups.tsx b/src/web/src/components/community/messages/composer-suggestion-popups.tsx
index 0fcd5efd7..1628cbaab 100644
--- a/src/web/src/components/community/messages/composer-suggestion-popups.tsx
+++ b/src/web/src/components/community/messages/composer-suggestion-popups.tsx
@@ -145,6 +145,7 @@ function ChannelRefRow({
<button
type="button"
role="option"
+ data-testid={tid.channelRefOption(item.id)}
aria-selected={selected}
className={[
"flex w-full items-center gap-2 rounded-md px-2 py-2 text-left text-sm transition-colors",
diff --git a/src/web/src/components/community/messages/composer-view.test.ts b/src/web/src/components/community/messages/composer-view.test.ts
index 22e24a7a5..51f7c7c70 100644
--- a/src/web/src/components/community/messages/composer-view.test.ts
+++ b/src/web/src/components/community/messages/composer-view.test.ts
@@ -105,6 +105,7 @@ describe("ComposerView", () => {
name: "general",
serverId: "server-1",
serverName: "One",
+ serverDiscriminator: "0001",
},
],
}
diff --git a/src/web/src/components/community/messages/use-composer-suggestions.test.ts b/src/web/src/components/community/messages/use-composer-suggestions.test.ts
index 5f30c97c9..e6e34c433 100644
--- a/src/web/src/components/community/messages/use-composer-suggestions.test.ts
+++ b/src/web/src/components/community/messages/use-composer-suggestions.test.ts
@@ -69,6 +69,7 @@ const channel = (
name: "general",
serverId: "server-1",
serverName: "One",
+ serverDiscriminator: "0001",
...overrides,
})
@@ -335,8 +336,27 @@ describe("useComposerSuggestions", () => {
}),
)
})
- expect(resultRef.current!.channelRefPopup).toBe(previousState)
- expect(resultRef.current!.channelRefPopup.items[0].serverName).toBe("One")
+ expect(resultRef.current!.channelRefPopup).not.toBe(previousState)
+ expect(resultRef.current!.channelRefPopup.items[0].serverName).toBe("Two")
+
+ mocks.rankChannel.mockReturnValue([channel({
+ serverName: "Two",
+ serverDiscriminator: "0002",
+ })])
+ await act(async () => {
+ renderer.update(
+ createElement(Harness, {
+ members: [],
+ context: "dm",
+ channelRefCandidates: [channel({
+ serverName: "Two",
+ serverDiscriminator: "0002",
+ })],
+ resultRef,
+ }),
+ )
+ })
+ expect(resultRef.current!.channelRefPopup.items[0].serverDiscriminator).toBe("0002")
const second = channel({ id: "channel-2", name: "random" })
await act(async () => {
diff --git a/src/web/src/components/community/messages/use-composer-suggestions.ts b/src/web/src/components/community/messages/use-composer-suggestions.ts
index c0df5c74c..a70e2bae4 100644
--- a/src/web/src/components/community/messages/use-composer-suggestions.ts
+++ b/src/web/src/components/community/messages/use-composer-suggestions.ts
@@ -55,7 +55,9 @@ function channelRefItemsEqual(
if (
a[index].id !== b[index].id ||
a[index].name !== b[index].name ||
- a[index].serverId !== b[index].serverId
+ a[index].serverId !== b[index].serverId ||
+ a[index].serverName !== b[index].serverName ||
+ a[index].serverDiscriminator !== b[index].serverDiscriminator
) return false
}
return true
diff --git a/src/web/src/hooks/community/use-servers.test.ts b/src/web/src/hooks/community/use-servers.test.ts
index 27cade9e3..d7ff40e5c 100644
--- a/src/web/src/hooks/community/use-servers.test.ts
+++ b/src/web/src/hooks/community/use-servers.test.ts
@@ -77,7 +77,7 @@ describe("useServer / serverQueryFn", () => {
expect(apiFetchMock).toHaveBeenCalledWith("/api/community/servers/srv_1/categories")
expect(apiFetchMock).toHaveBeenCalledWith("/api/community/servers/srv_1/channels")
expect(apiFetchMock).toHaveBeenCalledWith("/api/community/servers/srv_1/unreads")
- expect(data).toEqual({ ...detail, categories: [
+ expect(data).toEqual({ ...detail, discriminator: "0001", categories: [
{ id: "cat_1", name: "Main", private: 0, channels: [{ id: "ch_1", name: "general", categoryId: "cat_1", active: false, unread: true }] },
{ id: "__uncategorized__", name: "", private: 0, channels: [{ id: "ch_2", name: "loose", categoryId: null, active: false, unread: false }] },
], forumUnreadState: {} })
diff --git a/src/web/src/hooks/community/use-servers.ts b/src/web/src/hooks/community/use-servers.ts
index de77bd7e8..0b75532e2 100644
--- a/src/web/src/hooks/community/use-servers.ts
+++ b/src/web/src/hooks/community/use-servers.ts
@@ -81,6 +81,7 @@ export function useServers(): UseQueryResult<ServersResponse> & {
export type ServerDetail = {
id: string
name: string
+ discriminator: string
description: string
icon: string | null
ownerId: string
@@ -149,6 +150,7 @@ export const serverQueryFn = (serverId: string) => async (): Promise<ServerDetai
return {
id: server.id,
name: server.name,
+ discriminator: server.discriminator,
description: server.description ?? "",
icon: server.icon,
ownerId: server.ownerId,
diff --git a/src/web/src/lib/community/channel-ref-extension.test.ts b/src/web/src/lib/community/channel-ref-extension.test.ts
index 2ba3fe148..5e7af6630 100644
--- a/src/web/src/lib/community/channel-ref-extension.test.ts
+++ b/src/web/src/lib/community/channel-ref-extension.test.ts
@@ -2,17 +2,25 @@ import { describe, it, expect, vi } from "vitest"
import {
buildCommunityChannelRefExtension,
rankChannelRefItems,
+ toChannelRefCandidate,
toChannelRefCommandProps,
EMPTY_CHANNEL_REF_STATE,
type ChannelRefCandidate,
type ChannelRefPopupState,
} from "./channel-ref-extension"
-const candidate = (id: string, name: string, serverId = "s1", serverName = "Studio"): ChannelRefCandidate => ({
+const candidate = (
+ id: string,
+ name: string,
+ serverId = "s1",
+ serverName = "Studio",
+ serverDiscriminator = "0042",
+): ChannelRefCandidate => ({
id,
name,
serverId,
serverName,
+ serverDiscriminator,
})
describe("rankChannelRefItems", () => {
@@ -115,13 +123,27 @@ function build(
}
describe("toChannelRefCommandProps", () => {
- it("maps a ChannelRefCandidate to { id, label, serverId, serverName } — this is the one function shared by both the keyboard and mouse-click insertion paths", () => {
+ it("builds the same complete candidate for server-scoped and directory call sites", () => {
+ expect(toChannelRefCandidate(
+ { id: "srv_1", name: "Alook", discriminator: "5620" },
+ { id: "chn_1", name: "general" },
+ )).toEqual({
+ id: "chn_1",
+ name: "general",
+ serverId: "srv_1",
+ serverName: "Alook",
+ serverDiscriminator: "5620",
+ })
+ })
+
+ it("maps the complete canonical server identity through both insertion paths", () => {
const item = candidate("chn_1", "general", "srv_1", "Studio")
expect(toChannelRefCommandProps(item)).toEqual({
id: "chn_1",
label: "general",
serverId: "srv_1",
serverName: "Studio",
+ serverDiscriminator: "0042",
})
})
})
@@ -155,14 +177,14 @@ describe("buildCommunityChannelRefExtension — suggestion.items callback", () =
})
describe("buildCommunityChannelRefExtension — renderText/renderHTML", () => {
- it("renderText produces /serverName/label (name-based, not id-based)", () => {
+ it("renderText produces /serverName#discriminator/label", () => {
const { ext } = build()
const { renderText } = getRenderFns(ext)
expect(
renderText({
- node: { attrs: { id: "chn_abc", serverId: "srv_xyz", serverName: "Studio", label: "general" } },
+ node: { attrs: { id: "chn_abc", serverId: "srv_xyz", serverName: "Studio", serverDiscriminator: "0042", label: "general" } },
}),
- ).toBe("/Studio/general")
+ ).toBe("/Studio#0042/general")
})
it("renderText falls back independently per-field — serverName missing falls back to serverId, label missing falls back to id (mixed case, not all-or-nothing)", () => {
@@ -177,9 +199,19 @@ describe("buildCommunityChannelRefExtension — renderText/renderHTML", () => {
// serverName present, label missing.
expect(
renderText({
- node: { attrs: { id: "chn_abc", serverId: "srv_xyz", serverName: "Studio", label: null } },
+ node: { attrs: { id: "chn_abc", serverId: "srv_xyz", serverName: "Studio", serverDiscriminator: "0042", label: null } },
}),
- ).toBe("/Studio/chn_abc")
+ ).toBe("/Studio#0042/chn_abc")
+ })
+
+ it("never emits a bare server display name when the discriminator is missing", () => {
+ const { ext } = build()
+ const { renderText } = getRenderFns(ext)
+ expect(
+ renderText({
+ node: { attrs: { id: "chn_abc", serverId: "srv_xyz", serverName: "Studio", serverDiscriminator: null, label: "general" } },
+ }),
+ ).toBe("/srv_xyz/general")
})
it("renderHTML shows a compact /label chip", () => {
@@ -273,7 +305,7 @@ describe("buildCommunityChannelRefExtension — keyboard callback", () => {
expect(onKeyDown({ event: enter })).toBe(false)
})
- it("selecting a candidate via Enter calls command with { id, label, serverId, serverName } (via toChannelRefCommandProps) — not the raw ChannelRefCandidate", () => {
+ it("selecting a candidate via Enter preserves the discriminator via toChannelRefCommandProps", () => {
const command = vi.fn()
const items = [candidate("chn_1", "general", "srv_1", "Studio")]
const { ext, setPopup } = build(items, { items, selectedIndex: 0, command, rect: null })
@@ -283,7 +315,7 @@ describe("buildCommunityChannelRefExtension — keyboard callback", () => {
// Regression guard for the name→label mapping bug: passing the raw
// candidate through as-is would leave `attrs.label` `null` and silently
// render "/null" in the in-editor chip.
- expect(command).toHaveBeenCalledWith({ id: "chn_1", label: "general", serverId: "srv_1", serverName: "Studio" })
+ expect(command).toHaveBeenCalledWith({ id: "chn_1", label: "general", serverId: "srv_1", serverName: "Studio", serverDiscriminator: "0042" })
expect(command).not.toHaveBeenCalledWith(items[0])
expect(setPopup).toHaveBeenCalledWith(EMPTY_CHANNEL_REF_STATE)
})
@@ -295,7 +327,7 @@ describe("buildCommunityChannelRefExtension — keyboard callback", () => {
const onKeyDown = getKeyDownCallback(ext)
const tab = { key: "Tab", preventDefault: vi.fn(), isComposing: false } as unknown as KeyboardEvent
expect(onKeyDown({ event: tab })).toBe(true)
- expect(command).toHaveBeenCalledWith({ id: "chn_1", label: "general", serverId: "srv_1", serverName: "Studio" })
+ expect(command).toHaveBeenCalledWith({ id: "chn_1", label: "general", serverId: "srv_1", serverName: "Studio", serverDiscriminator: "0042" })
})
it("returns false when there are no items (popup effectively closed)", () => {
diff --git a/src/web/src/lib/community/channel-ref-extension.ts b/src/web/src/lib/community/channel-ref-extension.ts
index cc172c7ed..f77a7f392 100644
--- a/src/web/src/lib/community/channel-ref-extension.ts
+++ b/src/web/src/lib/community/channel-ref-extension.ts
@@ -1,11 +1,13 @@
import Mention from "@tiptap/extension-mention"
import { PluginKey } from "@tiptap/pm/state"
+import { formatHandle } from "@alook/shared"
export type ChannelRefCandidate = {
id: string // channelId (nanoid)
name: string // channel display name, for the popup row + in-editor chip
serverId: string // nanoid
serverName: string // for the popup row when listing across servers
+ serverDiscriminator: string // required public server identity suffix
}
// The shape actually passed to tiptap's `command(props)` — NOT
@@ -13,7 +15,26 @@ export type ChannelRefCandidate = {
// comment on the `name` → `label` mapping). Mirrors
// `MentionPopupState.command`'s own mapped-shape typing in
// `mention-extension.ts`.
-export type ChannelRefCommandProps = { id: string; label: string; serverId: string; serverName: string }
+export type ChannelRefCommandProps = {
+ id: string
+ label: string
+ serverId: string
+ serverName: string
+ serverDiscriminator: string
+}
+
+export function toChannelRefCandidate(
+ server: { id: string; name: string; discriminator: string },
+ channel: { id: string; name: string },
+): ChannelRefCandidate {
+ return {
+ id: channel.id,
+ name: channel.name,
+ serverId: server.id,
+ serverName: server.name,
+ serverDiscriminator: server.discriminator,
+ }
+}
/**
* Maps a `ChannelRefCandidate` (the shape the popup lists) to the props
@@ -25,7 +46,13 @@ export type ChannelRefCommandProps = { id: string; label: string; serverId: stri
* already exists for.
*/
export function toChannelRefCommandProps(item: ChannelRefCandidate): ChannelRefCommandProps {
- return { id: item.id, label: item.name, serverId: item.serverId, serverName: item.serverName }
+ return {
+ id: item.id,
+ label: item.name,
+ serverId: item.serverId,
+ serverName: item.serverName,
+ serverDiscriminator: item.serverDiscriminator,
+ }
}
export interface ChannelRefPopupState {
@@ -88,6 +115,7 @@ const ChannelRefNode = Mention.extend({
...this.parent?.(),
serverId: { default: null },
serverName: { default: null },
+ serverDiscriminator: { default: null },
}
},
})
@@ -108,7 +136,8 @@ const ChannelRefNode = Mention.extend({
* `renderText` inserts by display name, not id — server/channel names are
* now guaranteed ref-safe at creation/rename time (`slugify()`, applied by
* every write route), so `serverName`/`label` round-trip through
- * `chat-syntax-plugin.ts`'s `CHANNEL_REF_RE` and `resolveChannelRefBase`
+ * the required discriminator round-trip through `chat-syntax-plugin.ts`'s
+ * `CHANNEL_REF_RE` and `resolveChannelRefBase`
* (exact-string match) and render as something a human can actually read.
* Falls back to `serverId`/`id` if either name is ever missing
* (paste-from-HTML, drag-drop, etc.) — defensive, not the primary
@@ -138,18 +167,20 @@ export function buildCommunityChannelRefExtension(opts: {
// to the visible label so the recipient reads a real word instead of
// a broken pill — degraded, but not misleading. The command flow that
// DOES set both fields (Enter/Tab on a suggestion) is unaffected.
- // The server segment (`serverName ?? serverId`) and channel segment
- // (`label ?? id`) fall back independently — one can be missing while
- // the other isn't — and neither ever falls through to a literal
- // "null"/"undefined" string in the emitted text.
+ // A public server name is usable only with its discriminator. Malformed
+ // pasted nodes fall back to the internal server id rather than emitting an
+ // ambiguous bare display name; neither segment emits "null"/"undefined".
renderText: ({ node }) => {
- const { serverId, serverName, id, label } = node.attrs as {
+ const { serverId, serverName, serverDiscriminator, id, label } = node.attrs as {
serverId?: string | null
serverName?: string | null
+ serverDiscriminator?: string | null
id?: string | null
label?: string | null
}
- const server = serverName || serverId
+ const server = serverName && serverDiscriminator
+ ? formatHandle(serverName, serverDiscriminator)
+ : serverId
const channel = label || id
if (!server) return channel ? `/${channel}` : ""
return channel ? `/${server}/${channel}` : `/${server}`
diff --git a/src/web/src/lib/community/testids.test.ts b/src/web/src/lib/community/testids.test.ts
index 792f34315..8749fce7f 100644
--- a/src/web/src/lib/community/testids.test.ts
+++ b/src/web/src/lib/community/testids.test.ts
@@ -5,6 +5,7 @@ describe("community QA selectors", () => {
it("keys forum-title read models by their stable child identity", () => {
expect(tid.inboxUnreadChild("post_1")).toBe("community-inbox-unread-child-post_1")
expect(tid.channelRefPill("post_1")).toBe("community-channel-ref-pill-post_1")
+ expect(tid.channelRefOption("channel_1")).toBe("community-channel-ref-option-channel_1")
})
it("keys invite-card surfaces by their token", () => {
diff --git a/src/web/src/lib/community/testids.ts b/src/web/src/lib/community/testids.ts
index da793bfe6..817f1c940 100644
--- a/src/web/src/lib/community/testids.ts
+++ b/src/web/src/lib/community/testids.ts
@@ -76,6 +76,7 @@ export const tid = {
dmRow: (id: string) => `community-dm-row-${id}`,
memberRow: (id: string) => `community-member-row-${id}`,
mentionOption: (id: string) => `community-mention-option-${id}`,
+ channelRefOption: (id: string) => `community-channel-ref-option-${id}`,
reactionAdd: (msgId: string) => `community-reaction-add-${msgId}`,
messageShare: (msgId: string) => `community-message-share-${msgId}`,
messageShareCopy: `community-message-share-copy`,