From 883703dd0caa67418abf60c7ff12d6d16a72a723 Mon Sep 17 00:00:00 2001 From: illusia <64828020+EchoSixHIYA@users.noreply.github.com> Date: Fri, 18 Sep 2026 17:14:35 +0800 Subject: [PATCH] fix: preserve TeamSpeak channel order --- package-lock.json | 4 +- package.json | 2 +- src/server/voice-bridge.ts | 1 + web/src/composables/useVoiceWebSocket.ts | 1 + web/src/views/WebClient.vue | 67 +++++++++++++++++++++--- 5 files changed, 64 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index ecfbaec..d49205c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "AGPL-3.0-only", "dependencies": { "@discordjs/opus": "^0.10.0", - "@echosixhiya/teamspeak-client": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#a145338", + "@echosixhiya/teamspeak-client": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#128f9dc779a54258393c85ebfbe65bc9773e7495", "express": "^5.2.1", "pino": "^10.3.1", "werift": "0.24.4", @@ -63,7 +63,7 @@ }, "node_modules/@echosixhiya/teamspeak-client": { "version": "0.2.4", - "resolved": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#a145338c7a83044dee0951c412ad91292b570215", + "resolved": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#128f9dc779a54258393c85ebfbe65bc9773e7495", "license": "MIT", "dependencies": { "@noble/curves": "^2.0.1" diff --git a/package.json b/package.json index 8a26b03..f4a1f83 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ }, "dependencies": { "@discordjs/opus": "^0.10.0", - "@echosixhiya/teamspeak-client": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#a145338", + "@echosixhiya/teamspeak-client": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#128f9dc779a54258393c85ebfbe65bc9773e7495", "express": "^5.2.1", "pino": "^10.3.1", "werift": "0.24.4", diff --git a/src/server/voice-bridge.ts b/src/server/voice-bridge.ts index 851b0ca..9a145e3 100644 --- a/src/server/voice-bridge.ts +++ b/src/server/voice-bridge.ts @@ -1223,6 +1223,7 @@ function mapChannelTree(snapshot: TSDirectorySnapshot): unknown[] { return snapshot.channels.map((channel) => ({ id: String(channel.id), parentID: String(channel.parentID), + order: String(channel.order), name: channel.name || "未命名频道", description: channel.description || "", members: snapshot.clients diff --git a/web/src/composables/useVoiceWebSocket.ts b/web/src/composables/useVoiceWebSocket.ts index 76b3284..b8a7286 100644 --- a/web/src/composables/useVoiceWebSocket.ts +++ b/web/src/composables/useVoiceWebSocket.ts @@ -72,6 +72,7 @@ type SinkAudioElement = HTMLAudioElement & { export interface ChannelInfo { id: string; parentID: string; + order?: string; name: string; description?: string; members?: { id: number; nickname: string; uid?: string; away?: boolean; awayMessage?: string; inputMuted?: boolean; outputMuted?: boolean; channelCommander?: boolean }[]; diff --git a/web/src/views/WebClient.vue b/web/src/views/WebClient.vue index d81b8e5..79432c2 100644 --- a/web/src/views/WebClient.vue +++ b/web/src/views/WebClient.vue @@ -1742,7 +1742,12 @@ const heroBars = [12, 24, 18, 35, 18, 28, 42, 23, 50, 34, 19, 28, 39, 22, 46, 25 const channelTree = computed(() => { const source = [...channels]; - const byId = new Map(source.map((item) => [item.id, item])); + const sourceIndex = new Map(source.map((item, index) => [item.id, index])); + const enriched = source.map((item) => ({ + ...item, + members: (item.members ?? []).map((member) => ({ ...member, isSelf: member.id === voiceState.tsClientId })), + })); + const byId = new Map(enriched.map((item) => [item.id, item])); const depthCache = new Map(); function depthOf(item: ChannelInfo, visiting = new Set()): number { @@ -1754,13 +1759,59 @@ const channelTree = computed(() => { return depth; } - return source - .map((item) => ({ - ...item, - depth: depthOf(item), - members: (item.members ?? []).map((member) => ({ ...member, isSelf: member.id === voiceState.tsClientId })), - })) - .sort((a, b) => `${a.parentID}/${a.id}`.localeCompare(`${b.parentID}/${b.id}`)); + const childrenByParent = new Map(); + for (const item of enriched) { + const channel = { ...item, depth: depthOf(item) }; + const siblings = childrenByParent.get(channel.parentID) ?? []; + siblings.push(channel); + childrenByParent.set(channel.parentID, siblings); + } + + function orderSiblings(siblings: TreeChannel[]): TreeChannel[] { + const bySiblingId = new Map(siblings.map((item) => [item.id, item])); + const successors = new Map(); + const roots: TreeChannel[] = []; + const sourceOrder = (left: TreeChannel, right: TreeChannel) => + (sourceIndex.get(left.id) ?? Number.MAX_SAFE_INTEGER) - (sourceIndex.get(right.id) ?? Number.MAX_SAFE_INTEGER); + + for (const item of siblings) { + const predecessor = item.order && item.order !== "0" && bySiblingId.has(item.order) ? item.order : ""; + if (!predecessor) roots.push(item); + else successors.set(predecessor, [...(successors.get(predecessor) ?? []), item]); + } + + roots.sort(sourceOrder); + for (const items of successors.values()) items.sort(sourceOrder); + + const ordered: TreeChannel[] = []; + const visited = new Set(); + const append = (item: TreeChannel) => { + if (visited.has(item.id)) return; + visited.add(item.id); + ordered.push(item); + for (const successor of successors.get(item.id) ?? []) append(successor); + }; + for (const root of roots) append(root); + for (const item of [...siblings].sort(sourceOrder)) append(item); + return ordered; + } + + const orderedTree: TreeChannel[] = []; + const visit = (parentID: string) => { + for (const channel of orderSiblings(childrenByParent.get(parentID) ?? [])) { + orderedTree.push(channel); + visit(channel.id); + } + }; + visit("0"); + for (const channel of enriched) { + if (!orderedTree.some((item) => item.id === channel.id)) { + const fallback = { ...channel, depth: depthOf(channel) }; + orderedTree.push(fallback); + visit(channel.id); + } + } + return orderedTree; }); const currentChannel = computed(() => {