62 lines
3.1 KiB
JavaScript
62 lines
3.1 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Local contacts and the related server-tree options.
|
|
*
|
|
* The official TS6 contact list is a myTeamSpeak/Matrix service (direct rooms keyed by myTeamSpeak id, cloud
|
|
* presence, invites, categories, favourites). TeamsPeach has no account, so it keeps an account-free
|
|
* replacement on this computer only:
|
|
* - a contact is a TeamSpeak identity UID (the stable key the server reports for every client) with an optional
|
|
* local display alias and the nickname seen when it was added; nothing is sent to anyone when a contact is
|
|
* added, renamed or removed (no invite, no request, no acceptance by the other side);
|
|
* - presence is derived only from the snapshots of servers this client is connected to: "offline" means "not on
|
|
* any connected server", never a cloud status.
|
|
* Blocking reuses the existing chatIgnoredUsers list (chat-prefs.js) rather than a second list.
|
|
*
|
|
* Official appearanceSettings key (client_ui/main.js class qe) → TeamsPeach key default
|
|
* sort_contacts_alphabetically → contactsSortAlphabetically true
|
|
* show_offline_contacts → contactsShowOffline true
|
|
* group_offline_contacts → contactsGroupOffline true
|
|
* contact_grouping.state.online_is_expanded / offline_is_expanded (class T defaults)
|
|
* → contactsOnlineExpanded true / contactsOfflineExpanded false
|
|
* show_server_badges → showBadges true
|
|
* should_highlight_contacts → highlightContacts true
|
|
*/
|
|
|
|
const { validUid } = require('./chat-prefs');
|
|
|
|
const MAX_CONTACTS = 1000;
|
|
const MAX_TEXT = 128;
|
|
const CONTROL = /[\u0000-\u001f\u007f]/;
|
|
|
|
const isBool = (v) => typeof v === 'boolean';
|
|
const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
|
|
const validText = (v) => typeof v === 'string' && v.length <= MAX_TEXT && !CONTROL.test(v);
|
|
|
|
/** [{ uid, alias, name }]: unique UIDs; alias '' = no local rename; name = nickname seen when added (display fallback). */
|
|
function validContacts(list) {
|
|
if (!Array.isArray(list) || list.length > MAX_CONTACTS) return false;
|
|
const seen = new Set();
|
|
for (const entry of list) {
|
|
if (!isPlainObject(entry)) return false;
|
|
const keys = Object.keys(entry);
|
|
if (keys.length !== 3 || !keys.includes('uid') || !keys.includes('alias') || !keys.includes('name')) return false;
|
|
if (!validUid(entry.uid) || seen.has(entry.uid) || !validText(entry.alias) || !validText(entry.name)) return false;
|
|
seen.add(entry.uid);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
const CONTACT_SCHEMA = {
|
|
contacts: { default: [], check: validContacts },
|
|
contactsSortAlphabetically: { default: true, check: isBool },
|
|
contactsShowOffline: { default: true, check: isBool },
|
|
contactsGroupOffline: { default: true, check: isBool },
|
|
contactsOnlineExpanded: { default: true, check: isBool },
|
|
contactsOfflineExpanded: { default: false, check: isBool },
|
|
showBadges: { default: true, check: isBool },
|
|
highlightContacts: { default: true, check: isBool },
|
|
};
|
|
|
|
module.exports = { CONTACT_SCHEMA, MAX_CONTACTS, validContacts };
|