'use strict'; const fs = require('node:fs'); const path = require('node:path'); /** * Persisted local per-user volume modifiers (official TS6 `setClientVolumeModifier(sc, clid, dB, persist)` * with persist=true when the fader is released or Reset is chosen). TS6 stores them natively; the key it * uses is not visible. TeamsPeach keys them by the client's unique identity (base64 UID), so a person keeps * their volume on every server and after reconnecting; numeric client ids are never used as keys. * Mute is not persisted (session only), and 0 dB entries are removed. * * File `/client-volumes.json`: { schema, volumes: { : { db, updated } } }, at most * MAX_ENTRIES (the least recently updated entry is dropped first). Invalid entries are dropped on load; * writes are atomic (tmp + rename) and leave memory unchanged when they fail. * * Every core gets the whole map (`set_local_volumes replace:true`) whenever it becomes ready, and each * change as an upsert, so all simultaneous connections play a person at the same modifier. */ const SCHEMA = 'teamspeach-client-volumes/1'; const MAX_ENTRIES = 4096; const UID = /^[A-Za-z0-9+/]{1,126}={0,2}$/; function validUid(uid) { return typeof uid === 'string' && uid.length <= 128 && UID.test(uid); } function validDb(db) { return typeof db === 'number' && Number.isFinite(db) && db >= -200 && db <= 10 && Number.isInteger(db * 2); } class ClientVolumes { constructor(file) { this.file = file; /** @type {Map} */ this.volumes = new Map(); let raw = null; try { raw = JSON.parse(fs.readFileSync(file, 'utf8')); } catch { return; // first run or unreadable: empty } if (!raw || raw.schema !== SCHEMA || !raw.volumes || typeof raw.volumes !== 'object') return; const entries = Object.entries(raw.volumes) .filter(([uid, e]) => validUid(uid) && e && validDb(e.db) && e.db !== 0 && Number.isSafeInteger(e.updated) && e.updated >= 0) .sort((a, b) => b[1].updated - a[1].updated) .slice(0, MAX_ENTRIES); for (const [uid, e] of entries) this.volumes.set(uid, { db: e.db, updated: e.updated }); } get(uid) { return this.volumes.get(uid)?.db ?? 0; } /** [{uid, db}] for `set_local_volumes`. */ list() { return Array.from(this.volumes, ([uid, e]) => ({ uid, db: e.db })); } /** Stores (or with 0 dB removes) one modifier. Throws on invalid input or a failed write. */ set(uid, db, now = Date.now()) { if (!validUid(uid)) throw new Error('client volume: invalid uid'); if (!validDb(db)) throw new Error('client volume: db must be -200..10 in 0.5 steps'); const next = new Map(this.volumes); next.delete(uid); if (db !== 0) next.set(uid, { db, updated: now }); while (next.size > MAX_ENTRIES) { let oldest = null; for (const [k, e] of next) if (oldest === null || e.updated < next.get(oldest).updated) oldest = k; next.delete(oldest); } const body = { schema: SCHEMA, volumes: Object.fromEntries(next) }; fs.mkdirSync(path.dirname(this.file), { recursive: true }); const tmp = `${this.file}.tmp`; fs.writeFileSync(tmp, JSON.stringify(body)); fs.renameSync(tmp, this.file); this.volumes = next; } } /** * @param {object} o * @param {Electron.IpcMain} o.ipcMain * @param {(event) => boolean} o.trustedSender * @param {import('./connections').ConnectionManager} o.connections * @param {string} o.file * @param {(level: string, msg: string) => void} [o.log] */ function registerClientVolumesIpc({ ipcMain, trustedSender, connections, file, log = () => {} }) { const store = new ClientVolumes(file); const push = (id, args) => connections.request(id, 'set_local_volumes', args).then((r) => { if (r && r.ok === false && r.error?.code !== 'connection_closed') log('warn', `client volumes: ${id} ${r.error?.code} ${r.error?.message}`); }, (err) => log('warn', `client volumes: ${id} ${err?.message ?? err}`)); connections.on('status', (id, status) => { if (status?.status === 'ready') void push(id, { replace: true, volumes: store.list() }); }); ipcMain.handle('client-volumes:set', (event, uid, db) => { if (!trustedSender(event)) return { ok: false, error: { code: 'forbidden', message: 'forbidden' } }; try { store.set(uid, db); } catch (err) { return { ok: false, error: { code: 'bad_request', message: String(err?.message ?? err) } }; } for (const c of connections.list()) { if (c.core?.status === 'ready') void push(c.id, { replace: false, volumes: [{ uid, db }] }); } return { ok: true, db }; }); return store; } module.exports = { ClientVolumes, registerClientVolumesIpc, MAX_ENTRIES, SCHEMA };