@badzz88/baileys 8.5.4 → 8.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/WAProto/fix-imports.js +69 -79
- package/WAProto/index.d.ts +15305 -87935
- package/WAProto/index.js +41109 -194076
- package/engine-requirements.js +15 -8
- package/package.json +23 -19
- package/src/Defaults/index.js +186 -0
- package/src/Signal/Group/ciphertext-message.js +15 -0
- package/src/Signal/Group/group-session-builder.js +86 -0
- package/src/Signal/Group/group_cipher.js +82 -0
- package/src/Signal/Group/index.js +140 -0
- package/src/Signal/Group/keyhelper.js +77 -0
- package/src/Signal/Group/sender-chain-key.js +29 -0
- package/src/Signal/Group/sender-key-distribution-message.js +66 -0
- package/src/Signal/Group/sender-key-message.js +69 -0
- package/src/Signal/Group/sender-key-name.js +51 -0
- package/src/Signal/Group/sender-key-record.js +44 -0
- package/src/Signal/Group/sender-key-state.js +87 -0
- package/src/Signal/Group/sender-message-key.js +29 -0
- package/src/Signal/libsignal.js +455 -0
- package/src/Signal/lid-mapping.js +273 -0
- package/src/Socket/Client/index.js +31 -0
- package/src/Socket/Client/types.js +13 -0
- package/src/Socket/Client/websocket.js +61 -0
- package/src/Socket/aigroups.js +221 -0
- package/src/Socket/business.js +411 -0
- package/src/Socket/chats.js +1449 -0
- package/src/Socket/communities.js +463 -0
- package/src/Socket/graphql.js +523 -0
- package/src/Socket/groups.js +516 -0
- package/src/Socket/index.js +31 -0
- package/src/Socket/interactive-handler.js +527 -0
- package/src/Socket/interop.js +339 -0
- package/src/Socket/luxu.js +349 -0
- package/src/Socket/managed-account.js +98 -0
- package/src/Socket/messages-recv.js +2685 -0
- package/src/Socket/messages-send.js +2047 -0
- package/src/Socket/mex.js +57 -0
- package/src/Socket/newsletter.js +455 -0
- package/src/Socket/privacy.js +125 -0
- package/src/Socket/registration.js +236 -0
- package/src/Socket/socket.js +983 -0
- package/src/Socket/text-router.js +65 -0
- package/src/Socket/username.js +130 -0
- package/src/Store/index.js +33 -0
- package/src/Store/keyed-db.js +118 -0
- package/src/Store/make-cache-manager-store.js +84 -0
- package/src/Store/make-in-memory-store.js +551 -0
- package/src/Store/make-ordered-dictionary.js +81 -0
- package/src/Store/object-repository.js +26 -0
- package/src/Types/Auth.js +31 -0
- package/src/Types/Bussines.js +2 -0
- package/src/Types/Call.js +2 -0
- package/src/Types/Chat.js +4 -0
- package/src/Types/Contact.js +2 -0
- package/src/Types/Events.js +2 -0
- package/src/Types/GroupMetadata.js +2 -0
- package/src/Types/Label.js +26 -0
- package/src/Types/LabelAssociation.js +8 -0
- package/src/Types/Message.js +93 -0
- package/src/Types/Mex.js +112 -0
- package/src/Types/Newsletter.js +109 -0
- package/src/Types/Product.js +2 -0
- package/src/Types/Signal.js +2 -0
- package/src/Types/Socket.js +2 -0
- package/src/Types/State.js +62 -0
- package/src/Types/USync.js +2 -0
- package/src/Types/index.js +56 -0
- package/src/Utils/auth-utils.js +254 -0
- package/src/Utils/browser-utils.js +112 -0
- package/src/Utils/business.js +240 -0
- package/src/Utils/chat-utils.js +1230 -0
- package/src/Utils/command-loader.d.ts +27 -0
- package/src/Utils/command-loader.js +89 -0
- package/src/Utils/companion-reg-client-utils.js +40 -0
- package/src/Utils/consumer-application.js +105 -0
- package/src/Utils/crypto.js +118 -0
- package/src/Utils/curve25519-js.js +242 -0
- package/src/Utils/decode-wa-message.js +529 -0
- package/src/Utils/event-buffer.js +580 -0
- package/src/Utils/generics.js +483 -0
- package/src/Utils/group-history.js +44 -0
- package/src/Utils/history.js +232 -0
- package/src/Utils/identity-change-handler.js +52 -0
- package/src/Utils/index.js +58 -0
- package/src/Utils/jid-display-normalization.js +195 -0
- package/src/Utils/link-preview.js +135 -0
- package/src/Utils/logger.js +8 -0
- package/src/Utils/lt-hash.js +25 -0
- package/src/Utils/make-mutex.js +36 -0
- package/src/Utils/message-composer.js +471 -0
- package/src/Utils/message-retry-manager.js +217 -0
- package/src/Utils/messages-media.js +843 -0
- package/src/Utils/messages.js +2817 -0
- package/src/Utils/meta-ai-msmsg.js +222 -0
- package/src/Utils/native-bridge.js +68 -0
- package/src/Utils/noise-handler.js +169 -0
- package/src/Utils/offline-node-processor.js +34 -0
- package/src/Utils/pre-key-manager.js +90 -0
- package/src/Utils/process-message.js +950 -0
- package/src/Utils/reporting-utils.js +261 -0
- package/src/Utils/session-pool.d.ts +14 -0
- package/src/Utils/session-pool.js +70 -0
- package/src/Utils/signal.js +217 -0
- package/src/Utils/stanza-ack.js +30 -0
- package/src/Utils/sticker.d.ts +13 -0
- package/src/Utils/sticker.js +123 -0
- package/src/Utils/sync-action-utils.js +45 -0
- package/src/Utils/tc-token-utils.js +158 -0
- package/src/Utils/use-multi-file-auth-state.js +111 -0
- package/src/Utils/validate-connection.js +209 -0
- package/src/Utils/view-once-cache.d.ts +12 -0
- package/src/Utils/view-once-cache.js +63 -0
- package/src/Utils/voip-rekey.js +22 -0
- package/src/WABinary/constants.js +1304 -0
- package/src/WABinary/decode.js +342 -0
- package/src/WABinary/encode.js +225 -0
- package/src/WABinary/generic-utils.js +238 -0
- package/src/WABinary/index.js +34 -0
- package/src/WABinary/jid-utils.js +351 -0
- package/src/WABinary/types.js +2 -0
- package/src/WAM/BinaryInfo.js +13 -0
- package/src/WAM/constants.js +39484 -0
- package/src/WAM/encode.js +149 -0
- package/src/WAM/index.js +32 -0
- package/src/WAUSync/Protocols/USyncBotProfileProtocol.js +53 -0
- package/src/WAUSync/Protocols/USyncBusinessProtocol.js +44 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.js +55 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.js +55 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +31 -0
- package/src/WAUSync/Protocols/USyncFeatureProtocol.js +54 -0
- package/src/WAUSync/Protocols/USyncLIDProtocol.js +32 -0
- package/src/WAUSync/Protocols/USyncNewsletterProtocol.js +473 -0
- package/src/WAUSync/Protocols/USyncPictureProtocol.js +34 -0
- package/src/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/src/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
- package/src/WAUSync/Protocols/USyncUsernameProtocol.js +28 -0
- package/src/WAUSync/Protocols/index.js +40 -0
- package/src/WAUSync/USyncQuery.js +126 -0
- package/src/WAUSync/USyncUser.js +50 -0
- package/src/WAUSync/index.js +32 -0
- package/src/antiban.js +4152 -0
- package/{lib → src}/index.js +51 -49
- package/lib/Defaults/index.js +0 -195
- package/lib/Signal/Group/ciphertext-message.js +0 -15
- package/lib/Signal/Group/group-session-builder.js +0 -92
- package/lib/Signal/Group/group_cipher.js +0 -89
- package/lib/Signal/Group/index.js +0 -136
- package/lib/Signal/Group/keyhelper.js +0 -73
- package/lib/Signal/Group/sender-chain-key.js +0 -32
- package/lib/Signal/Group/sender-key-distribution-message.js +0 -66
- package/lib/Signal/Group/sender-key-message.js +0 -69
- package/lib/Signal/Group/sender-key-name.js +0 -50
- package/lib/Signal/Group/sender-key-record.js +0 -44
- package/lib/Signal/Group/sender-key-state.js +0 -97
- package/lib/Signal/Group/sender-message-key.js +0 -30
- package/lib/Signal/libsignal.js +0 -470
- package/lib/Signal/lid-mapping.js +0 -280
- package/lib/Socket/Client/index.js +0 -30
- package/lib/Socket/Client/types.js +0 -13
- package/lib/Socket/Client/websocket.js +0 -62
- package/lib/Socket/aigroups.js +0 -240
- package/lib/Socket/business.js +0 -414
- package/lib/Socket/chats.js +0 -2165
- package/lib/Socket/communities.js +0 -545
- package/lib/Socket/graphql.js +0 -863
- package/lib/Socket/groups.js +0 -685
- package/lib/Socket/index.js +0 -41
- package/lib/Socket/interactive-handler.js +0 -579
- package/lib/Socket/interop.js +0 -430
- package/lib/Socket/managed-account.js +0 -214
- package/lib/Socket/messages-recv.js +0 -3000
- package/lib/Socket/messages-send.js +0 -2155
- package/lib/Socket/mex.js +0 -47
- package/lib/Socket/newsletter.js +0 -814
- package/lib/Socket/privacy.js +0 -261
- package/lib/Socket/registration.js +0 -434
- package/lib/Socket/socket.js +0 -1074
- package/lib/Socket/username.js +0 -160
- package/lib/Store/index.js +0 -36
- package/lib/Store/make-cache-manager-store.js +0 -90
- package/lib/Store/make-in-memory-store.js +0 -488
- package/lib/Store/make-ordered-dictionary.js +0 -81
- package/lib/Store/object-repository.js +0 -29
- package/lib/Types/Auth.js +0 -38
- package/lib/Types/Bussines.js +0 -2
- package/lib/Types/Call.js +0 -2
- package/lib/Types/Chat.js +0 -4
- package/lib/Types/Contact.js +0 -2
- package/lib/Types/Events.js +0 -2
- package/lib/Types/GroupMetadata.js +0 -2
- package/lib/Types/Label.js +0 -27
- package/lib/Types/LabelAssociation.js +0 -9
- package/lib/Types/Message.js +0 -95
- package/lib/Types/Newsletter.js +0 -152
- package/lib/Types/Product.js +0 -2
- package/lib/Types/Signal.js +0 -2
- package/lib/Types/Socket.js +0 -2
- package/lib/Types/State.js +0 -70
- package/lib/Types/USync.js +0 -2
- package/lib/Types/index.js +0 -55
- package/lib/Utils/auth-utils.js +0 -301
- package/lib/Utils/browser-utils.js +0 -114
- package/lib/Utils/business.js +0 -243
- package/lib/Utils/chat-utils.js +0 -1272
- package/lib/Utils/consumer-application.js +0 -107
- package/lib/Utils/crypto.js +0 -125
- package/lib/Utils/decode-wa-message.js +0 -793
- package/lib/Utils/event-buffer.js +0 -583
- package/lib/Utils/generics.js +0 -617
- package/lib/Utils/group-history.js +0 -51
- package/lib/Utils/history.js +0 -244
- package/lib/Utils/identity-change-handler.js +0 -52
- package/lib/Utils/index.js +0 -53
- package/lib/Utils/jid-display-normalization.js +0 -218
- package/lib/Utils/link-preview.js +0 -138
- package/lib/Utils/logger.js +0 -9
- package/lib/Utils/lt-hash.js +0 -6
- package/lib/Utils/make-mutex.js +0 -36
- package/lib/Utils/message-composer.js +0 -479
- package/lib/Utils/message-inspect.js +0 -393
- package/lib/Utils/message-retry-manager.js +0 -205
- package/lib/Utils/messages-media.js +0 -925
- package/lib/Utils/messages.js +0 -2482
- package/lib/Utils/meta-ai-msmsg.js +0 -122
- package/lib/Utils/noise-handler.js +0 -194
- package/lib/Utils/offline-node-processor.js +0 -37
- package/lib/Utils/pre-key-manager.js +0 -97
- package/lib/Utils/process-message.js +0 -1139
- package/lib/Utils/reporting-utils.js +0 -262
- package/lib/Utils/signal.js +0 -189
- package/lib/Utils/stanza-ack.js +0 -67
- package/lib/Utils/sync-action-utils.js +0 -51
- package/lib/Utils/tc-token-utils.js +0 -156
- package/lib/Utils/use-multi-file-auth-state.js +0 -137
- package/lib/Utils/validate-connection.js +0 -259
- package/lib/Utils/voip-rekey.js +0 -22
- package/lib/WABinary/constants.js +0 -1304
- package/lib/WABinary/decode.js +0 -377
- package/lib/WABinary/encode.js +0 -58
- package/lib/WABinary/generic-utils.js +0 -150
- package/lib/WABinary/index.js +0 -33
- package/lib/WABinary/jid-utils.js +0 -374
- package/lib/WABinary/types.js +0 -2
- package/lib/WAM/BinaryInfo.js +0 -13
- package/lib/WAM/constants.js +0 -39486
- package/lib/WAM/encode.js +0 -142
- package/lib/WAM/index.js +0 -31
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +0 -55
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +0 -100
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -60
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -65
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -27
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +0 -72
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -31
- package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -865
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +0 -32
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +0 -29
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -44
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +0 -38
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +0 -28
- package/lib/WAUSync/Protocols/index.js +0 -40
- package/lib/WAUSync/USyncBackoff.js +0 -26
- package/lib/WAUSync/USyncQuery.js +0 -204
- package/lib/WAUSync/USyncUser.js +0 -58
- package/lib/WAUSync/index.js +0 -32
- package/lib/antiban.js +0 -4391
- /package/{lib → src}/Defaults/phonenumber-mcc.json +0 -0
|
@@ -1,583 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
var __importDefault =
|
|
3
|
-
(this && this.__importDefault) ||
|
|
4
|
-
function (mod) {
|
|
5
|
-
return mod && mod.__esModule ? mod : { default: mod }
|
|
6
|
-
}
|
|
7
|
-
Object.defineProperty(exports, '__esModule', { value: true })
|
|
8
|
-
exports.makeEventBuffer = void 0
|
|
9
|
-
const events_1 = __importDefault(require('events'))
|
|
10
|
-
const Types_1 = require('../Types')
|
|
11
|
-
const generics_1 = require('./generics')
|
|
12
|
-
const messages_1 = require('./messages')
|
|
13
|
-
const process_message_1 = require('./process-message')
|
|
14
|
-
const BUFFERABLE_EVENT = [
|
|
15
|
-
'messaging-history.set',
|
|
16
|
-
'chats.upsert',
|
|
17
|
-
'chats.update',
|
|
18
|
-
'chats.delete',
|
|
19
|
-
'contacts.upsert',
|
|
20
|
-
'contacts.update',
|
|
21
|
-
'messages.upsert',
|
|
22
|
-
'messages.update',
|
|
23
|
-
'messages.delete',
|
|
24
|
-
'messages.reaction',
|
|
25
|
-
'message-receipt.update',
|
|
26
|
-
'groups.update'
|
|
27
|
-
]
|
|
28
|
-
const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT)
|
|
29
|
-
|
|
30
|
-
const makeEventBuffer = logger => {
|
|
31
|
-
const ev = new events_1.default()
|
|
32
|
-
const historyCache = new Set()
|
|
33
|
-
let data = makeBufferData()
|
|
34
|
-
let isBuffering = false
|
|
35
|
-
let bufferTimeout = null
|
|
36
|
-
let flushPendingTimeout = null
|
|
37
|
-
let bufferCount = 0
|
|
38
|
-
const MAX_HISTORY_CACHE_SIZE = 10000
|
|
39
|
-
const BUFFER_TIMEOUT_MS = 30000
|
|
40
|
-
|
|
41
|
-
ev.on('event', map => {
|
|
42
|
-
for (const event in map) {
|
|
43
|
-
ev.emit(event, map[event])
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
function buffer() {
|
|
47
|
-
if (!isBuffering) {
|
|
48
|
-
logger.debug('Event buffer activated')
|
|
49
|
-
isBuffering = true
|
|
50
|
-
bufferCount = 0
|
|
51
|
-
if (bufferTimeout) {
|
|
52
|
-
clearTimeout(bufferTimeout)
|
|
53
|
-
}
|
|
54
|
-
bufferTimeout = setTimeout(() => {
|
|
55
|
-
if (isBuffering) {
|
|
56
|
-
logger.warn('Buffer timeout reached, auto-flushing')
|
|
57
|
-
flush()
|
|
58
|
-
}
|
|
59
|
-
}, BUFFER_TIMEOUT_MS)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
bufferCount++
|
|
63
|
-
}
|
|
64
|
-
function flush() {
|
|
65
|
-
if (!isBuffering) {
|
|
66
|
-
return false
|
|
67
|
-
}
|
|
68
|
-
logger.debug({ bufferCount }, 'Flushing event buffer')
|
|
69
|
-
isBuffering = false
|
|
70
|
-
bufferCount = 0
|
|
71
|
-
|
|
72
|
-
if (bufferTimeout) {
|
|
73
|
-
clearTimeout(bufferTimeout)
|
|
74
|
-
bufferTimeout = null
|
|
75
|
-
}
|
|
76
|
-
if (flushPendingTimeout) {
|
|
77
|
-
clearTimeout(flushPendingTimeout)
|
|
78
|
-
flushPendingTimeout = null
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
|
|
82
|
-
logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache')
|
|
83
|
-
historyCache.clear()
|
|
84
|
-
}
|
|
85
|
-
const newData = makeBufferData()
|
|
86
|
-
const chatUpdates = Object.values(data.chatUpdates)
|
|
87
|
-
let conditionalChatUpdatesLeft = 0
|
|
88
|
-
for (const update of chatUpdates) {
|
|
89
|
-
if (update.conditional) {
|
|
90
|
-
conditionalChatUpdatesLeft += 1
|
|
91
|
-
newData.chatUpdates[update.id] = update
|
|
92
|
-
delete data.chatUpdates[update.id]
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
const consolidatedData = consolidateEvents(data)
|
|
96
|
-
if (Object.keys(consolidatedData).length) {
|
|
97
|
-
ev.emit('event', consolidatedData)
|
|
98
|
-
}
|
|
99
|
-
data = newData
|
|
100
|
-
logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events')
|
|
101
|
-
return true
|
|
102
|
-
}
|
|
103
|
-
return {
|
|
104
|
-
process(handler) {
|
|
105
|
-
const listener = async map => {
|
|
106
|
-
await handler(map)
|
|
107
|
-
}
|
|
108
|
-
ev.on('event', listener)
|
|
109
|
-
return () => {
|
|
110
|
-
ev.off('event', listener)
|
|
111
|
-
}
|
|
112
|
-
},
|
|
113
|
-
emit(event, evData) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
if (event === 'messages.upsert') {
|
|
117
|
-
const { type } = evData
|
|
118
|
-
const existingUpserts = Object.values(data.messageUpserts)
|
|
119
|
-
if (existingUpserts.length > 0) {
|
|
120
|
-
const bufferedType = existingUpserts[0].type
|
|
121
|
-
if (bufferedType !== type) {
|
|
122
|
-
logger.debug({ bufferedType, newType: type }, 'messages.upsert type mismatch, emitting buffered messages')
|
|
123
|
-
|
|
124
|
-
ev.emit('event', {
|
|
125
|
-
'messages.upsert': {
|
|
126
|
-
messages: existingUpserts.map(m => m.message),
|
|
127
|
-
type: bufferedType
|
|
128
|
-
}
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
data.messageUpserts = {}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
|
|
136
|
-
append(data, historyCache, event, evData, logger)
|
|
137
|
-
return true
|
|
138
|
-
}
|
|
139
|
-
return ev.emit('event', { [event]: evData })
|
|
140
|
-
},
|
|
141
|
-
isBuffering() {
|
|
142
|
-
return isBuffering
|
|
143
|
-
},
|
|
144
|
-
buffer,
|
|
145
|
-
flush,
|
|
146
|
-
createBufferedFunction(work) {
|
|
147
|
-
return async (...args) => {
|
|
148
|
-
buffer()
|
|
149
|
-
try {
|
|
150
|
-
const result = await work(...args)
|
|
151
|
-
|
|
152
|
-
if (bufferCount === 1) {
|
|
153
|
-
setTimeout(() => {
|
|
154
|
-
if (isBuffering && bufferCount === 1) {
|
|
155
|
-
flush()
|
|
156
|
-
}
|
|
157
|
-
}, 100)
|
|
158
|
-
}
|
|
159
|
-
return result
|
|
160
|
-
} catch (error) {
|
|
161
|
-
throw error
|
|
162
|
-
} finally {
|
|
163
|
-
bufferCount = Math.max(0, bufferCount - 1)
|
|
164
|
-
if (bufferCount === 0) {
|
|
165
|
-
|
|
166
|
-
if (!flushPendingTimeout) {
|
|
167
|
-
flushPendingTimeout = setTimeout(flush, 100)
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
},
|
|
173
|
-
on: (...args) => ev.on(...args),
|
|
174
|
-
off: (...args) => ev.off(...args),
|
|
175
|
-
once: (...args) => ev.once(...args),
|
|
176
|
-
removeAllListeners: (...args) => ev.removeAllListeners(...args)
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
exports.makeEventBuffer = makeEventBuffer
|
|
180
|
-
const makeBufferData = () => {
|
|
181
|
-
return {
|
|
182
|
-
historySets: {
|
|
183
|
-
chats: {},
|
|
184
|
-
messages: {},
|
|
185
|
-
contacts: {},
|
|
186
|
-
isLatest: false,
|
|
187
|
-
empty: true
|
|
188
|
-
},
|
|
189
|
-
chatUpserts: {},
|
|
190
|
-
chatUpdates: {},
|
|
191
|
-
chatDeletes: new Set(),
|
|
192
|
-
contactUpserts: {},
|
|
193
|
-
contactUpdates: {},
|
|
194
|
-
messageUpserts: {},
|
|
195
|
-
messageUpdates: {},
|
|
196
|
-
messageReactions: {},
|
|
197
|
-
messageDeletes: {},
|
|
198
|
-
messageReceipts: {},
|
|
199
|
-
groupUpdates: {}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
function append(
|
|
203
|
-
data,
|
|
204
|
-
historyCache,
|
|
205
|
-
event,
|
|
206
|
-
|
|
207
|
-
eventData,
|
|
208
|
-
logger
|
|
209
|
-
) {
|
|
210
|
-
switch (event) {
|
|
211
|
-
case 'messaging-history.set':
|
|
212
|
-
for (const chat of eventData.chats) {
|
|
213
|
-
const id = chat.id || ''
|
|
214
|
-
const existingChat = data.historySets.chats[id]
|
|
215
|
-
if (existingChat) {
|
|
216
|
-
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType
|
|
217
|
-
}
|
|
218
|
-
if (!existingChat && !historyCache.has(id)) {
|
|
219
|
-
data.historySets.chats[id] = chat
|
|
220
|
-
historyCache.add(id)
|
|
221
|
-
absorbingChatUpdate(chat)
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
for (const contact of eventData.contacts) {
|
|
225
|
-
const existingContact = data.historySets.contacts[contact.id]
|
|
226
|
-
if (existingContact) {
|
|
227
|
-
Object.assign(existingContact, (0, generics_1.trimUndefined)(contact))
|
|
228
|
-
} else {
|
|
229
|
-
const historyContactId = `c:${contact.id}`
|
|
230
|
-
const hasAnyName = contact.notify || contact.name || contact.verifiedName
|
|
231
|
-
if (!historyCache.has(historyContactId) || hasAnyName) {
|
|
232
|
-
data.historySets.contacts[contact.id] = contact
|
|
233
|
-
historyCache.add(historyContactId)
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
for (const message of eventData.messages) {
|
|
238
|
-
const key = stringifyMessageKey(message.key)
|
|
239
|
-
const existingMsg = data.historySets.messages[key]
|
|
240
|
-
if (!existingMsg && !historyCache.has(key)) {
|
|
241
|
-
data.historySets.messages[key] = message
|
|
242
|
-
historyCache.add(key)
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
data.historySets.empty = false
|
|
246
|
-
data.historySets.syncType = eventData.syncType
|
|
247
|
-
data.historySets.progress = eventData.progress
|
|
248
|
-
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId
|
|
249
|
-
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest
|
|
250
|
-
break
|
|
251
|
-
case 'chats.upsert':
|
|
252
|
-
for (const chat of eventData) {
|
|
253
|
-
const id = chat.id || ''
|
|
254
|
-
let upsert = data.chatUpserts[id]
|
|
255
|
-
if (id && !upsert) {
|
|
256
|
-
upsert = data.historySets.chats[id]
|
|
257
|
-
if (upsert) {
|
|
258
|
-
logger.debug({ chatId: id }, 'absorbed chat upsert in chat set')
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
if (upsert) {
|
|
262
|
-
upsert = concatChats(upsert, chat)
|
|
263
|
-
} else {
|
|
264
|
-
upsert = chat
|
|
265
|
-
data.chatUpserts[id] = upsert
|
|
266
|
-
}
|
|
267
|
-
absorbingChatUpdate(upsert)
|
|
268
|
-
if (data.chatDeletes.has(id)) {
|
|
269
|
-
data.chatDeletes.delete(id)
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
break
|
|
273
|
-
case 'chats.update':
|
|
274
|
-
for (const update of eventData) {
|
|
275
|
-
const chatId = update.id
|
|
276
|
-
const conditionMatches = update.conditional ? update.conditional(data) : true
|
|
277
|
-
if (conditionMatches) {
|
|
278
|
-
delete update.conditional
|
|
279
|
-
|
|
280
|
-
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId]
|
|
281
|
-
if (upsert) {
|
|
282
|
-
concatChats(upsert, update)
|
|
283
|
-
} else {
|
|
284
|
-
|
|
285
|
-
const chatUpdate = data.chatUpdates[chatId] || {}
|
|
286
|
-
data.chatUpdates[chatId] = concatChats(chatUpdate, update)
|
|
287
|
-
}
|
|
288
|
-
} else if (conditionMatches === undefined) {
|
|
289
|
-
|
|
290
|
-
data.chatUpdates[chatId] = update
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
if (data.chatDeletes.has(chatId)) {
|
|
296
|
-
data.chatDeletes.delete(chatId)
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
break
|
|
300
|
-
case 'chats.delete':
|
|
301
|
-
for (const chatId of eventData) {
|
|
302
|
-
if (!data.chatDeletes.has(chatId)) {
|
|
303
|
-
data.chatDeletes.add(chatId)
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (data.chatUpdates[chatId]) {
|
|
307
|
-
delete data.chatUpdates[chatId]
|
|
308
|
-
}
|
|
309
|
-
if (data.chatUpserts[chatId]) {
|
|
310
|
-
delete data.chatUpserts[chatId]
|
|
311
|
-
}
|
|
312
|
-
if (data.historySets.chats[chatId]) {
|
|
313
|
-
delete data.historySets.chats[chatId]
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
break
|
|
317
|
-
case 'contacts.upsert':
|
|
318
|
-
for (const contact of eventData) {
|
|
319
|
-
let upsert = data.contactUpserts[contact.id]
|
|
320
|
-
if (!upsert) {
|
|
321
|
-
upsert = data.historySets.contacts[contact.id]
|
|
322
|
-
if (upsert) {
|
|
323
|
-
logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set')
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
if (upsert) {
|
|
327
|
-
upsert = Object.assign(upsert, (0, generics_1.trimUndefined)(contact))
|
|
328
|
-
} else {
|
|
329
|
-
upsert = contact
|
|
330
|
-
data.contactUpserts[contact.id] = upsert
|
|
331
|
-
}
|
|
332
|
-
if (data.contactUpdates[contact.id]) {
|
|
333
|
-
upsert = Object.assign(data.contactUpdates[contact.id], (0, generics_1.trimUndefined)(contact))
|
|
334
|
-
delete data.contactUpdates[contact.id]
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
break
|
|
338
|
-
case 'contacts.update':
|
|
339
|
-
const contactUpdates = eventData
|
|
340
|
-
for (const update of contactUpdates) {
|
|
341
|
-
const id = update.id
|
|
342
|
-
|
|
343
|
-
const upsert = data.historySets.contacts[id] || data.contactUpserts[id]
|
|
344
|
-
if (upsert) {
|
|
345
|
-
Object.assign(upsert, update)
|
|
346
|
-
} else {
|
|
347
|
-
|
|
348
|
-
const contactUpdate = data.contactUpdates[id] || {}
|
|
349
|
-
data.contactUpdates[id] = Object.assign(contactUpdate, update)
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
break
|
|
353
|
-
case 'messages.upsert':
|
|
354
|
-
const { messages, type } = eventData
|
|
355
|
-
for (const message of messages) {
|
|
356
|
-
const key = stringifyMessageKey(message.key)
|
|
357
|
-
let existing = data.messageUpserts[key]?.message
|
|
358
|
-
if (!existing) {
|
|
359
|
-
existing = data.historySets.messages[key]
|
|
360
|
-
if (existing) {
|
|
361
|
-
logger.debug({ messageId: key }, 'absorbed message upsert in message set')
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
if (existing) {
|
|
365
|
-
message.messageTimestamp = existing.messageTimestamp
|
|
366
|
-
}
|
|
367
|
-
if (data.messageUpdates[key]) {
|
|
368
|
-
logger.debug('absorbed prior message update in message upsert')
|
|
369
|
-
Object.assign(message, data.messageUpdates[key].update)
|
|
370
|
-
delete data.messageUpdates[key]
|
|
371
|
-
}
|
|
372
|
-
if (data.historySets.messages[key]) {
|
|
373
|
-
data.historySets.messages[key] = message
|
|
374
|
-
} else {
|
|
375
|
-
data.messageUpserts[key] = {
|
|
376
|
-
message,
|
|
377
|
-
type: type === 'notify' || data.messageUpserts[key]?.type === 'notify' ? 'notify' : type
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
break
|
|
382
|
-
case 'messages.update':
|
|
383
|
-
const msgUpdates = eventData
|
|
384
|
-
for (const { key, update } of msgUpdates) {
|
|
385
|
-
const keyStr = stringifyMessageKey(key)
|
|
386
|
-
const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message
|
|
387
|
-
if (existing) {
|
|
388
|
-
Object.assign(existing, update)
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
if (update.status === Types_1.WAMessageStatus.READ && !key.fromMe) {
|
|
393
|
-
decrementChatReadCounterIfMsgDidUnread(existing)
|
|
394
|
-
}
|
|
395
|
-
} else {
|
|
396
|
-
const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} }
|
|
397
|
-
Object.assign(msgUpdate.update, update)
|
|
398
|
-
data.messageUpdates[keyStr] = msgUpdate
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
break
|
|
402
|
-
case 'messages.delete':
|
|
403
|
-
const deleteData = eventData
|
|
404
|
-
if ('keys' in deleteData) {
|
|
405
|
-
const { keys } = deleteData
|
|
406
|
-
for (const key of keys) {
|
|
407
|
-
const keyStr = stringifyMessageKey(key)
|
|
408
|
-
if (!data.messageDeletes[keyStr]) {
|
|
409
|
-
data.messageDeletes[keyStr] = key
|
|
410
|
-
}
|
|
411
|
-
if (data.messageUpserts[keyStr]) {
|
|
412
|
-
delete data.messageUpserts[keyStr]
|
|
413
|
-
}
|
|
414
|
-
if (data.messageUpdates[keyStr]) {
|
|
415
|
-
delete data.messageUpdates[keyStr]
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
} else {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
break
|
|
426
|
-
case 'messages.reaction':
|
|
427
|
-
const reactions = eventData
|
|
428
|
-
for (const { key, reaction } of reactions) {
|
|
429
|
-
const keyStr = stringifyMessageKey(key)
|
|
430
|
-
const existing = data.messageUpserts[keyStr]
|
|
431
|
-
if (existing) {
|
|
432
|
-
;(0, messages_1.updateMessageWithReaction)(existing.message, reaction)
|
|
433
|
-
} else {
|
|
434
|
-
data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] }
|
|
435
|
-
;(0, messages_1.updateMessageWithReaction)(data.messageReactions[keyStr], reaction)
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
break
|
|
439
|
-
case 'message-receipt.update':
|
|
440
|
-
const receipts = eventData
|
|
441
|
-
for (const { key, receipt } of receipts) {
|
|
442
|
-
const keyStr = stringifyMessageKey(key)
|
|
443
|
-
const existing = data.messageUpserts[keyStr]
|
|
444
|
-
if (existing) {
|
|
445
|
-
;(0, messages_1.updateMessageWithReceipt)(existing.message, receipt)
|
|
446
|
-
} else {
|
|
447
|
-
data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] }
|
|
448
|
-
;(0, messages_1.updateMessageWithReceipt)(data.messageReceipts[keyStr], receipt)
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
break
|
|
452
|
-
case 'groups.update':
|
|
453
|
-
const groupUpdates = eventData
|
|
454
|
-
for (const update of groupUpdates) {
|
|
455
|
-
const id = update.id
|
|
456
|
-
const groupUpdate = data.groupUpdates[id] || {}
|
|
457
|
-
if (!data.groupUpdates[id]) {
|
|
458
|
-
data.groupUpdates[id] = Object.assign(groupUpdate, update)
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
break
|
|
462
|
-
default:
|
|
463
|
-
throw new Error(`"${event}" cannot be buffered`)
|
|
464
|
-
}
|
|
465
|
-
function absorbingChatUpdate(existing) {
|
|
466
|
-
const chatId = existing.id || ''
|
|
467
|
-
const update = data.chatUpdates[chatId]
|
|
468
|
-
if (update) {
|
|
469
|
-
const conditionMatches = update.conditional ? update.conditional(data) : true
|
|
470
|
-
if (conditionMatches) {
|
|
471
|
-
delete update.conditional
|
|
472
|
-
logger.debug({ chatId }, 'absorbed chat update in existing chat')
|
|
473
|
-
Object.assign(existing, concatChats(update, existing))
|
|
474
|
-
delete data.chatUpdates[chatId]
|
|
475
|
-
} else if (conditionMatches === false) {
|
|
476
|
-
logger.debug({ chatId }, 'chat update condition fail, removing')
|
|
477
|
-
delete data.chatUpdates[chatId]
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
function decrementChatReadCounterIfMsgDidUnread(message) {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
const chatId = message.key.remoteJid
|
|
485
|
-
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId]
|
|
486
|
-
if (
|
|
487
|
-
(0, process_message_1.isRealMessage)(message) &&
|
|
488
|
-
(0, process_message_1.shouldIncrementChatUnread)(message) &&
|
|
489
|
-
typeof chat?.unreadCount === 'number' &&
|
|
490
|
-
chat.unreadCount > 0
|
|
491
|
-
) {
|
|
492
|
-
logger.debug({ chatId: chat.id }, 'decrementing chat counter')
|
|
493
|
-
chat.unreadCount -= 1
|
|
494
|
-
if (chat.unreadCount === 0) {
|
|
495
|
-
delete chat.unreadCount
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
function consolidateEvents(data) {
|
|
501
|
-
const map = {}
|
|
502
|
-
if (!data.historySets.empty) {
|
|
503
|
-
map['messaging-history.set'] = {
|
|
504
|
-
chats: Object.values(data.historySets.chats),
|
|
505
|
-
messages: Object.values(data.historySets.messages),
|
|
506
|
-
contacts: Object.values(data.historySets.contacts),
|
|
507
|
-
syncType: data.historySets.syncType,
|
|
508
|
-
progress: data.historySets.progress,
|
|
509
|
-
isLatest: data.historySets.isLatest,
|
|
510
|
-
peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
const chatUpsertList = Object.values(data.chatUpserts)
|
|
514
|
-
if (chatUpsertList.length) {
|
|
515
|
-
map['chats.upsert'] = chatUpsertList
|
|
516
|
-
}
|
|
517
|
-
const chatUpdateList = Object.values(data.chatUpdates)
|
|
518
|
-
if (chatUpdateList.length) {
|
|
519
|
-
map['chats.update'] = chatUpdateList
|
|
520
|
-
}
|
|
521
|
-
const chatDeleteList = Array.from(data.chatDeletes)
|
|
522
|
-
if (chatDeleteList.length) {
|
|
523
|
-
map['chats.delete'] = chatDeleteList
|
|
524
|
-
}
|
|
525
|
-
const messageUpsertList = Object.values(data.messageUpserts)
|
|
526
|
-
if (messageUpsertList.length) {
|
|
527
|
-
const type = messageUpsertList[0].type
|
|
528
|
-
map['messages.upsert'] = {
|
|
529
|
-
messages: messageUpsertList.map(m => m.message),
|
|
530
|
-
type
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
const messageUpdateList = Object.values(data.messageUpdates)
|
|
534
|
-
if (messageUpdateList.length) {
|
|
535
|
-
map['messages.update'] = messageUpdateList
|
|
536
|
-
}
|
|
537
|
-
const messageDeleteList = Object.values(data.messageDeletes)
|
|
538
|
-
if (messageDeleteList.length) {
|
|
539
|
-
map['messages.delete'] = { keys: messageDeleteList }
|
|
540
|
-
}
|
|
541
|
-
const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) =>
|
|
542
|
-
reactions.flatMap(reaction => ({ key, reaction }))
|
|
543
|
-
)
|
|
544
|
-
if (messageReactionList.length) {
|
|
545
|
-
map['messages.reaction'] = messageReactionList
|
|
546
|
-
}
|
|
547
|
-
const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) =>
|
|
548
|
-
userReceipt.flatMap(receipt => ({ key, receipt }))
|
|
549
|
-
)
|
|
550
|
-
if (messageReceiptList.length) {
|
|
551
|
-
map['message-receipt.update'] = messageReceiptList
|
|
552
|
-
}
|
|
553
|
-
const contactUpsertList = Object.values(data.contactUpserts)
|
|
554
|
-
if (contactUpsertList.length) {
|
|
555
|
-
map['contacts.upsert'] = contactUpsertList
|
|
556
|
-
}
|
|
557
|
-
const contactUpdateList = Object.values(data.contactUpdates)
|
|
558
|
-
if (contactUpdateList.length) {
|
|
559
|
-
map['contacts.update'] = contactUpdateList
|
|
560
|
-
}
|
|
561
|
-
const groupUpdateList = Object.values(data.groupUpdates)
|
|
562
|
-
if (groupUpdateList.length) {
|
|
563
|
-
map['groups.update'] = groupUpdateList
|
|
564
|
-
}
|
|
565
|
-
return map
|
|
566
|
-
}
|
|
567
|
-
function concatChats(a, b) {
|
|
568
|
-
if (
|
|
569
|
-
b.unreadCount === null &&
|
|
570
|
-
a.unreadCount < 0
|
|
571
|
-
) {
|
|
572
|
-
a.unreadCount = undefined
|
|
573
|
-
b.unreadCount = undefined
|
|
574
|
-
}
|
|
575
|
-
if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
|
|
576
|
-
b = { ...b }
|
|
577
|
-
if (b.unreadCount >= 0) {
|
|
578
|
-
b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0)
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
return Object.assign(a, b)
|
|
582
|
-
}
|
|
583
|
-
const stringifyMessageKey = key => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`
|