levix-bot 4.9.1 → 5.0.0
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/README.md +57 -3
- package/SETUP.md +63 -7
- package/app.cjs +159 -46
- package/deploy/install.sh +10 -5
- package/package.json +9 -6
- package/public/dashboard/assets/40af6d88b63079a0-BZ8ojJNu.woff2 +0 -0
- package/public/dashboard/assets/481acf6760031cee-BypgxfGb.woff2 +0 -0
- package/public/dashboard/assets/6d3351fc535f8ef3-D2-eand5.woff2 +0 -0
- package/public/dashboard/assets/8ab0af53a3bda9d3-CyCXRvzh.woff2 +0 -0
- package/public/dashboard/assets/8f262393e9249e61-TQp-UTiE.woff2 +0 -0
- package/public/dashboard/assets/94d65887a83f1282-DmpS2jIq.woff2 +0 -0
- package/public/dashboard/assets/99e400330f36ae77-D3oMJlXt.woff2 +0 -0
- package/public/dashboard/assets/9d1f41b13e519d90-BVNSOH3d.woff2 +0 -0
- package/public/dashboard/assets/a005256cb957ecc7-CoYeBiSI.woff2 +0 -0
- package/public/dashboard/assets/b19c2080652a3c2b-DIC32ArD.woff2 +0 -0
- package/public/dashboard/assets/c4064400db3750ce-BehTv68k.woff2 +0 -0
- package/public/dashboard/assets/c69d345fa1068c2d-CmI34b-g.woff2 +0 -0
- package/public/dashboard/assets/cc27b30471250057-6fWv1k7M.woff2 +0 -0
- package/public/dashboard/assets/d8c30647abfeffe6-eXO_dkmS.woff2 +0 -0
- package/public/dashboard/assets/eb83d8d40af4411f-Buze_B52.woff2 +0 -0
- package/public/dashboard/assets/effc6711249e7349-qRpaaN48.woff2 +0 -0
- package/public/dashboard/assets/gateway-BDYet0pI.css +1 -0
- package/public/dashboard/assets/gateway-BYt6-s1O.js +1 -0
- package/public/dashboard/assets/index-B9wFpD_z.css +2 -0
- package/public/dashboard/assets/index-DWcQmu0_.js +124 -0
- package/public/dashboard/assets/index-Da-j89O-.js +458 -0
- package/public/dashboard/font-licenses/jetbrainsmono-OFL.txt +93 -0
- package/public/dashboard/font-licenses/plusjakartasans-OFL.txt +93 -0
- package/public/dashboard/font-licenses/tajawal-OFL.txt +93 -0
- package/public/dashboard/gateway.html +17 -0
- package/public/dashboard/index.html +3 -5
- package/public/ui-boot.js +33 -14
- package/scheduler.cjs +76 -23
- package/src/auth/auth-storage.cjs +54 -7
- package/src/auth/use-database-auth-state.js +70 -39
- package/src/automation/engine.js +412 -0
- package/src/automation/limits.cjs +6 -0
- package/src/automation/manage.js +290 -0
- package/src/automation/reactions.js +471 -0
- package/src/automation/rules.cjs +114 -0
- package/src/automation/status.cjs +76 -0
- package/src/backup/account.cjs +29 -0
- package/src/backup/activation.cjs +247 -0
- package/src/backup/content.cjs +36 -0
- package/src/backup/create.cjs +280 -0
- package/src/backup/extensions.cjs +101 -0
- package/src/backup/format.cjs +565 -0
- package/src/backup/jobs.cjs +111 -0
- package/src/backup/journal.cjs +41 -0
- package/src/backup/plan.cjs +223 -0
- package/src/backup/recovery.cjs +110 -0
- package/src/backup/restore.cjs +460 -0
- package/src/backup/uploads.cjs +146 -0
- package/src/bootstrap/core.js +50 -12
- package/src/bootstrap/panel.js +53 -2
- package/src/bootstrap/session-startup-policy.js +8 -1
- package/src/cli.js +163 -22
- package/src/commands/autodelete.cjs +41 -57
- package/src/commands/autoschedule.cjs +7 -33
- package/src/commands/blacklist.cjs +17 -35
- package/src/commands/block.cjs +4 -18
- package/src/commands/calc.cjs +22 -74
- package/src/commands/debt.cjs +28 -59
- package/src/commands/deleteschedule.cjs +7 -23
- package/src/commands/digit.cjs +5 -39
- package/src/commands/gemini.cjs +135 -218
- package/src/commands/group/add.cjs +28 -76
- package/src/commands/group/all.cjs +4 -15
- package/src/commands/group/antilink.cjs +28 -41
- package/src/commands/group/antispam.cjs +25 -35
- package/src/commands/group/approveall.cjs +7 -21
- package/src/commands/group/clearwarns.cjs +4 -18
- package/src/commands/group/deletenote.cjs +4 -18
- package/src/commands/group/demote.cjs +6 -23
- package/src/commands/group/flagged.cjs +10 -19
- package/src/commands/group/kick.cjs +26 -36
- package/src/commands/group/media.cjs +21 -36
- package/src/commands/group/members.cjs +16 -28
- package/src/commands/group/note.cjs +3 -14
- package/src/commands/group/policy.cjs +16 -37
- package/src/commands/group/promote.cjs +5 -22
- package/src/commands/group/removeall.cjs +6 -38
- package/src/commands/group/rules.cjs +4 -19
- package/src/commands/group/save.cjs +4 -18
- package/src/commands/group/setname.cjs +5 -19
- package/src/commands/group/setpp.cjs +8 -26
- package/src/commands/group/setrules.cjs +4 -15
- package/src/commands/group/setwarn.cjs +6 -23
- package/src/commands/group/tagadmins.cjs +5 -16
- package/src/commands/group/warn.cjs +5 -16
- package/src/commands/group/warns.cjs +12 -33
- package/src/commands/group/welcome.cjs +17 -34
- package/src/commands/group.cjs +91 -68
- package/src/commands/help.cjs +48 -89
- package/src/commands/lang.cjs +38 -97
- package/src/commands/listschedules.cjs +19 -30
- package/src/commands/loop.cjs +9 -55
- package/src/commands/md.cjs +2 -13
- package/src/commands/memory.cjs +53 -87
- package/src/commands/mod.cjs +40 -79
- package/src/commands/notes.cjs +4 -15
- package/src/commands/pack.cjs +26 -49
- package/src/commands/packs.cjs +8 -18
- package/src/commands/perm.cjs +48 -51
- package/src/commands/ping.cjs +2 -10
- package/src/commands/poll.cjs +5 -38
- package/src/commands/prayer.cjs +55 -36
- package/src/commands/qr.cjs +4 -18
- package/src/commands/rand.cjs +9 -58
- package/src/commands/restart.cjs +15 -33
- package/src/commands/schedule.cjs +5 -25
- package/src/commands/score.cjs +6 -27
- package/src/commands/send.cjs +4 -18
- package/src/commands/setprefix.cjs +11 -23
- package/src/commands/shortlink.cjs +13 -39
- package/src/commands/status.cjs +188 -76
- package/src/commands/sticker.cjs +5 -16
- package/src/commands/stickers.cjs +16 -20
- package/src/commands/stopbot.cjs +9 -25
- package/src/commands/stt.cjs +75 -107
- package/src/commands/todo.cjs +11 -31
- package/src/commands/toimage.cjs +11 -27
- package/src/commands/translate.cjs +53 -36
- package/src/commands/tts.cjs +46 -40
- package/src/commands/unblock.cjs +3 -17
- package/src/commands/update.cjs +23 -0
- package/src/commands/weather.cjs +40 -47
- package/src/config/ai-identity.cjs +14 -1
- package/src/config/baileys.config.js +16 -3
- package/src/config/data-dir.cjs +37 -0
- package/src/config/defaults.cjs +1 -0
- package/src/config/exit-codes.cjs +11 -0
- package/src/config/lock.cjs +55 -81
- package/src/config/official-links.cjs +61 -0
- package/src/config/paths.cjs +4 -33
- package/src/config/release.cjs +87 -0
- package/src/config/settings.cjs +199 -10
- package/src/core/connection.js +77 -9
- package/src/core/events.js +119 -33
- package/src/core/session.js +536 -84
- package/src/core/socket.js +81 -10
- package/src/db/copy.cjs +136 -0
- package/src/db/db.cjs +241 -36
- package/src/db/startup-guard.js +5 -0
- package/src/db/store.cjs +561 -54
- package/src/diagnostics/baileysLog.cjs +87 -0
- package/src/diagnostics/debugLog.cjs +16 -4
- package/src/diagnostics/device.cjs +4 -2
- package/src/diagnostics/nodeRuntime.cjs +131 -0
- package/src/diagnostics/signalSessionInspect.cjs +38 -0
- package/src/domain/command.js +24 -22
- package/src/extensions/autoReply.cjs +521 -0
- package/src/extensions/context.cjs +73 -0
- package/src/extensions/contract.cjs +69 -0
- package/src/extensions/entitlement/client.cjs +381 -0
- package/src/extensions/entitlement/index.cjs +629 -0
- package/src/extensions/entitlement/installation.cjs +117 -0
- package/src/extensions/entitlement/keys.cjs +72 -0
- package/src/extensions/entitlement/state.cjs +201 -0
- package/src/extensions/entitlement/target.cjs +7 -0
- package/src/extensions/entitlement/token.cjs +225 -0
- package/src/extensions/host.cjs +519 -0
- package/src/extensions/templateKeys.cjs +81 -0
- package/src/handlers/command.handler.js +191 -64
- package/src/handlers/group.handler.js +78 -18
- package/src/handlers/message.handler.js +56 -25
- package/src/i18n/catalog.generated.cjs +3537 -0
- package/src/index.js +51 -12
- package/src/legal/bundled/ar.json +102 -0
- package/src/legal/bundled/en.json +102 -0
- package/src/legal/bundled/es-419.json +102 -0
- package/src/legal/bundled/pt-BR.json +102 -0
- package/src/legal/cli.cjs +200 -0
- package/src/legal/consent.cjs +187 -0
- package/src/legal/document.cjs +156 -0
- package/src/legal/keys.cjs +100 -0
- package/src/legal/release.cjs +226 -0
- package/src/legal/remote.cjs +419 -0
- package/src/legal/terms.cjs +142 -0
- package/src/messaging/escape.cjs +31 -0
- package/src/messaging/health.cjs +65 -0
- package/src/messaging/identity.cjs +42 -0
- package/src/messaging/message.cjs +136 -0
- package/src/messaging/parser.cjs +88 -0
- package/src/messaging/router.cjs +217 -0
- package/src/middleware/permissions.middleware.js +34 -2
- package/src/panel/account-profile.cjs +489 -0
- package/src/panel/fallback-page.cjs +177 -0
- package/src/panel/password-reset.cjs +13 -14
- package/src/panel/recovery-email.cjs +3 -1
- package/src/routes/backup.api.esm.js +175 -0
- package/src/routes/dashboard.api.esm.js +183 -7
- package/src/routes/plus.api.esm.js +172 -0
- package/src/routes/reactions.api.esm.js +60 -0
- package/src/services/aiAgent.cjs +56 -43
- package/src/services/aiDocs.cjs +187 -0
- package/src/services/aiImage.cjs +183 -0
- package/src/services/aiMedia.cjs +146 -0
- package/src/services/aiProviders.cjs +76 -80
- package/src/services/aiRouter.cjs +2 -2
- package/src/services/aiToolAuth.cjs +76 -4
- package/src/services/aiTools.cjs +431 -105
- package/src/services/autoDelete.cjs +9 -1
- package/src/services/levix-docs.generated.json +2623 -0
- package/src/services/memoryPolicy.cjs +56 -0
- package/src/services/updateCheck.cjs +362 -0
- package/src/stickers/errors.cjs +53 -97
- package/src/stickers/library.cjs +13 -6
- package/src/stickers/owner.cjs +1 -1
- package/src/utils/commandContext.cjs +13 -0
- package/src/utils/commandDocs.cjs +42 -20
- package/src/utils/geminiHttpOptions.cjs +56 -0
- package/src/utils/geminiMedia.cjs +28 -3
- package/src/utils/groupMetadataCache.cjs +386 -21
- package/src/utils/httpsFetch.cjs +113 -0
- package/src/utils/i18n.cjs +117 -7
- package/src/utils/linkPreview.cjs +90 -0
- package/src/utils/logger.cjs +6 -0
- package/src/utils/markdownParser.cjs +3 -3
- package/src/utils/memory.cjs +375 -28
- package/src/utils/messageContent.cjs +7 -1
- package/src/utils/moderationAction.cjs +116 -73
- package/src/utils/moderationSummary.cjs +32 -56
- package/src/utils/permissionLevel.cjs +7 -28
- package/src/utils/permissions.cjs +23 -14
- package/src/utils/permissions.esm.js +99 -166
- package/src/utils/recentMessageCache.esm.js +241 -37
- package/src/utils/recurrence.cjs +78 -66
- package/src/utils/sendBotMessage.cjs +14 -29
- package/src/utils/sendBotMessage.esm.js +28 -13
- package/src/utils/statusMessage.cjs +94 -33
- package/src/utils/stickerBot.cjs +18 -16
- package/src/utils/storage.cjs +2 -0
- package/src/utils/storage.esm.js +2 -0
- package/src/utils/thumbnail.cjs +5 -21
- package/public/dashboard/assets/index-B1sOW2VX.js +0 -561
- package/public/dashboard/assets/index-CK8osB4o.css +0 -2
- package/public/dashboard.css +0 -3127
- package/public/dashboard.js +0 -1981
- package/public/gateway-i18n.js +0 -321
- package/public/gateway.js +0 -70
- package/views/dashboard.ejs +0 -775
- package/views/login.ejs +0 -408
- package/views/qr.ejs +0 -155
- package/views/setup.ejs +0 -118
package/src/core/connection.js
CHANGED
|
@@ -22,7 +22,28 @@ import { groupMetadataCache } from "./socket.js";
|
|
|
22
22
|
|
|
23
23
|
const require = createRequire(import.meta.url);
|
|
24
24
|
const logger = require("../utils/logger.cjs");
|
|
25
|
+
const {
|
|
26
|
+
captureRosterCursor,
|
|
27
|
+
storeBulkGroupMetadata,
|
|
28
|
+
} = require("../utils/groupMetadataCache.cjs");
|
|
25
29
|
const { primePermissions } = require("../utils/permissions.cjs");
|
|
30
|
+
const { activatePendingIfMatching } = require("../backup/activation.cjs");
|
|
31
|
+
const { maskAccount } = require("../backup/account.cjs");
|
|
32
|
+
|
|
33
|
+
function resetActivatedAccountCaches() {
|
|
34
|
+
for (const reset of [
|
|
35
|
+
() => require("../commands/gemini.cjs").clearAllContextBuffers(),
|
|
36
|
+
() => groupMetadataCache.flushAll(),
|
|
37
|
+
() => bootstrapOwners([]),
|
|
38
|
+
() => bootstrapAdmins([]),
|
|
39
|
+
() => require("../../scheduler.cjs").stopAllScheduledJobs(),
|
|
40
|
+
() => require("../db/store.cjs").invalidateAutoDeleteRuleCache(),
|
|
41
|
+
() => require("../stickers/library.cjs").resetForActivation(),
|
|
42
|
+
]) {
|
|
43
|
+
try { reset(); } catch { logger.error("[Backup] Could not reset an account cache"); }
|
|
44
|
+
}
|
|
45
|
+
lastFullGroupSyncAt = 0;
|
|
46
|
+
}
|
|
26
47
|
|
|
27
48
|
/**
|
|
28
49
|
* WhatsApp answers a `<failure reason="405">` when it refuses the connection
|
|
@@ -105,6 +126,43 @@ export function classifyDisconnect(statusCode) {
|
|
|
105
126
|
};
|
|
106
127
|
}
|
|
107
128
|
|
|
129
|
+
/**
|
|
130
|
+
* The facts inside a close, for the log. Baileys wraps a WebSocket error in a
|
|
131
|
+
* Boom whose default status is 500 and whose payload says only "Internal
|
|
132
|
+
* Server Error"; the useful part (a TLS, DNS or socket error) rides in
|
|
133
|
+
* `error.data`. No message content is ever in here.
|
|
134
|
+
*
|
|
135
|
+
* @param {any} error - `lastDisconnect.error`
|
|
136
|
+
*/
|
|
137
|
+
export function describeCloseError(error) {
|
|
138
|
+
if (!error || typeof error !== "object") return { detail: String(error ?? "") };
|
|
139
|
+
const cause = (e) =>
|
|
140
|
+
e && typeof e === "object"
|
|
141
|
+
? {
|
|
142
|
+
name: e.name,
|
|
143
|
+
detail: e.message,
|
|
144
|
+
code: e.code,
|
|
145
|
+
errno: e.errno,
|
|
146
|
+
syscall: e.syscall,
|
|
147
|
+
address: e.address,
|
|
148
|
+
port: e.port,
|
|
149
|
+
hostname: e.hostname,
|
|
150
|
+
reason: e.reason,
|
|
151
|
+
cause: e.cause?.message ?? e.cause?.code,
|
|
152
|
+
}
|
|
153
|
+
: e;
|
|
154
|
+
return {
|
|
155
|
+
name: error.name,
|
|
156
|
+
detail: error.message,
|
|
157
|
+
statusCode: error.output?.statusCode,
|
|
158
|
+
payload: error.output?.payload?.error,
|
|
159
|
+
data: cause(error.data),
|
|
160
|
+
cause: cause(error.cause),
|
|
161
|
+
stack:
|
|
162
|
+
typeof error.stack === "string" ? error.stack.split("\n").slice(0, 6).join("\n") : undefined,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
108
166
|
/**
|
|
109
167
|
* Everything that has to happen the moment a connection is open: record the
|
|
110
168
|
* paired account as an owner, seed the permission rosters, (re)start the
|
|
@@ -113,7 +171,12 @@ export function classifyDisconnect(statusCode) {
|
|
|
113
171
|
* @param {object} sock
|
|
114
172
|
* @param {(sock: object) => void} [initializeScheduledJobs]
|
|
115
173
|
*/
|
|
116
|
-
export
|
|
174
|
+
export function handleConnectionOpen(sock, initializeScheduledJobs) {
|
|
175
|
+
activatePendingIfMatching(resetActivatedAccountCaches);
|
|
176
|
+
return finishConnectionOpen(sock, initializeScheduledJobs);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function finishConnectionOpen(sock, initializeScheduledJobs) {
|
|
117
180
|
logger.info("✅ Connection opened successfully!");
|
|
118
181
|
|
|
119
182
|
// The paired account is always an owner — that's the whole bootstrap.
|
|
@@ -137,7 +200,7 @@ export async function handleConnectionOpen(sock, initializeScheduledJobs) {
|
|
|
137
200
|
.flatMap((user) => [user.jid, user.lid])
|
|
138
201
|
.filter(Boolean);
|
|
139
202
|
|
|
140
|
-
bootstrapOwners(ownersRoster);
|
|
203
|
+
bootstrapOwners(ownersRoster, [sock?.user?.id, sock?.user?.lid].filter(Boolean));
|
|
141
204
|
bootstrapAdmins(adminsRoster);
|
|
142
205
|
|
|
143
206
|
await primePermissions();
|
|
@@ -173,9 +236,7 @@ async function saveBotOwnerMetadata(sock) {
|
|
|
173
236
|
isOwner: true,
|
|
174
237
|
displayName,
|
|
175
238
|
});
|
|
176
|
-
logger.info(
|
|
177
|
-
`[User Metadata] Saved bot owner: ${phoneNumber} (JID: ${userJid}, LID: ${userLid})`,
|
|
178
|
-
);
|
|
239
|
+
logger.info(`[User Metadata] Saved bot owner: ${maskAccount({ pn: userJid })}`);
|
|
179
240
|
}
|
|
180
241
|
} catch (err) {
|
|
181
242
|
logger.error({ err }, "[Error] Failed to save bot owner metadata");
|
|
@@ -196,21 +257,28 @@ let lastFullGroupSyncAt = 0;
|
|
|
196
257
|
|
|
197
258
|
export async function cacheAllGroups(sock, { force = false, now = Date.now() } = {}) {
|
|
198
259
|
if (!force && lastFullGroupSyncAt && now - lastFullGroupSyncAt < GROUP_SYNC_TTL_MS) {
|
|
199
|
-
logger.info(
|
|
260
|
+
logger.info(
|
|
261
|
+
"[Cache] Recent full group sync is still fresh — skipping groupFetchAllParticipating",
|
|
262
|
+
);
|
|
200
263
|
return;
|
|
201
264
|
}
|
|
202
265
|
|
|
203
266
|
logger.info("[Cache] Fetching and caching metadata for all groups...");
|
|
204
267
|
|
|
268
|
+
// Captured before the request. A roster event during the round-trip keeps
|
|
269
|
+
// that group's newer refresh; this answer must not mark the old roster fresh.
|
|
270
|
+
const cursor = captureRosterCursor(now);
|
|
271
|
+
|
|
205
272
|
try {
|
|
206
273
|
const groups = await sock.groupFetchAllParticipating();
|
|
207
274
|
let cachedCount = 0;
|
|
208
275
|
|
|
209
276
|
for (const jid in groups) {
|
|
210
|
-
|
|
277
|
+
const stored = storeBulkGroupMetadata(jid, groups[jid], cursor);
|
|
278
|
+
if (!stored) continue;
|
|
211
279
|
upsertGroupDirectory(jid, {
|
|
212
|
-
subject:
|
|
213
|
-
participantCount:
|
|
280
|
+
subject: stored.subject || null,
|
|
281
|
+
participantCount: stored.participants?.length ?? null,
|
|
214
282
|
});
|
|
215
283
|
cachedCount++;
|
|
216
284
|
}
|
package/src/core/events.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createRequire } from "module";
|
|
2
|
+
import { createReactionAutomation } from "../automation/engine.js";
|
|
2
3
|
import {
|
|
3
4
|
handleGroupJoinRequests,
|
|
4
5
|
handleGroupParticipantsUpdate,
|
|
@@ -9,10 +10,14 @@ import {
|
|
|
9
10
|
storeLidPnMappings,
|
|
10
11
|
upsertGroupDirectory,
|
|
11
12
|
} from "../utils/storage.esm.js";
|
|
12
|
-
import { groupMetadataCache } from "./socket.js";
|
|
13
13
|
|
|
14
14
|
const require = createRequire(import.meta.url);
|
|
15
15
|
const logger = require("../utils/logger.cjs");
|
|
16
|
+
const {
|
|
17
|
+
markRosterChanged,
|
|
18
|
+
mergePartialGroupMetadata,
|
|
19
|
+
storeUnsolicitedFullRoster,
|
|
20
|
+
} = require("../utils/groupMetadataCache.cjs");
|
|
16
21
|
|
|
17
22
|
/**
|
|
18
23
|
* Wire one socket up.
|
|
@@ -31,21 +36,50 @@ export function setupEventListeners(
|
|
|
31
36
|
sock,
|
|
32
37
|
{ saveCreds, onConnectionUpdate, onHandshakeRejected } = {},
|
|
33
38
|
) {
|
|
34
|
-
|
|
39
|
+
const automation = createReactionAutomation(sock);
|
|
40
|
+
const reactions = automation.intake;
|
|
41
|
+
// One disposer removes exactly the callbacks this call registered. Baileys
|
|
42
|
+
// also listens on creds.update and lid-mapping.update, so this must not use
|
|
43
|
+
// removeAllListeners by event name. A callback already queued by contained()
|
|
44
|
+
// sees `disposed` and returns without calling its handler.
|
|
45
|
+
let disposed = false;
|
|
46
|
+
const bindings = [];
|
|
47
|
+
const listen = (emitter, event, fn) => {
|
|
48
|
+
if (!emitter || typeof emitter.on !== "function") return;
|
|
49
|
+
emitter.on(event, fn);
|
|
50
|
+
bindings.push([emitter, event, fn]);
|
|
51
|
+
};
|
|
52
|
+
// Async listeners go through this. A bare `async` listener hands its
|
|
35
53
|
// promise to Baileys' emitter, which drops it — so one rejection anywhere in
|
|
36
54
|
// the message pipeline becomes an unhandledRejection, and src/index.js turns
|
|
37
55
|
// that into a process exit. A group lookup failing mid-disconnect is exactly
|
|
38
56
|
// the case the reconnect ladder exists to survive, so it must not be fatal.
|
|
39
|
-
const contained = (label, handler) => (
|
|
57
|
+
const contained = (label, handler) => (...args) => {
|
|
40
58
|
Promise.resolve()
|
|
41
|
-
.then(() =>
|
|
59
|
+
.then(() => {
|
|
60
|
+
if (disposed) return;
|
|
61
|
+
return handler(...args);
|
|
62
|
+
})
|
|
42
63
|
.catch((err) => logger.error({ err }, `[Events] ${label} handler failed`));
|
|
43
64
|
};
|
|
65
|
+
// Intake is synchronous and bounded; do not queue a payload-capturing
|
|
66
|
+
// microtask per reaction in a storm. Keep existing async routing contained.
|
|
67
|
+
const intake = (label, handler) => (...args) => {
|
|
68
|
+
if (disposed) return;
|
|
69
|
+
try {
|
|
70
|
+
return handler(...args);
|
|
71
|
+
} catch {
|
|
72
|
+
reactions.close();
|
|
73
|
+
logger.warn({ event: label }, "[Events] reaction intake failed; paused until reconnect");
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const connectionUpdate = contained("connection.update", (update) => onConnectionUpdate?.(update));
|
|
44
77
|
|
|
45
|
-
sock.ev.
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
78
|
+
listen(sock.ev, "connection.update", (update) => {
|
|
79
|
+
if (update?.connection === "open") reactions.open();
|
|
80
|
+
if (update?.connection === "close") reactions.close();
|
|
81
|
+
connectionUpdate(update);
|
|
82
|
+
});
|
|
49
83
|
|
|
50
84
|
// A handshake the server answered with an HTTP status instead of an upgrade.
|
|
51
85
|
//
|
|
@@ -56,7 +90,8 @@ export function setupEventListeners(
|
|
|
56
90
|
// that guard and then does nothing with the event. A proxy answering 407
|
|
57
91
|
// therefore produced no error, no close and no connection.update at all: the
|
|
58
92
|
// session simply sat in `starting` until somebody noticed.
|
|
59
|
-
|
|
93
|
+
listen(
|
|
94
|
+
sock.ws,
|
|
60
95
|
"unexpected-response",
|
|
61
96
|
contained("unexpected-response", (_request, response) => {
|
|
62
97
|
onHandshakeRejected?.(response);
|
|
@@ -64,7 +99,7 @@ export function setupEventListeners(
|
|
|
64
99
|
);
|
|
65
100
|
|
|
66
101
|
// Save credentials
|
|
67
|
-
if (saveCreds) sock.ev
|
|
102
|
+
if (saveCreds) listen(sock.ev, "creds.update", saveCreds);
|
|
68
103
|
|
|
69
104
|
// Address-book names (`name`) and push names (`notify`) from the linked phone.
|
|
70
105
|
// No history sync — these events are the live contact list WhatsApp already
|
|
@@ -73,11 +108,11 @@ export function setupEventListeners(
|
|
|
73
108
|
const list = Array.isArray(contacts) ? contacts : contacts ? [contacts] : [];
|
|
74
109
|
for (const contact of list) applyContactRecord(contact);
|
|
75
110
|
};
|
|
76
|
-
sock.ev
|
|
77
|
-
sock.ev
|
|
111
|
+
listen(sock.ev, "contacts.upsert", contained("contacts.upsert", persistContacts));
|
|
112
|
+
listen(sock.ev, "contacts.update", contained("contacts.update", persistContacts));
|
|
78
113
|
|
|
79
114
|
// V7: LID mapping updates
|
|
80
|
-
sock.ev
|
|
115
|
+
listen(sock.ev, "lid-mapping.update", (updates) => {
|
|
81
116
|
logger.info(`[LID] Received ${Object.keys(updates).length} LID mapping updates`);
|
|
82
117
|
const mappings = [];
|
|
83
118
|
for (const [lid, pn] of Object.entries(updates)) {
|
|
@@ -90,24 +125,40 @@ export function setupEventListeners(
|
|
|
90
125
|
}
|
|
91
126
|
});
|
|
92
127
|
|
|
93
|
-
// Group metadata updates
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
128
|
+
// Group metadata updates. A participants array is a full roster (Baileys
|
|
129
|
+
// emits that from its groups dirty refresh). The request that produced it
|
|
130
|
+
// started at an unknown time, so it is stored only when this group had no
|
|
131
|
+
// roster event in the last 10 minutes. A newer event makes it a refresh
|
|
132
|
+
// hint. Anything else is a partial patch: merge it, keep the remaining
|
|
133
|
+
// TTL, and never invent an entry.
|
|
134
|
+
listen(sock.ev, "groups.upsert", (updates) => {
|
|
135
|
+
for (const group of updates || []) {
|
|
136
|
+
if (!group?.id) continue;
|
|
137
|
+
const stored = storeUnsolicitedFullRoster(sock, group.id, group);
|
|
138
|
+
if (!stored) continue;
|
|
139
|
+
logger.info("[Cache] Caching group metadata");
|
|
140
|
+
logger.debugOnly.info({ group: group.id }, "[Cache] Caching group metadata");
|
|
98
141
|
upsertGroupDirectory(group.id, {
|
|
99
|
-
subject:
|
|
100
|
-
participantCount:
|
|
142
|
+
subject: stored.subject || null,
|
|
143
|
+
participantCount: stored.participants?.length ?? null,
|
|
101
144
|
});
|
|
102
145
|
}
|
|
103
146
|
});
|
|
104
147
|
|
|
105
|
-
sock.ev
|
|
148
|
+
listen(sock.ev, "groups.update", (updates) => {
|
|
149
|
+
for (const group of updates || []) if (group?.id) automation.cancelChat(group.id);
|
|
106
150
|
for (const group of updates || []) {
|
|
107
151
|
if (!group?.id) continue;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
152
|
+
if (Array.isArray(group.participants)) {
|
|
153
|
+
const stored = storeUnsolicitedFullRoster(sock, group.id, group);
|
|
154
|
+
if (!stored) continue;
|
|
155
|
+
upsertGroupDirectory(group.id, {
|
|
156
|
+
subject: stored.subject || null,
|
|
157
|
+
participantCount: stored.participants?.length ?? null,
|
|
158
|
+
});
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const next = mergePartialGroupMetadata(group.id, group) || group;
|
|
111
162
|
upsertGroupDirectory(group.id, {
|
|
112
163
|
subject: next.subject || null,
|
|
113
164
|
participantCount: next.participants?.length ?? null,
|
|
@@ -116,20 +167,55 @@ export function setupEventListeners(
|
|
|
116
167
|
});
|
|
117
168
|
|
|
118
169
|
// Incoming messages
|
|
119
|
-
sock.ev.
|
|
120
|
-
"messages.upsert",
|
|
121
|
-
contained("messages.upsert", (
|
|
122
|
-
);
|
|
170
|
+
listen(sock.ev, "messages.upsert", (m) => {
|
|
171
|
+
intake("messages.upsert", (payload) => reactions.observeUpsert(payload))(m);
|
|
172
|
+
contained("messages.upsert", (payload) => handleIncomingMessage(sock, payload))(m);
|
|
173
|
+
});
|
|
123
174
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
"
|
|
127
|
-
|
|
175
|
+
listen(
|
|
176
|
+
sock.ev,
|
|
177
|
+
"messages.reaction",
|
|
178
|
+
intake("messages.reaction", (entries) => reactions.observeReactions(entries)),
|
|
179
|
+
);
|
|
180
|
+
listen(
|
|
181
|
+
sock.ev,
|
|
182
|
+
"messages.update",
|
|
183
|
+
intake("messages.update", (entries) => reactions.observeUpdates(entries)),
|
|
128
184
|
);
|
|
185
|
+
listen(
|
|
186
|
+
sock.ev,
|
|
187
|
+
"messages.delete",
|
|
188
|
+
intake("messages.delete", (payload) => reactions.observeDeletes(payload)),
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
// Group participant updates. Mark the roster stale in this turn, before the
|
|
192
|
+
// contained handler's microtask and before any await. Every action (add,
|
|
193
|
+
// remove, promote, demote, modify, leave) can change who must receive a
|
|
194
|
+
// sender key. Pending reaction actions for the group are cancelled in the
|
|
195
|
+
// same turn, because membership and roles may have changed.
|
|
196
|
+
listen(sock.ev, "group-participants.update", (update) => {
|
|
197
|
+
markRosterChanged(sock, update?.id);
|
|
198
|
+
if (update?.id) automation.cancelChat(update.id);
|
|
199
|
+
contained(
|
|
200
|
+
"group-participants.update",
|
|
201
|
+
(payload) => handleGroupParticipantsUpdate(sock, payload),
|
|
202
|
+
)(update);
|
|
203
|
+
});
|
|
129
204
|
|
|
130
205
|
// Group join requests
|
|
131
|
-
|
|
206
|
+
listen(
|
|
207
|
+
sock.ev,
|
|
132
208
|
"group-requests.update",
|
|
133
209
|
contained("group-requests.update", (events) => handleGroupJoinRequests(sock, events)),
|
|
134
210
|
);
|
|
211
|
+
|
|
212
|
+
return () => {
|
|
213
|
+
disposed = true;
|
|
214
|
+
for (const [emitter, event, fn] of bindings) {
|
|
215
|
+
try {
|
|
216
|
+
emitter.off?.(event, fn);
|
|
217
|
+
} catch {}
|
|
218
|
+
}
|
|
219
|
+
bindings.length = 0;
|
|
220
|
+
};
|
|
135
221
|
}
|