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