63 lines
2.2 KiB
JavaScript
63 lines
2.2 KiB
JavaScript
// src/web-presence.js - isolated webui presence overlay.
|
|
//
|
|
// Workaround: some ICQ UINs do not get BuddyArrived broadcast by the OSCAR server
|
|
// when logged into webui, so native/AIM clients and webui buddy lists can show them
|
|
// offline. Without patching the server, we can at least make webui clients see them
|
|
// online by emitting synthetic presence events over our SSE bus.
|
|
//
|
|
// Scope: ICQ/all-digit screen names only, to avoid interfering with AIM presence.
|
|
|
|
import { broadcast } from './poller.js';
|
|
import { allSessions, getSessionByScreenName } from './sessions.js';
|
|
|
|
const webuiOnline = new Set(); // normalized screen names
|
|
|
|
function normalize(sn) {
|
|
return String(sn || '').toLowerCase().replace(/\s+/g, '');
|
|
}
|
|
|
|
export function isIcqScreenName(screenName) {
|
|
return /^\d+$/.test(String(screenName || '').trim());
|
|
}
|
|
|
|
function makePresenceEvent(screenName, online) {
|
|
return {
|
|
type: 'presence',
|
|
time: Date.now(),
|
|
data: { screenName, online: !!online, idleMins: 0, source: 'webui-overlay' },
|
|
raw: { type: 'presence', eventData: { screenName, online: !!online, idleMins: 0, source: 'webui-overlay' } },
|
|
};
|
|
}
|
|
|
|
export function broadcastWebuiPresence(screenName, online) {
|
|
if (!isIcqScreenName(screenName)) return;
|
|
const event = makePresenceEvent(screenName, online);
|
|
for (const rec of allSessions()) {
|
|
try { broadcast(rec, event); } catch { /* ignore */ }
|
|
}
|
|
}
|
|
|
|
export function markWebuiOnline(screenName) {
|
|
if (!isIcqScreenName(screenName)) return;
|
|
webuiOnline.add(normalize(screenName));
|
|
console.log(`[web-presence] online ${screenName}, sessions=${[...allSessions()].length}`);
|
|
broadcastWebuiPresence(screenName, true);
|
|
}
|
|
|
|
export function markWebuiOfflineIfLast(screenName) {
|
|
if (!isIcqScreenName(screenName)) return;
|
|
const norm = normalize(screenName);
|
|
// If another webui session exists for same screen name, keep online.
|
|
if (getSessionByScreenName(screenName)) return;
|
|
webuiOnline.delete(norm);
|
|
broadcastWebuiPresence(screenName, false);
|
|
}
|
|
|
|
export function replayWebuiPresence(rec) {
|
|
for (const norm of webuiOnline) {
|
|
try {
|
|
console.log(`[web-presence] replay ${norm} to ${rec.screenName}`);
|
|
broadcast(rec, makePresenceEvent(norm, true));
|
|
} catch { /* ignore */ }
|
|
}
|
|
}
|