@genuxofficial/baileys 1.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/LICENSE +21 -0
- package/README.md +1 -0
- package/WAProto/GenerateStatics.sh +4 -0
- package/WAProto/WAProto.proto +3344 -0
- package/WAProto/index.d.ts +37016 -0
- package/WAProto/index.js +101044 -0
- package/WASignalGroup/GroupProtocol.js +1697 -0
- package/WASignalGroup/ciphertext_message.js +16 -0
- package/WASignalGroup/group_cipher.js +120 -0
- package/WASignalGroup/group_session_builder.js +46 -0
- package/WASignalGroup/index.js +5 -0
- package/WASignalGroup/keyhelper.js +21 -0
- package/WASignalGroup/protobufs.js +3 -0
- package/WASignalGroup/queue_job.js +69 -0
- package/WASignalGroup/sender_chain_key.js +50 -0
- package/WASignalGroup/sender_key_distribution_message.js +78 -0
- package/WASignalGroup/sender_key_message.js +92 -0
- package/WASignalGroup/sender_key_name.js +70 -0
- package/WASignalGroup/sender_key_record.js +56 -0
- package/WASignalGroup/sender_key_state.js +129 -0
- package/WASignalGroup/sender_message_key.js +39 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.d.ts +53 -0
- package/lib/Defaults/index.js +106 -0
- package/lib/Signal/libsignal.d.ts +3 -0
- package/lib/Signal/libsignal.js +152 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +18 -0
- package/lib/Socket/Client/types.d.ts +17 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +62 -0
- package/lib/Socket/business.d.ts +149 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.d.ts +85 -0
- package/lib/Socket/chats.js +850 -0
- package/lib/Socket/groups.d.ts +124 -0
- package/lib/Socket/groups.js +314 -0
- package/lib/Socket/index.d.ts +149 -0
- package/lib/Socket/index.js +10 -0
- package/lib/Socket/messages-recv.d.ts +136 -0
- package/lib/Socket/messages-recv.js +912 -0
- package/lib/Socket/messages-send.d.ts +130 -0
- package/lib/Socket/messages-send.js +673 -0
- package/lib/Socket/socket.d.ts +45 -0
- package/lib/Socket/socket.js +622 -0
- package/lib/Socket/usync.d.ts +38 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.d.ts +14 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.d.ts +118 -0
- package/lib/Store/make-in-memory-store.js +420 -0
- package/lib/Store/make-ordered-dictionary.d.ts +13 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.d.ts +105 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.d.ts +13 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.d.ts +108 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.d.ts +19 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.d.ts +172 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.d.ts +56 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.d.ts +46 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.d.ts +29 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.d.ts +271 -0
- package/lib/Types/Message.js +9 -0
- package/lib/Types/Product.d.ts +78 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.d.ts +57 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.d.ts +118 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.d.ts +27 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.d.ts +25 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.d.ts +63 -0
- package/lib/Types/index.js +41 -0
- package/lib/Utils/auth-utils.d.ts +18 -0
- package/lib/Utils/auth-utils.js +200 -0
- package/lib/Utils/baileys-event-stream.d.ts +16 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/business.d.ts +22 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.d.ts +72 -0
- package/lib/Utils/chat-utils.js +745 -0
- package/lib/Utils/crypto.d.ts +42 -0
- package/lib/Utils/crypto.js +181 -0
- package/lib/Utils/decode-wa-message.d.ts +36 -0
- package/lib/Utils/decode-wa-message.js +205 -0
- package/lib/Utils/event-buffer.d.ts +35 -0
- package/lib/Utils/event-buffer.js +520 -0
- package/lib/Utils/generics.d.ts +94 -0
- package/lib/Utils/generics.js +433 -0
- package/lib/Utils/history.d.ts +19 -0
- package/lib/Utils/history.js +94 -0
- package/lib/Utils/index.d.ts +17 -0
- package/lib/Utils/index.js +33 -0
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.js +116 -0
- package/lib/Utils/logger.d.ts +11 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.d.ts +7 -0
- package/lib/Utils/make-mutex.js +44 -0
- package/lib/Utils/messages-media.d.ts +108 -0
- package/lib/Utils/messages-media.js +667 -0
- package/lib/Utils/messages.d.ts +78 -0
- package/lib/Utils/messages.js +760 -0
- package/lib/Utils/noise-handler.d.ts +21 -0
- package/lib/Utils/noise-handler.js +150 -0
- package/lib/Utils/process-message.d.ts +42 -0
- package/lib/Utils/process-message.js +373 -0
- package/lib/Utils/signal.d.ts +33 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +12 -0
- package/lib/Utils/use-multi-file-auth-state.js +94 -0
- package/lib/Utils/validate-connection.d.ts +10 -0
- package/lib/Utils/validate-connection.js +175 -0
- package/lib/WABinary/constants.d.ts +27 -0
- package/lib/WABinary/constants.js +40 -0
- package/lib/WABinary/decode.d.ts +8 -0
- package/lib/WABinary/decode.js +254 -0
- package/lib/WABinary/encode.d.ts +4 -0
- package/lib/WABinary/encode.js +234 -0
- package/lib/WABinary/generic-utils.d.ts +16 -0
- package/lib/WABinary/generic-utils.js +110 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.d.ts +31 -0
- package/lib/WABinary/jid-utils.js +62 -0
- package/lib/WABinary/types.d.ts +18 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.d.ts +18 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.d.ts +39 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.d.ts +4 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/index.d.ts +4 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.d.ts +26 -0
- package/lib/WAUSync/USyncQuery.js +79 -0
- package/lib/WAUSync/USyncUser.d.ts +10 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +31 -0
- package/package.json +104 -0
@@ -0,0 +1,520 @@
|
|
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
|
+
// take the generic event and fire it as a baileys event
|
38
|
+
ev.on('event', (map) => {
|
39
|
+
for (const event in map) {
|
40
|
+
ev.emit(event, map[event]);
|
41
|
+
}
|
42
|
+
});
|
43
|
+
function buffer() {
|
44
|
+
buffersInProgress += 1;
|
45
|
+
}
|
46
|
+
function flush(force = false) {
|
47
|
+
// no buffer going on
|
48
|
+
if (!buffersInProgress) {
|
49
|
+
return false;
|
50
|
+
}
|
51
|
+
if (!force) {
|
52
|
+
// reduce the number of buffers in progress
|
53
|
+
buffersInProgress -= 1;
|
54
|
+
// if there are still some buffers going on
|
55
|
+
// then we don't flush now
|
56
|
+
if (buffersInProgress) {
|
57
|
+
return false;
|
58
|
+
}
|
59
|
+
}
|
60
|
+
const newData = makeBufferData();
|
61
|
+
const chatUpdates = Object.values(data.chatUpdates);
|
62
|
+
// gather the remaining conditional events so we re-queue them
|
63
|
+
let conditionalChatUpdatesLeft = 0;
|
64
|
+
for (const update of chatUpdates) {
|
65
|
+
if (update.conditional) {
|
66
|
+
conditionalChatUpdatesLeft += 1;
|
67
|
+
newData.chatUpdates[update.id] = update;
|
68
|
+
delete data.chatUpdates[update.id];
|
69
|
+
}
|
70
|
+
}
|
71
|
+
const consolidatedData = consolidateEvents(data);
|
72
|
+
if (Object.keys(consolidatedData).length) {
|
73
|
+
ev.emit('event', consolidatedData);
|
74
|
+
}
|
75
|
+
data = newData;
|
76
|
+
logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events');
|
77
|
+
return true;
|
78
|
+
}
|
79
|
+
return {
|
80
|
+
process(handler) {
|
81
|
+
const listener = (map) => {
|
82
|
+
handler(map);
|
83
|
+
};
|
84
|
+
ev.on('event', listener);
|
85
|
+
return () => {
|
86
|
+
ev.off('event', listener);
|
87
|
+
};
|
88
|
+
},
|
89
|
+
emit(event, evData) {
|
90
|
+
if (buffersInProgress && BUFFERABLE_EVENT_SET.has(event)) {
|
91
|
+
append(data, historyCache, event, evData, logger);
|
92
|
+
return true;
|
93
|
+
}
|
94
|
+
return ev.emit('event', { [event]: evData });
|
95
|
+
},
|
96
|
+
isBuffering() {
|
97
|
+
return buffersInProgress > 0;
|
98
|
+
},
|
99
|
+
buffer,
|
100
|
+
flush,
|
101
|
+
createBufferedFunction(work) {
|
102
|
+
return async (...args) => {
|
103
|
+
buffer();
|
104
|
+
try {
|
105
|
+
const result = await work(...args);
|
106
|
+
return result;
|
107
|
+
}
|
108
|
+
finally {
|
109
|
+
flush();
|
110
|
+
}
|
111
|
+
};
|
112
|
+
},
|
113
|
+
on: (...args) => ev.on(...args),
|
114
|
+
off: (...args) => ev.off(...args),
|
115
|
+
removeAllListeners: (...args) => ev.removeAllListeners(...args),
|
116
|
+
};
|
117
|
+
};
|
118
|
+
exports.makeEventBuffer = makeEventBuffer;
|
119
|
+
const makeBufferData = () => {
|
120
|
+
return {
|
121
|
+
historySets: {
|
122
|
+
chats: {},
|
123
|
+
messages: {},
|
124
|
+
contacts: {},
|
125
|
+
isLatest: false,
|
126
|
+
empty: true
|
127
|
+
},
|
128
|
+
chatUpserts: {},
|
129
|
+
chatUpdates: {},
|
130
|
+
chatDeletes: new Set(),
|
131
|
+
contactUpserts: {},
|
132
|
+
contactUpdates: {},
|
133
|
+
messageUpserts: {},
|
134
|
+
messageUpdates: {},
|
135
|
+
messageReactions: {},
|
136
|
+
messageDeletes: {},
|
137
|
+
messageReceipts: {},
|
138
|
+
groupUpdates: {}
|
139
|
+
};
|
140
|
+
};
|
141
|
+
function append(data, historyCache, event,
|
142
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
143
|
+
eventData, logger) {
|
144
|
+
var _a, _b, _c;
|
145
|
+
switch (event) {
|
146
|
+
case 'messaging-history.set':
|
147
|
+
for (const chat of eventData.chats) {
|
148
|
+
const existingChat = data.historySets.chats[chat.id];
|
149
|
+
if (existingChat) {
|
150
|
+
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType;
|
151
|
+
}
|
152
|
+
if (!existingChat && !historyCache.has(chat.id)) {
|
153
|
+
data.historySets.chats[chat.id] = chat;
|
154
|
+
historyCache.add(chat.id);
|
155
|
+
absorbingChatUpdate(chat);
|
156
|
+
}
|
157
|
+
}
|
158
|
+
for (const contact of eventData.contacts) {
|
159
|
+
const existingContact = data.historySets.contacts[contact.id];
|
160
|
+
if (existingContact) {
|
161
|
+
Object.assign(existingContact, (0, generics_1.trimUndefined)(contact));
|
162
|
+
}
|
163
|
+
else {
|
164
|
+
const historyContactId = `c:${contact.id}`;
|
165
|
+
const hasAnyName = contact.notify || contact.name || contact.verifiedName;
|
166
|
+
if (!historyCache.has(historyContactId) || hasAnyName) {
|
167
|
+
data.historySets.contacts[contact.id] = contact;
|
168
|
+
historyCache.add(historyContactId);
|
169
|
+
}
|
170
|
+
}
|
171
|
+
}
|
172
|
+
for (const message of eventData.messages) {
|
173
|
+
const key = stringifyMessageKey(message.key);
|
174
|
+
const existingMsg = data.historySets.messages[key];
|
175
|
+
if (!existingMsg && !historyCache.has(key)) {
|
176
|
+
data.historySets.messages[key] = message;
|
177
|
+
historyCache.add(key);
|
178
|
+
}
|
179
|
+
}
|
180
|
+
data.historySets.empty = false;
|
181
|
+
data.historySets.syncType = eventData.syncType;
|
182
|
+
data.historySets.progress = eventData.progress;
|
183
|
+
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId;
|
184
|
+
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest;
|
185
|
+
break;
|
186
|
+
case 'chats.upsert':
|
187
|
+
for (const chat of eventData) {
|
188
|
+
let upsert = data.chatUpserts[chat.id];
|
189
|
+
if (!upsert) {
|
190
|
+
upsert = data.historySets[chat.id];
|
191
|
+
if (upsert) {
|
192
|
+
logger.debug({ chatId: chat.id }, 'absorbed chat upsert in chat set');
|
193
|
+
}
|
194
|
+
}
|
195
|
+
if (upsert) {
|
196
|
+
upsert = concatChats(upsert, chat);
|
197
|
+
}
|
198
|
+
else {
|
199
|
+
upsert = chat;
|
200
|
+
data.chatUpserts[chat.id] = upsert;
|
201
|
+
}
|
202
|
+
absorbingChatUpdate(upsert);
|
203
|
+
if (data.chatDeletes.has(chat.id)) {
|
204
|
+
data.chatDeletes.delete(chat.id);
|
205
|
+
}
|
206
|
+
}
|
207
|
+
break;
|
208
|
+
case 'chats.update':
|
209
|
+
for (const update of eventData) {
|
210
|
+
const chatId = update.id;
|
211
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true;
|
212
|
+
if (conditionMatches) {
|
213
|
+
delete update.conditional;
|
214
|
+
// if there is an existing upsert, merge the update into it
|
215
|
+
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId];
|
216
|
+
if (upsert) {
|
217
|
+
concatChats(upsert, update);
|
218
|
+
}
|
219
|
+
else {
|
220
|
+
// merge the update into the existing update
|
221
|
+
const chatUpdate = data.chatUpdates[chatId] || {};
|
222
|
+
data.chatUpdates[chatId] = concatChats(chatUpdate, update);
|
223
|
+
}
|
224
|
+
}
|
225
|
+
else if (conditionMatches === undefined) {
|
226
|
+
// condition yet to be fulfilled
|
227
|
+
data.chatUpdates[chatId] = update;
|
228
|
+
}
|
229
|
+
// otherwise -- condition not met, update is invalid
|
230
|
+
// if the chat has been updated
|
231
|
+
// ignore any existing chat delete
|
232
|
+
if (data.chatDeletes.has(chatId)) {
|
233
|
+
data.chatDeletes.delete(chatId);
|
234
|
+
}
|
235
|
+
}
|
236
|
+
break;
|
237
|
+
case 'chats.delete':
|
238
|
+
for (const chatId of eventData) {
|
239
|
+
if (!data.chatDeletes.has(chatId)) {
|
240
|
+
data.chatDeletes.add(chatId);
|
241
|
+
}
|
242
|
+
// remove any prior updates & upserts
|
243
|
+
if (data.chatUpdates[chatId]) {
|
244
|
+
delete data.chatUpdates[chatId];
|
245
|
+
}
|
246
|
+
if (data.chatUpserts[chatId]) {
|
247
|
+
delete data.chatUpserts[chatId];
|
248
|
+
}
|
249
|
+
if (data.historySets.chats[chatId]) {
|
250
|
+
delete data.historySets.chats[chatId];
|
251
|
+
}
|
252
|
+
}
|
253
|
+
break;
|
254
|
+
case 'contacts.upsert':
|
255
|
+
for (const contact of eventData) {
|
256
|
+
let upsert = data.contactUpserts[contact.id];
|
257
|
+
if (!upsert) {
|
258
|
+
upsert = data.historySets.contacts[contact.id];
|
259
|
+
if (upsert) {
|
260
|
+
logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set');
|
261
|
+
}
|
262
|
+
}
|
263
|
+
if (upsert) {
|
264
|
+
upsert = Object.assign(upsert, (0, generics_1.trimUndefined)(contact));
|
265
|
+
}
|
266
|
+
else {
|
267
|
+
upsert = contact;
|
268
|
+
data.contactUpserts[contact.id] = upsert;
|
269
|
+
}
|
270
|
+
if (data.contactUpdates[contact.id]) {
|
271
|
+
upsert = Object.assign(data.contactUpdates[contact.id], (0, generics_1.trimUndefined)(contact));
|
272
|
+
delete data.contactUpdates[contact.id];
|
273
|
+
}
|
274
|
+
}
|
275
|
+
break;
|
276
|
+
case 'contacts.update':
|
277
|
+
const contactUpdates = eventData;
|
278
|
+
for (const update of contactUpdates) {
|
279
|
+
const id = update.id;
|
280
|
+
// merge into prior upsert
|
281
|
+
const upsert = data.historySets.contacts[id] || data.contactUpserts[id];
|
282
|
+
if (upsert) {
|
283
|
+
Object.assign(upsert, update);
|
284
|
+
}
|
285
|
+
else {
|
286
|
+
// merge into prior update
|
287
|
+
const contactUpdate = data.contactUpdates[id] || {};
|
288
|
+
data.contactUpdates[id] = Object.assign(contactUpdate, update);
|
289
|
+
}
|
290
|
+
}
|
291
|
+
break;
|
292
|
+
case 'messages.upsert':
|
293
|
+
const { messages, type } = eventData;
|
294
|
+
for (const message of messages) {
|
295
|
+
const key = stringifyMessageKey(message.key);
|
296
|
+
let existing = (_a = data.messageUpserts[key]) === null || _a === void 0 ? void 0 : _a.message;
|
297
|
+
if (!existing) {
|
298
|
+
existing = data.historySets.messages[key];
|
299
|
+
if (existing) {
|
300
|
+
logger.debug({ messageId: key }, 'absorbed message upsert in message set');
|
301
|
+
}
|
302
|
+
}
|
303
|
+
if (existing) {
|
304
|
+
message.messageTimestamp = existing.messageTimestamp;
|
305
|
+
}
|
306
|
+
if (data.messageUpdates[key]) {
|
307
|
+
logger.debug('absorbed prior message update in message upsert');
|
308
|
+
Object.assign(message, data.messageUpdates[key].update);
|
309
|
+
delete data.messageUpdates[key];
|
310
|
+
}
|
311
|
+
if (data.historySets.messages[key]) {
|
312
|
+
data.historySets.messages[key] = message;
|
313
|
+
}
|
314
|
+
else {
|
315
|
+
data.messageUpserts[key] = {
|
316
|
+
message,
|
317
|
+
type: type === 'notify' || ((_b = data.messageUpserts[key]) === null || _b === void 0 ? void 0 : _b.type) === 'notify'
|
318
|
+
? 'notify'
|
319
|
+
: type
|
320
|
+
};
|
321
|
+
}
|
322
|
+
}
|
323
|
+
break;
|
324
|
+
case 'messages.update':
|
325
|
+
const msgUpdates = eventData;
|
326
|
+
for (const { key, update } of msgUpdates) {
|
327
|
+
const keyStr = stringifyMessageKey(key);
|
328
|
+
const existing = data.historySets.messages[keyStr] || ((_c = data.messageUpserts[keyStr]) === null || _c === void 0 ? void 0 : _c.message);
|
329
|
+
if (existing) {
|
330
|
+
Object.assign(existing, update);
|
331
|
+
// if the message was received & read by us
|
332
|
+
// the chat counter must have been incremented
|
333
|
+
// so we need to decrement it
|
334
|
+
if (update.status === Types_1.WAMessageStatus.READ && !key.fromMe) {
|
335
|
+
decrementChatReadCounterIfMsgDidUnread(existing);
|
336
|
+
}
|
337
|
+
}
|
338
|
+
else {
|
339
|
+
const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} };
|
340
|
+
Object.assign(msgUpdate.update, update);
|
341
|
+
data.messageUpdates[keyStr] = msgUpdate;
|
342
|
+
}
|
343
|
+
}
|
344
|
+
break;
|
345
|
+
case 'messages.delete':
|
346
|
+
const deleteData = eventData;
|
347
|
+
if ('keys' in deleteData) {
|
348
|
+
const { keys } = deleteData;
|
349
|
+
for (const key of keys) {
|
350
|
+
const keyStr = stringifyMessageKey(key);
|
351
|
+
if (!data.messageDeletes[keyStr]) {
|
352
|
+
data.messageDeletes[keyStr] = key;
|
353
|
+
}
|
354
|
+
if (data.messageUpserts[keyStr]) {
|
355
|
+
delete data.messageUpserts[keyStr];
|
356
|
+
}
|
357
|
+
if (data.messageUpdates[keyStr]) {
|
358
|
+
delete data.messageUpdates[keyStr];
|
359
|
+
}
|
360
|
+
}
|
361
|
+
}
|
362
|
+
else {
|
363
|
+
// TODO: add support
|
364
|
+
}
|
365
|
+
break;
|
366
|
+
case 'messages.reaction':
|
367
|
+
const reactions = eventData;
|
368
|
+
for (const { key, reaction } of reactions) {
|
369
|
+
const keyStr = stringifyMessageKey(key);
|
370
|
+
const existing = data.messageUpserts[keyStr];
|
371
|
+
if (existing) {
|
372
|
+
(0, messages_1.updateMessageWithReaction)(existing.message, reaction);
|
373
|
+
}
|
374
|
+
else {
|
375
|
+
data.messageReactions[keyStr] = data.messageReactions[keyStr]
|
376
|
+
|| { key, reactions: [] };
|
377
|
+
(0, messages_1.updateMessageWithReaction)(data.messageReactions[keyStr], reaction);
|
378
|
+
}
|
379
|
+
}
|
380
|
+
break;
|
381
|
+
case 'message-receipt.update':
|
382
|
+
const receipts = eventData;
|
383
|
+
for (const { key, receipt } of receipts) {
|
384
|
+
const keyStr = stringifyMessageKey(key);
|
385
|
+
const existing = data.messageUpserts[keyStr];
|
386
|
+
if (existing) {
|
387
|
+
(0, messages_1.updateMessageWithReceipt)(existing.message, receipt);
|
388
|
+
}
|
389
|
+
else {
|
390
|
+
data.messageReceipts[keyStr] = data.messageReceipts[keyStr]
|
391
|
+
|| { key, userReceipt: [] };
|
392
|
+
(0, messages_1.updateMessageWithReceipt)(data.messageReceipts[keyStr], receipt);
|
393
|
+
}
|
394
|
+
}
|
395
|
+
break;
|
396
|
+
case 'groups.update':
|
397
|
+
const groupUpdates = eventData;
|
398
|
+
for (const update of groupUpdates) {
|
399
|
+
const id = update.id;
|
400
|
+
const groupUpdate = data.groupUpdates[id] || {};
|
401
|
+
if (!data.groupUpdates[id]) {
|
402
|
+
data.groupUpdates[id] = Object.assign(groupUpdate, update);
|
403
|
+
}
|
404
|
+
}
|
405
|
+
break;
|
406
|
+
default:
|
407
|
+
throw new Error(`"${event}" cannot be buffered`);
|
408
|
+
}
|
409
|
+
function absorbingChatUpdate(existing) {
|
410
|
+
const chatId = existing.id;
|
411
|
+
const update = data.chatUpdates[chatId];
|
412
|
+
if (update) {
|
413
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true;
|
414
|
+
if (conditionMatches) {
|
415
|
+
delete update.conditional;
|
416
|
+
logger.debug({ chatId }, 'absorbed chat update in existing chat');
|
417
|
+
Object.assign(existing, concatChats(update, existing));
|
418
|
+
delete data.chatUpdates[chatId];
|
419
|
+
}
|
420
|
+
else if (conditionMatches === false) {
|
421
|
+
logger.debug({ chatId }, 'chat update condition fail, removing');
|
422
|
+
delete data.chatUpdates[chatId];
|
423
|
+
}
|
424
|
+
}
|
425
|
+
}
|
426
|
+
function decrementChatReadCounterIfMsgDidUnread(message) {
|
427
|
+
// decrement chat unread counter
|
428
|
+
// if the message has already been marked read by us
|
429
|
+
const chatId = message.key.remoteJid;
|
430
|
+
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
|
431
|
+
if ((0, process_message_1.isRealMessage)(message, '')
|
432
|
+
&& (0, process_message_1.shouldIncrementChatUnread)(message)
|
433
|
+
&& typeof (chat === null || chat === void 0 ? void 0 : chat.unreadCount) === 'number'
|
434
|
+
&& chat.unreadCount > 0) {
|
435
|
+
logger.debug({ chatId: chat.id }, 'decrementing chat counter');
|
436
|
+
chat.unreadCount -= 1;
|
437
|
+
if (chat.unreadCount === 0) {
|
438
|
+
delete chat.unreadCount;
|
439
|
+
}
|
440
|
+
}
|
441
|
+
}
|
442
|
+
}
|
443
|
+
function consolidateEvents(data) {
|
444
|
+
const map = {};
|
445
|
+
if (!data.historySets.empty) {
|
446
|
+
map['messaging-history.set'] = {
|
447
|
+
chats: Object.values(data.historySets.chats),
|
448
|
+
messages: Object.values(data.historySets.messages),
|
449
|
+
contacts: Object.values(data.historySets.contacts),
|
450
|
+
syncType: data.historySets.syncType,
|
451
|
+
progress: data.historySets.progress,
|
452
|
+
isLatest: data.historySets.isLatest,
|
453
|
+
peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
|
454
|
+
};
|
455
|
+
}
|
456
|
+
const chatUpsertList = Object.values(data.chatUpserts);
|
457
|
+
if (chatUpsertList.length) {
|
458
|
+
map['chats.upsert'] = chatUpsertList;
|
459
|
+
}
|
460
|
+
const chatUpdateList = Object.values(data.chatUpdates);
|
461
|
+
if (chatUpdateList.length) {
|
462
|
+
map['chats.update'] = chatUpdateList;
|
463
|
+
}
|
464
|
+
const chatDeleteList = Array.from(data.chatDeletes);
|
465
|
+
if (chatDeleteList.length) {
|
466
|
+
map['chats.delete'] = chatDeleteList;
|
467
|
+
}
|
468
|
+
const messageUpsertList = Object.values(data.messageUpserts);
|
469
|
+
if (messageUpsertList.length) {
|
470
|
+
const type = messageUpsertList[0].type;
|
471
|
+
map['messages.upsert'] = {
|
472
|
+
messages: messageUpsertList.map(m => m.message),
|
473
|
+
type
|
474
|
+
};
|
475
|
+
}
|
476
|
+
const messageUpdateList = Object.values(data.messageUpdates);
|
477
|
+
if (messageUpdateList.length) {
|
478
|
+
map['messages.update'] = messageUpdateList;
|
479
|
+
}
|
480
|
+
const messageDeleteList = Object.values(data.messageDeletes);
|
481
|
+
if (messageDeleteList.length) {
|
482
|
+
map['messages.delete'] = { keys: messageDeleteList };
|
483
|
+
}
|
484
|
+
const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) => reactions.flatMap(reaction => ({ key, reaction })));
|
485
|
+
if (messageReactionList.length) {
|
486
|
+
map['messages.reaction'] = messageReactionList;
|
487
|
+
}
|
488
|
+
const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) => userReceipt.flatMap(receipt => ({ key, receipt })));
|
489
|
+
if (messageReceiptList.length) {
|
490
|
+
map['message-receipt.update'] = messageReceiptList;
|
491
|
+
}
|
492
|
+
const contactUpsertList = Object.values(data.contactUpserts);
|
493
|
+
if (contactUpsertList.length) {
|
494
|
+
map['contacts.upsert'] = contactUpsertList;
|
495
|
+
}
|
496
|
+
const contactUpdateList = Object.values(data.contactUpdates);
|
497
|
+
if (contactUpdateList.length) {
|
498
|
+
map['contacts.update'] = contactUpdateList;
|
499
|
+
}
|
500
|
+
const groupUpdateList = Object.values(data.groupUpdates);
|
501
|
+
if (groupUpdateList.length) {
|
502
|
+
map['groups.update'] = groupUpdateList;
|
503
|
+
}
|
504
|
+
return map;
|
505
|
+
}
|
506
|
+
function concatChats(a, b) {
|
507
|
+
if (b.unreadCount === null && // neutralize unread counter
|
508
|
+
a.unreadCount < 0) {
|
509
|
+
a.unreadCount = undefined;
|
510
|
+
b.unreadCount = undefined;
|
511
|
+
}
|
512
|
+
if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
|
513
|
+
b = { ...b };
|
514
|
+
if (b.unreadCount >= 0) {
|
515
|
+
b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0);
|
516
|
+
}
|
517
|
+
}
|
518
|
+
return Object.assign(a, b);
|
519
|
+
}
|
520
|
+
const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
|
@@ -0,0 +1,94 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
/// <reference types="node" />
|
3
|
+
import { AxiosRequestConfig } from 'axios';
|
4
|
+
import { proto } from '../../WAProto';
|
5
|
+
import { BaileysEventEmitter, BaileysEventMap, BrowsersMap, ConnectionState, WACallUpdateType, WAVersion } from '../Types';
|
6
|
+
import { BinaryNode } from '../WABinary';
|
7
|
+
import { ILogger } from './logger';
|
8
|
+
export declare const Browsers: BrowsersMap;
|
9
|
+
export declare const getPlatformId: (browser: string) => any;
|
10
|
+
export declare const BufferJSON: {
|
11
|
+
replacer: (k: any, value: any) => any;
|
12
|
+
reviver: (_: any, value: any) => any;
|
13
|
+
};
|
14
|
+
export declare const getKeyAuthor: (key: proto.IMessageKey | undefined | null, meId?: string) => string;
|
15
|
+
export declare const writeRandomPadMax16: (msg: Uint8Array) => Buffer;
|
16
|
+
export declare const unpadRandomMax16: (e: Uint8Array | Buffer) => Uint8Array;
|
17
|
+
export declare const encodeWAMessage: (message: proto.IMessage) => Buffer;
|
18
|
+
export declare const generateRegistrationId: () => number;
|
19
|
+
export declare const encodeBigEndian: (e: number, t?: number) => Uint8Array;
|
20
|
+
export declare const toNumber: (t: Long | number | null | undefined) => number;
|
21
|
+
/** unix timestamp of a date in seconds */
|
22
|
+
export declare const unixTimestampSeconds: (date?: Date) => number;
|
23
|
+
export type DebouncedTimeout = ReturnType<typeof debouncedTimeout>;
|
24
|
+
export declare const debouncedTimeout: (intervalMs?: number, task?: () => void) => {
|
25
|
+
start: (newIntervalMs?: number, newTask?: () => void) => void;
|
26
|
+
cancel: () => void;
|
27
|
+
setTask: (newTask: () => void) => () => void;
|
28
|
+
setInterval: (newInterval: number) => number;
|
29
|
+
};
|
30
|
+
export declare const delay: (ms: number) => Promise<void>;
|
31
|
+
export declare const delayCancellable: (ms: number) => {
|
32
|
+
delay: Promise<void>;
|
33
|
+
cancel: () => void;
|
34
|
+
};
|
35
|
+
export declare function promiseTimeout<T>(ms: number | undefined, promise: (resolve: (v: T) => void, reject: (error: any) => void) => void): Promise<T>;
|
36
|
+
export declare const generateMessageIDV2: (userId?: string) => string;
|
37
|
+
export declare const generateMessageID: () => string;
|
38
|
+
export declare function bindWaitForEvent<T extends keyof BaileysEventMap>(ev: BaileysEventEmitter, event: T): (check: (u: BaileysEventMap[T]) => Promise<boolean | undefined>, timeoutMs?: number) => Promise<void>;
|
39
|
+
export declare const bindWaitForConnectionUpdate: (ev: BaileysEventEmitter) => (check: (u: Partial<ConnectionState>) => Promise<boolean | undefined>, timeoutMs?: number) => Promise<void>;
|
40
|
+
export declare const printQRIfNecessaryListener: (ev: BaileysEventEmitter, logger: ILogger) => void;
|
41
|
+
/**
|
42
|
+
* utility that fetches latest baileys version from the master branch.
|
43
|
+
* Use to ensure your WA connection is always on the latest version
|
44
|
+
*/
|
45
|
+
export declare const fetchLatestBaileysVersion: (options?: AxiosRequestConfig<{}>) => Promise<{
|
46
|
+
version: WAVersion;
|
47
|
+
isLatest: boolean;
|
48
|
+
error?: undefined;
|
49
|
+
} | {
|
50
|
+
version: WAVersion;
|
51
|
+
isLatest: boolean;
|
52
|
+
error: any;
|
53
|
+
}>;
|
54
|
+
/**
|
55
|
+
* A utility that fetches the latest web version of whatsapp.
|
56
|
+
* Use to ensure your WA connection is always on the latest version
|
57
|
+
*/
|
58
|
+
export declare const fetchLatestWaWebVersion: (options: AxiosRequestConfig<{}>) => Promise<{
|
59
|
+
version: WAVersion;
|
60
|
+
isLatest: boolean;
|
61
|
+
error?: undefined;
|
62
|
+
} | {
|
63
|
+
version: WAVersion;
|
64
|
+
isLatest: boolean;
|
65
|
+
error: any;
|
66
|
+
}>;
|
67
|
+
/** unique message tag prefix for MD clients */
|
68
|
+
export declare const generateMdTagPrefix: () => string;
|
69
|
+
/**
|
70
|
+
* Given a type of receipt, returns what the new status of the message should be
|
71
|
+
* @param type type from receipt
|
72
|
+
*/
|
73
|
+
export declare const getStatusFromReceiptType: (type: string | undefined) => proto.WebMessageInfo.Status;
|
74
|
+
/**
|
75
|
+
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
|
76
|
+
* @param reason the string reason given, eg. "conflict"
|
77
|
+
*/
|
78
|
+
export declare const getErrorCodeFromStreamError: (node: BinaryNode) => {
|
79
|
+
reason: string;
|
80
|
+
statusCode: number;
|
81
|
+
};
|
82
|
+
export declare const getCallStatusFromNode: ({ tag, attrs }: BinaryNode) => WACallUpdateType;
|
83
|
+
export declare const getCodeFromWSError: (error: Error) => number;
|
84
|
+
/**
|
85
|
+
* Is the given platform WA business
|
86
|
+
* @param platform AuthenticationCreds.platform
|
87
|
+
*/
|
88
|
+
export declare const isWABusinessPlatform: (platform: string) => boolean;
|
89
|
+
export declare function trimUndefined(obj: {
|
90
|
+
[_: string]: any;
|
91
|
+
}): {
|
92
|
+
[_: string]: any;
|
93
|
+
};
|
94
|
+
export declare function bytesToCrockford(buffer: Buffer): string;
|