@pontalabs/baileys 1.0.6 → 1.0.8
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/LICENSE +1 -1
- package/README.md +839 -267
- package/WAProto/GenerateStatics.sh +4 -3
- package/WAProto/WAProto.proto +511 -1215
- package/WAProto/index.d.ts +55 -14017
- package/WAProto/index.js +29 -97689
- package/lib/Defaults/index.d.ts +74 -72
- package/lib/Defaults/index.js +108 -94
- package/lib/Signal/Group/ciphertext-message.d.ts +7 -8
- package/lib/Signal/Group/ciphertext-message.js +16 -9
- package/lib/Signal/Group/group-session-builder.d.ts +13 -11
- package/lib/Signal/Group/group-session-builder.js +61 -19
- package/lib/Signal/Group/group_cipher.d.ts +16 -14
- package/lib/Signal/Group/group_cipher.js +70 -41
- package/lib/Signal/Group/index.d.ts +11 -12
- package/lib/Signal/Group/index.js +61 -12
- package/lib/Signal/Group/keyhelper.d.ts +14 -9
- package/lib/Signal/Group/keyhelper.js +58 -10
- package/lib/Signal/Group/sender-chain-key.d.ts +13 -13
- package/lib/Signal/Group/sender-chain-key.js +34 -13
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -16
- package/lib/Signal/Group/sender-key-distribution-message.js +36 -28
- package/lib/Signal/Group/sender-key-message.d.ts +18 -18
- package/lib/Signal/Group/sender-key-message.js +45 -38
- package/lib/Signal/Group/sender-key-name.d.ts +15 -14
- package/lib/Signal/Group/sender-key-name.js +31 -20
- package/lib/Signal/Group/sender-key-record.d.ts +25 -24
- package/lib/Signal/Group/sender-key-record.js +35 -18
- package/lib/Signal/Group/sender-key-state.d.ts +34 -29
- package/lib/Signal/Group/sender-key-state.js +100 -37
- package/lib/Signal/Group/sender-message-key.d.ts +10 -11
- package/lib/Signal/Group/sender-message-key.js +22 -15
- package/lib/Signal/libsignal.d.ts +8 -5
- package/lib/Signal/libsignal.js +142 -288
- package/lib/Signal/lid-mapping.d.ts +28 -23
- package/lib/Signal/lid-mapping.js +184 -277
- package/lib/Socket/Client/index.d.ts +2 -3
- package/lib/Socket/Client/index.js +22 -3
- package/lib/Socket/Client/types.d.ts +15 -15
- package/lib/Socket/Client/types.js +15 -8
- package/lib/Socket/Client/websocket.d.ts +12 -12
- package/lib/Socket/Client/websocket.js +37 -29
- package/lib/Socket/business.d.ts +178 -208
- package/lib/Socket/business.js +127 -91
- package/lib/Socket/chats.d.ts +93 -120
- package/lib/Socket/chats.js +652 -727
- package/lib/Socket/community.js +10 -2
- package/lib/Socket/groups.d.ts +111 -143
- package/lib/Socket/groups.js +186 -165
- package/lib/Socket/index.d.ts +183 -252
- package/lib/Socket/index.js +18 -38
- package/lib/Socket/messages-recv.d.ts +167 -196
- package/lib/Socket/messages-recv.js +1567 -1443
- package/lib/Socket/messages-send.d.ts +158 -192
- package/lib/Socket/messages-send.js +1208 -1102
- package/lib/Socket/newsletter.d.ts +138 -163
- package/lib/Socket/newsletter.js +249 -153
- package/lib/Socket/socket.d.ts +36 -50
- package/lib/Socket/socket.js +820 -749
- package/lib/Store/index.d.ts +4 -4
- package/lib/Store/index.js +24 -4
- package/lib/Store/make-in-memory-store.d.ts +104 -44
- package/lib/Store/make-in-memory-store.js +213 -199
- package/lib/Store/make-ordered-dictionary.d.ts +12 -13
- package/lib/Store/make-ordered-dictionary.js +45 -38
- package/lib/Store/object-repository.d.ts +10 -11
- package/lib/Store/object-repository.js +16 -9
- package/lib/Types/Auth.d.ts +97 -93
- package/lib/Types/Auth.js +3 -2
- package/lib/Types/Bussines.js +3 -2
- package/lib/Types/Call.d.ts +13 -14
- package/lib/Types/Call.js +3 -2
- package/lib/Types/Chat.d.ts +97 -78
- package/lib/Types/Chat.js +9 -8
- package/lib/Types/Contact.d.ts +9 -12
- package/lib/Types/Contact.js +3 -2
- package/lib/Types/Events.d.ts +176 -206
- package/lib/Types/Events.js +3 -2
- package/lib/Types/GroupMetadata.d.ts +48 -53
- package/lib/Types/GroupMetadata.js +3 -2
- package/lib/Types/Label.d.ts +14 -13
- package/lib/Types/Label.js +30 -24
- package/lib/Types/LabelAssociation.d.ts +20 -15
- package/lib/Types/LabelAssociation.js +12 -6
- package/lib/Types/Message.d.ts +412 -248
- package/lib/Types/Message.js +19 -17
- package/lib/Types/Product.d.ts +71 -58
- package/lib/Types/Product.js +3 -2
- package/lib/Types/Signal.d.ts +82 -71
- package/lib/Types/Signal.js +3 -2
- package/lib/Types/Socket.d.ts +81 -76
- package/lib/Types/Socket.js +3 -3
- package/lib/Types/State.d.ts +19 -75
- package/lib/Types/State.js +14 -56
- package/lib/Types/USync.d.ts +8 -8
- package/lib/Types/USync.js +3 -2
- package/lib/Types/index.d.ts +62 -47
- package/lib/Types/index.js +50 -26
- package/lib/Utils/auth-utils.d.ts +10 -13
- package/lib/Utils/auth-utils.js +472 -213
- package/lib/Utils/business.d.ts +20 -14
- package/lib/Utils/business.js +134 -110
- package/lib/Utils/chat-utils.d.ts +70 -88
- package/lib/Utils/chat-utils.js +403 -466
- package/lib/Utils/crypto.d.ts +46 -27
- package/lib/Utils/crypto.js +188 -117
- package/lib/Utils/decode-wa-message.d.ts +45 -58
- package/lib/Utils/decode-wa-message.js +311 -212
- package/lib/Utils/event-buffer.d.ts +17 -14
- package/lib/Utils/event-buffer.js +296 -321
- package/lib/Utils/generics.d.ts +92 -66
- package/lib/Utils/generics.js +436 -270
- package/lib/Utils/history.d.ts +23 -24
- package/lib/Utils/history.js +76 -106
- package/lib/Utils/index.d.ts +21 -24
- package/lib/Utils/index.js +41 -24
- package/lib/Utils/link-preview.d.ts +14 -12
- package/lib/Utils/link-preview.js +75 -40
- package/lib/Utils/logger.d.ts +11 -10
- package/lib/Utils/logger.js +7 -3
- package/lib/Utils/lt-hash.d.ts +14 -8
- package/lib/Utils/lt-hash.js +53 -3
- package/lib/Utils/make-mutex.d.ts +7 -7
- package/lib/Utils/make-mutex.js +44 -28
- package/lib/Utils/message-retry-manager.d.ts +88 -115
- package/lib/Utils/message-retry-manager.js +160 -265
- package/lib/Utils/messages-media.d.ts +107 -105
- package/lib/Utils/messages-media.js +619 -548
- package/lib/Utils/messages.d.ts +66 -52
- package/lib/Utils/messages.js +1558 -1730
- package/lib/Utils/noise-handler.d.ts +18 -18
- package/lib/Utils/noise-handler.js +130 -176
- package/lib/Utils/process-message.d.ts +32 -43
- package/lib/Utils/process-message.js +282 -484
- package/lib/Utils/rich-message-utils.js +724 -1220
- package/lib/Utils/signal.d.ts +32 -37
- package/lib/Utils/signal.js +105 -140
- package/lib/Utils/use-multi-file-auth-state.d.ts +5 -5
- package/lib/Utils/use-multi-file-auth-state.js +120 -70
- package/lib/Utils/use-single-file-auth-state.d.ts +13 -11
- package/lib/Utils/use-single-file-auth-state.js +66 -92
- package/lib/Utils/validate-connection.d.ts +13 -11
- package/lib/Utils/validate-connection.js +113 -128
- package/lib/WABinary/constants.d.ts +27 -25
- package/lib/WABinary/constants.js +1292 -1451
- package/lib/WABinary/decode.d.ts +9 -7
- package/lib/WABinary/decode.js +189 -163
- package/lib/WABinary/encode.d.ts +3 -3
- package/lib/WABinary/encode.js +155 -110
- package/lib/WABinary/generic-utils.d.ts +27 -14
- package/lib/WABinary/generic-utils.js +105 -177
- package/lib/WABinary/index.d.ts +5 -6
- package/lib/WABinary/index.js +25 -6
- package/lib/WABinary/jid-utils.d.ts +54 -41
- package/lib/WABinary/jid-utils.js +152 -83
- package/lib/WABinary/types.d.ts +13 -10
- package/lib/WABinary/types.js +3 -2
- package/lib/WAM/BinaryInfo.d.ts +15 -8
- package/lib/WAM/BinaryInfo.js +14 -7
- package/lib/WAM/constants.d.ts +37 -30
- package/lib/WAM/constants.js +11983 -19465
- package/lib/WAM/encode.d.ts +3 -3
- package/lib/WAM/encode.js +109 -95
- package/lib/WAM/index.d.ts +3 -4
- package/lib/WAM/index.js +23 -4
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -9
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +21 -37
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -19
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +26 -18
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -11
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +20 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -11
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +24 -16
- package/lib/WAUSync/Protocols/index.d.ts +6 -6
- package/lib/WAUSync/Protocols/index.js +26 -6
- package/lib/WAUSync/USyncQuery.d.ts +28 -27
- package/lib/WAUSync/USyncQuery.js +62 -68
- package/lib/WAUSync/USyncUser.d.ts +10 -15
- package/lib/WAUSync/USyncUser.js +19 -20
- package/lib/WAUSync/index.d.ts +3 -4
- package/lib/WAUSync/index.js +23 -4
- package/lib/index.d.ts +12 -11
- package/lib/index.js +39 -12
- package/package.json +43 -87
- package/engine-requirements.js +0 -10
|
@@ -1,8 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod }
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
8
|
+
|
|
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
|
+
|
|
6
15
|
const BUFFERABLE_EVENT = [
|
|
7
16
|
'messaging-history.set',
|
|
8
17
|
'chats.upsert',
|
|
@@ -13,185 +22,155 @@ const BUFFERABLE_EVENT = [
|
|
|
13
22
|
'messages.upsert',
|
|
14
23
|
'messages.update',
|
|
15
24
|
'messages.delete',
|
|
25
|
+
//'messages.poll',
|
|
16
26
|
'messages.reaction',
|
|
17
27
|
'message-receipt.update',
|
|
18
|
-
'groups.update'
|
|
19
|
-
]
|
|
20
|
-
|
|
28
|
+
'groups.update',
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT)
|
|
32
|
+
|
|
21
33
|
/**
|
|
22
34
|
* The event buffer logically consolidates different events into a single event
|
|
23
35
|
* making the data processing more efficient.
|
|
24
36
|
*/
|
|
25
|
-
|
|
26
|
-
const ev = new
|
|
27
|
-
const historyCache = new Set()
|
|
28
|
-
let data = makeBufferData()
|
|
29
|
-
let isBuffering = false
|
|
30
|
-
let bufferTimeout = null
|
|
31
|
-
let flushPendingTimeout = null
|
|
32
|
-
let bufferCount = 0
|
|
33
|
-
const MAX_HISTORY_CACHE_SIZE = 10000
|
|
34
|
-
const BUFFER_TIMEOUT_MS = 30000
|
|
37
|
+
const makeEventBuffer = (logger) => {
|
|
38
|
+
const ev = new events_1.default()
|
|
39
|
+
const historyCache = new Set()
|
|
40
|
+
let data = makeBufferData()
|
|
41
|
+
let isBuffering = false
|
|
42
|
+
let bufferTimeout = null
|
|
43
|
+
let flushPendingTimeout = null
|
|
44
|
+
let bufferCount = 0
|
|
45
|
+
const MAX_HISTORY_CACHE_SIZE = 10000 // Limit the history cache size to prevent memory bloat
|
|
46
|
+
const BUFFER_TIMEOUT_MS = 30000 // 30 seconds
|
|
47
|
+
|
|
35
48
|
// take the generic event and fire it as a baileys event
|
|
36
49
|
ev.on('event', (map) => {
|
|
37
50
|
for (const event in map) {
|
|
38
|
-
ev.emit(event, map[event])
|
|
51
|
+
ev.emit(event, map[event])
|
|
39
52
|
}
|
|
40
|
-
})
|
|
53
|
+
})
|
|
41
54
|
function buffer() {
|
|
42
55
|
if (!isBuffering) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
56
|
+
logger.debug('Event buffer activated')
|
|
57
|
+
isBuffering = true
|
|
58
|
+
bufferCount++
|
|
59
|
+
|
|
60
|
+
// Auto-flush after a timeout to prevent infinite buffering
|
|
46
61
|
if (bufferTimeout) {
|
|
47
|
-
clearTimeout(bufferTimeout)
|
|
62
|
+
clearTimeout(bufferTimeout)
|
|
48
63
|
}
|
|
64
|
+
|
|
49
65
|
bufferTimeout = setTimeout(() => {
|
|
50
66
|
if (isBuffering) {
|
|
51
|
-
logger.warn('Buffer timeout reached, auto-flushing')
|
|
52
|
-
flush()
|
|
67
|
+
logger.warn('Buffer timeout reached, auto-flushing')
|
|
68
|
+
flush()
|
|
53
69
|
}
|
|
54
|
-
}, BUFFER_TIMEOUT_MS)
|
|
70
|
+
}, BUFFER_TIMEOUT_MS)
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
bufferCount++
|
|
55
74
|
}
|
|
56
|
-
// Always increment count when requested
|
|
57
|
-
bufferCount++;
|
|
58
75
|
}
|
|
59
76
|
function flush() {
|
|
60
77
|
if (!isBuffering) {
|
|
61
|
-
return false
|
|
78
|
+
return false
|
|
62
79
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
80
|
+
|
|
81
|
+
logger.debug({ bufferCount }, 'Flushing event buffer')
|
|
82
|
+
isBuffering = false
|
|
83
|
+
bufferCount = 0
|
|
84
|
+
|
|
85
|
+
// Clear timeouts
|
|
67
86
|
if (bufferTimeout) {
|
|
68
|
-
clearTimeout(bufferTimeout)
|
|
69
|
-
bufferTimeout = null
|
|
87
|
+
clearTimeout(bufferTimeout)
|
|
88
|
+
bufferTimeout = null
|
|
70
89
|
}
|
|
71
90
|
if (flushPendingTimeout) {
|
|
72
|
-
clearTimeout(flushPendingTimeout)
|
|
73
|
-
flushPendingTimeout = null
|
|
91
|
+
clearTimeout(flushPendingTimeout)
|
|
92
|
+
flushPendingTimeout = null
|
|
74
93
|
}
|
|
94
|
+
|
|
75
95
|
// Clear history cache if it exceeds the max size
|
|
76
96
|
if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
|
|
77
|
-
logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache')
|
|
78
|
-
historyCache.clear()
|
|
97
|
+
logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache')
|
|
98
|
+
historyCache.clear()
|
|
79
99
|
}
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
100
|
+
|
|
101
|
+
const newData = makeBufferData()
|
|
102
|
+
const chatUpdates = Object.values(data.chatUpdates)
|
|
103
|
+
let conditionalChatUpdatesLeft = 0
|
|
83
104
|
for (const update of chatUpdates) {
|
|
84
105
|
if (update.conditional) {
|
|
85
|
-
conditionalChatUpdatesLeft += 1
|
|
86
|
-
newData.chatUpdates[update.id] = update
|
|
87
|
-
delete data.chatUpdates[update.id]
|
|
106
|
+
conditionalChatUpdatesLeft += 1
|
|
107
|
+
newData.chatUpdates[update.id] = update
|
|
108
|
+
delete data.chatUpdates[update.id]
|
|
88
109
|
}
|
|
89
110
|
}
|
|
90
|
-
const consolidatedData = consolidateEvents(data)
|
|
111
|
+
const consolidatedData = consolidateEvents(data)
|
|
91
112
|
if (Object.keys(consolidatedData).length) {
|
|
92
|
-
ev.emit('event', consolidatedData)
|
|
113
|
+
ev.emit('event', consolidatedData)
|
|
93
114
|
}
|
|
94
|
-
data = newData
|
|
95
|
-
logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events')
|
|
96
|
-
return true
|
|
115
|
+
data = newData
|
|
116
|
+
logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events')
|
|
117
|
+
return true
|
|
97
118
|
}
|
|
98
119
|
return {
|
|
99
120
|
process(handler) {
|
|
100
|
-
const listener =
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
ev.on('event', listener)
|
|
121
|
+
const listener = (map) => {
|
|
122
|
+
handler(map)
|
|
123
|
+
}
|
|
124
|
+
ev.on('event', listener)
|
|
104
125
|
return () => {
|
|
105
|
-
ev.off('event', listener)
|
|
106
|
-
}
|
|
126
|
+
ev.off('event', listener)
|
|
127
|
+
}
|
|
107
128
|
},
|
|
108
129
|
emit(event, evData) {
|
|
109
|
-
// Check if this is a messages.upsert with a different type than what's buffered
|
|
110
|
-
// If so, flush the buffered messages first to avoid type overshadowing
|
|
111
|
-
if (event === 'messages.upsert') {
|
|
112
|
-
const { type } = evData;
|
|
113
|
-
const existingUpserts = Object.values(data.messageUpserts);
|
|
114
|
-
if (existingUpserts.length > 0) {
|
|
115
|
-
const bufferedType = existingUpserts[0].type;
|
|
116
|
-
if (bufferedType !== type) {
|
|
117
|
-
logger.debug({ bufferedType, newType: type }, 'messages.upsert type mismatch, emitting buffered messages');
|
|
118
|
-
// Emit the buffered messages with their correct type
|
|
119
|
-
ev.emit('event', {
|
|
120
|
-
'messages.upsert': {
|
|
121
|
-
messages: existingUpserts.map(m => m.message),
|
|
122
|
-
type: bufferedType
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
// Clear the message upserts from the buffer
|
|
126
|
-
data.messageUpserts = {};
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
130
|
if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
|
|
131
|
-
append(data, historyCache, event, evData, logger)
|
|
132
|
-
return true
|
|
131
|
+
append(data, historyCache, event, evData, logger)
|
|
132
|
+
return true
|
|
133
133
|
}
|
|
134
|
-
return ev.emit('event', { [event]: evData })
|
|
134
|
+
return ev.emit('event', { [event]: evData })
|
|
135
135
|
},
|
|
136
136
|
isBuffering() {
|
|
137
|
-
return isBuffering
|
|
137
|
+
return isBuffering
|
|
138
138
|
},
|
|
139
139
|
buffer,
|
|
140
140
|
flush,
|
|
141
141
|
createBufferedFunction(work) {
|
|
142
142
|
return async (...args) => {
|
|
143
|
-
buffer()
|
|
143
|
+
buffer()
|
|
144
144
|
try {
|
|
145
|
-
const result = await work(...args)
|
|
146
|
-
|
|
147
|
-
if (bufferCount === 1) {
|
|
148
|
-
setTimeout(() => {
|
|
149
|
-
if (isBuffering && bufferCount === 1) {
|
|
150
|
-
flush();
|
|
151
|
-
}
|
|
152
|
-
}, 100); // Small delay to allow nested buffers
|
|
153
|
-
}
|
|
154
|
-
return result;
|
|
145
|
+
const result = await work(...args)
|
|
146
|
+
return result
|
|
155
147
|
}
|
|
156
148
|
catch (error) {
|
|
157
|
-
throw error
|
|
149
|
+
throw error
|
|
158
150
|
}
|
|
159
151
|
finally {
|
|
160
|
-
bufferCount = Math.max(0, bufferCount - 1)
|
|
161
|
-
|
|
162
|
-
|
|
152
|
+
bufferCount = Math.max(0, bufferCount - 1)
|
|
153
|
+
|
|
154
|
+
if (bufferCount === 0 && isBuffering) {
|
|
155
|
+
// Only schedule ONE flush timer at a time to prevent timer storms
|
|
163
156
|
if (!flushPendingTimeout) {
|
|
164
|
-
flushPendingTimeout = setTimeout(
|
|
157
|
+
flushPendingTimeout = setTimeout(() => {
|
|
158
|
+
flushPendingTimeout = null
|
|
159
|
+
if (isBuffering && bufferCount === 0) {
|
|
160
|
+
flush()
|
|
161
|
+
}
|
|
162
|
+
}, 100)
|
|
165
163
|
}
|
|
166
164
|
}
|
|
167
165
|
}
|
|
168
|
-
}
|
|
166
|
+
}
|
|
169
167
|
},
|
|
170
168
|
on: (...args) => ev.on(...args),
|
|
171
169
|
off: (...args) => ev.off(...args),
|
|
172
170
|
removeAllListeners: (...args) => ev.removeAllListeners(...args),
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
clearTimeout(bufferTimeout);
|
|
177
|
-
bufferTimeout = null;
|
|
178
|
-
}
|
|
179
|
-
if (flushPendingTimeout) {
|
|
180
|
-
clearTimeout(flushPendingTimeout);
|
|
181
|
-
flushPendingTimeout = null;
|
|
182
|
-
}
|
|
183
|
-
// Clear history cache
|
|
184
|
-
historyCache.clear();
|
|
185
|
-
// Reset buffer data
|
|
186
|
-
data = makeBufferData();
|
|
187
|
-
isBuffering = false;
|
|
188
|
-
bufferCount = 0;
|
|
189
|
-
// Remove all listeners
|
|
190
|
-
ev.removeAllListeners();
|
|
191
|
-
logger.debug('Event buffer destroyed');
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
195
174
|
const makeBufferData = () => {
|
|
196
175
|
return {
|
|
197
176
|
historySets: {
|
|
@@ -208,415 +187,411 @@ const makeBufferData = () => {
|
|
|
208
187
|
contactUpdates: {},
|
|
209
188
|
messageUpserts: {},
|
|
210
189
|
messageUpdates: {},
|
|
190
|
+
//messagePollings: {},
|
|
211
191
|
messageReactions: {},
|
|
212
192
|
messageDeletes: {},
|
|
213
193
|
messageReceipts: {},
|
|
214
194
|
groupUpdates: {}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function append(data, historyCache, event, eventData, logger) {
|
|
199
|
+
var _a, _b, _c
|
|
220
200
|
switch (event) {
|
|
221
201
|
case 'messaging-history.set':
|
|
222
202
|
for (const chat of eventData.chats) {
|
|
223
|
-
const
|
|
224
|
-
const existingChat = data.historySets.chats[id];
|
|
203
|
+
const existingChat = data.historySets.chats[chat.id]
|
|
225
204
|
if (existingChat) {
|
|
226
|
-
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType
|
|
205
|
+
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType
|
|
227
206
|
}
|
|
228
|
-
if (!existingChat && !historyCache.has(id)) {
|
|
229
|
-
data.historySets.chats[id] = chat
|
|
230
|
-
historyCache.add(id)
|
|
231
|
-
absorbingChatUpdate(chat)
|
|
207
|
+
if (!existingChat && !historyCache.has(chat.id)) {
|
|
208
|
+
data.historySets.chats[chat.id] = chat
|
|
209
|
+
historyCache.add(chat.id)
|
|
210
|
+
absorbingChatUpdate(chat)
|
|
232
211
|
}
|
|
233
212
|
}
|
|
234
213
|
for (const contact of eventData.contacts) {
|
|
235
|
-
const existingContact = data.historySets.contacts[contact.id]
|
|
214
|
+
const existingContact = data.historySets.contacts[contact.id]
|
|
236
215
|
if (existingContact) {
|
|
237
|
-
Object.assign(existingContact, trimUndefined(contact))
|
|
216
|
+
Object.assign(existingContact, generics_1.trimUndefined(contact))
|
|
238
217
|
}
|
|
239
218
|
else {
|
|
240
|
-
const historyContactId = `c:${contact.id}
|
|
241
|
-
const hasAnyName = contact.notify || contact.name || contact.verifiedName
|
|
219
|
+
const historyContactId = `c:${contact.id}`
|
|
220
|
+
const hasAnyName = contact.notify || contact.name || contact.verifiedName
|
|
242
221
|
if (!historyCache.has(historyContactId) || hasAnyName) {
|
|
243
|
-
data.historySets.contacts[contact.id] = contact
|
|
244
|
-
historyCache.add(historyContactId)
|
|
222
|
+
data.historySets.contacts[contact.id] = contact
|
|
223
|
+
historyCache.add(historyContactId)
|
|
245
224
|
}
|
|
246
225
|
}
|
|
247
226
|
}
|
|
248
227
|
for (const message of eventData.messages) {
|
|
249
|
-
const key = stringifyMessageKey(message.key)
|
|
250
|
-
const existingMsg = data.historySets.messages[key]
|
|
228
|
+
const key = stringifyMessageKey(message.key)
|
|
229
|
+
const existingMsg = data.historySets.messages[key]
|
|
251
230
|
if (!existingMsg && !historyCache.has(key)) {
|
|
252
|
-
data.historySets.messages[key] = message
|
|
253
|
-
historyCache.add(key)
|
|
231
|
+
data.historySets.messages[key] = message
|
|
232
|
+
historyCache.add(key)
|
|
254
233
|
}
|
|
255
234
|
}
|
|
256
|
-
data.historySets.empty = false
|
|
257
|
-
data.historySets.syncType = eventData.syncType
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
const key = entry.groupJid ?? JSON.stringify(entry);
|
|
263
|
-
const existing = merged.get(key);
|
|
264
|
-
if (!existing) {
|
|
265
|
-
merged.set(key, { ...entry, pastParticipants: [...(entry.pastParticipants || [])] });
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
const seen = new Set((existing.pastParticipants || []).map(sigOf));
|
|
269
|
-
for (const p of entry.pastParticipants || []) {
|
|
270
|
-
const sig = sigOf(p);
|
|
271
|
-
if (!seen.has(sig)) {
|
|
272
|
-
existing.pastParticipants.push(p);
|
|
273
|
-
seen.add(sig);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
for (const entry of data.historySets.pastParticipants || [])
|
|
278
|
-
ingest(entry);
|
|
279
|
-
for (const entry of eventData.pastParticipants)
|
|
280
|
-
ingest(entry);
|
|
281
|
-
data.historySets.pastParticipants = [...merged.values()];
|
|
282
|
-
}
|
|
283
|
-
data.historySets.progress = eventData.progress;
|
|
284
|
-
data.historySets.chunkOrder = eventData.chunkOrder;
|
|
285
|
-
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId;
|
|
286
|
-
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest;
|
|
287
|
-
break;
|
|
235
|
+
data.historySets.empty = false
|
|
236
|
+
data.historySets.syncType = eventData.syncType
|
|
237
|
+
data.historySets.progress = eventData.progress
|
|
238
|
+
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId
|
|
239
|
+
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest
|
|
240
|
+
break
|
|
288
241
|
case 'chats.upsert':
|
|
289
242
|
for (const chat of eventData) {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
upsert = data.historySets.chats[id];
|
|
243
|
+
let upsert = data.chatUpserts[chat.id]
|
|
244
|
+
if (!upsert) {
|
|
245
|
+
upsert = data.historySets[chat.id]
|
|
294
246
|
if (upsert) {
|
|
295
|
-
logger.debug({ chatId: id }, 'absorbed chat upsert in chat set')
|
|
247
|
+
logger.debug({ chatId: chat.id }, 'absorbed chat upsert in chat set')
|
|
296
248
|
}
|
|
297
249
|
}
|
|
298
250
|
if (upsert) {
|
|
299
|
-
upsert = concatChats(upsert, chat)
|
|
251
|
+
upsert = concatChats(upsert, chat)
|
|
300
252
|
}
|
|
301
253
|
else {
|
|
302
|
-
upsert = chat
|
|
303
|
-
data.chatUpserts[id] = upsert
|
|
254
|
+
upsert = chat
|
|
255
|
+
data.chatUpserts[chat.id] = upsert
|
|
304
256
|
}
|
|
305
|
-
absorbingChatUpdate(upsert)
|
|
306
|
-
if (data.chatDeletes.has(id)) {
|
|
307
|
-
data.chatDeletes.delete(id)
|
|
257
|
+
absorbingChatUpdate(upsert)
|
|
258
|
+
if (data.chatDeletes.has(chat.id)) {
|
|
259
|
+
data.chatDeletes.delete(chat.id)
|
|
308
260
|
}
|
|
309
261
|
}
|
|
310
|
-
break
|
|
262
|
+
break
|
|
311
263
|
case 'chats.update':
|
|
312
264
|
for (const update of eventData) {
|
|
313
|
-
const chatId = update.id
|
|
314
|
-
const conditionMatches = update.conditional ? update.conditional(data) : true
|
|
265
|
+
const chatId = update.id
|
|
266
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true
|
|
315
267
|
if (conditionMatches) {
|
|
316
|
-
delete update.conditional
|
|
268
|
+
delete update.conditional
|
|
317
269
|
// if there is an existing upsert, merge the update into it
|
|
318
|
-
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId]
|
|
270
|
+
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId]
|
|
319
271
|
if (upsert) {
|
|
320
|
-
concatChats(upsert, update)
|
|
272
|
+
concatChats(upsert, update)
|
|
321
273
|
}
|
|
322
274
|
else {
|
|
323
275
|
// merge the update into the existing update
|
|
324
|
-
const chatUpdate = data.chatUpdates[chatId] || {}
|
|
325
|
-
data.chatUpdates[chatId] = concatChats(chatUpdate, update)
|
|
276
|
+
const chatUpdate = data.chatUpdates[chatId] || {}
|
|
277
|
+
data.chatUpdates[chatId] = concatChats(chatUpdate, update)
|
|
326
278
|
}
|
|
327
279
|
}
|
|
328
280
|
else if (conditionMatches === undefined) {
|
|
329
281
|
// condition yet to be fulfilled
|
|
330
|
-
data.chatUpdates[chatId] = update
|
|
282
|
+
data.chatUpdates[chatId] = update
|
|
331
283
|
}
|
|
332
284
|
// otherwise -- condition not met, update is invalid
|
|
333
285
|
// if the chat has been updated
|
|
334
286
|
// ignore any existing chat delete
|
|
335
287
|
if (data.chatDeletes.has(chatId)) {
|
|
336
|
-
data.chatDeletes.delete(chatId)
|
|
288
|
+
data.chatDeletes.delete(chatId)
|
|
337
289
|
}
|
|
338
290
|
}
|
|
339
|
-
break
|
|
291
|
+
break
|
|
340
292
|
case 'chats.delete':
|
|
341
293
|
for (const chatId of eventData) {
|
|
342
294
|
if (!data.chatDeletes.has(chatId)) {
|
|
343
|
-
data.chatDeletes.add(chatId)
|
|
295
|
+
data.chatDeletes.add(chatId)
|
|
344
296
|
}
|
|
345
297
|
// remove any prior updates & upserts
|
|
346
298
|
if (data.chatUpdates[chatId]) {
|
|
347
|
-
delete data.chatUpdates[chatId]
|
|
299
|
+
delete data.chatUpdates[chatId]
|
|
348
300
|
}
|
|
349
301
|
if (data.chatUpserts[chatId]) {
|
|
350
|
-
delete data.chatUpserts[chatId]
|
|
302
|
+
delete data.chatUpserts[chatId]
|
|
351
303
|
}
|
|
352
304
|
if (data.historySets.chats[chatId]) {
|
|
353
|
-
delete data.historySets.chats[chatId]
|
|
305
|
+
delete data.historySets.chats[chatId]
|
|
354
306
|
}
|
|
355
307
|
}
|
|
356
|
-
break
|
|
308
|
+
break
|
|
357
309
|
case 'contacts.upsert':
|
|
358
310
|
for (const contact of eventData) {
|
|
359
|
-
let upsert = data.contactUpserts[contact.id]
|
|
311
|
+
let upsert = data.contactUpserts[contact.id]
|
|
360
312
|
if (!upsert) {
|
|
361
|
-
upsert = data.historySets.contacts[contact.id]
|
|
313
|
+
upsert = data.historySets.contacts[contact.id]
|
|
362
314
|
if (upsert) {
|
|
363
|
-
logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set')
|
|
315
|
+
logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set')
|
|
364
316
|
}
|
|
365
317
|
}
|
|
366
318
|
if (upsert) {
|
|
367
|
-
upsert = Object.assign(upsert, trimUndefined(contact))
|
|
319
|
+
upsert = Object.assign(upsert, generics_1.trimUndefined(contact))
|
|
368
320
|
}
|
|
369
321
|
else {
|
|
370
|
-
upsert = contact
|
|
371
|
-
data.contactUpserts[contact.id] = upsert
|
|
322
|
+
upsert = contact
|
|
323
|
+
data.contactUpserts[contact.id] = upsert
|
|
372
324
|
}
|
|
373
325
|
if (data.contactUpdates[contact.id]) {
|
|
374
|
-
upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact))
|
|
375
|
-
delete data.contactUpdates[contact.id]
|
|
326
|
+
upsert = Object.assign(data.contactUpdates[contact.id], generics_1.trimUndefined(contact))
|
|
327
|
+
delete data.contactUpdates[contact.id]
|
|
376
328
|
}
|
|
377
329
|
}
|
|
378
|
-
break
|
|
330
|
+
break
|
|
379
331
|
case 'contacts.update':
|
|
380
|
-
const contactUpdates = eventData
|
|
332
|
+
const contactUpdates = eventData
|
|
381
333
|
for (const update of contactUpdates) {
|
|
382
|
-
const id = update.id
|
|
334
|
+
const id = update.id
|
|
383
335
|
// merge into prior upsert
|
|
384
|
-
const upsert = data.historySets.contacts[id] || data.contactUpserts[id]
|
|
336
|
+
const upsert = data.historySets.contacts[id] || data.contactUpserts[id]
|
|
385
337
|
if (upsert) {
|
|
386
|
-
Object.assign(upsert, update)
|
|
338
|
+
Object.assign(upsert, update)
|
|
387
339
|
}
|
|
388
340
|
else {
|
|
389
341
|
// merge into prior update
|
|
390
|
-
const contactUpdate = data.contactUpdates[id] || {}
|
|
391
|
-
data.contactUpdates[id] = Object.assign(contactUpdate, update)
|
|
342
|
+
const contactUpdate = data.contactUpdates[id] || {}
|
|
343
|
+
data.contactUpdates[id] = Object.assign(contactUpdate, update)
|
|
392
344
|
}
|
|
393
345
|
}
|
|
394
|
-
break
|
|
346
|
+
break
|
|
395
347
|
case 'messages.upsert':
|
|
396
|
-
const { messages, type } = eventData
|
|
348
|
+
const { messages, type } = eventData
|
|
397
349
|
for (const message of messages) {
|
|
398
|
-
const key = stringifyMessageKey(message.key)
|
|
399
|
-
let existing = data.messageUpserts[key]
|
|
350
|
+
const key = stringifyMessageKey(message.key)
|
|
351
|
+
let existing = (_a = data.messageUpserts[key]) === null || _a === void 0 ? void 0 : _a.message
|
|
400
352
|
if (!existing) {
|
|
401
|
-
existing = data.historySets.messages[key]
|
|
353
|
+
existing = data.historySets.messages[key]
|
|
402
354
|
if (existing) {
|
|
403
|
-
logger.debug({ messageId: key }, 'absorbed message upsert in message set')
|
|
355
|
+
logger.debug({ messageId: key }, 'absorbed message upsert in message set')
|
|
404
356
|
}
|
|
405
357
|
}
|
|
406
358
|
if (existing) {
|
|
407
|
-
message.messageTimestamp = existing.messageTimestamp
|
|
359
|
+
message.messageTimestamp = existing.messageTimestamp
|
|
408
360
|
}
|
|
409
361
|
if (data.messageUpdates[key]) {
|
|
410
|
-
logger.debug('absorbed prior message update in message upsert')
|
|
411
|
-
Object.assign(message, data.messageUpdates[key].update)
|
|
412
|
-
delete data.messageUpdates[key]
|
|
362
|
+
logger.debug('absorbed prior message update in message upsert')
|
|
363
|
+
Object.assign(message, data.messageUpdates[key].update)
|
|
364
|
+
delete data.messageUpdates[key]
|
|
413
365
|
}
|
|
414
366
|
if (data.historySets.messages[key]) {
|
|
415
|
-
data.historySets.messages[key] = message
|
|
367
|
+
data.historySets.messages[key] = message
|
|
416
368
|
}
|
|
417
369
|
else {
|
|
418
370
|
data.messageUpserts[key] = {
|
|
419
371
|
message,
|
|
420
|
-
type: type === 'notify' || data.messageUpserts[key]
|
|
421
|
-
|
|
372
|
+
type: type === 'notify' || ((_b = data.messageUpserts[key]) === null || _b === void 0 ? void 0 : _b.type) === 'notify'
|
|
373
|
+
? 'notify'
|
|
374
|
+
: type
|
|
375
|
+
}
|
|
422
376
|
}
|
|
423
377
|
}
|
|
424
|
-
break
|
|
378
|
+
break
|
|
425
379
|
case 'messages.update':
|
|
426
|
-
const msgUpdates = eventData
|
|
380
|
+
const msgUpdates = eventData
|
|
427
381
|
for (const { key, update } of msgUpdates) {
|
|
428
|
-
const keyStr = stringifyMessageKey(key)
|
|
429
|
-
const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]
|
|
382
|
+
const keyStr = stringifyMessageKey(key)
|
|
383
|
+
const existing = data.historySets.messages[keyStr] || ((_c = data.messageUpserts[keyStr]) === null || _c === void 0 ? void 0 : _c.message)
|
|
430
384
|
if (existing) {
|
|
431
|
-
Object.assign(existing, update)
|
|
385
|
+
Object.assign(existing, update)
|
|
432
386
|
// if the message was received & read by us
|
|
433
387
|
// the chat counter must have been incremented
|
|
434
388
|
// so we need to decrement it
|
|
435
|
-
if (update.status === WAMessageStatus.READ && !key.fromMe) {
|
|
436
|
-
decrementChatReadCounterIfMsgDidUnread(existing)
|
|
389
|
+
if (update.status === Types_1.WAMessageStatus.READ && !key.fromMe) {
|
|
390
|
+
decrementChatReadCounterIfMsgDidUnread(existing)
|
|
437
391
|
}
|
|
438
392
|
}
|
|
439
393
|
else {
|
|
440
|
-
const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} }
|
|
441
|
-
Object.assign(msgUpdate.update, update)
|
|
442
|
-
data.messageUpdates[keyStr] = msgUpdate
|
|
394
|
+
const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} }
|
|
395
|
+
Object.assign(msgUpdate.update, update)
|
|
396
|
+
data.messageUpdates[keyStr] = msgUpdate
|
|
443
397
|
}
|
|
444
398
|
}
|
|
445
|
-
break
|
|
399
|
+
break
|
|
446
400
|
case 'messages.delete':
|
|
447
|
-
const deleteData = eventData
|
|
401
|
+
const deleteData = eventData
|
|
448
402
|
if ('keys' in deleteData) {
|
|
449
|
-
const { keys } = deleteData
|
|
403
|
+
const { keys } = deleteData
|
|
450
404
|
for (const key of keys) {
|
|
451
|
-
const keyStr = stringifyMessageKey(key)
|
|
405
|
+
const keyStr = stringifyMessageKey(key)
|
|
452
406
|
if (!data.messageDeletes[keyStr]) {
|
|
453
|
-
data.messageDeletes[keyStr] = key
|
|
407
|
+
data.messageDeletes[keyStr] = key
|
|
454
408
|
}
|
|
455
409
|
if (data.messageUpserts[keyStr]) {
|
|
456
|
-
delete data.messageUpserts[keyStr]
|
|
410
|
+
delete data.messageUpserts[keyStr]
|
|
457
411
|
}
|
|
458
412
|
if (data.messageUpdates[keyStr]) {
|
|
459
|
-
delete data.messageUpdates[keyStr]
|
|
413
|
+
delete data.messageUpdates[keyStr]
|
|
460
414
|
}
|
|
461
415
|
}
|
|
462
416
|
}
|
|
463
417
|
else {
|
|
464
418
|
// TODO: add support
|
|
465
419
|
}
|
|
466
|
-
break
|
|
420
|
+
break
|
|
421
|
+
// case 'messages.poll':
|
|
422
|
+
// const pollUpdates = eventData
|
|
423
|
+
// for(const { vote, pollUpdateMessageKey } of pollUpdates) {
|
|
424
|
+
// const keyStr = stringifyMessageKey(pollUpdateMessageKey)
|
|
425
|
+
// const existing = data.messagePollings[keyStr]
|
|
426
|
+
// if(existing) {
|
|
427
|
+
// messages_1.updateMessageWithPollUpdate(existing.message, pollUpdates)
|
|
428
|
+
// } else {
|
|
429
|
+
// data.messagePollings[keyStr] = data.messagePollings[keyStr]
|
|
430
|
+
// || { pollUpdateMessageKey, pollUpdates: [] }
|
|
431
|
+
// messages_1.updateMessageWithPollUpdate(data.messagePollings[keyStr], vote)
|
|
432
|
+
// }
|
|
433
|
+
// }
|
|
434
|
+
// break
|
|
467
435
|
case 'messages.reaction':
|
|
468
|
-
const reactions = eventData
|
|
436
|
+
const reactions = eventData
|
|
469
437
|
for (const { key, reaction } of reactions) {
|
|
470
|
-
const keyStr = stringifyMessageKey(key)
|
|
471
|
-
const existing = data.messageUpserts[keyStr]
|
|
438
|
+
const keyStr = stringifyMessageKey(key)
|
|
439
|
+
const existing = data.messageUpserts[keyStr]
|
|
472
440
|
if (existing) {
|
|
473
|
-
updateMessageWithReaction(existing.message, reaction)
|
|
441
|
+
messages_1.updateMessageWithReaction(existing.message, reaction)
|
|
474
442
|
}
|
|
475
443
|
else {
|
|
476
|
-
data.messageReactions[keyStr] = data.messageReactions[keyStr]
|
|
477
|
-
|
|
444
|
+
data.messageReactions[keyStr] = data.messageReactions[keyStr]
|
|
445
|
+
|| { key, reactions: [] }
|
|
446
|
+
messages_1.updateMessageWithReaction(data.messageReactions[keyStr], reaction)
|
|
478
447
|
}
|
|
479
448
|
}
|
|
480
|
-
break
|
|
449
|
+
break
|
|
481
450
|
case 'message-receipt.update':
|
|
482
|
-
const receipts = eventData
|
|
451
|
+
const receipts = eventData
|
|
483
452
|
for (const { key, receipt } of receipts) {
|
|
484
|
-
const keyStr = stringifyMessageKey(key)
|
|
485
|
-
const existing = data.messageUpserts[keyStr]
|
|
453
|
+
const keyStr = stringifyMessageKey(key)
|
|
454
|
+
const existing = data.messageUpserts[keyStr]
|
|
486
455
|
if (existing) {
|
|
487
|
-
updateMessageWithReceipt(existing.message, receipt)
|
|
456
|
+
messages_1.updateMessageWithReceipt(existing.message, receipt)
|
|
488
457
|
}
|
|
489
458
|
else {
|
|
490
|
-
data.messageReceipts[keyStr] = data.messageReceipts[keyStr]
|
|
491
|
-
|
|
459
|
+
data.messageReceipts[keyStr] = data.messageReceipts[keyStr]
|
|
460
|
+
|| { key, userReceipt: [] }
|
|
461
|
+
messages_1.updateMessageWithReceipt(data.messageReceipts[keyStr], receipt)
|
|
492
462
|
}
|
|
493
463
|
}
|
|
494
|
-
break
|
|
464
|
+
break
|
|
495
465
|
case 'groups.update':
|
|
496
|
-
const groupUpdates = eventData
|
|
466
|
+
const groupUpdates = eventData
|
|
497
467
|
for (const update of groupUpdates) {
|
|
498
|
-
const id = update.id
|
|
499
|
-
const groupUpdate = data.groupUpdates[id] || {}
|
|
468
|
+
const id = update.id
|
|
469
|
+
const groupUpdate = data.groupUpdates[id] || {}
|
|
500
470
|
if (!data.groupUpdates[id]) {
|
|
501
|
-
data.groupUpdates[id] = Object.assign(groupUpdate, update)
|
|
471
|
+
data.groupUpdates[id] = Object.assign(groupUpdate, update)
|
|
502
472
|
}
|
|
503
473
|
}
|
|
504
|
-
break
|
|
474
|
+
break
|
|
505
475
|
default:
|
|
506
|
-
throw new Error(`"${event}" cannot be buffered`)
|
|
476
|
+
throw new Error(`"${event}" cannot be buffered`)
|
|
507
477
|
}
|
|
508
478
|
function absorbingChatUpdate(existing) {
|
|
509
|
-
const chatId = existing.id
|
|
510
|
-
const update = data.chatUpdates[chatId]
|
|
479
|
+
const chatId = existing.id
|
|
480
|
+
const update = data.chatUpdates[chatId]
|
|
511
481
|
if (update) {
|
|
512
|
-
const conditionMatches = update.conditional ? update.conditional(data) : true
|
|
482
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true
|
|
513
483
|
if (conditionMatches) {
|
|
514
|
-
delete update.conditional
|
|
515
|
-
logger.debug({ chatId }, 'absorbed chat update in existing chat')
|
|
516
|
-
Object.assign(existing, concatChats(update, existing))
|
|
517
|
-
delete data.chatUpdates[chatId]
|
|
484
|
+
delete update.conditional
|
|
485
|
+
logger.debug({ chatId }, 'absorbed chat update in existing chat')
|
|
486
|
+
Object.assign(existing, concatChats(update, existing))
|
|
487
|
+
delete data.chatUpdates[chatId]
|
|
518
488
|
}
|
|
519
489
|
else if (conditionMatches === false) {
|
|
520
|
-
logger.debug({ chatId }, 'chat update condition fail, removing')
|
|
521
|
-
delete data.chatUpdates[chatId]
|
|
490
|
+
logger.debug({ chatId }, 'chat update condition fail, removing')
|
|
491
|
+
delete data.chatUpdates[chatId]
|
|
522
492
|
}
|
|
523
493
|
}
|
|
524
494
|
}
|
|
525
495
|
function decrementChatReadCounterIfMsgDidUnread(message) {
|
|
526
496
|
// decrement chat unread counter
|
|
527
497
|
// if the message has already been marked read by us
|
|
528
|
-
const chatId = message.key.remoteJid
|
|
529
|
-
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId]
|
|
530
|
-
if (isRealMessage(message)
|
|
531
|
-
shouldIncrementChatUnread(message)
|
|
532
|
-
typeof chat?.unreadCount === 'number'
|
|
533
|
-
chat.unreadCount > 0) {
|
|
534
|
-
logger.debug({ chatId: chat.id }, 'decrementing chat counter')
|
|
535
|
-
chat.unreadCount -= 1
|
|
498
|
+
const chatId = message.key.remoteJid
|
|
499
|
+
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId]
|
|
500
|
+
if (process_message_1.isRealMessage(message, '')
|
|
501
|
+
&& process_message_1.shouldIncrementChatUnread(message)
|
|
502
|
+
&& typeof (chat?.unreadCount) === 'number'
|
|
503
|
+
&& chat.unreadCount > 0) {
|
|
504
|
+
logger.debug({ chatId: chat.id }, 'decrementing chat counter')
|
|
505
|
+
chat.unreadCount -= 1
|
|
536
506
|
if (chat.unreadCount === 0) {
|
|
537
|
-
delete chat.unreadCount
|
|
507
|
+
delete chat.unreadCount
|
|
538
508
|
}
|
|
539
509
|
}
|
|
540
510
|
}
|
|
541
511
|
}
|
|
542
512
|
function consolidateEvents(data) {
|
|
543
|
-
const map = {}
|
|
513
|
+
const map = {}
|
|
544
514
|
if (!data.historySets.empty) {
|
|
545
515
|
map['messaging-history.set'] = {
|
|
546
516
|
chats: Object.values(data.historySets.chats),
|
|
547
517
|
messages: Object.values(data.historySets.messages),
|
|
548
518
|
contacts: Object.values(data.historySets.contacts),
|
|
549
|
-
pastParticipants: data.historySets.pastParticipants,
|
|
550
519
|
syncType: data.historySets.syncType,
|
|
551
520
|
progress: data.historySets.progress,
|
|
552
521
|
isLatest: data.historySets.isLatest,
|
|
553
|
-
chunkOrder: data.historySets.chunkOrder,
|
|
554
522
|
peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
|
|
555
|
-
}
|
|
523
|
+
}
|
|
556
524
|
}
|
|
557
|
-
const chatUpsertList = Object.values(data.chatUpserts)
|
|
525
|
+
const chatUpsertList = Object.values(data.chatUpserts)
|
|
558
526
|
if (chatUpsertList.length) {
|
|
559
|
-
map['chats.upsert'] = chatUpsertList
|
|
527
|
+
map['chats.upsert'] = chatUpsertList
|
|
560
528
|
}
|
|
561
|
-
const chatUpdateList = Object.values(data.chatUpdates)
|
|
529
|
+
const chatUpdateList = Object.values(data.chatUpdates)
|
|
562
530
|
if (chatUpdateList.length) {
|
|
563
|
-
map['chats.update'] = chatUpdateList
|
|
531
|
+
map['chats.update'] = chatUpdateList
|
|
564
532
|
}
|
|
565
|
-
const chatDeleteList = Array.from(data.chatDeletes)
|
|
533
|
+
const chatDeleteList = Array.from(data.chatDeletes)
|
|
566
534
|
if (chatDeleteList.length) {
|
|
567
|
-
map['chats.delete'] = chatDeleteList
|
|
535
|
+
map['chats.delete'] = chatDeleteList
|
|
568
536
|
}
|
|
569
|
-
const messageUpsertList = Object.values(data.messageUpserts)
|
|
537
|
+
const messageUpsertList = Object.values(data.messageUpserts)
|
|
570
538
|
if (messageUpsertList.length) {
|
|
571
|
-
const type = messageUpsertList[0].type
|
|
539
|
+
const type = messageUpsertList[0].type
|
|
572
540
|
map['messages.upsert'] = {
|
|
573
541
|
messages: messageUpsertList.map(m => m.message),
|
|
574
542
|
type
|
|
575
|
-
}
|
|
543
|
+
}
|
|
576
544
|
}
|
|
577
|
-
const messageUpdateList = Object.values(data.messageUpdates)
|
|
545
|
+
const messageUpdateList = Object.values(data.messageUpdates)
|
|
578
546
|
if (messageUpdateList.length) {
|
|
579
|
-
map['messages.update'] = messageUpdateList
|
|
547
|
+
map['messages.update'] = messageUpdateList
|
|
580
548
|
}
|
|
581
|
-
const messageDeleteList = Object.values(data.messageDeletes)
|
|
549
|
+
const messageDeleteList = Object.values(data.messageDeletes)
|
|
582
550
|
if (messageDeleteList.length) {
|
|
583
|
-
map['messages.delete'] = { keys: messageDeleteList }
|
|
551
|
+
map['messages.delete'] = { keys: messageDeleteList }
|
|
584
552
|
}
|
|
585
|
-
const
|
|
553
|
+
// const messagePollingList = Object.values(data.messagePollings).flatMap(({ vote, pollUpdateMessageKey }) => vote.flatMap(polling => ({ polling, pollUpdateMessageKey })))
|
|
554
|
+
// if(messagePollingList.length) {
|
|
555
|
+
// map['messages.poll'] = messagePollingList
|
|
556
|
+
// }
|
|
557
|
+
const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) => reactions.flatMap(reaction => ({ key, reaction })))
|
|
586
558
|
if (messageReactionList.length) {
|
|
587
|
-
map['messages.reaction'] = messageReactionList
|
|
559
|
+
map['messages.reaction'] = messageReactionList
|
|
588
560
|
}
|
|
589
|
-
const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) => userReceipt.flatMap(receipt => ({ key, receipt })))
|
|
561
|
+
const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) => userReceipt.flatMap(receipt => ({ key, receipt })))
|
|
590
562
|
if (messageReceiptList.length) {
|
|
591
|
-
map['message-receipt.update'] = messageReceiptList
|
|
563
|
+
map['message-receipt.update'] = messageReceiptList
|
|
592
564
|
}
|
|
593
|
-
const contactUpsertList = Object.values(data.contactUpserts)
|
|
565
|
+
const contactUpsertList = Object.values(data.contactUpserts)
|
|
594
566
|
if (contactUpsertList.length) {
|
|
595
|
-
map['contacts.upsert'] = contactUpsertList
|
|
567
|
+
map['contacts.upsert'] = contactUpsertList
|
|
596
568
|
}
|
|
597
|
-
const contactUpdateList = Object.values(data.contactUpdates)
|
|
569
|
+
const contactUpdateList = Object.values(data.contactUpdates)
|
|
598
570
|
if (contactUpdateList.length) {
|
|
599
|
-
map['contacts.update'] = contactUpdateList
|
|
571
|
+
map['contacts.update'] = contactUpdateList
|
|
600
572
|
}
|
|
601
|
-
const groupUpdateList = Object.values(data.groupUpdates)
|
|
573
|
+
const groupUpdateList = Object.values(data.groupUpdates)
|
|
602
574
|
if (groupUpdateList.length) {
|
|
603
|
-
map['groups.update'] = groupUpdateList
|
|
575
|
+
map['groups.update'] = groupUpdateList
|
|
604
576
|
}
|
|
605
|
-
return map
|
|
577
|
+
return map
|
|
606
578
|
}
|
|
607
579
|
function concatChats(a, b) {
|
|
608
580
|
if (b.unreadCount === null && // neutralize unread counter
|
|
609
581
|
a.unreadCount < 0) {
|
|
610
|
-
a.unreadCount = undefined
|
|
611
|
-
b.unreadCount = undefined
|
|
582
|
+
a.unreadCount = undefined
|
|
583
|
+
b.unreadCount = undefined
|
|
612
584
|
}
|
|
613
585
|
if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
|
|
614
|
-
b = { ...b }
|
|
586
|
+
b = { ...b }
|
|
615
587
|
if (b.unreadCount >= 0) {
|
|
616
|
-
b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0)
|
|
588
|
+
b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0)
|
|
617
589
|
}
|
|
618
590
|
}
|
|
619
|
-
return Object.assign(a, b)
|
|
591
|
+
return Object.assign(a, b)
|
|
592
|
+
}
|
|
593
|
+
const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`
|
|
594
|
+
|
|
595
|
+
module.exports = {
|
|
596
|
+
makeEventBuffer
|
|
620
597
|
}
|
|
621
|
-
const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
|
|
622
|
-
//# sourceMappingURL=event-buffer.js.map
|