112 lines
3.6 KiB
JavaScript
112 lines
3.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.touchGamePresence = touchGamePresence;
|
|
exports.touchAppPresence = touchAppPresence;
|
|
exports.touchUserPresence = touchUserPresence;
|
|
exports.removeGamePresence = removeGamePresence;
|
|
exports.removeAppPresence = removeAppPresence;
|
|
exports.removeUserPresence = removeUserPresence;
|
|
exports.listOnlineUserIds = listOnlineUserIds;
|
|
const config_1 = require("../../config");
|
|
const client_1 = require("./client");
|
|
const PRESENCE_KEYS = {
|
|
game: 'prediqo:presence:game:users',
|
|
app: 'prediqo:presence:app:users',
|
|
};
|
|
/** Debounce app presence writes (HTTP is chatty). */
|
|
const lastAppTouchByUser = new Map();
|
|
const APP_TOUCH_DEBOUNCE_MS = Math.max(5000, parseInt(process.env.ONLINE_APP_PRESENCE_DEBOUNCE_MS ?? '15000', 10));
|
|
function presenceTtlMs() {
|
|
return Math.max(30000, parseInt(process.env.ONLINE_PRESENCE_TTL_MS ?? '90000', 10));
|
|
}
|
|
async function presenceRedis() {
|
|
if (!config_1.config.redis.enabled)
|
|
return null;
|
|
try {
|
|
await (0, client_1.connectRedis)();
|
|
}
|
|
catch (e) {
|
|
console.warn('[presence] Redis connect failed:', e instanceof Error ? e.message : e);
|
|
return null;
|
|
}
|
|
return (0, client_1.getRedisClient)();
|
|
}
|
|
async function touchPresence(userId, scope) {
|
|
if (!userId)
|
|
return;
|
|
const redis = await presenceRedis();
|
|
if (!redis)
|
|
return;
|
|
const now = Date.now();
|
|
const cutoff = now - presenceTtlMs();
|
|
try {
|
|
await redis
|
|
.multi()
|
|
.zadd(PRESENCE_KEYS[scope], now, userId)
|
|
.zremrangebyscore(PRESENCE_KEYS[scope], 0, cutoff)
|
|
.exec();
|
|
}
|
|
catch (e) {
|
|
console.warn('[presence] touch failed:', e instanceof Error ? e.message : e);
|
|
}
|
|
}
|
|
/** Marks a user as on the game page (WebSocket Candle Predict). */
|
|
async function touchGamePresence(userId) {
|
|
return touchPresence(userId, 'game');
|
|
}
|
|
/** Marks a user as having an active app session (authenticated activity). */
|
|
async function touchAppPresence(userId) {
|
|
if (!userId)
|
|
return;
|
|
const now = Date.now();
|
|
const last = lastAppTouchByUser.get(userId) ?? 0;
|
|
if (now - last < APP_TOUCH_DEBOUNCE_MS)
|
|
return;
|
|
lastAppTouchByUser.set(userId, now);
|
|
if (lastAppTouchByUser.size > 10000) {
|
|
for (const [id, ts] of lastAppTouchByUser) {
|
|
if (now - ts > presenceTtlMs())
|
|
lastAppTouchByUser.delete(id);
|
|
}
|
|
}
|
|
await touchPresence(userId, 'app');
|
|
}
|
|
/** @deprecated use touchGamePresence */
|
|
async function touchUserPresence(userId) {
|
|
return touchGamePresence(userId);
|
|
}
|
|
async function removeGamePresence(userId) {
|
|
if (!userId)
|
|
return;
|
|
const redis = await presenceRedis();
|
|
if (!redis)
|
|
return;
|
|
await redis.zrem(PRESENCE_KEYS.game, userId);
|
|
}
|
|
async function removeAppPresence(userId) {
|
|
if (!userId)
|
|
return;
|
|
lastAppTouchByUser.delete(userId);
|
|
const redis = await presenceRedis();
|
|
if (!redis)
|
|
return;
|
|
await redis.zrem(PRESENCE_KEYS.app, userId);
|
|
}
|
|
/** @deprecated use removeGamePresence */
|
|
async function removeUserPresence(userId) {
|
|
return removeGamePresence(userId);
|
|
}
|
|
async function prunePresence(redis, scope) {
|
|
const cutoff = Date.now() - presenceTtlMs();
|
|
await redis.zremrangebyscore(PRESENCE_KEYS[scope], 0, cutoff);
|
|
}
|
|
async function listOnlineUserIds(scope) {
|
|
if (!config_1.config.redis.enabled)
|
|
return null;
|
|
const redis = await presenceRedis();
|
|
if (!redis)
|
|
return null;
|
|
await prunePresence(redis, scope);
|
|
return redis.zrange(PRESENCE_KEYS[scope], 0, -1);
|
|
}
|
|
//# sourceMappingURL=onlinePresence.js.map
|