@mulingai-npm/redis 3.41.0 → 3.43.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { RedisClient } from '../redis-client';
|
|
2
|
+
import { ListenerPresence } from '../data/listener-presence';
|
|
3
|
+
export { LISTENER_PRESENT_MS, LISTENER_GONE_MS, LISTENER_HEARTBEAT_INTERVAL_MS, listenerPresence } from '../data/listener-presence';
|
|
4
|
+
export type { ListenerPresence } from '../data/listener-presence';
|
|
2
5
|
export type ListenerBreakpoint = 'mobile' | 'desktop' | 'display';
|
|
3
6
|
export type MulingstreamListenerData = {
|
|
4
7
|
listenerId: string;
|
|
@@ -12,12 +15,54 @@ export type MulingstreamListenerData = {
|
|
|
12
15
|
color?: string;
|
|
13
16
|
isListening?: boolean;
|
|
14
17
|
breakpoint?: ListenerBreakpoint;
|
|
18
|
+
/**
|
|
19
|
+
* The listener told us their page went into the background: screen locked,
|
|
20
|
+
* app switched, tab hidden.
|
|
21
|
+
*
|
|
22
|
+
* Set by the client rather than inferred, because the socket cannot tell the
|
|
23
|
+
* difference between a locked phone and a closed tab, and waiting for missed
|
|
24
|
+
* heartbeats to decide costs ninety seconds of translating for somebody who
|
|
25
|
+
* is demonstrably not looking. Cleared by the next heartbeat, which is what a
|
|
26
|
+
* returning page sends immediately.
|
|
27
|
+
*
|
|
28
|
+
* Being away is not being gone. See data/listener-presence.ts.
|
|
29
|
+
*/
|
|
30
|
+
isAway?: boolean;
|
|
15
31
|
};
|
|
16
32
|
export declare class MulingstreamListenerManager {
|
|
17
33
|
private redisClient;
|
|
18
34
|
constructor(redisClient: RedisClient);
|
|
19
35
|
private parseHashData;
|
|
20
36
|
addListener(listenerData: MulingstreamListenerData): Promise<string>;
|
|
37
|
+
/**
|
|
38
|
+
* Every listener currently known, across every room.
|
|
39
|
+
*
|
|
40
|
+
* WHY THIS USES SCAN AND NOT KEYS
|
|
41
|
+
*
|
|
42
|
+
* It used to be `KEYS 'listener:*'`, with a TODO next to it saying it should
|
|
43
|
+
* not be. KEYS walks the entire keyspace in one go and Redis is single
|
|
44
|
+
* threaded, so for as long as it runs nothing else in the process is served:
|
|
45
|
+
* not a heartbeat, not a chunk being handed to the pipeline, not a listener
|
|
46
|
+
* joining. The cost is invisible on a development database with a few dozen
|
|
47
|
+
* keys and grows with the busiest moment we will ever have, which is precisely
|
|
48
|
+
* when it must not stall. This is called by the cleanup sweep every sixty
|
|
49
|
+
* seconds, for the entire life of the process, so it would have been the most
|
|
50
|
+
* frequently executed blocking command in the system.
|
|
51
|
+
*
|
|
52
|
+
* SCAN answers the same question in cursored batches, yielding between each,
|
|
53
|
+
* so a large keyspace costs more round trips rather than one long stall. The
|
|
54
|
+
* guarantees are weaker, deliberately: a key added or removed mid-scan may or
|
|
55
|
+
* may not appear. That is exactly right for the caller, which is looking for
|
|
56
|
+
* listeners who went silent minutes ago. One arriving during the scan is one
|
|
57
|
+
* for the next sweep, sixty seconds later, and there is nothing to do about a
|
|
58
|
+
* brand new listener anyway.
|
|
59
|
+
*
|
|
60
|
+
* The pattern also has to reject keys that merely start with `listener:`. The
|
|
61
|
+
* legacy `listener:panel-state:*` keys matched the old pattern and were parsed
|
|
62
|
+
* as listener records, producing entries with NaN timestamps. They never
|
|
63
|
+
* caused harm, since NaN fails every comparison, which is exactly how a
|
|
64
|
+
* defect like that survives.
|
|
65
|
+
*/
|
|
21
66
|
getAllListeners(): Promise<MulingstreamListenerData[]>;
|
|
22
67
|
getListenersByRoom(roomId: string): Promise<MulingstreamListenerData[]>;
|
|
23
68
|
getListener(listenerIdOrToken: string): Promise<MulingstreamListenerData | null>;
|
|
@@ -30,13 +75,126 @@ export declare class MulingstreamListenerManager {
|
|
|
30
75
|
*/
|
|
31
76
|
updateBreakpoint(listenerIdOrToken: string, breakpoint: ListenerBreakpoint): Promise<MulingstreamListenerData | null>;
|
|
32
77
|
getTargetSocketIdsByRoomLanguage(roomId: string, language: string): Promise<string[]>;
|
|
78
|
+
/**
|
|
79
|
+
* Which languages this room has someone PRESENT on.
|
|
80
|
+
*
|
|
81
|
+
* Feeds consumersFor, which decides what gets translated at all, so an
|
|
82
|
+
* over-generous answer here is money spent on every chunk for nobody. It
|
|
83
|
+
* therefore counts present listeners only: somebody whose phone has been dark
|
|
84
|
+
* for two minutes is receiving nothing, and translating for them is pure loss.
|
|
85
|
+
*
|
|
86
|
+
* It used to count every listener the room had ever registered, including ones
|
|
87
|
+
* whose heartbeat had been silent for fourteen minutes, because the only thing
|
|
88
|
+
* that removed a listener was the cleanup sweep. See data/listener-presence.ts
|
|
89
|
+
* for why that single threshold could not be lowered and had to be split.
|
|
90
|
+
*/
|
|
33
91
|
getUniqueLanguagesByRoom(roomId: string): Promise<string[]>;
|
|
92
|
+
/**
|
|
93
|
+
* How many PRESENT listeners in this room are on a language.
|
|
94
|
+
*
|
|
95
|
+
* Decides whether a guest-added language slot can be handed back, and counts
|
|
96
|
+
* the same population `consumersFor` reads, so the two cannot disagree about
|
|
97
|
+
* whether a language has an audience: a slot is released exactly when the
|
|
98
|
+
* translation for it stops, never one without the other.
|
|
99
|
+
*
|
|
100
|
+
* Present rather than merely registered, and that is the deliberate part. A
|
|
101
|
+
* listener whose phone has been dark for two minutes is not hearing the
|
|
102
|
+
* language, so holding the room's last slot for them denies it to a guest
|
|
103
|
+
* standing in the room who is. If they come back and the slot has gone to
|
|
104
|
+
* somebody else, they choose again from what the room now has; if it is still
|
|
105
|
+
* free they simply take it back.
|
|
106
|
+
*/
|
|
107
|
+
countListenersOnLanguage(roomId: string, language: string): Promise<number>;
|
|
108
|
+
/**
|
|
109
|
+
* The client saying its page went into the background, or came back.
|
|
110
|
+
*
|
|
111
|
+
* This is the difference between reacting to a locked screen at once and
|
|
112
|
+
* reacting to it after three missed heartbeats. Both end in the same state;
|
|
113
|
+
* one of them costs ninety seconds of translation and synthesis for a phone
|
|
114
|
+
* nobody is looking at, on every language in the room.
|
|
115
|
+
*
|
|
116
|
+
* Does NOT touch lastHeartbeat. That field is now also the recorded end of the
|
|
117
|
+
* listener's session, so it has to keep meaning "when they were genuinely last
|
|
118
|
+
* there" and nothing else.
|
|
119
|
+
*/
|
|
120
|
+
setAwayState(listenerIdOrToken: string, isAway: boolean): Promise<MulingstreamListenerData | null>;
|
|
121
|
+
/**
|
|
122
|
+
* Present, away or gone, for one listener. For anything that needs to SHOW the
|
|
123
|
+
* distinction rather than act on it.
|
|
124
|
+
*/
|
|
125
|
+
getPresence(listenerIdOrToken: string): Promise<ListenerPresence | null>;
|
|
34
126
|
updateHeartbeat(listenerIdOrToken: string): Promise<MulingstreamListenerData | null>;
|
|
35
127
|
getListenerStats(roomId: string): Promise<{
|
|
36
128
|
totalListeners: number;
|
|
37
129
|
languageBreakdown: Record<string, number>;
|
|
38
130
|
}>;
|
|
131
|
+
/**
|
|
132
|
+
* How many people in this room are ACTUALLY RECEIVING translation right now.
|
|
133
|
+
*
|
|
134
|
+
* Church passes turn on this number rather than on how many people joined. A
|
|
135
|
+
* pass is spent only when translation reached a human being, so somebody
|
|
136
|
+
* sitting on the join screen must not burn a church's pass.
|
|
137
|
+
*
|
|
138
|
+
* ─── RECEIVING MEANS AUDIO **OR** TEXT ──────────────────────────────────
|
|
139
|
+
*
|
|
140
|
+
* Corrected 2026-08-19. This used to require `isListening`, the audio
|
|
141
|
+
* playback flag, so a guest who joined, chose their language and sat reading
|
|
142
|
+
* the translated text counted for nothing: the pass clock stayed at zero
|
|
143
|
+
* until they pressed Listening. David found it on a phone, and the counting
|
|
144
|
+
* spec had said otherwise all along, in as many words: "the listener session
|
|
145
|
+
* is open AND we are delivering translated output to it, audio or text".
|
|
146
|
+
*
|
|
147
|
+
* The reading is the product. A deaf guest, a guest in a quiet room, a guest
|
|
148
|
+
* with no headphones and a guest who simply prefers to read are all being
|
|
149
|
+
* served, all costing us translation on every chunk, and none of them are
|
|
150
|
+
* pressing an audio button. Charging only for audio would have meant a
|
|
151
|
+
* church whose congregation reads never spends a pass, which sounds generous
|
|
152
|
+
* until you notice we are paying for every word of it.
|
|
153
|
+
*
|
|
154
|
+
* So the test is PRESENCE, and nothing else. Presence already means joined,
|
|
155
|
+
* in a language, and with a heartbeat inside the last ninety seconds, which
|
|
156
|
+
* is exactly the population `consumersFor` is translating for. Whether they
|
|
157
|
+
* also chose to hear it is not our business, and the spec is explicit that we
|
|
158
|
+
* do not police attention: a phone in a pocket with audio playing is
|
|
159
|
+
* listening, and a phone in a hand with text on it is reading.
|
|
160
|
+
*
|
|
161
|
+
* `isListening` keeps its real job, which is a different question: whether to
|
|
162
|
+
* SYNTHESISE SPEECH. See getLanguagesWithActiveListeners. Speech is the most
|
|
163
|
+
* expensive thing we produce and nobody should pay to generate audio no one
|
|
164
|
+
* is playing. Text is already produced for the room either way.
|
|
165
|
+
*/
|
|
166
|
+
getReceivingCount(roomId: string): Promise<number>;
|
|
39
167
|
private capKey;
|
|
168
|
+
private previewKey;
|
|
169
|
+
private previewCountKey;
|
|
170
|
+
/** Open a preview window on one language. Re-pressing simply extends it. */
|
|
171
|
+
openPreview(roomId: string, language: string, windowSeconds: number): Promise<void>;
|
|
172
|
+
/** Seconds left on a language's preview window, or 0 when none is open. */
|
|
173
|
+
getPreviewRemaining(roomId: string, language: string): Promise<number>;
|
|
174
|
+
/**
|
|
175
|
+
* Which of `candidates` currently have a preview open.
|
|
176
|
+
*
|
|
177
|
+
* Takes the candidate list rather than scanning for a pattern on purpose:
|
|
178
|
+
* KEYS and SCAN against a live Redis to answer a question asked on every
|
|
179
|
+
* audio chunk is the kind of thing that is fine until it is not.
|
|
180
|
+
*/
|
|
181
|
+
getPreviewLanguages(roomId: string, candidates: string[]): Promise<string[]>;
|
|
182
|
+
/**
|
|
183
|
+
* Count one press against the room's allowance and say whether it is allowed.
|
|
184
|
+
*
|
|
185
|
+
* The cap exists because a preview translates for real. Without it, holding
|
|
186
|
+
* the button open is a way to run a translation service for nothing, and the
|
|
187
|
+
* obvious abuse is someone using a speaker preview as a free manual
|
|
188
|
+
* translation tool. The counter's own TTL resets the allowance, so a church
|
|
189
|
+
* running a genuine service every week is never permanently locked out.
|
|
190
|
+
*/
|
|
191
|
+
consumePreviewAllowance(roomId: string, maxPresses: number, resetSeconds: number): Promise<{
|
|
192
|
+
allowed: boolean;
|
|
193
|
+
used: number;
|
|
194
|
+
remaining: number;
|
|
195
|
+
}>;
|
|
196
|
+
/** Presses already spent in the current window. Read only, never increments. */
|
|
197
|
+
getPreviewUsage(roomId: string): Promise<number>;
|
|
40
198
|
/**
|
|
41
199
|
* Cache the room's listener cap. Called by the speaker service at go-live and
|
|
42
200
|
* on reconnect; `ttlSeconds` should comfortably exceed a session so the key
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MulingstreamListenerManager = void 0;
|
|
3
|
+
exports.MulingstreamListenerManager = exports.listenerPresence = exports.LISTENER_HEARTBEAT_INTERVAL_MS = exports.LISTENER_GONE_MS = exports.LISTENER_PRESENT_MS = void 0;
|
|
4
|
+
const listener_presence_1 = require("../data/listener-presence");
|
|
5
|
+
var listener_presence_2 = require("../data/listener-presence");
|
|
6
|
+
Object.defineProperty(exports, "LISTENER_PRESENT_MS", { enumerable: true, get: function () { return listener_presence_2.LISTENER_PRESENT_MS; } });
|
|
7
|
+
Object.defineProperty(exports, "LISTENER_GONE_MS", { enumerable: true, get: function () { return listener_presence_2.LISTENER_GONE_MS; } });
|
|
8
|
+
Object.defineProperty(exports, "LISTENER_HEARTBEAT_INTERVAL_MS", { enumerable: true, get: function () { return listener_presence_2.LISTENER_HEARTBEAT_INTERVAL_MS; } });
|
|
9
|
+
Object.defineProperty(exports, "listenerPresence", { enumerable: true, get: function () { return listener_presence_2.listenerPresence; } });
|
|
4
10
|
const EXPIRATION = 24 * 60 * 60; // 24 hours in seconds
|
|
5
11
|
/**
|
|
6
12
|
* Generates a bright RGB color suitable for dark backgrounds.
|
|
@@ -48,7 +54,8 @@ class MulingstreamListenerManager {
|
|
|
48
54
|
language: data.language || '',
|
|
49
55
|
color: data.color || '',
|
|
50
56
|
isListening: data.isListening === 'true',
|
|
51
|
-
breakpoint: bp === 'mobile' || bp === 'desktop' || bp === 'display' ? bp : undefined
|
|
57
|
+
breakpoint: bp === 'mobile' || bp === 'desktop' || bp === 'display' ? bp : undefined,
|
|
58
|
+
isAway: data.isAway === 'true'
|
|
52
59
|
};
|
|
53
60
|
}
|
|
54
61
|
// Creates a new listener.
|
|
@@ -74,25 +81,60 @@ class MulingstreamListenerManager {
|
|
|
74
81
|
language: (_c = listenerData.language) !== null && _c !== void 0 ? _c : '',
|
|
75
82
|
color,
|
|
76
83
|
isListening: 'false',
|
|
77
|
-
breakpoint: (_d = listenerData.breakpoint) !== null && _d !== void 0 ? _d : ''
|
|
84
|
+
breakpoint: (_d = listenerData.breakpoint) !== null && _d !== void 0 ? _d : '',
|
|
85
|
+
// A listener who just joined is looking at the page by definition.
|
|
86
|
+
isAway: 'false'
|
|
78
87
|
});
|
|
79
88
|
// expire listener
|
|
80
89
|
await this.redisClient.expire(`listener:${listenerId}`, EXPIRATION);
|
|
81
90
|
return listenerId;
|
|
82
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Every listener currently known, across every room.
|
|
94
|
+
*
|
|
95
|
+
* WHY THIS USES SCAN AND NOT KEYS
|
|
96
|
+
*
|
|
97
|
+
* It used to be `KEYS 'listener:*'`, with a TODO next to it saying it should
|
|
98
|
+
* not be. KEYS walks the entire keyspace in one go and Redis is single
|
|
99
|
+
* threaded, so for as long as it runs nothing else in the process is served:
|
|
100
|
+
* not a heartbeat, not a chunk being handed to the pipeline, not a listener
|
|
101
|
+
* joining. The cost is invisible on a development database with a few dozen
|
|
102
|
+
* keys and grows with the busiest moment we will ever have, which is precisely
|
|
103
|
+
* when it must not stall. This is called by the cleanup sweep every sixty
|
|
104
|
+
* seconds, for the entire life of the process, so it would have been the most
|
|
105
|
+
* frequently executed blocking command in the system.
|
|
106
|
+
*
|
|
107
|
+
* SCAN answers the same question in cursored batches, yielding between each,
|
|
108
|
+
* so a large keyspace costs more round trips rather than one long stall. The
|
|
109
|
+
* guarantees are weaker, deliberately: a key added or removed mid-scan may or
|
|
110
|
+
* may not appear. That is exactly right for the caller, which is looking for
|
|
111
|
+
* listeners who went silent minutes ago. One arriving during the scan is one
|
|
112
|
+
* for the next sweep, sixty seconds later, and there is nothing to do about a
|
|
113
|
+
* brand new listener anyway.
|
|
114
|
+
*
|
|
115
|
+
* The pattern also has to reject keys that merely start with `listener:`. The
|
|
116
|
+
* legacy `listener:panel-state:*` keys matched the old pattern and were parsed
|
|
117
|
+
* as listener records, producing entries with NaN timestamps. They never
|
|
118
|
+
* caused harm, since NaN fails every comparison, which is exactly how a
|
|
119
|
+
* defect like that survives.
|
|
120
|
+
*/
|
|
83
121
|
async getAllListeners() {
|
|
84
|
-
// get all keys that match 'listener:*'
|
|
85
|
-
// TODO: if we have many keys, consider using SCAN instead of KEYS to avoid performance issues.
|
|
86
|
-
const keys = await this.redisClient.keys('listener:*');
|
|
87
|
-
if (!keys || keys.length === 0) {
|
|
88
|
-
return [];
|
|
89
|
-
}
|
|
90
|
-
// fetch each hash with HGETALL
|
|
91
122
|
const listeners = [];
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
123
|
+
let cursor = 0;
|
|
124
|
+
do {
|
|
125
|
+
const [nextCursor, keys] = await this.redisClient.scan(cursor, 'listener:*', 500);
|
|
126
|
+
cursor = Number(nextCursor);
|
|
127
|
+
for (const key of keys || []) {
|
|
128
|
+
// `listener:{id}` only. Anything with a further colon belongs to
|
|
129
|
+
// some other feature that happens to share the prefix.
|
|
130
|
+
if (key.split(':').length !== 2)
|
|
131
|
+
continue;
|
|
132
|
+
const data = await this.redisClient.hgetall(key);
|
|
133
|
+
if (!data || Object.keys(data).length === 0)
|
|
134
|
+
continue;
|
|
135
|
+
listeners.push(this.parseHashData(data));
|
|
136
|
+
}
|
|
137
|
+
} while (cursor !== 0);
|
|
96
138
|
return listeners;
|
|
97
139
|
}
|
|
98
140
|
async getListenersByRoom(roomId) {
|
|
@@ -189,13 +231,23 @@ class MulingstreamListenerManager {
|
|
|
189
231
|
});
|
|
190
232
|
return filteredListeners.map((listener) => listener.socketId);
|
|
191
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Which languages this room has someone PRESENT on.
|
|
236
|
+
*
|
|
237
|
+
* Feeds consumersFor, which decides what gets translated at all, so an
|
|
238
|
+
* over-generous answer here is money spent on every chunk for nobody. It
|
|
239
|
+
* therefore counts present listeners only: somebody whose phone has been dark
|
|
240
|
+
* for two minutes is receiving nothing, and translating for them is pure loss.
|
|
241
|
+
*
|
|
242
|
+
* It used to count every listener the room had ever registered, including ones
|
|
243
|
+
* whose heartbeat had been silent for fourteen minutes, because the only thing
|
|
244
|
+
* that removed a listener was the cleanup sweep. See data/listener-presence.ts
|
|
245
|
+
* for why that single threshold could not be lowered and had to be split.
|
|
246
|
+
*/
|
|
192
247
|
async getUniqueLanguagesByRoom(roomId) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
// if I wanted to get only active listeners
|
|
197
|
-
// const listeners = (await this.getListenersByRoom(roomId)).filter(l => l.isActive);
|
|
198
|
-
// 2) Count how many times each language appears.
|
|
248
|
+
const now = Date.now();
|
|
249
|
+
const listeners = (await this.getListenersByRoom(roomId)).filter((listener) => (0, listener_presence_1.isListenerPresent)(listener.lastHeartbeat, listener.isAway === true, now));
|
|
250
|
+
// Count how many times each language appears.
|
|
199
251
|
const languageCountMap = {};
|
|
200
252
|
for (const listener of listeners) {
|
|
201
253
|
// skip blank/unset language
|
|
@@ -211,6 +263,57 @@ class MulingstreamListenerManager {
|
|
|
211
263
|
// 4) Map back to just the language strings in order
|
|
212
264
|
return sortedEntries.map(([language]) => language);
|
|
213
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* How many PRESENT listeners in this room are on a language.
|
|
268
|
+
*
|
|
269
|
+
* Decides whether a guest-added language slot can be handed back, and counts
|
|
270
|
+
* the same population `consumersFor` reads, so the two cannot disagree about
|
|
271
|
+
* whether a language has an audience: a slot is released exactly when the
|
|
272
|
+
* translation for it stops, never one without the other.
|
|
273
|
+
*
|
|
274
|
+
* Present rather than merely registered, and that is the deliberate part. A
|
|
275
|
+
* listener whose phone has been dark for two minutes is not hearing the
|
|
276
|
+
* language, so holding the room's last slot for them denies it to a guest
|
|
277
|
+
* standing in the room who is. If they come back and the slot has gone to
|
|
278
|
+
* somebody else, they choose again from what the room now has; if it is still
|
|
279
|
+
* free they simply take it back.
|
|
280
|
+
*/
|
|
281
|
+
async countListenersOnLanguage(roomId, language) {
|
|
282
|
+
if (!roomId || !language)
|
|
283
|
+
return 0;
|
|
284
|
+
const now = Date.now();
|
|
285
|
+
const listeners = await this.getListenersByRoom(roomId);
|
|
286
|
+
return listeners.filter((listener) => listener.language === language && (0, listener_presence_1.isListenerPresent)(listener.lastHeartbeat, listener.isAway === true, now)).length;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* The client saying its page went into the background, or came back.
|
|
290
|
+
*
|
|
291
|
+
* This is the difference between reacting to a locked screen at once and
|
|
292
|
+
* reacting to it after three missed heartbeats. Both end in the same state;
|
|
293
|
+
* one of them costs ninety seconds of translation and synthesis for a phone
|
|
294
|
+
* nobody is looking at, on every language in the room.
|
|
295
|
+
*
|
|
296
|
+
* Does NOT touch lastHeartbeat. That field is now also the recorded end of the
|
|
297
|
+
* listener's session, so it has to keep meaning "when they were genuinely last
|
|
298
|
+
* there" and nothing else.
|
|
299
|
+
*/
|
|
300
|
+
async setAwayState(listenerIdOrToken, isAway) {
|
|
301
|
+
const listener = await this.getListener(listenerIdOrToken);
|
|
302
|
+
if (!listener)
|
|
303
|
+
return null;
|
|
304
|
+
await this.redisClient.hset(`listener:${listener.listenerId}`, { isAway: isAway.toString() });
|
|
305
|
+
return { ...listener, isAway };
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Present, away or gone, for one listener. For anything that needs to SHOW the
|
|
309
|
+
* distinction rather than act on it.
|
|
310
|
+
*/
|
|
311
|
+
async getPresence(listenerIdOrToken) {
|
|
312
|
+
const listener = await this.getListener(listenerIdOrToken);
|
|
313
|
+
if (!listener)
|
|
314
|
+
return null;
|
|
315
|
+
return (0, listener_presence_1.listenerPresence)(listener.lastHeartbeat, listener.isAway === true);
|
|
316
|
+
}
|
|
214
317
|
async updateHeartbeat(listenerIdOrToken) {
|
|
215
318
|
const listener = await this.getListener(listenerIdOrToken);
|
|
216
319
|
if (!listener) {
|
|
@@ -218,9 +321,16 @@ class MulingstreamListenerManager {
|
|
|
218
321
|
return null;
|
|
219
322
|
}
|
|
220
323
|
const now = Date.now();
|
|
221
|
-
// Update the heartbeat timestamp
|
|
324
|
+
// Update the heartbeat timestamp, and clear away in the same write.
|
|
325
|
+
//
|
|
326
|
+
// A heartbeat only arrives from a page that is running, so receiving one is
|
|
327
|
+
// proof the listener is back: an explicit "I have returned" event would be
|
|
328
|
+
// a second way of saying the same thing, and a second way to get it wrong.
|
|
329
|
+
// A returning page sends a heartbeat immediately on visibility, so this is
|
|
330
|
+
// as fast as anything else could be.
|
|
222
331
|
await this.redisClient.hset(`listener:${listener.listenerId}`, {
|
|
223
|
-
lastHeartbeat: now.toString()
|
|
332
|
+
lastHeartbeat: now.toString(),
|
|
333
|
+
isAway: 'false'
|
|
224
334
|
});
|
|
225
335
|
// Reset expiration on activity
|
|
226
336
|
await this.redisClient.expire(`listener:${listener.listenerId}`, EXPIRATION);
|
|
@@ -239,6 +349,46 @@ class MulingstreamListenerManager {
|
|
|
239
349
|
languageBreakdown
|
|
240
350
|
};
|
|
241
351
|
}
|
|
352
|
+
/**
|
|
353
|
+
* How many people in this room are ACTUALLY RECEIVING translation right now.
|
|
354
|
+
*
|
|
355
|
+
* Church passes turn on this number rather than on how many people joined. A
|
|
356
|
+
* pass is spent only when translation reached a human being, so somebody
|
|
357
|
+
* sitting on the join screen must not burn a church's pass.
|
|
358
|
+
*
|
|
359
|
+
* ─── RECEIVING MEANS AUDIO **OR** TEXT ──────────────────────────────────
|
|
360
|
+
*
|
|
361
|
+
* Corrected 2026-08-19. This used to require `isListening`, the audio
|
|
362
|
+
* playback flag, so a guest who joined, chose their language and sat reading
|
|
363
|
+
* the translated text counted for nothing: the pass clock stayed at zero
|
|
364
|
+
* until they pressed Listening. David found it on a phone, and the counting
|
|
365
|
+
* spec had said otherwise all along, in as many words: "the listener session
|
|
366
|
+
* is open AND we are delivering translated output to it, audio or text".
|
|
367
|
+
*
|
|
368
|
+
* The reading is the product. A deaf guest, a guest in a quiet room, a guest
|
|
369
|
+
* with no headphones and a guest who simply prefers to read are all being
|
|
370
|
+
* served, all costing us translation on every chunk, and none of them are
|
|
371
|
+
* pressing an audio button. Charging only for audio would have meant a
|
|
372
|
+
* church whose congregation reads never spends a pass, which sounds generous
|
|
373
|
+
* until you notice we are paying for every word of it.
|
|
374
|
+
*
|
|
375
|
+
* So the test is PRESENCE, and nothing else. Presence already means joined,
|
|
376
|
+
* in a language, and with a heartbeat inside the last ninety seconds, which
|
|
377
|
+
* is exactly the population `consumersFor` is translating for. Whether they
|
|
378
|
+
* also chose to hear it is not our business, and the spec is explicit that we
|
|
379
|
+
* do not police attention: a phone in a pocket with audio playing is
|
|
380
|
+
* listening, and a phone in a hand with text on it is reading.
|
|
381
|
+
*
|
|
382
|
+
* `isListening` keeps its real job, which is a different question: whether to
|
|
383
|
+
* SYNTHESISE SPEECH. See getLanguagesWithActiveListeners. Speech is the most
|
|
384
|
+
* expensive thing we produce and nobody should pay to generate audio no one
|
|
385
|
+
* is playing. Text is already produced for the room either way.
|
|
386
|
+
*/
|
|
387
|
+
async getReceivingCount(roomId) {
|
|
388
|
+
const now = Date.now();
|
|
389
|
+
const listeners = await this.getListenersByRoom(roomId);
|
|
390
|
+
return listeners.filter((l) => (0, listener_presence_1.isListenerPresent)(l.lastHeartbeat, l.isAway === true, now)).length;
|
|
391
|
+
}
|
|
242
392
|
// ─── Per-room listener capacity (design B, 2026-07-09) ────────────────────
|
|
243
393
|
// The cap value is the room owner's plan `max_audience`, resolved ONCE by the
|
|
244
394
|
// speaker service when it goes live (it already fetches the plan entitlements)
|
|
@@ -250,6 +400,82 @@ class MulingstreamListenerManager {
|
|
|
250
400
|
capKey(roomId) {
|
|
251
401
|
return `room:${roomId}:listener-cap`;
|
|
252
402
|
}
|
|
403
|
+
/*
|
|
404
|
+
* SPEAKER PREVIEW
|
|
405
|
+
*
|
|
406
|
+
* A speaker looking at their own translation feed is a consumer of
|
|
407
|
+
* translation but must never be a payer for it: nobody should be able to
|
|
408
|
+
* spend a church's pass, or their own credits, by watching their own screen.
|
|
409
|
+
* So a preview is a short window they open deliberately, not a state they
|
|
410
|
+
* fall into by leaving a tab open.
|
|
411
|
+
*
|
|
412
|
+
* In Redis rather than in the pipeline's memory because the request arrives
|
|
413
|
+
* at the room service and the decision is read by the pipeline service, and
|
|
414
|
+
* because a pipeline replica restarting mid window must not silently start
|
|
415
|
+
* translating for nobody. The TTL IS the expiry: nothing has to remember to
|
|
416
|
+
* close it, and a crash fails in the safe direction.
|
|
417
|
+
*/
|
|
418
|
+
previewKey(roomId, language) {
|
|
419
|
+
return `room:${roomId}:preview:${language}`;
|
|
420
|
+
}
|
|
421
|
+
previewCountKey(roomId) {
|
|
422
|
+
return `room:${roomId}:preview-count`;
|
|
423
|
+
}
|
|
424
|
+
/** Open a preview window on one language. Re-pressing simply extends it. */
|
|
425
|
+
async openPreview(roomId, language, windowSeconds) {
|
|
426
|
+
if (!roomId || !language || windowSeconds <= 0)
|
|
427
|
+
return;
|
|
428
|
+
const key = this.previewKey(roomId, language);
|
|
429
|
+
await this.redisClient.set(key, '1');
|
|
430
|
+
await this.redisClient.expire(key, Math.floor(windowSeconds));
|
|
431
|
+
}
|
|
432
|
+
/** Seconds left on a language's preview window, or 0 when none is open. */
|
|
433
|
+
async getPreviewRemaining(roomId, language) {
|
|
434
|
+
const ttl = await this.redisClient.ttl(this.previewKey(roomId, language));
|
|
435
|
+
return ttl > 0 ? ttl : 0;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Which of `candidates` currently have a preview open.
|
|
439
|
+
*
|
|
440
|
+
* Takes the candidate list rather than scanning for a pattern on purpose:
|
|
441
|
+
* KEYS and SCAN against a live Redis to answer a question asked on every
|
|
442
|
+
* audio chunk is the kind of thing that is fine until it is not.
|
|
443
|
+
*/
|
|
444
|
+
async getPreviewLanguages(roomId, candidates) {
|
|
445
|
+
if (!roomId || candidates.length === 0)
|
|
446
|
+
return [];
|
|
447
|
+
const open = [];
|
|
448
|
+
for (const language of candidates) {
|
|
449
|
+
if (await this.redisClient.get(this.previewKey(roomId, language)))
|
|
450
|
+
open.push(language);
|
|
451
|
+
}
|
|
452
|
+
return open;
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Count one press against the room's allowance and say whether it is allowed.
|
|
456
|
+
*
|
|
457
|
+
* The cap exists because a preview translates for real. Without it, holding
|
|
458
|
+
* the button open is a way to run a translation service for nothing, and the
|
|
459
|
+
* obvious abuse is someone using a speaker preview as a free manual
|
|
460
|
+
* translation tool. The counter's own TTL resets the allowance, so a church
|
|
461
|
+
* running a genuine service every week is never permanently locked out.
|
|
462
|
+
*/
|
|
463
|
+
async consumePreviewAllowance(roomId, maxPresses, resetSeconds) {
|
|
464
|
+
const key = this.previewCountKey(roomId);
|
|
465
|
+
const used = await this.redisClient.incr(key);
|
|
466
|
+
// Only the first press starts the clock, so the window is a fixed period
|
|
467
|
+
// from first use rather than one that slides forward on every press.
|
|
468
|
+
if (used === 1)
|
|
469
|
+
await this.redisClient.expire(key, Math.floor(resetSeconds));
|
|
470
|
+
const allowed = used <= maxPresses;
|
|
471
|
+
return { allowed, used, remaining: Math.max(0, maxPresses - used) };
|
|
472
|
+
}
|
|
473
|
+
/** Presses already spent in the current window. Read only, never increments. */
|
|
474
|
+
async getPreviewUsage(roomId) {
|
|
475
|
+
const raw = await this.redisClient.get(this.previewCountKey(roomId));
|
|
476
|
+
const used = raw ? parseInt(raw, 10) : 0;
|
|
477
|
+
return Number.isFinite(used) && used > 0 ? used : 0;
|
|
478
|
+
}
|
|
253
479
|
/**
|
|
254
480
|
* Cache the room's listener cap. Called by the speaker service at go-live and
|
|
255
481
|
* on reconnect; `ttlSeconds` should comfortably exceed a session so the key
|
|
@@ -330,6 +556,7 @@ class MulingstreamListenerManager {
|
|
|
330
556
|
* Used by pipeline to determine which languages need TTS generation.
|
|
331
557
|
*/
|
|
332
558
|
async getLanguagesWithActiveListeners(roomId) {
|
|
559
|
+
const now = Date.now();
|
|
333
560
|
const listeners = await this.getListenersByRoom(roomId);
|
|
334
561
|
const languageCountMap = {};
|
|
335
562
|
for (const listener of listeners) {
|
|
@@ -337,6 +564,10 @@ class MulingstreamListenerManager {
|
|
|
337
564
|
continue;
|
|
338
565
|
if (!listener.isListening)
|
|
339
566
|
continue;
|
|
567
|
+
// Speech synthesis is the most expensive thing we do. A language whose
|
|
568
|
+
// only listener has a dark phone must not be synthesised.
|
|
569
|
+
if (!(0, listener_presence_1.isListenerPresent)(listener.lastHeartbeat, listener.isAway === true, now))
|
|
570
|
+
continue;
|
|
340
571
|
const lang = listener.language;
|
|
341
572
|
languageCountMap[lang] = (languageCountMap[lang] || 0) + 1;
|
|
342
573
|
}
|
|
@@ -36,6 +36,18 @@ export declare class MulingstreamSpeakerManager {
|
|
|
36
36
|
removeSpeakerBySocketId(socketId: string): Promise<boolean>;
|
|
37
37
|
removeSpeakersByUserId(userId: string): Promise<number>;
|
|
38
38
|
removeSpeakersByRoomId(roomId: string): Promise<number>;
|
|
39
|
+
/**
|
|
40
|
+
* Remove a speaker by its own id, without needing a live socket mapping.
|
|
41
|
+
*
|
|
42
|
+
* This is the removal that always works, and it is the one the cleanup
|
|
43
|
+
* sweeps must use. Removing by socket depends on `socket:<id>:speaker`
|
|
44
|
+
* still pointing at the record, and after a reconnect self-heal it does
|
|
45
|
+
* not: see updateSocketId. A ghost with a broken mapping is exactly the
|
|
46
|
+
* record a sweep is trying to delete, so resolving it through the mapping
|
|
47
|
+
* fails on precisely the case that matters and the ghost survives every
|
|
48
|
+
* sweep for the full 24 hour TTL.
|
|
49
|
+
*/
|
|
50
|
+
removeSpeakerBySpeakerId(speakerId: string): Promise<boolean>;
|
|
39
51
|
private removeSpeakerById;
|
|
40
52
|
getSpeakerBySpeakerId(speakerId: string): Promise<MulingstreamSpeakerData | null>;
|
|
41
53
|
getSpeakerBySocketId(socketId: string): Promise<MulingstreamSpeakerData | null>;
|
|
@@ -54,6 +66,22 @@ export declare class MulingstreamSpeakerManager {
|
|
|
54
66
|
/**
|
|
55
67
|
* Update socketId for a speaker (called by heartbeat self-heal when reconnect creates a new socket).
|
|
56
68
|
* Mirrors the listener-side self-heal pattern — see MULINGSTREAM_RELIABILITY_ROADMAP.md §0.2.
|
|
69
|
+
*
|
|
70
|
+
* ─── THE MAPPING MOVES WITH THE FIELD, OR THE RECORD BECOMES A GHOST ───
|
|
71
|
+
*
|
|
72
|
+
* This used to rewrite the field alone, and that one omission is where
|
|
73
|
+
* every ghost speaker came from. `socket:<id>:speaker` is how a socket is
|
|
74
|
+
* resolved back to its speaker, and it is how disconnect, leave and both
|
|
75
|
+
* cleanup sweeps find the record they mean to delete. Leave it pointing at
|
|
76
|
+
* the OLD socket and the record becomes unreachable from the socket that
|
|
77
|
+
* actually owns it: the speaker's own disconnect finds nothing to remove,
|
|
78
|
+
* so the record survives its socket and lives out the 24 hour TTL.
|
|
79
|
+
*
|
|
80
|
+
* Room 500032 held four of them on 2026-08-27, one per Go Live that day,
|
|
81
|
+
* and three had a socketId field that did not match the socket in their own
|
|
82
|
+
* key: the signature of exactly this. They are not harmless. Ghosts inflate
|
|
83
|
+
* every "is anyone still in this room" count, and cleaning one up is what
|
|
84
|
+
* told a live congregation the speaker had left.
|
|
57
85
|
*/
|
|
58
86
|
updateSocketId(speakerId: string, newSocketId: string): Promise<boolean>;
|
|
59
87
|
updateTargetLanguages(socketId: string, languages: string[]): Promise<boolean>;
|
|
@@ -129,6 +129,20 @@ class MulingstreamSpeakerManager {
|
|
|
129
129
|
deleted += 1;
|
|
130
130
|
return deleted;
|
|
131
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Remove a speaker by its own id, without needing a live socket mapping.
|
|
134
|
+
*
|
|
135
|
+
* This is the removal that always works, and it is the one the cleanup
|
|
136
|
+
* sweeps must use. Removing by socket depends on `socket:<id>:speaker`
|
|
137
|
+
* still pointing at the record, and after a reconnect self-heal it does
|
|
138
|
+
* not: see updateSocketId. A ghost with a broken mapping is exactly the
|
|
139
|
+
* record a sweep is trying to delete, so resolving it through the mapping
|
|
140
|
+
* fails on precisely the case that matters and the ghost survives every
|
|
141
|
+
* sweep for the full 24 hour TTL.
|
|
142
|
+
*/
|
|
143
|
+
async removeSpeakerBySpeakerId(speakerId) {
|
|
144
|
+
return this.removeSpeakerById(speakerId);
|
|
145
|
+
}
|
|
132
146
|
async removeSpeakerById(speakerId) {
|
|
133
147
|
const key = this.buildKey(speakerId);
|
|
134
148
|
const data = await this.redisClient.hgetall(key);
|
|
@@ -136,6 +150,21 @@ class MulingstreamSpeakerManager {
|
|
|
136
150
|
await this.cleanIndexes(speakerId);
|
|
137
151
|
return false;
|
|
138
152
|
}
|
|
153
|
+
/*
|
|
154
|
+
* Drop the mapping for the socket this record CURRENTLY holds, before
|
|
155
|
+
* the hash goes.
|
|
156
|
+
*
|
|
157
|
+
* cleanIndexes can only delete the socket baked into the speakerId,
|
|
158
|
+
* which is the socket the record was created with. A record that has
|
|
159
|
+
* been through a reconnect self-heal holds a different one in its
|
|
160
|
+
* field, and nothing else will ever delete that key: it would sit
|
|
161
|
+
* pointing at a deleted speaker until its TTL, and getSpeakerBySocketId
|
|
162
|
+
* would resolve a socket to a record that no longer exists.
|
|
163
|
+
*/
|
|
164
|
+
const currentSocketId = data.socketId;
|
|
165
|
+
if (currentSocketId) {
|
|
166
|
+
await this.redisClient.del(`socket:${currentSocketId}:speaker`);
|
|
167
|
+
}
|
|
139
168
|
await this.redisClient.del(key);
|
|
140
169
|
await this.cleanIndexes(speakerId);
|
|
141
170
|
return true;
|
|
@@ -231,12 +260,37 @@ class MulingstreamSpeakerManager {
|
|
|
231
260
|
/**
|
|
232
261
|
* Update socketId for a speaker (called by heartbeat self-heal when reconnect creates a new socket).
|
|
233
262
|
* Mirrors the listener-side self-heal pattern — see MULINGSTREAM_RELIABILITY_ROADMAP.md §0.2.
|
|
263
|
+
*
|
|
264
|
+
* ─── THE MAPPING MOVES WITH THE FIELD, OR THE RECORD BECOMES A GHOST ───
|
|
265
|
+
*
|
|
266
|
+
* This used to rewrite the field alone, and that one omission is where
|
|
267
|
+
* every ghost speaker came from. `socket:<id>:speaker` is how a socket is
|
|
268
|
+
* resolved back to its speaker, and it is how disconnect, leave and both
|
|
269
|
+
* cleanup sweeps find the record they mean to delete. Leave it pointing at
|
|
270
|
+
* the OLD socket and the record becomes unreachable from the socket that
|
|
271
|
+
* actually owns it: the speaker's own disconnect finds nothing to remove,
|
|
272
|
+
* so the record survives its socket and lives out the 24 hour TTL.
|
|
273
|
+
*
|
|
274
|
+
* Room 500032 held four of them on 2026-08-27, one per Go Live that day,
|
|
275
|
+
* and three had a socketId field that did not match the socket in their own
|
|
276
|
+
* key: the signature of exactly this. They are not harmless. Ghosts inflate
|
|
277
|
+
* every "is anyone still in this room" count, and cleaning one up is what
|
|
278
|
+
* told a live congregation the speaker had left.
|
|
234
279
|
*/
|
|
235
280
|
async updateSocketId(speakerId, newSocketId) {
|
|
236
281
|
const speaker = await this.getSpeakerBySpeakerId(speakerId);
|
|
237
282
|
if (speaker === null)
|
|
238
283
|
return false;
|
|
284
|
+
const previousSocketId = speaker.socketId;
|
|
239
285
|
await this.redisClient.hset(this.buildKey(speakerId), { socketId: newSocketId });
|
|
286
|
+
// Old mapping first: if the process dies between the two, an absent
|
|
287
|
+
// mapping is recoverable (the next heartbeat re-heals it) while a
|
|
288
|
+
// mapping pointing at the wrong record is not.
|
|
289
|
+
if (previousSocketId && previousSocketId !== newSocketId) {
|
|
290
|
+
await this.redisClient.del(`socket:${previousSocketId}:speaker`);
|
|
291
|
+
}
|
|
292
|
+
await this.redisClient.set(`socket:${newSocketId}:speaker`, speakerId);
|
|
293
|
+
await this.redisClient.expire(`socket:${newSocketId}:speaker`, EXPIRATION);
|
|
240
294
|
return true;
|
|
241
295
|
}
|
|
242
296
|
async updateTargetLanguages(socketId, languages) {
|