Files
client/app/main/remote-app-snapshot.js
T

183 lines
9.7 KiB
JavaScript

'use strict';
// Maps TeamsPeach core snapshots (docs/bridge.md §5 `snapshot`, `talking`, `audio_state`) to the Remote Apps
// message shapes. TeamSpeak's own documentation (installed docs/Remote Apps.md) only states that the auth reply
// carries "connected servers, clients and channels" and that events are `{type, payload}`; the field names used
// here are the ones the public consumer DerTyp7/react-teamspeak-remote-app-api (commit 5ea9cb9,
// src/interfaces/teamspeak.ts + src/handlers/teamspeak/messageHandler.ts) reads from the official client:
// auth.payload.connections[] {id, clientId, channelInfos:{rootChannels, subChannels}, clientInfos[]}
// clientMoved {connectionId, clientId, oldChannelId, newChannelId, properties}
// clientPropertiesUpdated {connectionId, clientId, properties}
// talkStatusChanged {connectionId, clientId, status}
// connectStatusChanged {connectionId, status (0 disconnected, 4 connected), info:{clientId}}
// channels {connectionId, info:{rootChannels, subChannels}}
// channelCreated {connectionId, channelId, parentId, properties}
// clientSelfPropertyUpdated {connectionId, flag:"inputHardware", oldValue, newValue}
// Only properties whose value the core actually reports are included; fields the consumer types but TeamsPeach
// cannot know (connection `properties`, clientMoved `type`/`visibility`, talkStatusChanged `isWhisper`,
// connectStatusChanged `error`, server-property events, channel edit/delete/move events) are omitted, not invented.
// TeamSpeak channel_codec values (TS3 protocol: 0 Speex NB, 1 Speex WB, 2 Speex UWB, 3 CELT mono, 4 Opus Voice,
// 5 Opus Music) for the core's codec names.
const CODEC = Object.freeze({ speex_narrowband: 0, speex_wideband: 1, speex_ultrawideband: 2, celt_mono: 3, opus_voice: 4, opus_music: 5 });
const CONNECT_STATUS = Object.freeze({ disconnected: 0, connected: 4 });
/** Core connection ids are `c<N>`; the remote protocol carries numeric connection ids. */
function connectionNumber(id) {
const match = typeof id === 'string' && /^c([1-9][0-9]{0,8})$/.exec(id);
return match ? Number(match[1]) : null;
}
const isNum = (v) => typeof v === 'number' && Number.isFinite(v);
const isBool = (v) => typeof v === 'boolean';
function channelProperties(ch) {
const p = { name: String(ch.name ?? '') };
if (typeof ch.topic === 'string') p.topic = ch.topic;
if (isBool(ch.has_password)) p.flagPassword = ch.has_password;
if (isBool(ch.is_default)) p.flagDefault = ch.is_default;
// Core: max_clients null = unlimited.
p.flagMaxclientsUnlimited = ch.max_clients === null || ch.max_clients === undefined;
if (isNum(ch.max_clients)) p.maxclients = ch.max_clients;
if (isNum(ch.needed_talk_power)) p.neededTalkPower = ch.needed_talk_power;
if (Object.hasOwn(CODEC, ch.codec)) p.codec = CODEC[ch.codec];
if (isNum(ch.codec_quality)) p.codecQuality = ch.codec_quality;
// Core null = no icon; TeamSpeak uses 0 for "no icon".
p.iconId = isNum(ch.icon_id) ? ch.icon_id : 0;
if (typeof ch.banner_url === 'string') p.bannerGfxUrl = ch.banner_url;
if (isNum(ch.banner_mode)) p.bannerMode = ch.banner_mode;
if (isNum(ch.order)) p.order = String(ch.order);
return p;
}
function channelEntry(ch) {
return { id: ch.id, order: String(ch.order ?? 0), parentId: String(ch.parent_id ?? 0), properties: channelProperties(ch) };
}
/** consumer IChannelInfos: root channels plus children grouped by parent id. */
function channelInfos(channels) {
const rootChannels = [];
const subChannels = {};
for (const ch of channels) {
const entry = channelEntry(ch);
if (!ch.parent_id) rootChannels.push(entry);
else (subChannels[ch.parent_id] ??= []).push(entry);
}
return { rootChannels, subChannels };
}
function clientProperties(c) {
const p = { nickname: String(c.nickname ?? '') };
if (typeof c.uid === 'string') p.uniqueIdentifier = c.uid;
if (c.client_type === 'normal' || c.client_type === 'query') p.type = c.client_type === 'query' ? 1 : 0;
for (const [from, to] of [['input_muted', 'inputMuted'], ['output_muted', 'outputMuted'], ['input_hardware', 'inputHardware'],
['output_hardware', 'outputHardware'], ['away', 'away'], ['is_talker', 'isTalker'], ['is_channel_commander', 'isChannelCommander'],
['is_priority_speaker', 'isPrioritySpeaker'], ['is_recording', 'isRecording']]) {
if (isBool(c[from])) p[to] = c[from];
}
// The core turns TeamSpeak's empty strings into null; the wire value of "unset" is the empty string.
p.awayMessage = typeof c.away_message === 'string' ? c.away_message : '';
p.description = typeof c.description === 'string' ? c.description : '';
p.country = typeof c.country === 'string' ? c.country : '';
p.badges = typeof c.badges === 'string' ? c.badges : '';
if (isNum(c.talk_power)) p.talkPower = c.talk_power;
if (c.talk_request !== undefined) {
p.talkRequest = c.talk_request ? 1 : 0;
p.talkRequestMsg = typeof c.talk_request?.message === 'string' ? c.talk_request.message : '';
}
if (Array.isArray(c.server_groups)) p.serverGroups = c.server_groups.join(',');
if (isNum(c.channel_group_id)) p.channelGroupId = String(c.channel_group_id);
if (isNum(c.channel_group_inherited_from)) p.channelGroupInheritedChannelId = String(c.channel_group_inherited_from);
p.iconId = isNum(c.icon_id) ? c.icon_id : 0;
return p;
}
/** Validated, indexed view of one core snapshot, or null when it does not describe a live server view. */
function indexSnapshot(snapshot) {
if (!snapshot || typeof snapshot !== 'object' || !isNum(snapshot.own_client_id)) return null;
const channels = Array.isArray(snapshot.channels) ? snapshot.channels.filter((ch) => ch && isNum(ch.id)) : [];
const clients = Array.isArray(snapshot.clients) ? snapshot.clients.filter((c) => c && isNum(c.id) && isNum(c.channel_id)) : [];
return {
own: snapshot.own_client_id,
channelList: channels,
channels: new Map(channels.map((ch) => [ch.id, ch])),
clients: new Map(clients.map((c) => [c.id, { channelId: c.channel_id, raw: c, properties: clientProperties(c) }])),
};
}
/** consumer IConnection for the auth reply. */
function connectionEntry(connectionId, view) {
return {
id: connectionId,
clientId: view.own,
channelInfos: channelInfos(view.channelList),
clientInfos: Array.from(view.clients, ([id, c]) => ({ id, channelId: c.channelId, properties: c.properties })),
};
}
/** All live connections in creation order; `views` maps core id → indexed snapshot. */
function authConnections(views) {
const out = [];
for (const [id, view] of views) {
const number = connectionNumber(id);
if (number !== null && view) out.push(connectionEntry(number, view));
}
return out.sort((a, b) => a.id - b.id);
}
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
/** Events turning `prev` into `next` for one connection (either may be null). */
function diffEvents(connectionId, prev, next) {
const events = [];
if (!prev && !next) return events;
if (prev && !next) {
events.push({ type: 'connectStatusChanged', payload: { connectionId, status: CONNECT_STATUS.disconnected, info: { clientId: prev.own } } });
return events;
}
if (!prev || prev.own !== next.own) {
if (prev) events.push({ type: 'connectStatusChanged', payload: { connectionId, status: CONNECT_STATUS.disconnected, info: { clientId: prev.own } } });
// Consumer sequence on connect: connection, then its channels, then every visible client as a join.
events.push({ type: 'connectStatusChanged', payload: { connectionId, status: CONNECT_STATUS.connected, info: { clientId: next.own } } });
events.push({ type: 'channels', payload: { connectionId, info: channelInfos(next.channelList) } });
for (const [clientId, c] of next.clients) {
events.push({ type: 'clientMoved', payload: { connectionId, clientId, oldChannelId: 0, newChannelId: c.channelId, properties: c.properties } });
}
return events;
}
for (const ch of next.channelList) {
if (!prev.channels.has(ch.id)) {
events.push({ type: 'channelCreated', payload: { connectionId, channelId: ch.id, parentId: String(ch.parent_id ?? 0), properties: channelProperties(ch) } });
}
}
for (const [clientId, c] of next.clients) {
const before = prev.clients.get(clientId);
if (!before) {
events.push({ type: 'clientMoved', payload: { connectionId, clientId, oldChannelId: 0, newChannelId: c.channelId, properties: c.properties } });
continue;
}
if (before.channelId !== c.channelId) {
events.push({ type: 'clientMoved', payload: { connectionId, clientId, oldChannelId: before.channelId, newChannelId: c.channelId, properties: c.properties } });
}
if (!same(before.properties, c.properties)) {
events.push({ type: 'clientPropertiesUpdated', payload: { connectionId, clientId, properties: c.properties } });
}
if (clientId === next.own && isBool(before.raw.input_hardware) && isBool(c.raw.input_hardware) && before.raw.input_hardware !== c.raw.input_hardware) {
events.push({ type: 'clientSelfPropertyUpdated', payload: { connectionId, flag: 'inputHardware', oldValue: before.raw.input_hardware, newValue: c.raw.input_hardware } });
}
}
for (const [clientId, before] of prev.clients) {
if (!next.clients.has(clientId)) {
events.push({ type: 'clientMoved', payload: { connectionId, clientId, oldChannelId: before.channelId, newChannelId: 0, properties: before.properties } });
}
}
return events;
}
/** TS3 SDK talk status: 0 not talking, 1 talking. */
function talkEvent(connectionId, clientId, talking) {
return { type: 'talkStatusChanged', payload: { connectionId, clientId, status: talking ? 1 : 0 } };
}
module.exports = { connectionNumber, indexSnapshot, authConnections, diffEvents, talkEvent, channelInfos, clientProperties, channelProperties, CODEC };