kage-baileys 3.0.6 → 3.2.4
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/Example/connection.cjs +4 -0
- package/README.MD +4726 -48
- package/README.md +5941 -0
- package/WAProto/WAProto.proto +3395 -415
- package/WAProto/compat.cjs +12 -0
- package/WAProto/index.d.ts +47104 -19888
- package/WAProto/index.js +131979 -57361
- package/lib/Addon/http-error.js +43 -0
- package/lib/Addon/index.js +32 -0
- package/lib/Addon/keyed-db.js +250 -0
- package/lib/Addon/link-preview.js +82 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/connection.js +110 -0
- package/lib/Defaults/constants.js +156 -0
- package/lib/Defaults/history.js +46 -0
- package/lib/Defaults/index.js +230 -0
- package/lib/Defaults/media.js +104 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Defaults/prefix.js +44 -0
- package/lib/Extensions/business-advanced.js +382 -0
- package/lib/Extensions/business.js +159 -0
- package/lib/Extensions/chats-advanced.js +1106 -0
- package/lib/Extensions/chats.js +349 -0
- package/lib/Extensions/communities-advanced.js +430 -0
- package/lib/Extensions/community.js +94 -0
- package/lib/Extensions/groups-advanced.js +343 -0
- package/lib/Extensions/index.js +30 -0
- package/lib/Extensions/messages-recv-advanced.js +1690 -0
- package/lib/Extensions/messages-send-advanced.js +622 -0
- package/lib/Extensions/messages-send.js +1380 -0
- package/lib/Extensions/newsletter-advanced.js +228 -0
- package/lib/Extensions/newsletter.js +72 -0
- package/lib/Extensions/socket.js +389 -0
- package/lib/Modded/message_builder.js +2084 -4
- package/lib/Signal/Group/ciphertext-message.js +54 -0
- package/lib/Signal/Group/group-session-builder.js +124 -0
- package/lib/Signal/Group/group_cipher.js +216 -0
- package/lib/Signal/Group/index.js +124 -0
- package/lib/Signal/Group/keyhelper.js +108 -0
- package/lib/Signal/Group/sender-chain-key.js +83 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +157 -0
- package/lib/Signal/Group/sender-key-message.js +166 -0
- package/lib/Signal/Group/sender-key-name.js +130 -0
- package/lib/Signal/Group/sender-key-record.js +113 -0
- package/lib/Signal/Group/sender-key-state.js +208 -0
- package/lib/Signal/Group/sender-message-key.js +82 -0
- package/lib/Signal/libsignal.js +935 -0
- package/lib/Signal/lid-mapping.js +545 -1
- package/lib/Socket/Client/index.js +60 -0
- package/lib/Socket/Client/types.js +51 -0
- package/lib/Socket/Client/websocket.js +152 -0
- package/lib/Socket/business.js +831 -0
- package/lib/Socket/chats.js +2535 -49
- package/lib/Socket/communities.js +450 -1
- package/lib/Socket/community.js +1022 -0
- package/lib/Socket/dugong.js +1 -1
- package/lib/Socket/groups.js +747 -0
- package/lib/Socket/index.js +60 -2
- package/lib/Socket/messages-recv.js +3450 -5
- package/lib/Socket/messages-send.js +3123 -0
- package/lib/Socket/mex.js +126 -1
- package/lib/Socket/newsletter.js +465 -0
- package/lib/Socket/socket.js +2217 -1
- package/lib/Store/index.js +79 -0
- package/lib/Store/make-cache-manager-store.js +186 -0
- package/lib/Store/make-in-memory-store.js +878 -0
- package/lib/Store/make-ordered-dictionary.js +187 -0
- package/lib/Store/object-repository.js +78 -0
- package/lib/Types/Auth.js +25 -0
- package/lib/Types/Bussines.js +25 -1
- package/lib/Types/Call.js +25 -0
- package/lib/Types/Chat.js +39 -0
- package/lib/Types/Contact.js +25 -0
- package/lib/Types/Events.js +25 -0
- package/lib/Types/GroupMetadata.js +25 -0
- package/lib/Types/Label.js +76 -0
- package/lib/Types/LabelAssociation.js +40 -0
- package/lib/Types/Message.js +57 -0
- package/lib/Types/Mex.js +56 -1
- package/lib/Types/MexUpdates.js +46 -0
- package/lib/Types/Newsletter.js +56 -0
- package/lib/Types/Product.js +25 -0
- package/lib/Types/RichType.js +23 -0
- package/lib/Types/Signal.js +25 -0
- package/lib/Types/Socket.js +25 -0
- package/lib/Types/State.js +92 -0
- package/lib/Types/USync.js +25 -0
- package/lib/Types/index.js +133 -0
- package/lib/Utils/auth-utils.js +703 -0
- package/lib/Utils/automation-tools.js +51 -16
- package/lib/Utils/baileys-event-stream.js +146 -0
- package/lib/Utils/browser-utils.js +95 -0
- package/lib/Utils/business.js +525 -0
- package/lib/Utils/chat-utils.js +1861 -0
- package/lib/Utils/companion-reg-client-utils.js +55 -1
- package/lib/Utils/crypto.js +355 -0
- package/lib/Utils/decode-wa-message.js +709 -0
- package/lib/Utils/event-buffer.js +1323 -8
- package/lib/Utils/generics.js +945 -1
- package/lib/Utils/history.js +266 -0
- package/lib/Utils/identity-change-handler.js +62 -1
- package/lib/Utils/index.js +114 -0
- package/lib/Utils/link-preview.js +179 -0
- package/lib/Utils/logger.js +42 -0
- package/lib/Utils/lt-hash.js +73 -0
- package/lib/Utils/make-mutex.js +104 -0
- package/lib/Utils/message-inspector.js +110 -0
- package/lib/Utils/message-retry-manager.js +507 -2
- package/lib/Utils/messages-media.js +1931 -1
- package/lib/Utils/messages.js +3748 -0
- package/lib/Utils/noise-handler.js +423 -0
- package/lib/Utils/offline-node-processor.js +53 -1
- package/lib/Utils/pre-key-manager.js +255 -1
- package/lib/Utils/process-message.js +1592 -0
- package/lib/Utils/reporting-utils.js +272 -1
- package/lib/Utils/rich-message-utils.js +377 -0
- package/lib/Utils/rich-messages.js +1 -1
- package/lib/Utils/signal.js +437 -0
- package/lib/Utils/stanza-ack.js +51 -1
- package/lib/Utils/status-refresh.js +37 -0
- package/lib/Utils/status-tools.js +4 -2
- package/lib/Utils/sync-action-utils.js +63 -1
- package/lib/Utils/tc-token-utils.js +185 -1
- package/lib/Utils/terminal-banner.js +31 -0
- package/lib/Utils/use-multi-file-auth-state.js +271 -0
- package/lib/Utils/use-single-file-auth-state.js +108 -0
- package/lib/Utils/use-sqlite-auth-state.js +142 -0
- package/lib/Utils/validate-connection.js +497 -1
- package/lib/Utils/view-once-compat.js +13 -5
- package/lib/VoIP/audio-feeder.js +150 -0
- package/lib/VoIP/index.js +289 -0
- package/lib/VoIP/relay-transport.js +608 -0
- package/lib/VoIP/signaling.js +583 -0
- package/lib/VoIP/types.js +30 -0
- package/lib/VoIP/wasm-engine.js +1261 -2
- package/lib/VoIP/worker-bootstrap.js +1090 -2
- package/lib/WABinary/constants.js +2805 -0
- package/lib/WABinary/decode.js +649 -2
- package/lib/WABinary/encode.js +564 -2
- package/lib/WABinary/generic-utils.js +401 -0
- package/lib/WABinary/index.js +82 -0
- package/lib/WABinary/jid-utils.js +290 -0
- package/lib/WABinary/types.js +25 -0
- package/lib/WAM/BinaryInfo.js +50 -0
- package/lib/WAM/constants.js +45747 -0
- package/lib/WAM/encode.js +393 -0
- package/lib/WAM/index.js +78 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +135 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +110 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +142 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +85 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +69 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +107 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +38 -1
- package/lib/WAUSync/Protocols/index.js +83 -0
- package/lib/WAUSync/USyncQuery.js +241 -2
- package/lib/WAUSync/USyncUser.js +86 -0
- package/lib/WAUSync/index.js +78 -0
- package/lib/assets/wasm/loader.js +1875 -1
- package/lib/assets/wasm/worker-modules.js +11047 -1
- package/lib/index.d.ts +126 -0
- package/lib/index.js +53 -21
- package/lib/index.mjs +437 -0
- package/licenses/upstream-MIT.txt +22 -0
- package/maintenance/api-coverage.json +1642 -0
- package/maintenance/archivos-sha256.json +207 -0
- package/maintenance/consumer-validation.log +3 -0
- package/maintenance/dependency-audit.json +4 -4
- package/maintenance/extension-inventory.json +57 -0
- package/maintenance/protocol-update.json +1101 -0
- package/maintenance/protocol-wire-audit.json +4 -0
- package/maintenance/reference-socket-surface.json +209 -0
- package/maintenance/resumen-validacion.json +50 -0
- package/maintenance/validation.log +2725 -0
- package/maintenance/verification.json +566 -0
- package/package.json +107 -9
- package/scripts/check-imports.cjs +10 -0
- package/scripts/check-package.cjs +40 -0
- package/scripts/check-syntax.cjs +1 -1
- package/scripts/generate-protobuf.cjs +6 -1
- package/scripts/generate-public-types.cjs +9 -0
- package/scripts/kage-base64-fix.cjs +45 -0
- package/scripts/maintenance.cjs +10 -0
- package/test/advanced.test.cjs +24 -0
- package/test/base64-fix.test.cjs +49 -0
- package/test/maintenance.test.cjs +1 -1
- package/test/npm-publication.test.cjs +141 -0
- package/test/recepcion-reparada.test.cjs +98 -0
- package/test/resilience-binary.test.cjs +69 -0
- package/test/resilience-transport.test.cjs +34 -0
- package/test/types.test.ts +5 -0
- package/test/upgrade.test.cjs +104 -0
- package/tsconfig.json +20 -0
- package/WAProto/p.html +0 -1
- package/lib/KageExtra/Modded/message_builder.js +0 -2330
- package/lib/KageExtra/VoIP/audio-feeder.js +0 -136
- package/lib/KageExtra/VoIP/index.js +0 -282
- package/lib/KageExtra/VoIP/relay-transport.js +0 -559
- package/lib/KageExtra/VoIP/signaling.js +0 -603
- package/lib/KageExtra/VoIP/types.js +0 -17
- package/lib/KageExtra/VoIP/wasm-engine.js +0 -1214
- package/lib/KageExtra/VoIP/worker-bootstrap.js +0 -1042
- package/lib/KageExtra/assets/wasm/loader.js +0 -5
- package/lib/KageExtra/assets/wasm/whatsapp.wasm +0 -0
- package/lib/KageExtra/assets/wasm/worker-modules.js +0 -273
- package/lib/KageExtra/index.js +0 -3
- package/lib/KageExtra/package.json +0 -1
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -53
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -24
- package/maintenance/revision-full-check.txt +0 -1097
- package/maintenance/revision-library-tests.tap +0 -1088
- package/maintenance/revision-protobuf.txt +0 -5
- package/maintenance/revision-syntax.txt +0 -5
- package/maintenance/schema-coverage.json +0 -23
- package/maintenance/schema-merge.json +0 -30
- package/maintenance/validation-node20.txt +0 -191
- package/maintenance/validation-node22.txt +0 -237
package/lib/Utils/signal.js
CHANGED
|
@@ -151,3 +151,440 @@ const getNextPreKeysNode = async (state, count) => {
|
|
|
151
151
|
return { update, node };
|
|
152
152
|
};
|
|
153
153
|
exports.getNextPreKeysNode = getNextPreKeysNode;
|
|
154
|
+
|
|
155
|
+
// Kage: compatibilidad de API.
|
|
156
|
+
const __kageModule78 = (() => {
|
|
157
|
+
const module = { exports: {} }; const exports = module.exports;
|
|
158
|
+
"use strict"
|
|
159
|
+
|
|
160
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
161
|
+
|
|
162
|
+
const { KEY_BUNDLE_TYPE } = require("../Defaults/constants")
|
|
163
|
+
const {
|
|
164
|
+
S_WHATSAPP_NET,
|
|
165
|
+
getBinaryNodeChild,
|
|
166
|
+
getBinaryNodeChildren,
|
|
167
|
+
getBinaryNodeChildUInt,
|
|
168
|
+
getBinaryNodeChildBuffer,
|
|
169
|
+
assertNodeErrorFree,
|
|
170
|
+
jidDecode,
|
|
171
|
+
getServerFromDomainType,
|
|
172
|
+
WAJIDDomains,
|
|
173
|
+
} = require("../WABinary")
|
|
174
|
+
const {
|
|
175
|
+
Curve,
|
|
176
|
+
generateSignalPubKey
|
|
177
|
+
} = require("./crypto")
|
|
178
|
+
const { encodeBigEndian } = require("./generics")
|
|
179
|
+
|
|
180
|
+
function chunk(array, size) {
|
|
181
|
+
const chunks = []
|
|
182
|
+
for (let i = 0; i < array.length; i += size) {
|
|
183
|
+
chunks.push(array.slice(i, i + size))
|
|
184
|
+
}
|
|
185
|
+
return chunks
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const createSignalIdentity = (wid, accountSignatureKey) => {
|
|
189
|
+
return {
|
|
190
|
+
identifier: { name: wid, deviceId: 0 },
|
|
191
|
+
identifierKey: generateSignalPubKey(accountSignatureKey)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const getPreKeys = async ({ get }, min, limit) => {
|
|
196
|
+
const idList = []
|
|
197
|
+
for (let id = min; id < limit; id++) {
|
|
198
|
+
idList.push(id.toString())
|
|
199
|
+
}
|
|
200
|
+
return get('pre-key', idList)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const generateOrGetPreKeys = (creds, range) => {
|
|
204
|
+
const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId
|
|
205
|
+
const remaining = range - avaliable
|
|
206
|
+
const lastPreKeyId = creds.nextPreKeyId + remaining - 1
|
|
207
|
+
const newPreKeys = {}
|
|
208
|
+
if (remaining > 0) {
|
|
209
|
+
for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) {
|
|
210
|
+
newPreKeys[i] = Curve.generateKeyPair()
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
newPreKeys,
|
|
215
|
+
lastPreKeyId,
|
|
216
|
+
preKeysRange: [creds.firstUnuploadedPreKeyId, range],
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const xmppSignedPreKey = (key) => ({
|
|
221
|
+
tag: 'skey',
|
|
222
|
+
attrs: {},
|
|
223
|
+
content: [
|
|
224
|
+
{ tag: 'id', attrs: {}, content: encodeBigEndian(key.keyId, 3) },
|
|
225
|
+
{ tag: 'value', attrs: {}, content: key.keyPair.public },
|
|
226
|
+
{ tag: 'signature', attrs: {}, content: key.signature }
|
|
227
|
+
]
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
const xmppPreKey = (pair, id) => ({
|
|
231
|
+
tag: 'key',
|
|
232
|
+
attrs: {},
|
|
233
|
+
content: [
|
|
234
|
+
{ tag: 'id', attrs: {}, content: encodeBigEndian(id, 3) },
|
|
235
|
+
{ tag: 'value', attrs: {}, content: pair.public }
|
|
236
|
+
]
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
const parseAndInjectE2ESessions = async (node, repository) => {
|
|
240
|
+
const extractKey = (key) => key
|
|
241
|
+
? {
|
|
242
|
+
keyId: getBinaryNodeChildUInt(key, 'id', 3),
|
|
243
|
+
publicKey: generateSignalPubKey(getBinaryNodeChildBuffer(key, 'value')),
|
|
244
|
+
signature: getBinaryNodeChildBuffer(key, 'signature')
|
|
245
|
+
}
|
|
246
|
+
: undefined
|
|
247
|
+
const nodes = getBinaryNodeChildren(getBinaryNodeChild(node, 'list'), 'user')
|
|
248
|
+
|
|
249
|
+
for (const node of nodes) {
|
|
250
|
+
assertNodeErrorFree(node)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Most of the work in repository.injectE2ESession is CPU intensive, not IO
|
|
254
|
+
// So Promise.all doesn't really help here,
|
|
255
|
+
// but blocks even loop if we're using it inside keys.transaction, and it makes it "sync" actually
|
|
256
|
+
// This way we chunk it in smaller parts and between those parts we can yield to the event loop
|
|
257
|
+
// It's rare case when you need to E2E sessions for so many users, but it's possible
|
|
258
|
+
const chunkSize = 100
|
|
259
|
+
const chunks = chunk(nodes, chunkSize)
|
|
260
|
+
|
|
261
|
+
for (const nodesChunk of chunks) {
|
|
262
|
+
for (const node of nodesChunk) {
|
|
263
|
+
const signedKey = getBinaryNodeChild(node, 'skey')
|
|
264
|
+
const key = getBinaryNodeChild(node, 'key')
|
|
265
|
+
const identity = getBinaryNodeChildBuffer(node, 'identity')
|
|
266
|
+
const jid = node.attrs.jid
|
|
267
|
+
const registrationId = getBinaryNodeChildUInt(node, 'registration', 4)
|
|
268
|
+
await repository.injectE2ESession({
|
|
269
|
+
jid,
|
|
270
|
+
session: {
|
|
271
|
+
registrationId: registrationId,
|
|
272
|
+
identityKey: generateSignalPubKey(identity),
|
|
273
|
+
signedPreKey: extractKey(signedKey),
|
|
274
|
+
preKey: extractKey(key)
|
|
275
|
+
}
|
|
276
|
+
})
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const extractDeviceJids = (result, myJid, myLid, excludeZeroDevices) => {
|
|
282
|
+
const { user: myUser, device: myDevice } = jidDecode(myJid)
|
|
283
|
+
const extracted = []
|
|
284
|
+
|
|
285
|
+
for (const userResult of result) {
|
|
286
|
+
const { devices, id } = userResult
|
|
287
|
+
const decoded = jidDecode(id), { user, server } = decoded
|
|
288
|
+
|
|
289
|
+
let { domainType } = decoded
|
|
290
|
+
|
|
291
|
+
const deviceList = devices?.deviceList
|
|
292
|
+
|
|
293
|
+
if (!Array.isArray(deviceList)) continue
|
|
294
|
+
|
|
295
|
+
for (const { id: device, keyIndex, isHosted } of deviceList) {
|
|
296
|
+
if ((!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
|
|
297
|
+
((myUser !== user && myLid !== user) || myDevice !== device) && // either different user or if me user, not this device
|
|
298
|
+
(device === 0 || !!keyIndex) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
|
|
299
|
+
) {
|
|
300
|
+
if (isHosted) {
|
|
301
|
+
domainType = domainType === WAJIDDomains.LID ? WAJIDDomains.HOSTED_LID : WAJIDDomains.HOSTED
|
|
302
|
+
}
|
|
303
|
+
extracted.push({
|
|
304
|
+
user,
|
|
305
|
+
device,
|
|
306
|
+
domainType,
|
|
307
|
+
server: getServerFromDomainType(server, domainType)
|
|
308
|
+
})
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return extracted
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* get the next N keys for upload or processing
|
|
317
|
+
* @param count number of pre-keys to get or generate
|
|
318
|
+
*/
|
|
319
|
+
const getNextPreKeys = async ({ creds, keys }, count) => {
|
|
320
|
+
const { newPreKeys, lastPreKeyId, preKeysRange } = generateOrGetPreKeys(creds, count)
|
|
321
|
+
const update = {
|
|
322
|
+
nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
|
|
323
|
+
firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1)
|
|
324
|
+
}
|
|
325
|
+
await keys.set({ 'pre-key': newPreKeys })
|
|
326
|
+
const preKeys = await getPreKeys(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1])
|
|
327
|
+
return { update, preKeys }
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const getNextPreKeysNode = async (state, count) => {
|
|
331
|
+
const { creds } = state
|
|
332
|
+
const { update, preKeys } = await getNextPreKeys(state, count)
|
|
333
|
+
const node = {
|
|
334
|
+
tag: 'iq',
|
|
335
|
+
attrs: {
|
|
336
|
+
xmlns: 'encrypt',
|
|
337
|
+
type: 'set',
|
|
338
|
+
to: S_WHATSAPP_NET,
|
|
339
|
+
},
|
|
340
|
+
content: [
|
|
341
|
+
{ tag: 'registration', attrs: {}, content: encodeBigEndian(creds.registrationId) },
|
|
342
|
+
{ tag: 'type', attrs: {}, content: KEY_BUNDLE_TYPE },
|
|
343
|
+
{ tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
|
|
344
|
+
{ tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => xmppPreKey(preKeys[+k], +k)) },
|
|
345
|
+
xmppSignedPreKey(creds.signedPreKey)
|
|
346
|
+
]
|
|
347
|
+
}
|
|
348
|
+
return { update, node }
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
module.exports = {
|
|
352
|
+
createSignalIdentity,
|
|
353
|
+
getPreKeys,
|
|
354
|
+
generateOrGetPreKeys,
|
|
355
|
+
xmppSignedPreKey,
|
|
356
|
+
xmppPreKey,
|
|
357
|
+
parseAndInjectE2ESessions,
|
|
358
|
+
extractDeviceJids,
|
|
359
|
+
getNextPreKeys,
|
|
360
|
+
getNextPreKeysNode
|
|
361
|
+
}
|
|
362
|
+
return module.exports;
|
|
363
|
+
})();
|
|
364
|
+
for (const key of Object.keys(__kageModule78)) {
|
|
365
|
+
if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule78[key]});
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Kage: funciones avanzadas.
|
|
369
|
+
const __kageModule184 = (() => {
|
|
370
|
+
const module = { exports: {} }; const exports = module.exports;
|
|
371
|
+
"use strict";
|
|
372
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
373
|
+
exports.getNextPreKeysNode = exports.getNextPreKeys = exports.extractDeviceJids = exports.parseAndInjectE2ESessions = exports.extractE2ESessionFromRetryReceipt = exports.xmppPreKey = exports.xmppSignedPreKey = exports.generateOrGetPreKeys = exports.getPreKeys = exports.createSignalIdentity = void 0;
|
|
374
|
+
const index_js_1 = require("../Defaults/index.js");
|
|
375
|
+
const index_js_2 = require("../WABinary/index.js");
|
|
376
|
+
const crypto_js_1 = require("./crypto.js");
|
|
377
|
+
const generics_js_1 = require("./generics.js");
|
|
378
|
+
function chunk(array, size) {
|
|
379
|
+
const chunks = [];
|
|
380
|
+
for (let i = 0; i < array.length; i += size) {
|
|
381
|
+
chunks.push(array.slice(i, i + size));
|
|
382
|
+
}
|
|
383
|
+
return chunks;
|
|
384
|
+
}
|
|
385
|
+
const createSignalIdentity = (wid, accountSignatureKey) => {
|
|
386
|
+
return {
|
|
387
|
+
identifier: { name: wid, deviceId: 0 },
|
|
388
|
+
identifierKey: (0, crypto_js_1.generateSignalPubKey)(accountSignatureKey)
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
exports.createSignalIdentity = createSignalIdentity;
|
|
392
|
+
const getPreKeys = async ({ get }, min, limit) => {
|
|
393
|
+
const idList = [];
|
|
394
|
+
for (let id = min; id < limit; id++) {
|
|
395
|
+
idList.push(id.toString());
|
|
396
|
+
}
|
|
397
|
+
return get('pre-key', idList);
|
|
398
|
+
};
|
|
399
|
+
exports.getPreKeys = getPreKeys;
|
|
400
|
+
const generateOrGetPreKeys = (creds, range) => {
|
|
401
|
+
const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId;
|
|
402
|
+
const remaining = range - avaliable;
|
|
403
|
+
const lastPreKeyId = creds.nextPreKeyId + remaining - 1;
|
|
404
|
+
const newPreKeys = {};
|
|
405
|
+
if (remaining > 0) {
|
|
406
|
+
for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) {
|
|
407
|
+
newPreKeys[i] = crypto_js_1.Curve.generateKeyPair();
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return {
|
|
411
|
+
newPreKeys,
|
|
412
|
+
lastPreKeyId,
|
|
413
|
+
preKeysRange: [creds.firstUnuploadedPreKeyId, range]
|
|
414
|
+
};
|
|
415
|
+
};
|
|
416
|
+
exports.generateOrGetPreKeys = generateOrGetPreKeys;
|
|
417
|
+
const xmppSignedPreKey = (key) => ({
|
|
418
|
+
tag: 'skey',
|
|
419
|
+
attrs: {},
|
|
420
|
+
content: [
|
|
421
|
+
{ tag: 'id', attrs: {}, content: (0, generics_js_1.encodeBigEndian)(key.keyId, 3) },
|
|
422
|
+
{ tag: 'value', attrs: {}, content: key.keyPair.public },
|
|
423
|
+
{ tag: 'signature', attrs: {}, content: key.signature }
|
|
424
|
+
]
|
|
425
|
+
});
|
|
426
|
+
exports.xmppSignedPreKey = xmppSignedPreKey;
|
|
427
|
+
const xmppPreKey = (pair, id) => ({
|
|
428
|
+
tag: 'key',
|
|
429
|
+
attrs: {},
|
|
430
|
+
content: [
|
|
431
|
+
{ tag: 'id', attrs: {}, content: (0, generics_js_1.encodeBigEndian)(id, 3) },
|
|
432
|
+
{ tag: 'value', attrs: {}, content: pair.public }
|
|
433
|
+
]
|
|
434
|
+
});
|
|
435
|
+
exports.xmppPreKey = xmppPreKey;
|
|
436
|
+
const isValidUInt = (n) => typeof n === 'number' && Number.isInteger(n);
|
|
437
|
+
const extractE2ESessionFromRetryReceipt = (receipt) => {
|
|
438
|
+
const keysNode = (0, index_js_2.getBinaryNodeChild)(receipt, 'keys');
|
|
439
|
+
if (!keysNode)
|
|
440
|
+
return null;
|
|
441
|
+
const typeBuf = (0, index_js_2.getBinaryNodeChildBuffer)(keysNode, 'type');
|
|
442
|
+
if (!typeBuf || typeBuf.length !== 1 || typeBuf[0] !== index_js_1.KEY_BUNDLE_TYPE[0])
|
|
443
|
+
return null;
|
|
444
|
+
const identity = (0, index_js_2.getBinaryNodeChildBuffer)(keysNode, 'identity');
|
|
445
|
+
const skey = (0, index_js_2.getBinaryNodeChild)(keysNode, 'skey');
|
|
446
|
+
if (!identity || identity.length !== 32 || !skey)
|
|
447
|
+
return null;
|
|
448
|
+
const registrationId = (0, index_js_2.getBinaryNodeChildUInt)(receipt, 'registration', 4);
|
|
449
|
+
if (!isValidUInt(registrationId))
|
|
450
|
+
return null;
|
|
451
|
+
const signedPubKey = (0, index_js_2.getBinaryNodeChildBuffer)(skey, 'value');
|
|
452
|
+
const signedSig = (0, index_js_2.getBinaryNodeChildBuffer)(skey, 'signature');
|
|
453
|
+
const signedKeyId = (0, index_js_2.getBinaryNodeChildUInt)(skey, 'id', 3);
|
|
454
|
+
if (!signedPubKey || signedPubKey.length !== 32 || !signedSig || !isValidUInt(signedKeyId)) {
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
const preKeyNode = (0, index_js_2.getBinaryNodeChild)(keysNode, 'key');
|
|
458
|
+
let preKey;
|
|
459
|
+
if (preKeyNode) {
|
|
460
|
+
const preKeyPub = (0, index_js_2.getBinaryNodeChildBuffer)(preKeyNode, 'value');
|
|
461
|
+
const preKeyId = (0, index_js_2.getBinaryNodeChildUInt)(preKeyNode, 'id', 3);
|
|
462
|
+
if (!preKeyPub || preKeyPub.length !== 32 || !isValidUInt(preKeyId)) {
|
|
463
|
+
return null;
|
|
464
|
+
}
|
|
465
|
+
preKey = { keyId: preKeyId, publicKey: (0, crypto_js_1.generateSignalPubKey)(preKeyPub) };
|
|
466
|
+
}
|
|
467
|
+
return {
|
|
468
|
+
registrationId,
|
|
469
|
+
identityKey: (0, crypto_js_1.generateSignalPubKey)(identity),
|
|
470
|
+
signedPreKey: {
|
|
471
|
+
keyId: signedKeyId,
|
|
472
|
+
publicKey: (0, crypto_js_1.generateSignalPubKey)(signedPubKey),
|
|
473
|
+
signature: signedSig
|
|
474
|
+
},
|
|
475
|
+
preKey
|
|
476
|
+
};
|
|
477
|
+
};
|
|
478
|
+
exports.extractE2ESessionFromRetryReceipt = extractE2ESessionFromRetryReceipt;
|
|
479
|
+
const parseAndInjectE2ESessions = async (node, repository) => {
|
|
480
|
+
const extractKey = (key) => key
|
|
481
|
+
? {
|
|
482
|
+
keyId: (0, index_js_2.getBinaryNodeChildUInt)(key, 'id', 3),
|
|
483
|
+
publicKey: (0, crypto_js_1.generateSignalPubKey)((0, index_js_2.getBinaryNodeChildBuffer)(key, 'value')),
|
|
484
|
+
signature: (0, index_js_2.getBinaryNodeChildBuffer)(key, 'signature')
|
|
485
|
+
}
|
|
486
|
+
: undefined;
|
|
487
|
+
const nodes = (0, index_js_2.getBinaryNodeChildren)((0, index_js_2.getBinaryNodeChild)(node, 'list'), 'user');
|
|
488
|
+
for (const node of nodes) {
|
|
489
|
+
(0, index_js_2.assertNodeErrorFree)(node);
|
|
490
|
+
}
|
|
491
|
+
// Most of the work in repository.injectE2ESession is CPU intensive, not IO
|
|
492
|
+
// So Promise.all doesn't really help here,
|
|
493
|
+
// but blocks even loop if we're using it inside keys.transaction, and it makes it "sync" actually
|
|
494
|
+
// This way we chunk it in smaller parts and between those parts we can yield to the event loop
|
|
495
|
+
// It's rare case when you need to E2E sessions for so many users, but it's possible
|
|
496
|
+
const chunkSize = 100;
|
|
497
|
+
const chunks = chunk(nodes, chunkSize);
|
|
498
|
+
for (const nodesChunk of chunks) {
|
|
499
|
+
for (const node of nodesChunk) {
|
|
500
|
+
const signedKey = (0, index_js_2.getBinaryNodeChild)(node, 'skey');
|
|
501
|
+
const key = (0, index_js_2.getBinaryNodeChild)(node, 'key');
|
|
502
|
+
const identity = (0, index_js_2.getBinaryNodeChildBuffer)(node, 'identity');
|
|
503
|
+
const jid = node.attrs.jid;
|
|
504
|
+
const registrationId = (0, index_js_2.getBinaryNodeChildUInt)(node, 'registration', 4);
|
|
505
|
+
await repository.injectE2ESession({
|
|
506
|
+
jid,
|
|
507
|
+
session: {
|
|
508
|
+
registrationId: registrationId,
|
|
509
|
+
identityKey: (0, crypto_js_1.generateSignalPubKey)(identity),
|
|
510
|
+
signedPreKey: extractKey(signedKey),
|
|
511
|
+
preKey: extractKey(key)
|
|
512
|
+
}
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
exports.parseAndInjectE2ESessions = parseAndInjectE2ESessions;
|
|
518
|
+
const extractDeviceJids = (result, myJid, myLid, excludeZeroDevices) => {
|
|
519
|
+
const { user: myUser, device: myDevice } = (0, index_js_2.jidDecode)(myJid);
|
|
520
|
+
const extracted = [];
|
|
521
|
+
for (const userResult of result) {
|
|
522
|
+
const { devices, id } = userResult;
|
|
523
|
+
const decoded = (0, index_js_2.jidDecode)(id), { user, server } = decoded;
|
|
524
|
+
let { domainType } = decoded;
|
|
525
|
+
const deviceList = devices?.deviceList;
|
|
526
|
+
if (!Array.isArray(deviceList))
|
|
527
|
+
continue;
|
|
528
|
+
for (const { id: device, keyIndex, isHosted } of deviceList) {
|
|
529
|
+
if ((!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
|
|
530
|
+
((myUser !== user && myLid !== user) || myDevice !== device) && // either different user or if me user, not this device
|
|
531
|
+
(device === 0 || !!keyIndex) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
|
|
532
|
+
) {
|
|
533
|
+
if (isHosted) {
|
|
534
|
+
domainType = domainType === index_js_2.WAJIDDomains.LID ? index_js_2.WAJIDDomains.HOSTED_LID : index_js_2.WAJIDDomains.HOSTED;
|
|
535
|
+
}
|
|
536
|
+
extracted.push({
|
|
537
|
+
user,
|
|
538
|
+
device,
|
|
539
|
+
domainType,
|
|
540
|
+
server: (0, index_js_2.getServerFromDomainType)(server, domainType)
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return extracted;
|
|
546
|
+
};
|
|
547
|
+
exports.extractDeviceJids = extractDeviceJids;
|
|
548
|
+
/**
|
|
549
|
+
* get the next N keys for upload or processing
|
|
550
|
+
* @param count number of pre-keys to get or generate
|
|
551
|
+
*/
|
|
552
|
+
const getNextPreKeys = async ({ creds, keys }, count) => {
|
|
553
|
+
const { newPreKeys, lastPreKeyId, preKeysRange } = (0, exports.generateOrGetPreKeys)(creds, count);
|
|
554
|
+
const update = {
|
|
555
|
+
nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
|
|
556
|
+
firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1)
|
|
557
|
+
};
|
|
558
|
+
await keys.set({ 'pre-key': newPreKeys });
|
|
559
|
+
const preKeys = await (0, exports.getPreKeys)(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1]);
|
|
560
|
+
return { update, preKeys };
|
|
561
|
+
};
|
|
562
|
+
exports.getNextPreKeys = getNextPreKeys;
|
|
563
|
+
const getNextPreKeysNode = async (state, count) => {
|
|
564
|
+
const { creds } = state;
|
|
565
|
+
const { update, preKeys } = await (0, exports.getNextPreKeys)(state, count);
|
|
566
|
+
const node = {
|
|
567
|
+
tag: 'iq',
|
|
568
|
+
attrs: {
|
|
569
|
+
xmlns: 'encrypt',
|
|
570
|
+
type: 'set',
|
|
571
|
+
to: index_js_2.S_WHATSAPP_NET
|
|
572
|
+
},
|
|
573
|
+
content: [
|
|
574
|
+
{ tag: 'registration', attrs: {}, content: (0, generics_js_1.encodeBigEndian)(creds.registrationId) },
|
|
575
|
+
{ tag: 'type', attrs: {}, content: index_js_1.KEY_BUNDLE_TYPE },
|
|
576
|
+
{ tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
|
|
577
|
+
{ tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => (0, exports.xmppPreKey)(preKeys[+k], +k)) },
|
|
578
|
+
(0, exports.xmppSignedPreKey)(creds.signedPreKey)
|
|
579
|
+
]
|
|
580
|
+
};
|
|
581
|
+
return { update, node };
|
|
582
|
+
};
|
|
583
|
+
exports.getNextPreKeysNode = getNextPreKeysNode;
|
|
584
|
+
|
|
585
|
+
return module.exports;
|
|
586
|
+
})();
|
|
587
|
+
for (const key of Object.keys(__kageModule184)) {
|
|
588
|
+
if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule184[key]});
|
|
589
|
+
}
|
|
590
|
+
Object.defineProperty(exports, Symbol.for('kage.advanced'), {value:__kageModule184, configurable:true});
|
package/lib/Utils/stanza-ack.js
CHANGED
|
@@ -35,4 +35,54 @@
|
|
|
35
35
|
}
|
|
36
36
|
return stanza;
|
|
37
37
|
} exports.buildAckStanza = buildAckStanza;
|
|
38
|
-
//#
|
|
38
|
+
//#
|
|
39
|
+
// Kage: funciones avanzadas.
|
|
40
|
+
const __kageModule185 = (() => {
|
|
41
|
+
const module = { exports: {} }; const exports = module.exports;
|
|
42
|
+
"use strict";
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
exports.buildAckStanza = buildAckStanza;
|
|
45
|
+
/**
|
|
46
|
+
* Builds an ACK stanza for a received node.
|
|
47
|
+
* Pure function -- no I/O, no side effects.
|
|
48
|
+
*
|
|
49
|
+
* Mirrors WhatsApp Web's ACK construction:
|
|
50
|
+
* - WAWebHandleMsgSendAck.sendAck / sendNack
|
|
51
|
+
* - WAWebCreateNackFromStanza.createNackFromStanza
|
|
52
|
+
*/
|
|
53
|
+
function buildAckStanza(node, errorCode, meId) {
|
|
54
|
+
const { tag, attrs } = node;
|
|
55
|
+
const stanza = {
|
|
56
|
+
tag: 'ack',
|
|
57
|
+
attrs: {
|
|
58
|
+
id: attrs.id,
|
|
59
|
+
to: attrs.from,
|
|
60
|
+
class: tag
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
if (errorCode) {
|
|
64
|
+
stanza.attrs.error = errorCode.toString();
|
|
65
|
+
}
|
|
66
|
+
if (attrs.participant) {
|
|
67
|
+
stanza.attrs.participant = attrs.participant;
|
|
68
|
+
}
|
|
69
|
+
if (attrs.recipient) {
|
|
70
|
+
stanza.attrs.recipient = attrs.recipient;
|
|
71
|
+
}
|
|
72
|
+
// WA Web always includes type when present: `n.type || DROP_ATTR`
|
|
73
|
+
if (attrs.type) {
|
|
74
|
+
stanza.attrs.type = attrs.type;
|
|
75
|
+
}
|
|
76
|
+
// WA Web WAWebHandleMsgSendAck.sendAck/sendNack always include `from` for message-class ACKs
|
|
77
|
+
if (tag === 'message' && meId) {
|
|
78
|
+
stanza.attrs.from = meId;
|
|
79
|
+
}
|
|
80
|
+
return stanza;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return module.exports;
|
|
84
|
+
})();
|
|
85
|
+
for (const key of Object.keys(__kageModule185)) {
|
|
86
|
+
if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule185[key]});
|
|
87
|
+
}
|
|
88
|
+
Object.defineProperty(exports, Symbol.for('kage.advanced'), {value:__kageModule185, configurable:true});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const cooldowns=new WeakMap();
|
|
3
|
+
async function refreshReceivedStatuses(conn, options={}) {
|
|
4
|
+
const status=require('./status-tools');
|
|
5
|
+
const capture=options.capture || (raw=>status.captureStatus(raw,conn));
|
|
6
|
+
const list=options.list || (()=>status.listStatuses(conn));
|
|
7
|
+
let state=cooldowns.get(conn);if(!state){state={last:0,keys:new Map(),job:null};cooldowns.set(conn,state);}
|
|
8
|
+
if(state.job)return state.job;
|
|
9
|
+
if(Date.now()-state.last<10000)return {received:list().length,imported:0,requested:0,errors:[],throttled:true};
|
|
10
|
+
state.last=Date.now();
|
|
11
|
+
state.job=(async()=>{
|
|
12
|
+
let imported=0,requested=0;const errors=[];
|
|
13
|
+
const sources=[options.messages,conn.chats?.['status@broadcast']?.messages,options.store?.messages?.['status@broadcast']];
|
|
14
|
+
if(options.capture) sources.push(status.listStatuses(conn).map(entry=>entry.raw).filter(Boolean));
|
|
15
|
+
for(const source of sources){
|
|
16
|
+
const items=Array.isArray(source)?source:source instanceof Map?[...source.values()]:source?.array||[];
|
|
17
|
+
for(const raw of items.slice(-500))if(raw?.key?.remoteJid==='status@broadcast'&&capture(raw))imported++;
|
|
18
|
+
}
|
|
19
|
+
const entries=list();
|
|
20
|
+
// There is no arbitrary status-list query here. Request only known message keys delivered to us.
|
|
21
|
+
const missing=entries.filter(entry=>!entry.type&&entry.sourceKey?.id&&!['expired','deleted'].includes(entry.availability)&&entry.expiresAt>Date.now());
|
|
22
|
+
for(const entry of missing.slice(0,5)){
|
|
23
|
+
const key=JSON.stringify([entry.author,entry.sourceKey.id]);
|
|
24
|
+
if(Date.now()-(state.keys.get(key)||0)<60000)continue;
|
|
25
|
+
if(typeof conn.requestPlaceholderResend!=='function')break;
|
|
26
|
+
state.keys.set(key,Date.now()); requested++;
|
|
27
|
+
let timer;
|
|
28
|
+
try { await Promise.race([conn.requestPlaceholderResend(entry.sourceKey),new Promise((_,reject)=>{timer=setTimeout(()=>reject(new Error('STATUS_REFRESH_PENDING')),4000);})]); }
|
|
29
|
+
catch(error){errors.push({id:entry.id,error:String(error.message||error).slice(0,200)});}
|
|
30
|
+
finally{clearTimeout(timer);}
|
|
31
|
+
}
|
|
32
|
+
for(const [key,time]of state.keys)if(Date.now()-time>86400000)state.keys.delete(key);
|
|
33
|
+
return {received:list().length,imported,requested,errors,throttled:false};
|
|
34
|
+
})();
|
|
35
|
+
try{return await state.job;}finally{state.job=null;}
|
|
36
|
+
}
|
|
37
|
+
exports.refreshReceivedStatuses=refreshReceivedStatuses;
|
|
@@ -19,8 +19,10 @@ function persist(state) {
|
|
|
19
19
|
}
|
|
20
20
|
function stateFor(conn) {
|
|
21
21
|
let state = caches.get(conn)
|
|
22
|
+
const identity = normalizeJid(conn.user?.jid || conn.user?.id)
|
|
23
|
+
if (state && state.account !== identity) { persist(state); caches.delete(conn); state = null }
|
|
22
24
|
if (!state) {
|
|
23
|
-
state = { entries: new Map(), next: 1, last: 0, busy: false }
|
|
25
|
+
state = { entries: new Map(), next: 1, last: 0, busy: false, account: identity }
|
|
24
26
|
const account = normalizeJid(conn.user?.jid || conn.user?.id)
|
|
25
27
|
if (account && conn.statusStorageDir !== false) {
|
|
26
28
|
state.dir = resolve(conn.statusStorageDir || 'data/status')
|
|
@@ -71,7 +73,7 @@ function statusContent(message = {}) {
|
|
|
71
73
|
const contexts = []
|
|
72
74
|
for (let i = 0; i < 12 && current; i++) {
|
|
73
75
|
for (const value of Object.values(current)) if (value?.contextInfo) contexts.push(value.contextInfo)
|
|
74
|
-
const key = ['ephemeralMessage', 'viewOnceMessage', 'viewOnceMessageV2', 'viewOnceMessageV2Extension',
|
|
76
|
+
const key = ['deviceSentMessage', 'ephemeralMessage', 'viewOnceMessage', 'viewOnceMessageV2', 'viewOnceMessageV2Extension',
|
|
75
77
|
'documentWithCaptionMessage', 'statusMentionMessage', 'groupStatusMentionMessage', 'groupStatusMessage', 'groupStatusMessageV2']
|
|
76
78
|
.find(name => current[name]?.message)
|
|
77
79
|
if (!key) break
|
|
@@ -45,4 +45,66 @@ var _indexjs3 = require('../WABinary/index.js');
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
}; exports.emitSyncActionResults = emitSyncActionResults;
|
|
48
|
-
//#
|
|
48
|
+
//#
|
|
49
|
+
// Kage: funciones avanzadas.
|
|
50
|
+
const __kageModule186 = (() => {
|
|
51
|
+
const module = { exports: {} }; const exports = module.exports;
|
|
52
|
+
"use strict";
|
|
53
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
+
exports.emitSyncActionResults = exports.processContactAction = void 0;
|
|
55
|
+
const index_js_1 = require("../WABinary/index.js");
|
|
56
|
+
/**
|
|
57
|
+
* Process contactAction and return events to emit.
|
|
58
|
+
* Pure function - no side effects.
|
|
59
|
+
*/
|
|
60
|
+
const processContactAction = (action, id, logger) => {
|
|
61
|
+
const results = [];
|
|
62
|
+
if (!id) {
|
|
63
|
+
logger?.warn({ hasFullName: !!action.fullName, hasLidJid: !!action.lidJid, hasPnJid: !!action.pnJid }, 'contactAction sync: missing id in index');
|
|
64
|
+
return results;
|
|
65
|
+
}
|
|
66
|
+
const lidJid = action.lidJid;
|
|
67
|
+
const idIsPn = (0, index_js_1.isPnUser)(id);
|
|
68
|
+
// PN is in index[1], not in contactAction.pnJid which is usually null
|
|
69
|
+
const phoneNumber = idIsPn ? id : action.pnJid || undefined;
|
|
70
|
+
// Always emit contacts.upsert
|
|
71
|
+
results.push({
|
|
72
|
+
event: 'contacts.upsert',
|
|
73
|
+
data: [
|
|
74
|
+
{
|
|
75
|
+
id,
|
|
76
|
+
name: action.fullName || action.firstName || action.username || undefined,
|
|
77
|
+
username: action.username || undefined,
|
|
78
|
+
lid: lidJid || undefined,
|
|
79
|
+
phoneNumber
|
|
80
|
+
}
|
|
81
|
+
]
|
|
82
|
+
});
|
|
83
|
+
// Emit lid-mapping.update if we have valid LID-PN pair
|
|
84
|
+
if (lidJid && (0, index_js_1.isLidUser)(lidJid) && idIsPn) {
|
|
85
|
+
results.push({
|
|
86
|
+
event: 'lid-mapping.update',
|
|
87
|
+
data: { lid: lidJid, pn: id }
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return results;
|
|
91
|
+
};
|
|
92
|
+
exports.processContactAction = processContactAction;
|
|
93
|
+
const emitSyncActionResults = (ev, results) => {
|
|
94
|
+
for (const result of results) {
|
|
95
|
+
if (result.event === 'contacts.upsert') {
|
|
96
|
+
ev.emit('contacts.upsert', result.data);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
ev.emit('lid-mapping.update', result.data);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
exports.emitSyncActionResults = emitSyncActionResults;
|
|
104
|
+
|
|
105
|
+
return module.exports;
|
|
106
|
+
})();
|
|
107
|
+
for (const key of Object.keys(__kageModule186)) {
|
|
108
|
+
if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule186[key]});
|
|
109
|
+
}
|
|
110
|
+
Object.defineProperty(exports, Symbol.for('kage.advanced'), {value:__kageModule186, configurable:true});
|