@badzz88/baileys 8.5.6 → 8.5.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/README.md +337 -1055
- package/WAProto/WAProto.proto +28 -1189
- package/WAProto/fix-imports.js +79 -69
- package/WAProto/index.d.ts +71491 -15254
- package/WAProto/index.js +126436 -19814
- package/engine-requirements.js +8 -15
- package/lib/Defaults/index.js +130 -0
- package/{src → lib}/Signal/Group/ciphertext-message.js +2 -5
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/{src → lib}/Signal/Group/group_cipher.js +11 -11
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/keyhelper.js +18 -0
- package/{src → lib}/Signal/Group/sender-chain-key.js +6 -9
- package/{src → lib}/Signal/Group/sender-key-distribution-message.js +6 -9
- package/{src → lib}/Signal/Group/sender-key-message.js +9 -12
- package/{src → lib}/Signal/Group/sender-key-name.js +2 -5
- package/{src → lib}/Signal/Group/sender-key-record.js +8 -11
- package/{src → lib}/Signal/Group/sender-key-state.js +6 -9
- package/lib/Signal/Group/sender-message-key.js +26 -0
- package/{src → lib}/Signal/libsignal.js +131 -155
- package/{src → lib}/Signal/lid-mapping.js +27 -23
- package/lib/Socket/Client/index.js +3 -0
- package/lib/Socket/Client/types.js +11 -0
- package/{src → lib}/Socket/Client/websocket.js +11 -18
- package/{src → lib}/Socket/business.js +35 -67
- package/{src → lib}/Socket/chats.js +294 -550
- package/{src → lib}/Socket/communities.js +82 -114
- package/lib/Socket/graphql.js +716 -0
- package/lib/Socket/groups.js +374 -0
- package/lib/Socket/index.js +18 -0
- package/lib/Socket/interop.js +463 -0
- package/{src → lib}/Socket/luxu.js +120 -82
- package/lib/Socket/messages-recv.js +1916 -0
- package/lib/Socket/messages-send.js +1436 -0
- package/lib/Socket/mex.js +54 -0
- package/lib/Socket/newsletter.js +757 -0
- package/lib/Socket/privacy.js +318 -0
- package/{src → lib}/Socket/socket.js +356 -244
- package/lib/Socket/username.js +236 -0
- package/lib/Store/index.js +10 -0
- package/{src → lib}/Store/keyed-db.js +11 -21
- package/lib/Store/make-cache-manager-store.js +85 -0
- package/lib/Store/make-in-memory-store.js +244 -0
- package/{src → lib}/Store/make-ordered-dictionary.js +19 -25
- package/{src → lib}/Store/object-repository.js +11 -5
- 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/Mex.js +111 -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 +56 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +26 -0
- package/{src → lib}/Utils/auth-utils.js +91 -43
- package/lib/Utils/browser-utils.js +48 -0
- package/lib/Utils/business.js +231 -0
- package/lib/Utils/chat-utils.js +872 -0
- package/lib/Utils/companion-reg-client-utils.js +35 -0
- package/lib/Utils/crypto.js +118 -0
- package/lib/Utils/decode-wa-message.js +350 -0
- package/{src → lib}/Utils/event-buffer.js +81 -39
- package/lib/Utils/generics.js +403 -0
- package/lib/Utils/history.js +134 -0
- package/{src → lib}/Utils/identity-change-handler.js +9 -11
- package/lib/Utils/index.js +23 -0
- package/lib/Utils/link-preview.js +85 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/lt-hash.js +8 -0
- package/{src → lib}/Utils/make-mutex.js +7 -10
- package/lib/Utils/message-composer.js +273 -0
- package/{src → lib}/Utils/message-retry-manager.js +76 -28
- package/lib/Utils/messages-media.js +870 -0
- package/lib/Utils/messages.js +1337 -0
- package/lib/Utils/noise-handler.js +201 -0
- package/{src → lib}/Utils/offline-node-processor.js +10 -4
- package/{src → lib}/Utils/pre-key-manager.js +27 -11
- package/lib/Utils/process-message.js +630 -0
- package/{src → lib}/Utils/reporting-utils.js +12 -15
- package/lib/Utils/signal.js +201 -0
- package/{src → lib}/Utils/stanza-ack.js +12 -4
- package/lib/Utils/sticker.js +110 -0
- package/{src → lib}/Utils/sync-action-utils.js +14 -10
- package/{src → lib}/Utils/tc-token-utils.js +48 -43
- package/{src → lib}/Utils/use-multi-file-auth-state.js +35 -25
- package/lib/Utils/validate-connection.js +203 -0
- package/{src → lib}/WABinary/constants.js +9 -12
- package/{src → lib}/WABinary/decode.js +38 -118
- package/{src → lib}/WABinary/encode.js +6 -11
- package/lib/WABinary/generic-utils.js +204 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.js +98 -0
- package/lib/WABinary/types.js +2 -0
- package/{src → lib}/WAM/BinaryInfo.js +2 -5
- package/{src → lib}/WAM/constants.js +4 -16635
- package/{src → lib}/WAM/encode.js +18 -17
- package/lib/WAM/index.js +4 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +43 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +54 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +58 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +27 -0
- package/lib/WAUSync/Protocols/index.js +12 -0
- package/lib/WAUSync/USyncQuery.js +151 -0
- package/lib/WAUSync/USyncUser.js +56 -0
- package/lib/WAUSync/index.js +3 -0
- package/{src → lib}/index.js +16 -51
- package/package.json +129 -99
- package/LICENSE +0 -21
- package/src/Defaults/index.js +0 -186
- package/src/Defaults/phonenumber-mcc.json +0 -223
- package/src/Signal/Group/group-session-builder.js +0 -86
- package/src/Signal/Group/index.js +0 -140
- package/src/Signal/Group/keyhelper.js +0 -77
- package/src/Signal/Group/sender-message-key.js +0 -29
- package/src/Socket/Client/index.js +0 -31
- package/src/Socket/Client/types.js +0 -13
- package/src/Socket/aigroups.js +0 -221
- package/src/Socket/graphql.js +0 -523
- package/src/Socket/groups.js +0 -516
- package/src/Socket/index.js +0 -31
- package/src/Socket/interactive-handler.js +0 -527
- package/src/Socket/interop.js +0 -339
- package/src/Socket/managed-account.js +0 -98
- package/src/Socket/messages-recv.js +0 -2685
- package/src/Socket/messages-send.js +0 -2047
- package/src/Socket/mex.js +0 -57
- package/src/Socket/newsletter.js +0 -455
- package/src/Socket/privacy.js +0 -125
- package/src/Socket/registration.js +0 -236
- package/src/Socket/text-router.js +0 -65
- package/src/Socket/username.js +0 -130
- package/src/Store/index.js +0 -33
- package/src/Store/make-cache-manager-store.js +0 -84
- package/src/Store/make-in-memory-store.js +0 -551
- package/src/Types/Auth.js +0 -31
- package/src/Types/Bussines.js +0 -2
- package/src/Types/Call.js +0 -2
- package/src/Types/Chat.js +0 -4
- package/src/Types/Contact.js +0 -2
- package/src/Types/Events.js +0 -2
- package/src/Types/GroupMetadata.js +0 -2
- package/src/Types/Label.js +0 -26
- package/src/Types/LabelAssociation.js +0 -8
- package/src/Types/Message.js +0 -93
- package/src/Types/Mex.js +0 -112
- package/src/Types/Newsletter.js +0 -109
- package/src/Types/Product.js +0 -2
- package/src/Types/Signal.js +0 -2
- package/src/Types/Socket.js +0 -2
- package/src/Types/State.js +0 -62
- package/src/Types/USync.js +0 -2
- package/src/Types/index.js +0 -56
- package/src/Utils/browser-utils.js +0 -112
- package/src/Utils/business.js +0 -240
- package/src/Utils/chat-utils.js +0 -1230
- package/src/Utils/command-loader.d.ts +0 -27
- package/src/Utils/command-loader.js +0 -89
- package/src/Utils/companion-reg-client-utils.js +0 -40
- package/src/Utils/consumer-application.js +0 -105
- package/src/Utils/crypto.js +0 -118
- package/src/Utils/curve25519-js.js +0 -242
- package/src/Utils/decode-wa-message.js +0 -529
- package/src/Utils/generics.js +0 -483
- package/src/Utils/group-history.js +0 -44
- package/src/Utils/history.js +0 -232
- package/src/Utils/index.js +0 -58
- package/src/Utils/jid-display-normalization.js +0 -195
- package/src/Utils/link-preview.js +0 -135
- package/src/Utils/logger.js +0 -8
- package/src/Utils/lt-hash.js +0 -25
- package/src/Utils/message-composer.js +0 -471
- package/src/Utils/messages-media.js +0 -843
- package/src/Utils/messages.js +0 -2817
- package/src/Utils/meta-ai-msmsg.js +0 -222
- package/src/Utils/native-bridge.js +0 -68
- package/src/Utils/noise-handler.js +0 -169
- package/src/Utils/process-message.js +0 -950
- package/src/Utils/session-pool.d.ts +0 -14
- package/src/Utils/session-pool.js +0 -70
- package/src/Utils/signal.js +0 -217
- package/src/Utils/sticker.d.ts +0 -13
- package/src/Utils/sticker.js +0 -123
- package/src/Utils/validate-connection.js +0 -209
- package/src/Utils/view-once-cache.d.ts +0 -12
- package/src/Utils/view-once-cache.js +0 -63
- package/src/Utils/voip-rekey.js +0 -22
- package/src/WABinary/generic-utils.js +0 -238
- package/src/WABinary/index.js +0 -34
- package/src/WABinary/jid-utils.js +0 -351
- package/src/WABinary/types.js +0 -2
- package/src/WAM/index.js +0 -32
- package/src/WAUSync/Protocols/USyncBotProfileProtocol.js +0 -53
- package/src/WAUSync/Protocols/USyncBusinessProtocol.js +0 -44
- package/src/WAUSync/Protocols/USyncContactProtocol.js +0 -55
- package/src/WAUSync/Protocols/USyncDeviceProtocol.js +0 -55
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -31
- package/src/WAUSync/Protocols/USyncFeatureProtocol.js +0 -54
- package/src/WAUSync/Protocols/USyncLIDProtocol.js +0 -32
- package/src/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -473
- package/src/WAUSync/Protocols/USyncPictureProtocol.js +0 -34
- package/src/WAUSync/Protocols/USyncSidelistProtocol.js +0 -29
- package/src/WAUSync/Protocols/USyncStatusProtocol.js +0 -42
- package/src/WAUSync/Protocols/USyncTextStatusProtocol.js +0 -38
- package/src/WAUSync/Protocols/USyncUsernameProtocol.js +0 -28
- package/src/WAUSync/Protocols/index.js +0 -40
- package/src/WAUSync/USyncQuery.js +0 -126
- package/src/WAUSync/USyncUser.js +0 -50
- package/src/WAUSync/index.js +0 -32
- package/src/antiban.js +0 -4152
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
7
|
-
exports.makeEventBuffer = void 0;
|
|
8
|
-
const events_1 = __importDefault(require('events'));
|
|
9
|
-
const Types_1 = require('../Types');
|
|
10
|
-
const generics_1 = require('./generics');
|
|
11
|
-
const messages_1 = require('./messages');
|
|
12
|
-
const process_message_1 = require('./process-message');
|
|
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';
|
|
13
6
|
const BUFFERABLE_EVENT = [
|
|
14
7
|
'messaging-history.set',
|
|
15
8
|
'chats.upsert',
|
|
@@ -25,17 +18,22 @@ const BUFFERABLE_EVENT = [
|
|
|
25
18
|
'groups.update'
|
|
26
19
|
];
|
|
27
20
|
const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT);
|
|
28
|
-
|
|
29
|
-
|
|
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();
|
|
30
27
|
const historyCache = new Set();
|
|
31
28
|
let data = makeBufferData();
|
|
32
29
|
let isBuffering = false;
|
|
33
30
|
let bufferTimeout = null;
|
|
34
|
-
let flushPendingTimeout = null;
|
|
31
|
+
let flushPendingTimeout = null; // Add a specific timer for the debounced flush to prevent leak
|
|
35
32
|
let bufferCount = 0;
|
|
36
|
-
const MAX_HISTORY_CACHE_SIZE = 10000;
|
|
37
|
-
const BUFFER_TIMEOUT_MS = 30000;
|
|
38
|
-
|
|
33
|
+
const MAX_HISTORY_CACHE_SIZE = 10000; // Limit the history cache size to prevent memory bloat
|
|
34
|
+
const BUFFER_TIMEOUT_MS = 30000; // 30 seconds
|
|
35
|
+
// take the generic event and fire it as a baileys event
|
|
36
|
+
ev.on('event', (map) => {
|
|
39
37
|
for (const event in map) {
|
|
40
38
|
ev.emit(event, map[event]);
|
|
41
39
|
}
|
|
@@ -55,6 +53,7 @@ const makeEventBuffer = logger => {
|
|
|
55
53
|
}
|
|
56
54
|
}, BUFFER_TIMEOUT_MS);
|
|
57
55
|
}
|
|
56
|
+
// Always increment count when requested
|
|
58
57
|
bufferCount++;
|
|
59
58
|
}
|
|
60
59
|
function flush() {
|
|
@@ -64,6 +63,7 @@ const makeEventBuffer = logger => {
|
|
|
64
63
|
logger.debug({ bufferCount }, 'Flushing event buffer');
|
|
65
64
|
isBuffering = false;
|
|
66
65
|
bufferCount = 0;
|
|
66
|
+
// Clear timeout
|
|
67
67
|
if (bufferTimeout) {
|
|
68
68
|
clearTimeout(bufferTimeout);
|
|
69
69
|
bufferTimeout = null;
|
|
@@ -72,6 +72,7 @@ const makeEventBuffer = logger => {
|
|
|
72
72
|
clearTimeout(flushPendingTimeout);
|
|
73
73
|
flushPendingTimeout = null;
|
|
74
74
|
}
|
|
75
|
+
// Clear history cache if it exceeds the max size
|
|
75
76
|
if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
|
|
76
77
|
logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache');
|
|
77
78
|
historyCache.clear();
|
|
@@ -105,6 +106,8 @@ const makeEventBuffer = logger => {
|
|
|
105
106
|
};
|
|
106
107
|
},
|
|
107
108
|
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
|
|
108
111
|
if (event === 'messages.upsert') {
|
|
109
112
|
const { type } = evData;
|
|
110
113
|
const existingUpserts = Object.values(data.messageUpserts);
|
|
@@ -112,12 +115,14 @@ const makeEventBuffer = logger => {
|
|
|
112
115
|
const bufferedType = existingUpserts[0].type;
|
|
113
116
|
if (bufferedType !== type) {
|
|
114
117
|
logger.debug({ bufferedType, newType: type }, 'messages.upsert type mismatch, emitting buffered messages');
|
|
118
|
+
// Emit the buffered messages with their correct type
|
|
115
119
|
ev.emit('event', {
|
|
116
120
|
'messages.upsert': {
|
|
117
121
|
messages: existingUpserts.map(m => m.message),
|
|
118
122
|
type: bufferedType
|
|
119
123
|
}
|
|
120
124
|
});
|
|
125
|
+
// Clear the message upserts from the buffer
|
|
121
126
|
data.messageUpserts = {};
|
|
122
127
|
}
|
|
123
128
|
}
|
|
@@ -138,12 +143,13 @@ const makeEventBuffer = logger => {
|
|
|
138
143
|
buffer();
|
|
139
144
|
try {
|
|
140
145
|
const result = await work(...args);
|
|
146
|
+
// If this is the only buffer, flush after a small delay
|
|
141
147
|
if (bufferCount === 1) {
|
|
142
148
|
setTimeout(() => {
|
|
143
149
|
if (isBuffering && bufferCount === 1) {
|
|
144
150
|
flush();
|
|
145
151
|
}
|
|
146
|
-
}, 100);
|
|
152
|
+
}, 100); // Small delay to allow nested buffers
|
|
147
153
|
}
|
|
148
154
|
return result;
|
|
149
155
|
}
|
|
@@ -153,6 +159,7 @@ const makeEventBuffer = logger => {
|
|
|
153
159
|
finally {
|
|
154
160
|
bufferCount = Math.max(0, bufferCount - 1);
|
|
155
161
|
if (bufferCount === 0) {
|
|
162
|
+
// Only schedule ONE timeout, not 10,000
|
|
156
163
|
if (!flushPendingTimeout) {
|
|
157
164
|
flushPendingTimeout = setTimeout(flush, 100);
|
|
158
165
|
}
|
|
@@ -162,10 +169,29 @@ const makeEventBuffer = logger => {
|
|
|
162
169
|
},
|
|
163
170
|
on: (...args) => ev.on(...args),
|
|
164
171
|
off: (...args) => ev.off(...args),
|
|
165
|
-
removeAllListeners: (...args) => ev.removeAllListeners(...args)
|
|
172
|
+
removeAllListeners: (...args) => ev.removeAllListeners(...args),
|
|
173
|
+
destroy() {
|
|
174
|
+
// Clear buffer timeout
|
|
175
|
+
if (bufferTimeout) {
|
|
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
|
+
}
|
|
166
193
|
};
|
|
167
194
|
};
|
|
168
|
-
exports.makeEventBuffer = makeEventBuffer;
|
|
169
195
|
const makeBufferData = () => {
|
|
170
196
|
return {
|
|
171
197
|
historySets: {
|
|
@@ -188,7 +214,9 @@ const makeBufferData = () => {
|
|
|
188
214
|
groupUpdates: {}
|
|
189
215
|
};
|
|
190
216
|
};
|
|
191
|
-
function append(data, historyCache, event,
|
|
217
|
+
function append(data, historyCache, event,
|
|
218
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
219
|
+
eventData, logger) {
|
|
192
220
|
switch (event) {
|
|
193
221
|
case 'messaging-history.set':
|
|
194
222
|
for (const chat of eventData.chats) {
|
|
@@ -206,7 +234,7 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
206
234
|
for (const contact of eventData.contacts) {
|
|
207
235
|
const existingContact = data.historySets.contacts[contact.id];
|
|
208
236
|
if (existingContact) {
|
|
209
|
-
Object.assign(existingContact,
|
|
237
|
+
Object.assign(existingContact, trimUndefined(contact));
|
|
210
238
|
}
|
|
211
239
|
else {
|
|
212
240
|
const historyContactId = `c:${contact.id}`;
|
|
@@ -229,8 +257,8 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
229
257
|
data.historySets.syncType = eventData.syncType;
|
|
230
258
|
if (eventData.pastParticipants?.length) {
|
|
231
259
|
const merged = new Map();
|
|
232
|
-
const sigOf = p => `${p.userJid || ''}:${p.leaveTs || ''}:${p.leaveReason || ''}`;
|
|
233
|
-
const ingest = entry => {
|
|
260
|
+
const sigOf = (p) => `${p.userJid || ''}:${p.leaveTs || ''}:${p.leaveReason || ''}`;
|
|
261
|
+
const ingest = (entry) => {
|
|
234
262
|
const key = entry.groupJid ?? JSON.stringify(entry);
|
|
235
263
|
const existing = merged.get(key);
|
|
236
264
|
if (!existing) {
|
|
@@ -252,8 +280,8 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
252
280
|
ingest(entry);
|
|
253
281
|
data.historySets.pastParticipants = [...merged.values()];
|
|
254
282
|
}
|
|
255
|
-
data.historySets.chunkOrder = eventData.chunkOrder;
|
|
256
283
|
data.historySets.progress = eventData.progress;
|
|
284
|
+
data.historySets.chunkOrder = eventData.chunkOrder;
|
|
257
285
|
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId;
|
|
258
286
|
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest;
|
|
259
287
|
break;
|
|
@@ -286,18 +314,24 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
286
314
|
const conditionMatches = update.conditional ? update.conditional(data) : true;
|
|
287
315
|
if (conditionMatches) {
|
|
288
316
|
delete update.conditional;
|
|
317
|
+
// if there is an existing upsert, merge the update into it
|
|
289
318
|
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId];
|
|
290
319
|
if (upsert) {
|
|
291
320
|
concatChats(upsert, update);
|
|
292
321
|
}
|
|
293
322
|
else {
|
|
323
|
+
// merge the update into the existing update
|
|
294
324
|
const chatUpdate = data.chatUpdates[chatId] || {};
|
|
295
325
|
data.chatUpdates[chatId] = concatChats(chatUpdate, update);
|
|
296
326
|
}
|
|
297
327
|
}
|
|
298
328
|
else if (conditionMatches === undefined) {
|
|
329
|
+
// condition yet to be fulfilled
|
|
299
330
|
data.chatUpdates[chatId] = update;
|
|
300
331
|
}
|
|
332
|
+
// otherwise -- condition not met, update is invalid
|
|
333
|
+
// if the chat has been updated
|
|
334
|
+
// ignore any existing chat delete
|
|
301
335
|
if (data.chatDeletes.has(chatId)) {
|
|
302
336
|
data.chatDeletes.delete(chatId);
|
|
303
337
|
}
|
|
@@ -308,6 +342,7 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
308
342
|
if (!data.chatDeletes.has(chatId)) {
|
|
309
343
|
data.chatDeletes.add(chatId);
|
|
310
344
|
}
|
|
345
|
+
// remove any prior updates & upserts
|
|
311
346
|
if (data.chatUpdates[chatId]) {
|
|
312
347
|
delete data.chatUpdates[chatId];
|
|
313
348
|
}
|
|
@@ -329,14 +364,14 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
329
364
|
}
|
|
330
365
|
}
|
|
331
366
|
if (upsert) {
|
|
332
|
-
upsert = Object.assign(upsert,
|
|
367
|
+
upsert = Object.assign(upsert, trimUndefined(contact));
|
|
333
368
|
}
|
|
334
369
|
else {
|
|
335
370
|
upsert = contact;
|
|
336
371
|
data.contactUpserts[contact.id] = upsert;
|
|
337
372
|
}
|
|
338
373
|
if (data.contactUpdates[contact.id]) {
|
|
339
|
-
upsert = Object.assign(data.contactUpdates[contact.id],
|
|
374
|
+
upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact));
|
|
340
375
|
delete data.contactUpdates[contact.id];
|
|
341
376
|
}
|
|
342
377
|
}
|
|
@@ -345,11 +380,13 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
345
380
|
const contactUpdates = eventData;
|
|
346
381
|
for (const update of contactUpdates) {
|
|
347
382
|
const id = update.id;
|
|
383
|
+
// merge into prior upsert
|
|
348
384
|
const upsert = data.historySets.contacts[id] || data.contactUpserts[id];
|
|
349
385
|
if (upsert) {
|
|
350
386
|
Object.assign(upsert, update);
|
|
351
387
|
}
|
|
352
388
|
else {
|
|
389
|
+
// merge into prior update
|
|
353
390
|
const contactUpdate = data.contactUpdates[id] || {};
|
|
354
391
|
data.contactUpdates[id] = Object.assign(contactUpdate, update);
|
|
355
392
|
}
|
|
@@ -392,7 +429,10 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
392
429
|
const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message;
|
|
393
430
|
if (existing) {
|
|
394
431
|
Object.assign(existing, update);
|
|
395
|
-
if
|
|
432
|
+
// if the message was received & read by us
|
|
433
|
+
// the chat counter must have been incremented
|
|
434
|
+
// so we need to decrement it
|
|
435
|
+
if (update.status === WAMessageStatus.READ && !key.fromMe) {
|
|
396
436
|
decrementChatReadCounterIfMsgDidUnread(existing);
|
|
397
437
|
}
|
|
398
438
|
}
|
|
@@ -421,6 +461,7 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
421
461
|
}
|
|
422
462
|
}
|
|
423
463
|
else {
|
|
464
|
+
// TODO: add support
|
|
424
465
|
}
|
|
425
466
|
break;
|
|
426
467
|
case 'messages.reaction':
|
|
@@ -429,12 +470,11 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
429
470
|
const keyStr = stringifyMessageKey(key);
|
|
430
471
|
const existing = data.messageUpserts[keyStr];
|
|
431
472
|
if (existing) {
|
|
432
|
-
;
|
|
433
|
-
(0, messages_1.updateMessageWithReaction)(existing.message, reaction);
|
|
473
|
+
updateMessageWithReaction(existing.message, reaction);
|
|
434
474
|
}
|
|
435
475
|
else {
|
|
436
476
|
data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] };
|
|
437
|
-
|
|
477
|
+
updateMessageWithReaction(data.messageReactions[keyStr], reaction);
|
|
438
478
|
}
|
|
439
479
|
}
|
|
440
480
|
break;
|
|
@@ -444,12 +484,11 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
444
484
|
const keyStr = stringifyMessageKey(key);
|
|
445
485
|
const existing = data.messageUpserts[keyStr];
|
|
446
486
|
if (existing) {
|
|
447
|
-
;
|
|
448
|
-
(0, messages_1.updateMessageWithReceipt)(existing.message, receipt);
|
|
487
|
+
updateMessageWithReceipt(existing.message, receipt);
|
|
449
488
|
}
|
|
450
489
|
else {
|
|
451
490
|
data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] };
|
|
452
|
-
|
|
491
|
+
updateMessageWithReceipt(data.messageReceipts[keyStr], receipt);
|
|
453
492
|
}
|
|
454
493
|
}
|
|
455
494
|
break;
|
|
@@ -484,10 +523,12 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
484
523
|
}
|
|
485
524
|
}
|
|
486
525
|
function decrementChatReadCounterIfMsgDidUnread(message) {
|
|
526
|
+
// decrement chat unread counter
|
|
527
|
+
// if the message has already been marked read by us
|
|
487
528
|
const chatId = message.key.remoteJid;
|
|
488
529
|
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
|
|
489
|
-
if (
|
|
490
|
-
|
|
530
|
+
if (isRealMessage(message) &&
|
|
531
|
+
shouldIncrementChatUnread(message) &&
|
|
491
532
|
typeof chat?.unreadCount === 'number' &&
|
|
492
533
|
chat.unreadCount > 0) {
|
|
493
534
|
logger.debug({ chatId: chat.id }, 'decrementing chat counter');
|
|
@@ -564,7 +605,7 @@ function consolidateEvents(data) {
|
|
|
564
605
|
return map;
|
|
565
606
|
}
|
|
566
607
|
function concatChats(a, b) {
|
|
567
|
-
if (b.unreadCount === null &&
|
|
608
|
+
if (b.unreadCount === null && // neutralize unread counter
|
|
568
609
|
a.unreadCount < 0) {
|
|
569
610
|
a.unreadCount = undefined;
|
|
570
611
|
b.unreadCount = undefined;
|
|
@@ -577,4 +618,5 @@ function concatChats(a, b) {
|
|
|
577
618
|
}
|
|
578
619
|
return Object.assign(a, b);
|
|
579
620
|
}
|
|
580
|
-
const stringifyMessageKey = key => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
|
|
621
|
+
const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
|
|
622
|
+
//# sourceMappingURL=event-buffer.js.map
|