kage-baileys 3.0.6 → 3.2.3
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/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
|
@@ -34,6 +34,7 @@ const makeEventBuffer = (logger) => {
|
|
|
34
34
|
const historyCache = new Set();
|
|
35
35
|
let data = makeBufferData();
|
|
36
36
|
let buffersInProgress = 0;
|
|
37
|
+
const leases = new Set();
|
|
37
38
|
// take the generic event and fire it as a Kage event
|
|
38
39
|
ev.on('event', (map) => {
|
|
39
40
|
for (const event in map) {
|
|
@@ -41,18 +42,33 @@ const makeEventBuffer = (logger) => {
|
|
|
41
42
|
}
|
|
42
43
|
});
|
|
43
44
|
function buffer() {
|
|
44
|
-
|
|
45
|
+
const lease = {};
|
|
46
|
+
leases.add(lease);
|
|
47
|
+
buffersInProgress = leases.size;
|
|
48
|
+
let released = false;
|
|
49
|
+
return () => {
|
|
50
|
+
if (released) return false;
|
|
51
|
+
released = true;
|
|
52
|
+
if (!leases.delete(lease)) return false;
|
|
53
|
+
buffersInProgress = leases.size + 1;
|
|
54
|
+
return flush(false, true);
|
|
55
|
+
};
|
|
45
56
|
}
|
|
46
|
-
function flush(force = false) {
|
|
57
|
+
function flush(force = false, releasingLease = false) {
|
|
47
58
|
// no buffer going on
|
|
48
59
|
if (!buffersInProgress) {
|
|
49
60
|
return false;
|
|
50
61
|
}
|
|
51
62
|
if (force) {
|
|
63
|
+
leases.clear();
|
|
52
64
|
buffersInProgress = 0;
|
|
53
65
|
} else {
|
|
54
|
-
//
|
|
55
|
-
|
|
66
|
+
// Legacy flush releases one lease; scoped releases remove their own token.
|
|
67
|
+
if (!releasingLease) {
|
|
68
|
+
const last = Array.from(leases).pop();
|
|
69
|
+
leases.delete(last);
|
|
70
|
+
}
|
|
71
|
+
buffersInProgress = leases.size;
|
|
56
72
|
// if there are still some buffers going on
|
|
57
73
|
// then we don't flush now
|
|
58
74
|
if (buffersInProgress) {
|
|
@@ -71,17 +87,19 @@ const makeEventBuffer = (logger) => {
|
|
|
71
87
|
}
|
|
72
88
|
}
|
|
73
89
|
const consolidatedData = consolidateEvents(data);
|
|
90
|
+
data = newData;
|
|
74
91
|
if (Object.keys(consolidatedData).length) {
|
|
75
92
|
ev.emit('event', consolidatedData);
|
|
76
93
|
}
|
|
77
|
-
data = newData;
|
|
78
94
|
logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events');
|
|
79
95
|
return true;
|
|
80
96
|
}
|
|
81
97
|
return {
|
|
82
98
|
process(handler) {
|
|
83
99
|
const listener = (map) => {
|
|
84
|
-
|
|
100
|
+
try {
|
|
101
|
+
Promise.resolve(handler(map)).catch(error => logger.error({ err: error }, 'event processor failed'));
|
|
102
|
+
} catch (error) { logger.error({ err: error }, 'event processor failed'); }
|
|
85
103
|
};
|
|
86
104
|
ev.on('event', listener);
|
|
87
105
|
return () => {
|
|
@@ -102,13 +120,13 @@ const makeEventBuffer = (logger) => {
|
|
|
102
120
|
flush,
|
|
103
121
|
createBufferedFunction(work) {
|
|
104
122
|
return async (...args) => {
|
|
105
|
-
buffer();
|
|
123
|
+
const release = buffer();
|
|
106
124
|
try {
|
|
107
125
|
const result = await work(...args);
|
|
108
126
|
return result;
|
|
109
127
|
}
|
|
110
128
|
finally {
|
|
111
|
-
|
|
129
|
+
release();
|
|
112
130
|
}
|
|
113
131
|
};
|
|
114
132
|
},
|
|
@@ -518,3 +536,1300 @@ function concatChats(a, b) {
|
|
|
518
536
|
return Object.assign(a, b);
|
|
519
537
|
}
|
|
520
538
|
const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
|
|
539
|
+
|
|
540
|
+
// Kage: compatibilidad de API.
|
|
541
|
+
const __kageModule64 = (() => {
|
|
542
|
+
const module = { exports: {} }; const exports = module.exports;
|
|
543
|
+
"use strict"
|
|
544
|
+
|
|
545
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
546
|
+
|
|
547
|
+
const EventEmitter = require("events")
|
|
548
|
+
const { WAMessageStatus } = require("../Types")
|
|
549
|
+
const { trimUndefined } = require("./generics")
|
|
550
|
+
const {
|
|
551
|
+
updateMessageWithReceipt,
|
|
552
|
+
updateMessageWithReaction
|
|
553
|
+
} = require("./messages")
|
|
554
|
+
const { isRealMessage } = require("./process-message")
|
|
555
|
+
|
|
556
|
+
const BUFFERABLE_EVENT = [
|
|
557
|
+
'messaging-history.set',
|
|
558
|
+
'chats.upsert',
|
|
559
|
+
'chats.update',
|
|
560
|
+
'chats.delete',
|
|
561
|
+
'contacts.upsert',
|
|
562
|
+
'contacts.update',
|
|
563
|
+
'messages.upsert',
|
|
564
|
+
'messages.update',
|
|
565
|
+
'messages.delete',
|
|
566
|
+
//'messages.poll',
|
|
567
|
+
'messages.reaction',
|
|
568
|
+
'message-receipt.update',
|
|
569
|
+
'groups.update',
|
|
570
|
+
]
|
|
571
|
+
|
|
572
|
+
const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT)
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* The event buffer logically consolidates different events into a single event
|
|
576
|
+
* making the data processing more efficient.
|
|
577
|
+
*/
|
|
578
|
+
const makeEventBuffer = (logger) => {
|
|
579
|
+
const ev = new EventEmitter()
|
|
580
|
+
const historyCache = new Set()
|
|
581
|
+
let data = makeBufferData()
|
|
582
|
+
let isBuffering = false
|
|
583
|
+
let bufferTimeout = null
|
|
584
|
+
let bufferCount = 0
|
|
585
|
+
const MAX_HISTORY_CACHE_SIZE = 10000 // Limit the history cache size to prevent memory bloat
|
|
586
|
+
const BUFFER_TIMEOUT_MS = 30000 // 30 seconds
|
|
587
|
+
|
|
588
|
+
// take the generic event and fire it as a baileys event
|
|
589
|
+
ev.on('event', (map) => {
|
|
590
|
+
for (const event in map) {
|
|
591
|
+
ev.emit(event, map[event])
|
|
592
|
+
}
|
|
593
|
+
})
|
|
594
|
+
function buffer() {
|
|
595
|
+
if (!isBuffering) {
|
|
596
|
+
logger.debug('Event buffer activated')
|
|
597
|
+
isBuffering = true
|
|
598
|
+
bufferCount++
|
|
599
|
+
|
|
600
|
+
// Auto-flush after a timeout to prevent infinite buffering
|
|
601
|
+
if (bufferTimeout) {
|
|
602
|
+
clearTimeout(bufferTimeout)
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
bufferTimeout = setTimeout(() => {
|
|
606
|
+
if (isBuffering) {
|
|
607
|
+
logger.warn('Buffer timeout reached, auto-flushing')
|
|
608
|
+
flush()
|
|
609
|
+
}
|
|
610
|
+
}, BUFFER_TIMEOUT_MS)
|
|
611
|
+
}
|
|
612
|
+
else {
|
|
613
|
+
bufferCount++
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
function flush() {
|
|
617
|
+
if (!isBuffering) {
|
|
618
|
+
return false
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
logger.debug({ bufferCount }, 'Flushing event buffer')
|
|
622
|
+
isBuffering = false
|
|
623
|
+
bufferCount = 0
|
|
624
|
+
|
|
625
|
+
// Clear timeout
|
|
626
|
+
if (bufferTimeout) {
|
|
627
|
+
clearTimeout(bufferTimeout)
|
|
628
|
+
bufferTimeout = null
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// Clear history cache if it exceeds the max size
|
|
632
|
+
if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
|
|
633
|
+
logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache')
|
|
634
|
+
historyCache.clear()
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
const newData = makeBufferData()
|
|
638
|
+
const chatUpdates = Object.values(data.chatUpdates)
|
|
639
|
+
let conditionalChatUpdatesLeft = 0
|
|
640
|
+
for (const update of chatUpdates) {
|
|
641
|
+
if (update.conditional) {
|
|
642
|
+
conditionalChatUpdatesLeft += 1
|
|
643
|
+
newData.chatUpdates[update.id] = update
|
|
644
|
+
delete data.chatUpdates[update.id]
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
const consolidatedData = consolidateEvents(data)
|
|
648
|
+
if (Object.keys(consolidatedData).length) {
|
|
649
|
+
ev.emit('event', consolidatedData)
|
|
650
|
+
}
|
|
651
|
+
data = newData
|
|
652
|
+
logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events')
|
|
653
|
+
return true
|
|
654
|
+
}
|
|
655
|
+
return {
|
|
656
|
+
process(handler) {
|
|
657
|
+
const listener = (map) => {
|
|
658
|
+
handler(map)
|
|
659
|
+
}
|
|
660
|
+
ev.on('event', listener)
|
|
661
|
+
return () => {
|
|
662
|
+
ev.off('event', listener)
|
|
663
|
+
}
|
|
664
|
+
},
|
|
665
|
+
emit(event, evData) {
|
|
666
|
+
// Check if this is a messages.upsert with a different type than what's buffered
|
|
667
|
+
// If so, flush the buffered messages first to avoid type overshadowing
|
|
668
|
+
if (event === 'messages.upsert') {
|
|
669
|
+
const { type } = evData
|
|
670
|
+
const existingUpserts = Object.values(data.messageUpserts)
|
|
671
|
+
|
|
672
|
+
if (existingUpserts.length > 0) {
|
|
673
|
+
const bufferedType = existingUpserts[0].type
|
|
674
|
+
|
|
675
|
+
if (bufferedType !== type) {
|
|
676
|
+
logger.debug({ bufferedType, newType: type }, 'messages.upsert type mismatch, emitting buffered messages')
|
|
677
|
+
|
|
678
|
+
// Emit the buffered messages with their correct type
|
|
679
|
+
ev.emit('event', {
|
|
680
|
+
'messages.upsert': {
|
|
681
|
+
messages: existingUpserts.map(m => m.message),
|
|
682
|
+
type: bufferedType
|
|
683
|
+
}
|
|
684
|
+
})
|
|
685
|
+
|
|
686
|
+
// Clear the message upserts from the buffer
|
|
687
|
+
data.messageUpserts = {}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
|
|
693
|
+
append(data, historyCache, event, evData, logger)
|
|
694
|
+
return true
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
return ev.emit('event', { [event]: evData })
|
|
698
|
+
},
|
|
699
|
+
isBuffering() {
|
|
700
|
+
return isBuffering
|
|
701
|
+
},
|
|
702
|
+
buffer,
|
|
703
|
+
flush,
|
|
704
|
+
createBufferedFunction(work) {
|
|
705
|
+
return async (...args) => {
|
|
706
|
+
buffer()
|
|
707
|
+
try {
|
|
708
|
+
const result = await work(...args)
|
|
709
|
+
|
|
710
|
+
// If this is the only buffer, flush after a small delay
|
|
711
|
+
if (bufferCount === 1) {
|
|
712
|
+
setTimeout(() => {
|
|
713
|
+
if (isBuffering && bufferCount === 1) {
|
|
714
|
+
flush()
|
|
715
|
+
}
|
|
716
|
+
}, 100) // Small delay to allow nested buffers
|
|
717
|
+
}
|
|
718
|
+
return result
|
|
719
|
+
}
|
|
720
|
+
catch (error) {
|
|
721
|
+
throw error
|
|
722
|
+
}
|
|
723
|
+
finally {
|
|
724
|
+
bufferCount = Math.max(0, bufferCount - 1)
|
|
725
|
+
|
|
726
|
+
if (bufferCount === 0) {
|
|
727
|
+
// Auto-flush when no other buffers are active
|
|
728
|
+
setTimeout(flush, 100)
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
on: (...args) => ev.on(...args),
|
|
734
|
+
off: (...args) => ev.off(...args),
|
|
735
|
+
removeAllListeners: (...args) => ev.removeAllListeners(...args),
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
const makeBufferData = () => {
|
|
740
|
+
return {
|
|
741
|
+
historySets: {
|
|
742
|
+
chats: {},
|
|
743
|
+
messages: {},
|
|
744
|
+
contacts: {},
|
|
745
|
+
isLatest: false,
|
|
746
|
+
empty: true
|
|
747
|
+
},
|
|
748
|
+
chatUpserts: {},
|
|
749
|
+
chatUpdates: {},
|
|
750
|
+
chatDeletes: new Set(),
|
|
751
|
+
contactUpserts: {},
|
|
752
|
+
contactUpdates: {},
|
|
753
|
+
messageUpserts: {},
|
|
754
|
+
messageUpdates: {},
|
|
755
|
+
//messagePollings: {},
|
|
756
|
+
messageReactions: {},
|
|
757
|
+
messageDeletes: {},
|
|
758
|
+
messageReceipts: {},
|
|
759
|
+
groupUpdates: {}
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function append(data, historyCache, event, eventData, logger) {
|
|
764
|
+
switch (event) {
|
|
765
|
+
case 'messaging-history.set':
|
|
766
|
+
for (const chat of eventData.chats) {
|
|
767
|
+
const id = chat.id || ''
|
|
768
|
+
const existingChat = data.historySets.chats[id]
|
|
769
|
+
|
|
770
|
+
if (existingChat) {
|
|
771
|
+
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
if (!existingChat && !historyCache.has(id)) {
|
|
775
|
+
data.historySets.chats[id] = chat
|
|
776
|
+
historyCache.add(id)
|
|
777
|
+
absorbingChatUpdate(chat)
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
for (const contact of eventData.contacts) {
|
|
782
|
+
const existingContact = data.historySets.contacts[contact.id]
|
|
783
|
+
|
|
784
|
+
if (existingContact) {
|
|
785
|
+
Object.assign(existingContact, trimUndefined(contact))
|
|
786
|
+
}
|
|
787
|
+
else {
|
|
788
|
+
const historyContactId = `c:${contact.id}`
|
|
789
|
+
const hasAnyName = contact.notify || contact.name || contact.verifiedName
|
|
790
|
+
|
|
791
|
+
if (!historyCache.has(historyContactId) || hasAnyName) {
|
|
792
|
+
data.historySets.contacts[contact.id] = contact
|
|
793
|
+
historyCache.add(historyContactId)
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
for (const message of eventData.messages) {
|
|
799
|
+
const key = stringifyMessageKey(message.key)
|
|
800
|
+
const existingMsg = data.historySets.messages[key]
|
|
801
|
+
|
|
802
|
+
if (!existingMsg && !historyCache.has(key)) {
|
|
803
|
+
data.historySets.messages[key] = message
|
|
804
|
+
historyCache.add(key)
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
data.historySets.empty = false
|
|
809
|
+
data.historySets.syncType = eventData.syncType
|
|
810
|
+
data.historySets.progress = eventData.progress
|
|
811
|
+
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId
|
|
812
|
+
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest
|
|
813
|
+
break
|
|
814
|
+
case 'chats.upsert':
|
|
815
|
+
for (const chat of eventData) {
|
|
816
|
+
const id = chat.id || ''
|
|
817
|
+
|
|
818
|
+
let upsert = data.chatUpserts[id]
|
|
819
|
+
|
|
820
|
+
if (id && !upsert) {
|
|
821
|
+
upsert = data.historySets.chats[id]
|
|
822
|
+
|
|
823
|
+
if (upsert) {
|
|
824
|
+
logger.debug({ chatId: id }, 'absorbed chat upsert in chat set')
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
if (upsert) {
|
|
829
|
+
upsert = concatChats(upsert, chat)
|
|
830
|
+
}
|
|
831
|
+
else {
|
|
832
|
+
upsert = chat
|
|
833
|
+
data.chatUpserts[id] = upsert
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
absorbingChatUpdate(upsert)
|
|
837
|
+
|
|
838
|
+
if (data.chatDeletes.has(id)) {
|
|
839
|
+
data.chatDeletes.delete(id)
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
break
|
|
843
|
+
case 'chats.update':
|
|
844
|
+
for (const update of eventData) {
|
|
845
|
+
const chatId = update.id
|
|
846
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true
|
|
847
|
+
if (conditionMatches) {
|
|
848
|
+
delete update.conditional
|
|
849
|
+
// if there is an existing upsert, merge the update into it
|
|
850
|
+
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId]
|
|
851
|
+
if (upsert) {
|
|
852
|
+
concatChats(upsert, update)
|
|
853
|
+
}
|
|
854
|
+
else {
|
|
855
|
+
// merge the update into the existing update
|
|
856
|
+
const chatUpdate = data.chatUpdates[chatId] || {}
|
|
857
|
+
data.chatUpdates[chatId] = concatChats(chatUpdate, update)
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
else if (conditionMatches === undefined) {
|
|
861
|
+
// condition yet to be fulfilled
|
|
862
|
+
data.chatUpdates[chatId] = update
|
|
863
|
+
}
|
|
864
|
+
// otherwise -- condition not met, update is invalid
|
|
865
|
+
// if the chat has been updated
|
|
866
|
+
// ignore any existing chat delete
|
|
867
|
+
if (data.chatDeletes.has(chatId)) {
|
|
868
|
+
data.chatDeletes.delete(chatId)
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
break
|
|
872
|
+
case 'chats.delete':
|
|
873
|
+
for (const chatId of eventData) {
|
|
874
|
+
if (!data.chatDeletes.has(chatId)) {
|
|
875
|
+
data.chatDeletes.add(chatId)
|
|
876
|
+
}
|
|
877
|
+
// remove any prior updates & upserts
|
|
878
|
+
if (data.chatUpdates[chatId]) {
|
|
879
|
+
delete data.chatUpdates[chatId]
|
|
880
|
+
}
|
|
881
|
+
if (data.chatUpserts[chatId]) {
|
|
882
|
+
delete data.chatUpserts[chatId]
|
|
883
|
+
}
|
|
884
|
+
if (data.historySets.chats[chatId]) {
|
|
885
|
+
delete data.historySets.chats[chatId]
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
break
|
|
889
|
+
case 'contacts.upsert':
|
|
890
|
+
for (const contact of eventData) {
|
|
891
|
+
let upsert = data.contactUpserts[contact.id]
|
|
892
|
+
if (!upsert) {
|
|
893
|
+
upsert = data.historySets.contacts[contact.id]
|
|
894
|
+
if (upsert) {
|
|
895
|
+
logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set')
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
if (upsert) {
|
|
899
|
+
upsert = Object.assign(upsert, trimUndefined(contact))
|
|
900
|
+
}
|
|
901
|
+
else {
|
|
902
|
+
upsert = contact
|
|
903
|
+
data.contactUpserts[contact.id] = upsert
|
|
904
|
+
}
|
|
905
|
+
if (data.contactUpdates[contact.id]) {
|
|
906
|
+
upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact))
|
|
907
|
+
delete data.contactUpdates[contact.id]
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
break
|
|
911
|
+
case 'contacts.update':
|
|
912
|
+
const contactUpdates = eventData
|
|
913
|
+
for (const update of contactUpdates) {
|
|
914
|
+
const id = update.id
|
|
915
|
+
// merge into prior upsert
|
|
916
|
+
const upsert = data.historySets.contacts[id] || data.contactUpserts[id]
|
|
917
|
+
if (upsert) {
|
|
918
|
+
Object.assign(upsert, update)
|
|
919
|
+
}
|
|
920
|
+
else {
|
|
921
|
+
// merge into prior update
|
|
922
|
+
const contactUpdate = data.contactUpdates[id] || {}
|
|
923
|
+
data.contactUpdates[id] = Object.assign(contactUpdate, update)
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
break
|
|
927
|
+
case 'messages.upsert':
|
|
928
|
+
const { messages, type } = eventData
|
|
929
|
+
|
|
930
|
+
for (const message of messages) {
|
|
931
|
+
const key = stringifyMessageKey(message.key)
|
|
932
|
+
|
|
933
|
+
let existing = data.messageUpserts[key]?.message
|
|
934
|
+
|
|
935
|
+
if (!existing) {
|
|
936
|
+
existing = data.historySets.messages[key]
|
|
937
|
+
|
|
938
|
+
if (existing) {
|
|
939
|
+
logger.debug({ messageId: key }, 'absorbed message upsert in message set')
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
if (existing) {
|
|
944
|
+
message.messageTimestamp = existing.messageTimestamp
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
if (data.messageUpdates[key]) {
|
|
948
|
+
logger.debug('absorbed prior message update in message upsert')
|
|
949
|
+
Object.assign(message, data.messageUpdates[key].update)
|
|
950
|
+
delete data.messageUpdates[key]
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
if (data.historySets.messages[key]) {
|
|
954
|
+
data.historySets.messages[key] = message
|
|
955
|
+
}
|
|
956
|
+
else {
|
|
957
|
+
data.messageUpserts[key] = {
|
|
958
|
+
message,
|
|
959
|
+
type: type === 'notify' || data.messageUpserts[key]?.type === 'notify' ? 'notify' : type
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
break
|
|
964
|
+
case 'messages.update':
|
|
965
|
+
const msgUpdates = eventData
|
|
966
|
+
|
|
967
|
+
for (const { key, update } of msgUpdates) {
|
|
968
|
+
const keyStr = stringifyMessageKey(key)
|
|
969
|
+
const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message
|
|
970
|
+
|
|
971
|
+
if (existing) {
|
|
972
|
+
Object.assign(existing, update)
|
|
973
|
+
|
|
974
|
+
// if the message was received & read by us
|
|
975
|
+
// the chat counter must have been incremented
|
|
976
|
+
// so we need to decrement it
|
|
977
|
+
if (update.status === WAMessageStatus.READ && !key.fromMe) {
|
|
978
|
+
decrementChatReadCounterIfMsgDidUnread(existing)
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
else {
|
|
982
|
+
const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} }
|
|
983
|
+
Object.assign(msgUpdate.update, update)
|
|
984
|
+
data.messageUpdates[keyStr] = msgUpdate
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
break
|
|
988
|
+
case 'messages.delete':
|
|
989
|
+
const deleteData = eventData
|
|
990
|
+
if ('keys' in deleteData) {
|
|
991
|
+
const { keys } = deleteData
|
|
992
|
+
for (const key of keys) {
|
|
993
|
+
const keyStr = stringifyMessageKey(key)
|
|
994
|
+
if (!data.messageDeletes[keyStr]) {
|
|
995
|
+
data.messageDeletes[keyStr] = key
|
|
996
|
+
}
|
|
997
|
+
if (data.messageUpserts[keyStr]) {
|
|
998
|
+
delete data.messageUpserts[keyStr]
|
|
999
|
+
}
|
|
1000
|
+
if (data.messageUpdates[keyStr]) {
|
|
1001
|
+
delete data.messageUpdates[keyStr]
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
else {
|
|
1006
|
+
// TODO: add support
|
|
1007
|
+
}
|
|
1008
|
+
break
|
|
1009
|
+
// case 'messages.poll':
|
|
1010
|
+
// const pollUpdates = eventData
|
|
1011
|
+
// for(const { vote, pollUpdateMessageKey } of pollUpdates) {
|
|
1012
|
+
// const keyStr = stringifyMessageKey(pollUpdateMessageKey)
|
|
1013
|
+
// const existing = data.messagePollings[keyStr]
|
|
1014
|
+
// if(existing) {
|
|
1015
|
+
// updateMessageWithPollUpdate(existing.message, pollUpdates)
|
|
1016
|
+
// } else {
|
|
1017
|
+
// data.messagePollings[keyStr] = data.messagePollings[keyStr]
|
|
1018
|
+
// || { pollUpdateMessageKey, pollUpdates: [] }
|
|
1019
|
+
// updateMessageWithPollUpdate(data.messagePollings[keyStr], vote)
|
|
1020
|
+
// }
|
|
1021
|
+
// }
|
|
1022
|
+
// break
|
|
1023
|
+
case 'messages.reaction':
|
|
1024
|
+
const reactions = eventData
|
|
1025
|
+
for (const { key, reaction } of reactions) {
|
|
1026
|
+
const keyStr = stringifyMessageKey(key)
|
|
1027
|
+
const existing = data.messageUpserts[keyStr]
|
|
1028
|
+
if (existing) {
|
|
1029
|
+
updateMessageWithReaction(existing.message, reaction)
|
|
1030
|
+
}
|
|
1031
|
+
else {
|
|
1032
|
+
data.messageReactions[keyStr] = data.messageReactions[keyStr]
|
|
1033
|
+
|| { key, reactions: [] }
|
|
1034
|
+
updateMessageWithReaction(data.messageReactions[keyStr], reaction)
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
break
|
|
1038
|
+
case 'message-receipt.update':
|
|
1039
|
+
const receipts = eventData
|
|
1040
|
+
for (const { key, receipt } of receipts) {
|
|
1041
|
+
const keyStr = stringifyMessageKey(key)
|
|
1042
|
+
const existing = data.messageUpserts[keyStr]
|
|
1043
|
+
if (existing) {
|
|
1044
|
+
updateMessageWithReceipt(existing.message, receipt)
|
|
1045
|
+
}
|
|
1046
|
+
else {
|
|
1047
|
+
data.messageReceipts[keyStr] = data.messageReceipts[keyStr]
|
|
1048
|
+
|| { key, userReceipt: [] }
|
|
1049
|
+
updateMessageWithReceipt(data.messageReceipts[keyStr], receipt)
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
break
|
|
1053
|
+
case 'groups.update':
|
|
1054
|
+
const groupUpdates = eventData
|
|
1055
|
+
for (const update of groupUpdates) {
|
|
1056
|
+
const id = update.id
|
|
1057
|
+
const groupUpdate = data.groupUpdates[id] || {}
|
|
1058
|
+
if (!data.groupUpdates[id]) {
|
|
1059
|
+
data.groupUpdates[id] = Object.assign(groupUpdate, update)
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
break
|
|
1063
|
+
default:
|
|
1064
|
+
throw new Error(`"${event}" cannot be buffered`)
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
function absorbingChatUpdate(existing) {
|
|
1068
|
+
const chatId = existing.id || ''
|
|
1069
|
+
const update = data.chatUpdates[chatId]
|
|
1070
|
+
|
|
1071
|
+
if (update) {
|
|
1072
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true
|
|
1073
|
+
|
|
1074
|
+
if (conditionMatches) {
|
|
1075
|
+
delete update.conditional
|
|
1076
|
+
logger.debug({ chatId }, 'absorbed chat update in existing chat')
|
|
1077
|
+
Object.assign(existing, concatChats(update, existing))
|
|
1078
|
+
delete data.chatUpdates[chatId]
|
|
1079
|
+
}
|
|
1080
|
+
else if (conditionMatches === false) {
|
|
1081
|
+
logger.debug({ chatId }, 'chat update condition fail, removing')
|
|
1082
|
+
delete data.chatUpdates[chatId]
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
function decrementChatReadCounterIfMsgDidUnread(message) {
|
|
1088
|
+
// decrement chat unread counter
|
|
1089
|
+
// if the message has already been marked read by us
|
|
1090
|
+
const chatId = message.key.remoteJid
|
|
1091
|
+
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId]
|
|
1092
|
+
|
|
1093
|
+
if (isRealMessage(message) &&
|
|
1094
|
+
shouldIncrementChatUnread(message) &&
|
|
1095
|
+
typeof chat?.unreadCount === 'number' &&
|
|
1096
|
+
chat.unreadCount > 0) {
|
|
1097
|
+
logger.debug({ chatId: chat.id }, 'decrementing chat counter')
|
|
1098
|
+
chat.unreadCount -= 1
|
|
1099
|
+
|
|
1100
|
+
if (chat.unreadCount === 0) {
|
|
1101
|
+
delete chat.unreadCount
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
function consolidateEvents(data) {
|
|
1107
|
+
const map = {}
|
|
1108
|
+
if (!data.historySets.empty) {
|
|
1109
|
+
map['messaging-history.set'] = {
|
|
1110
|
+
chats: Object.values(data.historySets.chats),
|
|
1111
|
+
messages: Object.values(data.historySets.messages),
|
|
1112
|
+
contacts: Object.values(data.historySets.contacts),
|
|
1113
|
+
syncType: data.historySets.syncType,
|
|
1114
|
+
progress: data.historySets.progress,
|
|
1115
|
+
isLatest: data.historySets.isLatest,
|
|
1116
|
+
peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
const chatUpsertList = Object.values(data.chatUpserts)
|
|
1120
|
+
if (chatUpsertList.length) {
|
|
1121
|
+
map['chats.upsert'] = chatUpsertList
|
|
1122
|
+
}
|
|
1123
|
+
const chatUpdateList = Object.values(data.chatUpdates)
|
|
1124
|
+
if (chatUpdateList.length) {
|
|
1125
|
+
map['chats.update'] = chatUpdateList
|
|
1126
|
+
}
|
|
1127
|
+
const chatDeleteList = Array.from(data.chatDeletes)
|
|
1128
|
+
if (chatDeleteList.length) {
|
|
1129
|
+
map['chats.delete'] = chatDeleteList
|
|
1130
|
+
}
|
|
1131
|
+
const messageUpsertList = Object.values(data.messageUpserts)
|
|
1132
|
+
if (messageUpsertList.length) {
|
|
1133
|
+
const type = messageUpsertList[0].type
|
|
1134
|
+
map['messages.upsert'] = {
|
|
1135
|
+
messages: messageUpsertList.map(m => m.message),
|
|
1136
|
+
type
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
const messageUpdateList = Object.values(data.messageUpdates)
|
|
1140
|
+
if (messageUpdateList.length) {
|
|
1141
|
+
map['messages.update'] = messageUpdateList
|
|
1142
|
+
}
|
|
1143
|
+
const messageDeleteList = Object.values(data.messageDeletes)
|
|
1144
|
+
if (messageDeleteList.length) {
|
|
1145
|
+
map['messages.delete'] = { keys: messageDeleteList }
|
|
1146
|
+
}
|
|
1147
|
+
// const messagePollingList = Object.values(data.messagePollings).flatMap(({ vote, pollUpdateMessageKey }) => vote.flatMap(polling => ({ polling, pollUpdateMessageKey })))
|
|
1148
|
+
// if(messagePollingList.length) {
|
|
1149
|
+
// map['messages.poll'] = messagePollingList
|
|
1150
|
+
// }
|
|
1151
|
+
const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) => reactions.flatMap(reaction => ({ key, reaction })))
|
|
1152
|
+
if (messageReactionList.length) {
|
|
1153
|
+
map['messages.reaction'] = messageReactionList
|
|
1154
|
+
}
|
|
1155
|
+
const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) => userReceipt.flatMap(receipt => ({ key, receipt })))
|
|
1156
|
+
if (messageReceiptList.length) {
|
|
1157
|
+
map['message-receipt.update'] = messageReceiptList
|
|
1158
|
+
}
|
|
1159
|
+
const contactUpsertList = Object.values(data.contactUpserts)
|
|
1160
|
+
if (contactUpsertList.length) {
|
|
1161
|
+
map['contacts.upsert'] = contactUpsertList
|
|
1162
|
+
}
|
|
1163
|
+
const contactUpdateList = Object.values(data.contactUpdates)
|
|
1164
|
+
if (contactUpdateList.length) {
|
|
1165
|
+
map['contacts.update'] = contactUpdateList
|
|
1166
|
+
}
|
|
1167
|
+
const groupUpdateList = Object.values(data.groupUpdates)
|
|
1168
|
+
if (groupUpdateList.length) {
|
|
1169
|
+
map['groups.update'] = groupUpdateList
|
|
1170
|
+
}
|
|
1171
|
+
return map
|
|
1172
|
+
}
|
|
1173
|
+
function concatChats(a, b) {
|
|
1174
|
+
if (b.unreadCount === null && // neutralize unread counter
|
|
1175
|
+
a.unreadCount < 0) {
|
|
1176
|
+
a.unreadCount = undefined
|
|
1177
|
+
b.unreadCount = undefined
|
|
1178
|
+
}
|
|
1179
|
+
if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
|
|
1180
|
+
b = { ...b }
|
|
1181
|
+
if (b.unreadCount >= 0) {
|
|
1182
|
+
b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0)
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
return Object.assign(a, b)
|
|
1186
|
+
}
|
|
1187
|
+
const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`
|
|
1188
|
+
|
|
1189
|
+
module.exports = {
|
|
1190
|
+
makeEventBuffer
|
|
1191
|
+
}
|
|
1192
|
+
return module.exports;
|
|
1193
|
+
})();
|
|
1194
|
+
for (const key of Object.keys(__kageModule64)) {
|
|
1195
|
+
if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule64[key]});
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
// Kage: funciones avanzadas.
|
|
1199
|
+
const __kageModule168 = (() => {
|
|
1200
|
+
const module = { exports: {} }; const exports = module.exports;
|
|
1201
|
+
"use strict";
|
|
1202
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
1203
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
1204
|
+
};
|
|
1205
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1206
|
+
exports.makeEventBuffer = void 0;
|
|
1207
|
+
const node_events_1 = __importDefault(require("node:events"));
|
|
1208
|
+
const index_js_1 = require("../Types/index.js");
|
|
1209
|
+
const generics_js_1 = require("./generics.js");
|
|
1210
|
+
const messages_js_1 = require("./messages.js");
|
|
1211
|
+
const process_message_js_1 = require("./process-message.js");
|
|
1212
|
+
const BUFFERABLE_EVENT = [
|
|
1213
|
+
'messaging-history.set',
|
|
1214
|
+
'chats.upsert',
|
|
1215
|
+
'chats.update',
|
|
1216
|
+
'chats.delete',
|
|
1217
|
+
'contacts.upsert',
|
|
1218
|
+
'contacts.update',
|
|
1219
|
+
'messages.upsert',
|
|
1220
|
+
'messages.update',
|
|
1221
|
+
'messages.delete',
|
|
1222
|
+
'messages.reaction',
|
|
1223
|
+
'message-receipt.update',
|
|
1224
|
+
'groups.update'
|
|
1225
|
+
];
|
|
1226
|
+
const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT);
|
|
1227
|
+
/**
|
|
1228
|
+
* The event buffer logically consolidates different events into a single event
|
|
1229
|
+
* making the data processing more efficient.
|
|
1230
|
+
*/
|
|
1231
|
+
const makeEventBuffer = (logger) => {
|
|
1232
|
+
const ev = new node_events_1.default();
|
|
1233
|
+
const historyCache = new Set();
|
|
1234
|
+
let data = makeBufferData();
|
|
1235
|
+
let isBuffering = false;
|
|
1236
|
+
let bufferTimeout = null;
|
|
1237
|
+
let flushPendingTimeout = null; // Add a specific timer for the debounced flush to prevent leak
|
|
1238
|
+
let bufferCount = 0;
|
|
1239
|
+
const MAX_HISTORY_CACHE_SIZE = 10000; // Limit the history cache size to prevent memory bloat
|
|
1240
|
+
const BUFFER_TIMEOUT_MS = 30000; // 30 seconds
|
|
1241
|
+
// take the generic event and fire it as a baileys event
|
|
1242
|
+
ev.on('event', (map) => {
|
|
1243
|
+
for (const event in map) {
|
|
1244
|
+
ev.emit(event, map[event]);
|
|
1245
|
+
}
|
|
1246
|
+
});
|
|
1247
|
+
function buffer() {
|
|
1248
|
+
if (!isBuffering) {
|
|
1249
|
+
logger.debug('Event buffer activated');
|
|
1250
|
+
isBuffering = true;
|
|
1251
|
+
bufferCount = 0;
|
|
1252
|
+
if (bufferTimeout) {
|
|
1253
|
+
clearTimeout(bufferTimeout);
|
|
1254
|
+
}
|
|
1255
|
+
bufferTimeout = setTimeout(() => {
|
|
1256
|
+
if (isBuffering) {
|
|
1257
|
+
logger.warn('Buffer timeout reached, auto-flushing');
|
|
1258
|
+
flush();
|
|
1259
|
+
}
|
|
1260
|
+
}, BUFFER_TIMEOUT_MS);
|
|
1261
|
+
}
|
|
1262
|
+
// Always increment count when requested
|
|
1263
|
+
bufferCount++;
|
|
1264
|
+
}
|
|
1265
|
+
function flush() {
|
|
1266
|
+
if (!isBuffering) {
|
|
1267
|
+
return false;
|
|
1268
|
+
}
|
|
1269
|
+
logger.debug({ bufferCount }, 'Flushing event buffer');
|
|
1270
|
+
isBuffering = false;
|
|
1271
|
+
bufferCount = 0;
|
|
1272
|
+
// Clear timeout
|
|
1273
|
+
if (bufferTimeout) {
|
|
1274
|
+
clearTimeout(bufferTimeout);
|
|
1275
|
+
bufferTimeout = null;
|
|
1276
|
+
}
|
|
1277
|
+
if (flushPendingTimeout) {
|
|
1278
|
+
clearTimeout(flushPendingTimeout);
|
|
1279
|
+
flushPendingTimeout = null;
|
|
1280
|
+
}
|
|
1281
|
+
// Clear history cache if it exceeds the max size
|
|
1282
|
+
if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
|
|
1283
|
+
logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache');
|
|
1284
|
+
historyCache.clear();
|
|
1285
|
+
}
|
|
1286
|
+
const newData = makeBufferData();
|
|
1287
|
+
const chatUpdates = Object.values(data.chatUpdates);
|
|
1288
|
+
let conditionalChatUpdatesLeft = 0;
|
|
1289
|
+
for (const update of chatUpdates) {
|
|
1290
|
+
if (update.conditional) {
|
|
1291
|
+
conditionalChatUpdatesLeft += 1;
|
|
1292
|
+
newData.chatUpdates[update.id] = update;
|
|
1293
|
+
delete data.chatUpdates[update.id];
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
const consolidatedData = consolidateEvents(data);
|
|
1297
|
+
if (Object.keys(consolidatedData).length) {
|
|
1298
|
+
ev.emit('event', consolidatedData);
|
|
1299
|
+
}
|
|
1300
|
+
data = newData;
|
|
1301
|
+
logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events');
|
|
1302
|
+
return true;
|
|
1303
|
+
}
|
|
1304
|
+
return {
|
|
1305
|
+
process(handler) {
|
|
1306
|
+
const listener = async (map) => {
|
|
1307
|
+
await handler(map);
|
|
1308
|
+
};
|
|
1309
|
+
ev.on('event', listener);
|
|
1310
|
+
return () => {
|
|
1311
|
+
ev.off('event', listener);
|
|
1312
|
+
};
|
|
1313
|
+
},
|
|
1314
|
+
emit(event, evData) {
|
|
1315
|
+
// Check if this is a messages.upsert with a different type than what's buffered
|
|
1316
|
+
// If so, flush the buffered messages first to avoid type overshadowing
|
|
1317
|
+
if (event === 'messages.upsert') {
|
|
1318
|
+
const { type } = evData;
|
|
1319
|
+
const existingUpserts = Object.values(data.messageUpserts);
|
|
1320
|
+
if (existingUpserts.length > 0) {
|
|
1321
|
+
const bufferedType = existingUpserts[0].type;
|
|
1322
|
+
if (bufferedType !== type) {
|
|
1323
|
+
logger.debug({ bufferedType, newType: type }, 'messages.upsert type mismatch, emitting buffered messages');
|
|
1324
|
+
// Emit the buffered messages with their correct type
|
|
1325
|
+
ev.emit('event', {
|
|
1326
|
+
'messages.upsert': {
|
|
1327
|
+
messages: existingUpserts.map(m => m.message),
|
|
1328
|
+
type: bufferedType
|
|
1329
|
+
}
|
|
1330
|
+
});
|
|
1331
|
+
// Clear the message upserts from the buffer
|
|
1332
|
+
data.messageUpserts = {};
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
|
|
1337
|
+
append(data, historyCache, event, evData, logger);
|
|
1338
|
+
return true;
|
|
1339
|
+
}
|
|
1340
|
+
return ev.emit('event', { [event]: evData });
|
|
1341
|
+
},
|
|
1342
|
+
isBuffering() {
|
|
1343
|
+
return isBuffering;
|
|
1344
|
+
},
|
|
1345
|
+
buffer,
|
|
1346
|
+
flush,
|
|
1347
|
+
createBufferedFunction(work) {
|
|
1348
|
+
return async (...args) => {
|
|
1349
|
+
buffer();
|
|
1350
|
+
try {
|
|
1351
|
+
const result = await work(...args);
|
|
1352
|
+
// If this is the only buffer, flush after a small delay
|
|
1353
|
+
if (bufferCount === 1) {
|
|
1354
|
+
setTimeout(() => {
|
|
1355
|
+
if (isBuffering && bufferCount === 1) {
|
|
1356
|
+
flush();
|
|
1357
|
+
}
|
|
1358
|
+
}, 100); // Small delay to allow nested buffers
|
|
1359
|
+
}
|
|
1360
|
+
return result;
|
|
1361
|
+
}
|
|
1362
|
+
catch (error) {
|
|
1363
|
+
throw error;
|
|
1364
|
+
}
|
|
1365
|
+
finally {
|
|
1366
|
+
bufferCount = Math.max(0, bufferCount - 1);
|
|
1367
|
+
if (bufferCount === 0) {
|
|
1368
|
+
// Only schedule ONE timeout, not 10,000
|
|
1369
|
+
if (!flushPendingTimeout) {
|
|
1370
|
+
flushPendingTimeout = setTimeout(flush, 100);
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
},
|
|
1376
|
+
on: (...args) => ev.on(...args),
|
|
1377
|
+
off: (...args) => ev.off(...args),
|
|
1378
|
+
removeAllListeners: (...args) => ev.removeAllListeners(...args),
|
|
1379
|
+
destroy() {
|
|
1380
|
+
// Clear buffer timeout
|
|
1381
|
+
if (bufferTimeout) {
|
|
1382
|
+
clearTimeout(bufferTimeout);
|
|
1383
|
+
bufferTimeout = null;
|
|
1384
|
+
}
|
|
1385
|
+
if (flushPendingTimeout) {
|
|
1386
|
+
clearTimeout(flushPendingTimeout);
|
|
1387
|
+
flushPendingTimeout = null;
|
|
1388
|
+
}
|
|
1389
|
+
// Clear history cache
|
|
1390
|
+
historyCache.clear();
|
|
1391
|
+
// Reset buffer data
|
|
1392
|
+
data = makeBufferData();
|
|
1393
|
+
isBuffering = false;
|
|
1394
|
+
bufferCount = 0;
|
|
1395
|
+
// Remove all listeners
|
|
1396
|
+
ev.removeAllListeners();
|
|
1397
|
+
logger.debug('Event buffer destroyed');
|
|
1398
|
+
}
|
|
1399
|
+
};
|
|
1400
|
+
};
|
|
1401
|
+
exports.makeEventBuffer = makeEventBuffer;
|
|
1402
|
+
const makeBufferData = () => {
|
|
1403
|
+
return {
|
|
1404
|
+
historySets: {
|
|
1405
|
+
chats: {},
|
|
1406
|
+
messages: {},
|
|
1407
|
+
contacts: {},
|
|
1408
|
+
isLatest: false,
|
|
1409
|
+
empty: true
|
|
1410
|
+
},
|
|
1411
|
+
chatUpserts: {},
|
|
1412
|
+
chatUpdates: {},
|
|
1413
|
+
chatDeletes: new Set(),
|
|
1414
|
+
contactUpserts: {},
|
|
1415
|
+
contactUpdates: {},
|
|
1416
|
+
messageUpserts: {},
|
|
1417
|
+
messageUpdates: {},
|
|
1418
|
+
messageReactions: {},
|
|
1419
|
+
messageDeletes: {},
|
|
1420
|
+
messageReceipts: {},
|
|
1421
|
+
groupUpdates: {}
|
|
1422
|
+
};
|
|
1423
|
+
};
|
|
1424
|
+
function append(data, historyCache, event,
|
|
1425
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1426
|
+
eventData, logger) {
|
|
1427
|
+
switch (event) {
|
|
1428
|
+
case 'messaging-history.set':
|
|
1429
|
+
for (const chat of eventData.chats) {
|
|
1430
|
+
const id = chat.id || '';
|
|
1431
|
+
const existingChat = data.historySets.chats[id];
|
|
1432
|
+
if (existingChat) {
|
|
1433
|
+
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType;
|
|
1434
|
+
}
|
|
1435
|
+
if (!existingChat && !historyCache.has(id)) {
|
|
1436
|
+
data.historySets.chats[id] = chat;
|
|
1437
|
+
historyCache.add(id);
|
|
1438
|
+
absorbingChatUpdate(chat);
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
for (const contact of eventData.contacts) {
|
|
1442
|
+
const existingContact = data.historySets.contacts[contact.id];
|
|
1443
|
+
if (existingContact) {
|
|
1444
|
+
Object.assign(existingContact, (0, generics_js_1.trimUndefined)(contact));
|
|
1445
|
+
}
|
|
1446
|
+
else {
|
|
1447
|
+
const historyContactId = `c:${contact.id}`;
|
|
1448
|
+
const hasAnyName = contact.notify || contact.name || contact.verifiedName;
|
|
1449
|
+
if (!historyCache.has(historyContactId) || hasAnyName) {
|
|
1450
|
+
data.historySets.contacts[contact.id] = contact;
|
|
1451
|
+
historyCache.add(historyContactId);
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
for (const message of eventData.messages) {
|
|
1456
|
+
const key = stringifyMessageKey(message.key);
|
|
1457
|
+
const existingMsg = data.historySets.messages[key];
|
|
1458
|
+
if (!existingMsg && !historyCache.has(key)) {
|
|
1459
|
+
data.historySets.messages[key] = message;
|
|
1460
|
+
historyCache.add(key);
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
data.historySets.empty = false;
|
|
1464
|
+
data.historySets.syncType = eventData.syncType;
|
|
1465
|
+
if (eventData.pastParticipants?.length) {
|
|
1466
|
+
const merged = new Map();
|
|
1467
|
+
const sigOf = (p) => `${p.userJid || ''}:${p.leaveTs || ''}:${p.leaveReason || ''}`;
|
|
1468
|
+
const ingest = (entry) => {
|
|
1469
|
+
const key = entry.groupJid ?? JSON.stringify(entry);
|
|
1470
|
+
const existing = merged.get(key);
|
|
1471
|
+
if (!existing) {
|
|
1472
|
+
merged.set(key, { ...entry, pastParticipants: [...(entry.pastParticipants || [])] });
|
|
1473
|
+
return;
|
|
1474
|
+
}
|
|
1475
|
+
const seen = new Set((existing.pastParticipants || []).map(sigOf));
|
|
1476
|
+
for (const p of entry.pastParticipants || []) {
|
|
1477
|
+
const sig = sigOf(p);
|
|
1478
|
+
if (!seen.has(sig)) {
|
|
1479
|
+
existing.pastParticipants.push(p);
|
|
1480
|
+
seen.add(sig);
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
};
|
|
1484
|
+
for (const entry of data.historySets.pastParticipants || [])
|
|
1485
|
+
ingest(entry);
|
|
1486
|
+
for (const entry of eventData.pastParticipants)
|
|
1487
|
+
ingest(entry);
|
|
1488
|
+
data.historySets.pastParticipants = [...merged.values()];
|
|
1489
|
+
}
|
|
1490
|
+
data.historySets.progress = eventData.progress;
|
|
1491
|
+
data.historySets.chunkOrder = eventData.chunkOrder;
|
|
1492
|
+
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId;
|
|
1493
|
+
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest;
|
|
1494
|
+
break;
|
|
1495
|
+
case 'chats.upsert':
|
|
1496
|
+
for (const chat of eventData) {
|
|
1497
|
+
const id = chat.id || '';
|
|
1498
|
+
let upsert = data.chatUpserts[id];
|
|
1499
|
+
if (id && !upsert) {
|
|
1500
|
+
upsert = data.historySets.chats[id];
|
|
1501
|
+
if (upsert) {
|
|
1502
|
+
logger.debug({ chatId: id }, 'absorbed chat upsert in chat set');
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
if (upsert) {
|
|
1506
|
+
upsert = concatChats(upsert, chat);
|
|
1507
|
+
}
|
|
1508
|
+
else {
|
|
1509
|
+
upsert = chat;
|
|
1510
|
+
data.chatUpserts[id] = upsert;
|
|
1511
|
+
}
|
|
1512
|
+
absorbingChatUpdate(upsert);
|
|
1513
|
+
if (data.chatDeletes.has(id)) {
|
|
1514
|
+
data.chatDeletes.delete(id);
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
break;
|
|
1518
|
+
case 'chats.update':
|
|
1519
|
+
for (const update of eventData) {
|
|
1520
|
+
const chatId = update.id;
|
|
1521
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true;
|
|
1522
|
+
if (conditionMatches) {
|
|
1523
|
+
delete update.conditional;
|
|
1524
|
+
// if there is an existing upsert, merge the update into it
|
|
1525
|
+
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId];
|
|
1526
|
+
if (upsert) {
|
|
1527
|
+
concatChats(upsert, update);
|
|
1528
|
+
}
|
|
1529
|
+
else {
|
|
1530
|
+
// merge the update into the existing update
|
|
1531
|
+
const chatUpdate = data.chatUpdates[chatId] || {};
|
|
1532
|
+
data.chatUpdates[chatId] = concatChats(chatUpdate, update);
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
else if (conditionMatches === undefined) {
|
|
1536
|
+
// condition yet to be fulfilled
|
|
1537
|
+
data.chatUpdates[chatId] = update;
|
|
1538
|
+
}
|
|
1539
|
+
// otherwise -- condition not met, update is invalid
|
|
1540
|
+
// if the chat has been updated
|
|
1541
|
+
// ignore any existing chat delete
|
|
1542
|
+
if (data.chatDeletes.has(chatId)) {
|
|
1543
|
+
data.chatDeletes.delete(chatId);
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
break;
|
|
1547
|
+
case 'chats.delete':
|
|
1548
|
+
for (const chatId of eventData) {
|
|
1549
|
+
if (!data.chatDeletes.has(chatId)) {
|
|
1550
|
+
data.chatDeletes.add(chatId);
|
|
1551
|
+
}
|
|
1552
|
+
// remove any prior updates & upserts
|
|
1553
|
+
if (data.chatUpdates[chatId]) {
|
|
1554
|
+
delete data.chatUpdates[chatId];
|
|
1555
|
+
}
|
|
1556
|
+
if (data.chatUpserts[chatId]) {
|
|
1557
|
+
delete data.chatUpserts[chatId];
|
|
1558
|
+
}
|
|
1559
|
+
if (data.historySets.chats[chatId]) {
|
|
1560
|
+
delete data.historySets.chats[chatId];
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
break;
|
|
1564
|
+
case 'contacts.upsert':
|
|
1565
|
+
for (const contact of eventData) {
|
|
1566
|
+
let upsert = data.contactUpserts[contact.id];
|
|
1567
|
+
if (!upsert) {
|
|
1568
|
+
upsert = data.historySets.contacts[contact.id];
|
|
1569
|
+
if (upsert) {
|
|
1570
|
+
logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set');
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
if (upsert) {
|
|
1574
|
+
upsert = Object.assign(upsert, (0, generics_js_1.trimUndefined)(contact));
|
|
1575
|
+
}
|
|
1576
|
+
else {
|
|
1577
|
+
upsert = contact;
|
|
1578
|
+
data.contactUpserts[contact.id] = upsert;
|
|
1579
|
+
}
|
|
1580
|
+
if (data.contactUpdates[contact.id]) {
|
|
1581
|
+
upsert = Object.assign(data.contactUpdates[contact.id], (0, generics_js_1.trimUndefined)(contact));
|
|
1582
|
+
delete data.contactUpdates[contact.id];
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
break;
|
|
1586
|
+
case 'contacts.update':
|
|
1587
|
+
const contactUpdates = eventData;
|
|
1588
|
+
for (const update of contactUpdates) {
|
|
1589
|
+
const id = update.id;
|
|
1590
|
+
// merge into prior upsert
|
|
1591
|
+
const upsert = data.historySets.contacts[id] || data.contactUpserts[id];
|
|
1592
|
+
if (upsert) {
|
|
1593
|
+
Object.assign(upsert, update);
|
|
1594
|
+
}
|
|
1595
|
+
else {
|
|
1596
|
+
// merge into prior update
|
|
1597
|
+
const contactUpdate = data.contactUpdates[id] || {};
|
|
1598
|
+
data.contactUpdates[id] = Object.assign(contactUpdate, update);
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
break;
|
|
1602
|
+
case 'messages.upsert':
|
|
1603
|
+
const { messages, type } = eventData;
|
|
1604
|
+
for (const message of messages) {
|
|
1605
|
+
const key = stringifyMessageKey(message.key);
|
|
1606
|
+
let existing = data.messageUpserts[key]?.message;
|
|
1607
|
+
if (!existing) {
|
|
1608
|
+
existing = data.historySets.messages[key];
|
|
1609
|
+
if (existing) {
|
|
1610
|
+
logger.debug({ messageId: key }, 'absorbed message upsert in message set');
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
if (existing) {
|
|
1614
|
+
message.messageTimestamp = existing.messageTimestamp;
|
|
1615
|
+
}
|
|
1616
|
+
if (data.messageUpdates[key]) {
|
|
1617
|
+
logger.debug('absorbed prior message update in message upsert');
|
|
1618
|
+
Object.assign(message, data.messageUpdates[key].update);
|
|
1619
|
+
delete data.messageUpdates[key];
|
|
1620
|
+
}
|
|
1621
|
+
if (data.historySets.messages[key]) {
|
|
1622
|
+
data.historySets.messages[key] = message;
|
|
1623
|
+
}
|
|
1624
|
+
else {
|
|
1625
|
+
data.messageUpserts[key] = {
|
|
1626
|
+
message,
|
|
1627
|
+
type: type === 'notify' || data.messageUpserts[key]?.type === 'notify' ? 'notify' : type
|
|
1628
|
+
};
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
break;
|
|
1632
|
+
case 'messages.update':
|
|
1633
|
+
const msgUpdates = eventData;
|
|
1634
|
+
for (const { key, update } of msgUpdates) {
|
|
1635
|
+
const keyStr = stringifyMessageKey(key);
|
|
1636
|
+
const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message;
|
|
1637
|
+
if (existing) {
|
|
1638
|
+
Object.assign(existing, update);
|
|
1639
|
+
// if the message was received & read by us
|
|
1640
|
+
// the chat counter must have been incremented
|
|
1641
|
+
// so we need to decrement it
|
|
1642
|
+
if (update.status === index_js_1.WAMessageStatus.READ && !key.fromMe) {
|
|
1643
|
+
decrementChatReadCounterIfMsgDidUnread(existing);
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
else {
|
|
1647
|
+
const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} };
|
|
1648
|
+
Object.assign(msgUpdate.update, update);
|
|
1649
|
+
data.messageUpdates[keyStr] = msgUpdate;
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
break;
|
|
1653
|
+
case 'messages.delete':
|
|
1654
|
+
const deleteData = eventData;
|
|
1655
|
+
if ('keys' in deleteData) {
|
|
1656
|
+
const { keys } = deleteData;
|
|
1657
|
+
for (const key of keys) {
|
|
1658
|
+
const keyStr = stringifyMessageKey(key);
|
|
1659
|
+
if (!data.messageDeletes[keyStr]) {
|
|
1660
|
+
data.messageDeletes[keyStr] = key;
|
|
1661
|
+
}
|
|
1662
|
+
if (data.messageUpserts[keyStr]) {
|
|
1663
|
+
delete data.messageUpserts[keyStr];
|
|
1664
|
+
}
|
|
1665
|
+
if (data.messageUpdates[keyStr]) {
|
|
1666
|
+
delete data.messageUpdates[keyStr];
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
else {
|
|
1671
|
+
// TODO: add support
|
|
1672
|
+
}
|
|
1673
|
+
break;
|
|
1674
|
+
case 'messages.reaction':
|
|
1675
|
+
const reactions = eventData;
|
|
1676
|
+
for (const { key, reaction } of reactions) {
|
|
1677
|
+
const keyStr = stringifyMessageKey(key);
|
|
1678
|
+
const existing = data.messageUpserts[keyStr];
|
|
1679
|
+
if (existing) {
|
|
1680
|
+
(0, messages_js_1.updateMessageWithReaction)(existing.message, reaction);
|
|
1681
|
+
}
|
|
1682
|
+
else {
|
|
1683
|
+
data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] };
|
|
1684
|
+
(0, messages_js_1.updateMessageWithReaction)(data.messageReactions[keyStr], reaction);
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
break;
|
|
1688
|
+
case 'message-receipt.update':
|
|
1689
|
+
const receipts = eventData;
|
|
1690
|
+
for (const { key, receipt } of receipts) {
|
|
1691
|
+
const keyStr = stringifyMessageKey(key);
|
|
1692
|
+
const existing = data.messageUpserts[keyStr];
|
|
1693
|
+
if (existing) {
|
|
1694
|
+
(0, messages_js_1.updateMessageWithReceipt)(existing.message, receipt);
|
|
1695
|
+
}
|
|
1696
|
+
else {
|
|
1697
|
+
data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] };
|
|
1698
|
+
(0, messages_js_1.updateMessageWithReceipt)(data.messageReceipts[keyStr], receipt);
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
break;
|
|
1702
|
+
case 'groups.update':
|
|
1703
|
+
const groupUpdates = eventData;
|
|
1704
|
+
for (const update of groupUpdates) {
|
|
1705
|
+
const id = update.id;
|
|
1706
|
+
const groupUpdate = data.groupUpdates[id] || {};
|
|
1707
|
+
if (!data.groupUpdates[id]) {
|
|
1708
|
+
data.groupUpdates[id] = Object.assign(groupUpdate, update);
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
break;
|
|
1712
|
+
default:
|
|
1713
|
+
throw new Error(`"${event}" cannot be buffered`);
|
|
1714
|
+
}
|
|
1715
|
+
function absorbingChatUpdate(existing) {
|
|
1716
|
+
const chatId = existing.id || '';
|
|
1717
|
+
const update = data.chatUpdates[chatId];
|
|
1718
|
+
if (update) {
|
|
1719
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true;
|
|
1720
|
+
if (conditionMatches) {
|
|
1721
|
+
delete update.conditional;
|
|
1722
|
+
logger.debug({ chatId }, 'absorbed chat update in existing chat');
|
|
1723
|
+
Object.assign(existing, concatChats(update, existing));
|
|
1724
|
+
delete data.chatUpdates[chatId];
|
|
1725
|
+
}
|
|
1726
|
+
else if (conditionMatches === false) {
|
|
1727
|
+
logger.debug({ chatId }, 'chat update condition fail, removing');
|
|
1728
|
+
delete data.chatUpdates[chatId];
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
function decrementChatReadCounterIfMsgDidUnread(message) {
|
|
1733
|
+
// decrement chat unread counter
|
|
1734
|
+
// if the message has already been marked read by us
|
|
1735
|
+
const chatId = message.key.remoteJid;
|
|
1736
|
+
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
|
|
1737
|
+
if ((0, process_message_js_1.isRealMessage)(message) &&
|
|
1738
|
+
(0, process_message_js_1.shouldIncrementChatUnread)(message) &&
|
|
1739
|
+
typeof chat?.unreadCount === 'number' &&
|
|
1740
|
+
chat.unreadCount > 0) {
|
|
1741
|
+
logger.debug({ chatId: chat.id }, 'decrementing chat counter');
|
|
1742
|
+
chat.unreadCount -= 1;
|
|
1743
|
+
if (chat.unreadCount === 0) {
|
|
1744
|
+
delete chat.unreadCount;
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
function consolidateEvents(data) {
|
|
1750
|
+
const map = {};
|
|
1751
|
+
if (!data.historySets.empty) {
|
|
1752
|
+
map['messaging-history.set'] = {
|
|
1753
|
+
chats: Object.values(data.historySets.chats),
|
|
1754
|
+
messages: Object.values(data.historySets.messages),
|
|
1755
|
+
contacts: Object.values(data.historySets.contacts),
|
|
1756
|
+
pastParticipants: data.historySets.pastParticipants,
|
|
1757
|
+
syncType: data.historySets.syncType,
|
|
1758
|
+
progress: data.historySets.progress,
|
|
1759
|
+
isLatest: data.historySets.isLatest,
|
|
1760
|
+
chunkOrder: data.historySets.chunkOrder,
|
|
1761
|
+
peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
|
|
1762
|
+
};
|
|
1763
|
+
}
|
|
1764
|
+
const chatUpsertList = Object.values(data.chatUpserts);
|
|
1765
|
+
if (chatUpsertList.length) {
|
|
1766
|
+
map['chats.upsert'] = chatUpsertList;
|
|
1767
|
+
}
|
|
1768
|
+
const chatUpdateList = Object.values(data.chatUpdates);
|
|
1769
|
+
if (chatUpdateList.length) {
|
|
1770
|
+
map['chats.update'] = chatUpdateList;
|
|
1771
|
+
}
|
|
1772
|
+
const chatDeleteList = Array.from(data.chatDeletes);
|
|
1773
|
+
if (chatDeleteList.length) {
|
|
1774
|
+
map['chats.delete'] = chatDeleteList;
|
|
1775
|
+
}
|
|
1776
|
+
const messageUpsertList = Object.values(data.messageUpserts);
|
|
1777
|
+
if (messageUpsertList.length) {
|
|
1778
|
+
const type = messageUpsertList[0].type;
|
|
1779
|
+
map['messages.upsert'] = {
|
|
1780
|
+
messages: messageUpsertList.map(m => m.message),
|
|
1781
|
+
type
|
|
1782
|
+
};
|
|
1783
|
+
}
|
|
1784
|
+
const messageUpdateList = Object.values(data.messageUpdates);
|
|
1785
|
+
if (messageUpdateList.length) {
|
|
1786
|
+
map['messages.update'] = messageUpdateList;
|
|
1787
|
+
}
|
|
1788
|
+
const messageDeleteList = Object.values(data.messageDeletes);
|
|
1789
|
+
if (messageDeleteList.length) {
|
|
1790
|
+
map['messages.delete'] = { keys: messageDeleteList };
|
|
1791
|
+
}
|
|
1792
|
+
const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) => reactions.flatMap(reaction => ({ key, reaction })));
|
|
1793
|
+
if (messageReactionList.length) {
|
|
1794
|
+
map['messages.reaction'] = messageReactionList;
|
|
1795
|
+
}
|
|
1796
|
+
const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) => userReceipt.flatMap(receipt => ({ key, receipt })));
|
|
1797
|
+
if (messageReceiptList.length) {
|
|
1798
|
+
map['message-receipt.update'] = messageReceiptList;
|
|
1799
|
+
}
|
|
1800
|
+
const contactUpsertList = Object.values(data.contactUpserts);
|
|
1801
|
+
if (contactUpsertList.length) {
|
|
1802
|
+
map['contacts.upsert'] = contactUpsertList;
|
|
1803
|
+
}
|
|
1804
|
+
const contactUpdateList = Object.values(data.contactUpdates);
|
|
1805
|
+
if (contactUpdateList.length) {
|
|
1806
|
+
map['contacts.update'] = contactUpdateList;
|
|
1807
|
+
}
|
|
1808
|
+
const groupUpdateList = Object.values(data.groupUpdates);
|
|
1809
|
+
if (groupUpdateList.length) {
|
|
1810
|
+
map['groups.update'] = groupUpdateList;
|
|
1811
|
+
}
|
|
1812
|
+
return map;
|
|
1813
|
+
}
|
|
1814
|
+
function concatChats(a, b) {
|
|
1815
|
+
if (b.unreadCount === null && // neutralize unread counter
|
|
1816
|
+
a.unreadCount < 0) {
|
|
1817
|
+
a.unreadCount = undefined;
|
|
1818
|
+
b.unreadCount = undefined;
|
|
1819
|
+
}
|
|
1820
|
+
if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
|
|
1821
|
+
b = { ...b };
|
|
1822
|
+
if (b.unreadCount >= 0) {
|
|
1823
|
+
b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0);
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
return Object.assign(a, b);
|
|
1827
|
+
}
|
|
1828
|
+
const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
|
|
1829
|
+
|
|
1830
|
+
return module.exports;
|
|
1831
|
+
})();
|
|
1832
|
+
for (const key of Object.keys(__kageModule168)) {
|
|
1833
|
+
if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule168[key]});
|
|
1834
|
+
}
|
|
1835
|
+
Object.defineProperty(exports, Symbol.for('kage.advanced'), {value:__kageModule168, configurable:true});
|