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