@gonzih/cc-discord 0.1.9 → 0.1.10
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.
- package/dist/notifier.js +28 -14
- package/package.json +1 -1
package/dist/notifier.js
CHANGED
|
@@ -222,24 +222,21 @@ export function startNotifier(bot, notifyChannelId, namespace, redis, handleUser
|
|
|
222
222
|
clearTimeout(buf.timer);
|
|
223
223
|
buf.timer = setTimeout(() => flushMetaAgentBuffer(ns, targetChannelId), META_AGENT_FLUSH_DELAY_MS);
|
|
224
224
|
});
|
|
225
|
-
// Poll
|
|
226
|
-
const notifyListRedisKey = notifyListKey(namespace);
|
|
225
|
+
// Poll notifyListKey(ns) LIST every 5 seconds — covers primary + all routed namespaces.
|
|
227
226
|
const MAX_PER_CYCLE = 20;
|
|
228
|
-
const
|
|
229
|
-
const
|
|
230
|
-
if (targetId == null)
|
|
231
|
-
return;
|
|
227
|
+
const pollOneNamespace = async (ns, targetChannelId) => {
|
|
228
|
+
const listKey = notifyListKey(ns);
|
|
232
229
|
const items = [];
|
|
233
230
|
try {
|
|
234
231
|
for (let i = 0; i < MAX_PER_CYCLE; i++) {
|
|
235
|
-
const item = await redis.rpop(
|
|
232
|
+
const item = await redis.rpop(listKey);
|
|
236
233
|
if (item === null)
|
|
237
234
|
break;
|
|
238
235
|
items.push(item);
|
|
239
236
|
}
|
|
240
237
|
}
|
|
241
238
|
catch (err) {
|
|
242
|
-
log("warn",
|
|
239
|
+
log("warn", `notify list rpop failed (ns=${ns}):`, err.message);
|
|
243
240
|
return;
|
|
244
241
|
}
|
|
245
242
|
if (items.length === 0)
|
|
@@ -247,30 +244,47 @@ export function startNotifier(bot, notifyChannelId, namespace, redis, handleUser
|
|
|
247
244
|
let remaining = 0;
|
|
248
245
|
if (items.length === MAX_PER_CYCLE) {
|
|
249
246
|
try {
|
|
250
|
-
remaining = await redis.llen(
|
|
247
|
+
remaining = await redis.llen(listKey);
|
|
251
248
|
}
|
|
252
249
|
catch (err) {
|
|
253
|
-
log("warn",
|
|
250
|
+
log("warn", `notify list llen failed (ns=${ns}):`, err.message);
|
|
254
251
|
}
|
|
255
252
|
}
|
|
256
253
|
for (const raw of items) {
|
|
257
254
|
const notification = parseNotification(raw);
|
|
258
255
|
if (notification === null)
|
|
259
256
|
continue; // routing excludes discord
|
|
260
|
-
|
|
257
|
+
// Primary namespace: honour chatId-based per-channel routing via reverseSnowflakeLookup.
|
|
258
|
+
// Routed namespaces: always deliver to the registered Discord channelId — no leakage.
|
|
259
|
+
const destChannelId = ns === namespace
|
|
260
|
+
? (resolveNotifyChannel(notification.chatId, notifyChannelId, getActiveChannelId, reverseSnowflakeLookup) ?? targetChannelId)
|
|
261
|
+
: targetChannelId;
|
|
261
262
|
bot.sendToChannelById(destChannelId, notification.text).catch((err) => {
|
|
262
|
-
log("warn",
|
|
263
|
+
log("warn", `notify list send failed (ns=${ns}):`, err.message);
|
|
263
264
|
});
|
|
264
265
|
if (forwardNotification) {
|
|
265
266
|
forwardNotification(destChannelId, notification.text);
|
|
266
267
|
}
|
|
267
268
|
}
|
|
268
269
|
if (remaining > 0) {
|
|
269
|
-
bot.sendToChannelById(
|
|
270
|
-
log("warn",
|
|
270
|
+
bot.sendToChannelById(targetChannelId, `...and ${remaining} more notifications`).catch((err) => {
|
|
271
|
+
log("warn", `notify list summary send failed (ns=${ns}):`, err.message);
|
|
271
272
|
});
|
|
272
273
|
}
|
|
273
274
|
};
|
|
275
|
+
const pollNotifyList = async () => {
|
|
276
|
+
// Primary namespace
|
|
277
|
+
const primaryTargetId = notifyChannelId ?? getActiveChannelId?.();
|
|
278
|
+
if (primaryTargetId != null) {
|
|
279
|
+
await pollOneNamespace(namespace, primaryTargetId);
|
|
280
|
+
}
|
|
281
|
+
// All registered routed namespaces
|
|
282
|
+
for (const [ns, channelId] of routedChannelIds) {
|
|
283
|
+
if (ns !== namespace) {
|
|
284
|
+
await pollOneNamespace(ns, channelId);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
};
|
|
274
288
|
setInterval(() => {
|
|
275
289
|
void pollNotifyList();
|
|
276
290
|
}, 5_000);
|