@aseppxyzz12/baileys 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +25 -0
- package/README.md +2579 -0
- package/WAProto/AICommon/AICommon.js +19396 -0
- package/WAProto/AICommon/AICommon.proto +820 -0
- package/WAProto/Adv/Adv.js +1137 -0
- package/WAProto/Adv/Adv.proto +42 -0
- package/WAProto/BotMetadata/BotMetadata.js +8975 -0
- package/WAProto/BotMetadata/BotMetadata.proto +484 -0
- package/WAProto/Cert/Cert.js +893 -0
- package/WAProto/Cert/Cert.proto +30 -0
- package/WAProto/ChatLockSettings/ChatLockSettings.js +673 -0
- package/WAProto/ChatLockSettings/ChatLockSettings.proto +9 -0
- package/WAProto/CompanionReg/CompanionReg.js +2457 -0
- package/WAProto/CompanionReg/CompanionReg.proto +103 -0
- package/WAProto/DeviceCapabilities/DeviceCapabilities.js +846 -0
- package/WAProto/DeviceCapabilities/DeviceCapabilities.proto +38 -0
- package/WAProto/E2E/E2E.js +78728 -0
- package/WAProto/E2E/E2E.proto +1970 -0
- package/WAProto/Ephemeral/Ephemeral.js +143 -0
- package/WAProto/Ephemeral/Ephemeral.proto +7 -0
- package/WAProto/HistorySync/HistorySync.js +95931 -0
- package/WAProto/HistorySync/HistorySync.proto +229 -0
- package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.js +414 -0
- package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.proto +13 -0
- package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.js +980 -0
- package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.proto +38 -0
- package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.js +81185 -0
- package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.proto +88 -0
- package/WAProto/MmsRetry/MmsRetry.js +310 -0
- package/WAProto/MmsRetry/MmsRetry.proto +19 -0
- package/WAProto/Protocol/Protocol.js +399 -0
- package/WAProto/Protocol/Protocol.proto +22 -0
- package/WAProto/Reporting/Reporting.js +535 -0
- package/WAProto/Reporting/Reporting.proto +22 -0
- package/WAProto/ServerSync/ServerSync.js +1830 -0
- package/WAProto/ServerSync/ServerSync.proto +70 -0
- package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.js +3267 -0
- package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.proto +95 -0
- package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.js +1140 -0
- package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.proto +44 -0
- package/WAProto/StatusAttributions/StatusAttributions.js +1604 -0
- package/WAProto/StatusAttributions/StatusAttributions.proto +89 -0
- package/WAProto/SyncAction/SyncAction.js +17424 -0
- package/WAProto/SyncAction/SyncAction.proto +663 -0
- package/WAProto/UserPassword/UserPassword.js +544 -0
- package/WAProto/UserPassword/UserPassword.proto +28 -0
- package/WAProto/VnameCert/VnameCert.js +1466 -0
- package/WAProto/VnameCert/VnameCert.proto +65 -0
- package/WAProto/Wa6/Wa6.js +4601 -0
- package/WAProto/Wa6/Wa6.proto +241 -0
- package/WAProto/Web/Web.js +90819 -0
- package/WAProto/Web/Web.proto +605 -0
- package/WAProto/index.js +30 -0
- package/engine-requirements.js +9 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/connection.js +39 -0
- package/lib/Defaults/constants.js +50 -0
- package/lib/Defaults/history.js +13 -0
- package/lib/Defaults/index.js +36 -0
- package/lib/Defaults/media.js +43 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Defaults/prefix.js +12 -0
- package/lib/Function/Download/tiktok.js +19 -0
- package/lib/Function/Tools/bypass.js +19 -0
- package/lib/Function/index.js +13 -0
- package/lib/Signal/Group/ciphertext-message.js +14 -0
- package/lib/Signal/Group/group-session-builder.js +46 -0
- package/lib/Signal/Group/group_cipher.js +104 -0
- package/lib/Signal/Group/index.js +42 -0
- package/lib/Signal/Group/keyhelper.js +21 -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 +78 -0
- package/lib/Signal/Group/sender-key-name.js +49 -0
- package/lib/Signal/Group/sender-key-record.js +45 -0
- package/lib/Signal/Group/sender-key-state.js +100 -0
- package/lib/Signal/Group/sender-message-key.js +28 -0
- package/lib/Signal/libsignal.js +364 -0
- package/lib/Signal/lid-mapping.js +164 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.js +31 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/types.js +12 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/Client/websocket.js +67 -0
- package/lib/Socket/business.js +291 -0
- package/lib/Socket/chats.js +874 -0
- package/lib/Socket/community.js +454 -0
- package/lib/Socket/dugong.js +658 -0
- package/lib/Socket/groups.js +357 -0
- package/lib/Socket/index.js +13 -0
- package/lib/Socket/messages-recv.js +1509 -0
- package/lib/Socket/messages-send.js +1505 -0
- package/lib/Socket/mex.js +54 -0
- package/lib/Socket/newsletter.js +171 -0
- package/lib/Socket/registration.js +167 -0
- package/lib/Socket/rich-content.js +279 -0
- package/lib/Socket/socket.js +905 -0
- package/lib/Socket/username.js +157 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +37 -0
- package/lib/Store/keyed-db.js +114 -0
- package/lib/Store/make-cache-manager-store.js +77 -0
- package/lib/Store/make-in-memory-store.js +423 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.js +26 -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 +10 -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 +4 -0
- package/lib/Types/Message.js +11 -0
- package/lib/Types/Mex.js +40 -0
- package/lib/Types/MexUpdates.js +15 -0
- package/lib/Types/Newsletter.js +32 -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 +9 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +60 -0
- package/lib/Utils/auth-utils.js +256 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/bot-toolkit.js +455 -0
- package/lib/Utils/browser-utils.js +29 -0
- package/lib/Utils/business.js +233 -0
- package/lib/Utils/chat-utils.js +904 -0
- package/lib/Utils/companion-reg-client-utils.js +41 -0
- package/lib/Utils/crypto.js +144 -0
- package/lib/Utils/decode-wa-message.js +305 -0
- package/lib/Utils/event-buffer.js +555 -0
- package/lib/Utils/generics.js +411 -0
- package/lib/Utils/history.js +100 -0
- package/lib/Utils/identity-change-handler.js +53 -0
- package/lib/Utils/index.js +57 -0
- package/lib/Utils/link-preview.js +76 -0
- package/lib/Utils/logger.js +4 -0
- package/lib/Utils/lt-hash.js +45 -0
- package/lib/Utils/make-mutex.js +33 -0
- package/lib/Utils/message-composer.js +289 -0
- package/lib/Utils/message-retry-manager.js +134 -0
- package/lib/Utils/messages-media.js +806 -0
- package/lib/Utils/messages.js +1908 -0
- package/lib/Utils/noise-handler.js +157 -0
- package/lib/Utils/offline-node-processor.js +42 -0
- package/lib/Utils/pre-key-manager.js +86 -0
- package/lib/Utils/process-message.js +814 -0
- package/lib/Utils/reporting-utils.js +264 -0
- package/lib/Utils/signal.js +183 -0
- package/lib/Utils/stanza-ack.js +36 -0
- package/lib/Utils/sync-action-utils.js +52 -0
- package/lib/Utils/tc-token-utils.js +167 -0
- package/lib/Utils/use-multi-file-auth-state.js +104 -0
- package/lib/Utils/validate-connection.js +229 -0
- package/lib/Utils/vialeys-event-stream.js +41 -0
- package/lib/WABinary/constants.js +1308 -0
- package/lib/WABinary/decode.js +233 -0
- package/lib/WABinary/encode.js +212 -0
- package/lib/WABinary/generic-utils.js +124 -0
- package/lib/WABinary/index.js +36 -0
- package/lib/WABinary/jid-utils.js +101 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +12 -0
- package/lib/WAM/constants.js +20491 -0
- package/lib/WAM/encode.js +148 -0
- package/lib/WAM/index.js +34 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +62 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +50 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +25 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +38 -0
- package/lib/WAUSync/USyncQuery.js +97 -0
- package/lib/WAUSync/USyncUser.js +33 -0
- package/lib/WAUSync/index.js +34 -0
- package/lib/index.js +113 -0
- package/lib/messages-send.js +1496 -0
- package/package.json +100 -0
|
@@ -0,0 +1,555 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const EventEmitter = require("events");
|
|
4
|
+
const { WAMessageStatus: WAMessageStatus } = require("../Types");
|
|
5
|
+
const { trimUndefined: trimUndefined } = require("./generics");
|
|
6
|
+
const {
|
|
7
|
+
updateMessageWithReceipt: updateMessageWithReceipt,
|
|
8
|
+
updateMessageWithReaction: updateMessageWithReaction,
|
|
9
|
+
} = require("./messages");
|
|
10
|
+
const { isRealMessage: isRealMessage } = require("./process-message");
|
|
11
|
+
const BUFFERABLE_EVENT = [
|
|
12
|
+
"messaging-history.set",
|
|
13
|
+
"chats.upsert",
|
|
14
|
+
"chats.update",
|
|
15
|
+
"chats.delete",
|
|
16
|
+
"contacts.upsert",
|
|
17
|
+
"contacts.update",
|
|
18
|
+
"messages.upsert",
|
|
19
|
+
"messages.update",
|
|
20
|
+
"messages.delete",
|
|
21
|
+
"messages.reaction",
|
|
22
|
+
"message-receipt.update",
|
|
23
|
+
"groups.update",
|
|
24
|
+
];
|
|
25
|
+
const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT);
|
|
26
|
+
const makeEventBuffer = (logger) => {
|
|
27
|
+
const ev = new EventEmitter();
|
|
28
|
+
const historyCache = new Set();
|
|
29
|
+
let data = makeBufferData();
|
|
30
|
+
let isBuffering = false;
|
|
31
|
+
let bufferTimeout = null;
|
|
32
|
+
let bufferCount = 0;
|
|
33
|
+
const MAX_HISTORY_CACHE_SIZE = 1e4;
|
|
34
|
+
const BUFFER_TIMEOUT_MS = 3e4;
|
|
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
|
+
if (bufferTimeout) {
|
|
46
|
+
clearTimeout(bufferTimeout);
|
|
47
|
+
}
|
|
48
|
+
bufferTimeout = setTimeout(() => {
|
|
49
|
+
if (isBuffering) {
|
|
50
|
+
logger.warn("Buffer timeout reached, auto-flushing");
|
|
51
|
+
flush();
|
|
52
|
+
}
|
|
53
|
+
}, BUFFER_TIMEOUT_MS);
|
|
54
|
+
} else {
|
|
55
|
+
bufferCount++;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function flush() {
|
|
59
|
+
if (!isBuffering) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
logger.debug({ bufferCount: bufferCount }, "Flushing event buffer");
|
|
63
|
+
isBuffering = false;
|
|
64
|
+
bufferCount = 0;
|
|
65
|
+
if (bufferTimeout) {
|
|
66
|
+
clearTimeout(bufferTimeout);
|
|
67
|
+
bufferTimeout = null;
|
|
68
|
+
}
|
|
69
|
+
if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
|
|
70
|
+
logger.debug({ cacheSize: historyCache.size }, "Clearing history cache");
|
|
71
|
+
historyCache.clear();
|
|
72
|
+
}
|
|
73
|
+
const newData = makeBufferData();
|
|
74
|
+
const chatUpdates = Object.values(data.chatUpdates);
|
|
75
|
+
let conditionalChatUpdatesLeft = 0;
|
|
76
|
+
for (const update of chatUpdates) {
|
|
77
|
+
if (update.conditional) {
|
|
78
|
+
conditionalChatUpdatesLeft += 1;
|
|
79
|
+
newData.chatUpdates[update.id] = update;
|
|
80
|
+
delete data.chatUpdates[update.id];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const consolidatedData = consolidateEvents(data);
|
|
84
|
+
if (Object.keys(consolidatedData).length) {
|
|
85
|
+
ev.emit("event", consolidatedData);
|
|
86
|
+
}
|
|
87
|
+
data = newData;
|
|
88
|
+
logger.trace(
|
|
89
|
+
{ conditionalChatUpdatesLeft: conditionalChatUpdatesLeft },
|
|
90
|
+
"released buffered events"
|
|
91
|
+
);
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
process(handler) {
|
|
96
|
+
const listener = (map) => {
|
|
97
|
+
handler(map);
|
|
98
|
+
};
|
|
99
|
+
ev.on("event", listener);
|
|
100
|
+
return () => {
|
|
101
|
+
ev.off("event", listener);
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
emit(event, evData) {
|
|
105
|
+
if (event === "messages.upsert") {
|
|
106
|
+
const { type: type } = evData;
|
|
107
|
+
const existingUpserts = Object.values(data.messageUpserts);
|
|
108
|
+
if (existingUpserts.length > 0) {
|
|
109
|
+
const bufferedType = existingUpserts[0].type;
|
|
110
|
+
if (bufferedType !== type) {
|
|
111
|
+
logger.debug(
|
|
112
|
+
{ bufferedType: bufferedType, newType: type },
|
|
113
|
+
"messages.upsert type mismatch, emitting buffered messages"
|
|
114
|
+
);
|
|
115
|
+
ev.emit("event", {
|
|
116
|
+
"messages.upsert": {
|
|
117
|
+
messages: existingUpserts.map((m) => m.message),
|
|
118
|
+
type: bufferedType,
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
data.messageUpserts = {};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
|
|
126
|
+
append(data, historyCache, event, evData, logger);
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
return ev.emit("event", { [event]: evData });
|
|
130
|
+
},
|
|
131
|
+
isBuffering() {
|
|
132
|
+
return isBuffering;
|
|
133
|
+
},
|
|
134
|
+
buffer: buffer,
|
|
135
|
+
flush: flush,
|
|
136
|
+
createBufferedFunction(work) {
|
|
137
|
+
return async (...args) => {
|
|
138
|
+
buffer();
|
|
139
|
+
try {
|
|
140
|
+
const result = await work(...args);
|
|
141
|
+
if (bufferCount === 1) {
|
|
142
|
+
setTimeout(() => {
|
|
143
|
+
if (isBuffering && bufferCount === 1) {
|
|
144
|
+
flush();
|
|
145
|
+
}
|
|
146
|
+
}, 100);
|
|
147
|
+
}
|
|
148
|
+
return result;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
throw error;
|
|
151
|
+
} finally {
|
|
152
|
+
bufferCount = Math.max(0, bufferCount - 1);
|
|
153
|
+
if (bufferCount === 0) {
|
|
154
|
+
setTimeout(flush, 100);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
},
|
|
159
|
+
on: (...args) => ev.on(...args),
|
|
160
|
+
off: (...args) => ev.off(...args),
|
|
161
|
+
removeAllListeners: (...args) => ev.removeAllListeners(...args),
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
const makeBufferData = () => ({
|
|
165
|
+
historySets: {
|
|
166
|
+
chats: {},
|
|
167
|
+
messages: {},
|
|
168
|
+
contacts: {},
|
|
169
|
+
isLatest: false,
|
|
170
|
+
empty: true,
|
|
171
|
+
},
|
|
172
|
+
chatUpserts: {},
|
|
173
|
+
chatUpdates: {},
|
|
174
|
+
chatDeletes: new Set(),
|
|
175
|
+
contactUpserts: {},
|
|
176
|
+
contactUpdates: {},
|
|
177
|
+
messageUpserts: {},
|
|
178
|
+
messageUpdates: {},
|
|
179
|
+
messageReactions: {},
|
|
180
|
+
messageDeletes: {},
|
|
181
|
+
messageReceipts: {},
|
|
182
|
+
groupUpdates: {},
|
|
183
|
+
});
|
|
184
|
+
function append(data, historyCache, event, eventData, logger) {
|
|
185
|
+
switch (event) {
|
|
186
|
+
case "messaging-history.set":
|
|
187
|
+
for (const chat of eventData.chats) {
|
|
188
|
+
const id = chat.id || "";
|
|
189
|
+
const existingChat = data.historySets.chats[id];
|
|
190
|
+
if (existingChat) {
|
|
191
|
+
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType;
|
|
192
|
+
}
|
|
193
|
+
if (!existingChat && !historyCache.has(id)) {
|
|
194
|
+
data.historySets.chats[id] = chat;
|
|
195
|
+
historyCache.add(id);
|
|
196
|
+
absorbingChatUpdate(chat);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
for (const contact of eventData.contacts) {
|
|
200
|
+
const existingContact = data.historySets.contacts[contact.id];
|
|
201
|
+
if (existingContact) {
|
|
202
|
+
Object.assign(existingContact, trimUndefined(contact));
|
|
203
|
+
} else {
|
|
204
|
+
const historyContactId = `c:${contact.id}`;
|
|
205
|
+
const hasAnyName = contact.notify || contact.name || contact.verifiedName;
|
|
206
|
+
if (!historyCache.has(historyContactId) || hasAnyName) {
|
|
207
|
+
data.historySets.contacts[contact.id] = contact;
|
|
208
|
+
historyCache.add(historyContactId);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
for (const message of eventData.messages) {
|
|
213
|
+
const key = stringifyMessageKey(message.key);
|
|
214
|
+
const existingMsg = data.historySets.messages[key];
|
|
215
|
+
if (!existingMsg && !historyCache.has(key)) {
|
|
216
|
+
data.historySets.messages[key] = message;
|
|
217
|
+
historyCache.add(key);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
data.historySets.empty = false;
|
|
221
|
+
data.historySets.syncType = eventData.syncType;
|
|
222
|
+
data.historySets.progress = eventData.progress;
|
|
223
|
+
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId;
|
|
224
|
+
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest;
|
|
225
|
+
break;
|
|
226
|
+
case "chats.upsert":
|
|
227
|
+
for (const chat of eventData) {
|
|
228
|
+
const id = chat.id || "";
|
|
229
|
+
let upsert = data.chatUpserts[id];
|
|
230
|
+
if (id && !upsert) {
|
|
231
|
+
upsert = data.historySets.chats[id];
|
|
232
|
+
if (upsert) {
|
|
233
|
+
logger.debug({ chatId: id }, "absorbed chat upsert in chat set");
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (upsert) {
|
|
237
|
+
upsert = concatChats(upsert, chat);
|
|
238
|
+
} else {
|
|
239
|
+
upsert = chat;
|
|
240
|
+
data.chatUpserts[id] = upsert;
|
|
241
|
+
}
|
|
242
|
+
absorbingChatUpdate(upsert);
|
|
243
|
+
if (data.chatDeletes.has(id)) {
|
|
244
|
+
data.chatDeletes.delete(id);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
break;
|
|
248
|
+
case "chats.update":
|
|
249
|
+
for (const update of eventData) {
|
|
250
|
+
const chatId = update.id;
|
|
251
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true;
|
|
252
|
+
if (conditionMatches) {
|
|
253
|
+
delete update.conditional;
|
|
254
|
+
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId];
|
|
255
|
+
if (upsert) {
|
|
256
|
+
concatChats(upsert, update);
|
|
257
|
+
} else {
|
|
258
|
+
const chatUpdate = data.chatUpdates[chatId] || {};
|
|
259
|
+
data.chatUpdates[chatId] = concatChats(chatUpdate, update);
|
|
260
|
+
}
|
|
261
|
+
} else if (conditionMatches === undefined) {
|
|
262
|
+
data.chatUpdates[chatId] = update;
|
|
263
|
+
}
|
|
264
|
+
if (data.chatDeletes.has(chatId)) {
|
|
265
|
+
data.chatDeletes.delete(chatId);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
break;
|
|
269
|
+
case "chats.delete":
|
|
270
|
+
for (const chatId of eventData) {
|
|
271
|
+
if (!data.chatDeletes.has(chatId)) {
|
|
272
|
+
data.chatDeletes.add(chatId);
|
|
273
|
+
}
|
|
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(
|
|
292
|
+
{ contactId: contact.id },
|
|
293
|
+
"absorbed contact upsert in contact set"
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (upsert) {
|
|
298
|
+
upsert = Object.assign(upsert, trimUndefined(contact));
|
|
299
|
+
} else {
|
|
300
|
+
upsert = contact;
|
|
301
|
+
data.contactUpserts[contact.id] = upsert;
|
|
302
|
+
}
|
|
303
|
+
if (data.contactUpdates[contact.id]) {
|
|
304
|
+
upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact));
|
|
305
|
+
delete data.contactUpdates[contact.id];
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
break;
|
|
309
|
+
case "contacts.update":
|
|
310
|
+
const contactUpdates = eventData;
|
|
311
|
+
for (const update of contactUpdates) {
|
|
312
|
+
const id = update.id;
|
|
313
|
+
const upsert = data.historySets.contacts[id] || data.contactUpserts[id];
|
|
314
|
+
if (upsert) {
|
|
315
|
+
Object.assign(upsert, update);
|
|
316
|
+
} else {
|
|
317
|
+
const contactUpdate = data.contactUpdates[id] || {};
|
|
318
|
+
data.contactUpdates[id] = Object.assign(contactUpdate, update);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
break;
|
|
322
|
+
case "messages.upsert":
|
|
323
|
+
const { messages: messages, type: type } = eventData;
|
|
324
|
+
for (const message of messages) {
|
|
325
|
+
const key = stringifyMessageKey(message.key);
|
|
326
|
+
let existing = data.messageUpserts[key]?.message;
|
|
327
|
+
if (!existing) {
|
|
328
|
+
existing = data.historySets.messages[key];
|
|
329
|
+
if (existing) {
|
|
330
|
+
logger.debug({ messageId: key }, "absorbed message upsert in message set");
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (existing) {
|
|
334
|
+
message.messageTimestamp = existing.messageTimestamp;
|
|
335
|
+
}
|
|
336
|
+
if (data.messageUpdates[key]) {
|
|
337
|
+
logger.debug("absorbed prior message update in message upsert");
|
|
338
|
+
Object.assign(message, data.messageUpdates[key].update);
|
|
339
|
+
delete data.messageUpdates[key];
|
|
340
|
+
}
|
|
341
|
+
if (data.historySets.messages[key]) {
|
|
342
|
+
data.historySets.messages[key] = message;
|
|
343
|
+
} else {
|
|
344
|
+
data.messageUpserts[key] = {
|
|
345
|
+
message: message,
|
|
346
|
+
type:
|
|
347
|
+
type === "notify" || data.messageUpserts[key]?.type === "notify"
|
|
348
|
+
? "notify"
|
|
349
|
+
: type,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
break;
|
|
354
|
+
case "messages.update":
|
|
355
|
+
const msgUpdates = eventData;
|
|
356
|
+
for (const { key: key, update: update } of msgUpdates) {
|
|
357
|
+
const keyStr = stringifyMessageKey(key);
|
|
358
|
+
const existing =
|
|
359
|
+
data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message;
|
|
360
|
+
if (existing) {
|
|
361
|
+
Object.assign(existing, update);
|
|
362
|
+
if (update.status === WAMessageStatus.READ && !key.fromMe) {
|
|
363
|
+
decrementChatReadCounterIfMsgDidUnread(existing);
|
|
364
|
+
}
|
|
365
|
+
} else {
|
|
366
|
+
const msgUpdate = data.messageUpdates[keyStr] || {
|
|
367
|
+
key: key,
|
|
368
|
+
update: {},
|
|
369
|
+
};
|
|
370
|
+
Object.assign(msgUpdate.update, update);
|
|
371
|
+
data.messageUpdates[keyStr] = msgUpdate;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
break;
|
|
375
|
+
case "messages.delete":
|
|
376
|
+
const deleteData = eventData;
|
|
377
|
+
if ("keys" in deleteData) {
|
|
378
|
+
const { keys: keys } = deleteData;
|
|
379
|
+
for (const key of keys) {
|
|
380
|
+
const keyStr = stringifyMessageKey(key);
|
|
381
|
+
if (!data.messageDeletes[keyStr]) {
|
|
382
|
+
data.messageDeletes[keyStr] = key;
|
|
383
|
+
}
|
|
384
|
+
if (data.messageUpserts[keyStr]) {
|
|
385
|
+
delete data.messageUpserts[keyStr];
|
|
386
|
+
}
|
|
387
|
+
if (data.messageUpdates[keyStr]) {
|
|
388
|
+
delete data.messageUpdates[keyStr];
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
} else {
|
|
392
|
+
}
|
|
393
|
+
break;
|
|
394
|
+
case "messages.reaction":
|
|
395
|
+
const reactions = eventData;
|
|
396
|
+
for (const { key: key, reaction: reaction } of reactions) {
|
|
397
|
+
const keyStr = stringifyMessageKey(key);
|
|
398
|
+
const existing = data.messageUpserts[keyStr];
|
|
399
|
+
if (existing) {
|
|
400
|
+
updateMessageWithReaction(existing.message, reaction);
|
|
401
|
+
} else {
|
|
402
|
+
data.messageReactions[keyStr] = data.messageReactions[keyStr] || {
|
|
403
|
+
key: key,
|
|
404
|
+
reactions: [],
|
|
405
|
+
};
|
|
406
|
+
updateMessageWithReaction(data.messageReactions[keyStr], reaction);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
break;
|
|
410
|
+
case "message-receipt.update":
|
|
411
|
+
const receipts = eventData;
|
|
412
|
+
for (const { key: key, receipt: receipt } of receipts) {
|
|
413
|
+
const keyStr = stringifyMessageKey(key);
|
|
414
|
+
const existing = data.messageUpserts[keyStr];
|
|
415
|
+
if (existing) {
|
|
416
|
+
updateMessageWithReceipt(existing.message, receipt);
|
|
417
|
+
} else {
|
|
418
|
+
data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || {
|
|
419
|
+
key: key,
|
|
420
|
+
userReceipt: [],
|
|
421
|
+
};
|
|
422
|
+
updateMessageWithReceipt(data.messageReceipts[keyStr], receipt);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
break;
|
|
426
|
+
case "groups.update":
|
|
427
|
+
const groupUpdates = eventData;
|
|
428
|
+
for (const update of groupUpdates) {
|
|
429
|
+
const id = update.id;
|
|
430
|
+
const groupUpdate = data.groupUpdates[id] || {};
|
|
431
|
+
if (!data.groupUpdates[id]) {
|
|
432
|
+
data.groupUpdates[id] = Object.assign(groupUpdate, update);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
break;
|
|
436
|
+
default:
|
|
437
|
+
throw new Error(`"${event}" cannot be buffered`);
|
|
438
|
+
}
|
|
439
|
+
function absorbingChatUpdate(existing) {
|
|
440
|
+
const chatId = existing.id || "";
|
|
441
|
+
const update = data.chatUpdates[chatId];
|
|
442
|
+
if (update) {
|
|
443
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true;
|
|
444
|
+
if (conditionMatches) {
|
|
445
|
+
delete update.conditional;
|
|
446
|
+
logger.debug({ chatId: chatId }, "absorbed chat update in existing chat");
|
|
447
|
+
Object.assign(existing, concatChats(update, existing));
|
|
448
|
+
delete data.chatUpdates[chatId];
|
|
449
|
+
} else if (conditionMatches === false) {
|
|
450
|
+
logger.debug({ chatId: chatId }, "chat update condition fail, removing");
|
|
451
|
+
delete data.chatUpdates[chatId];
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
function decrementChatReadCounterIfMsgDidUnread(message) {
|
|
456
|
+
const chatId = message.key.remoteJid;
|
|
457
|
+
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
|
|
458
|
+
if (
|
|
459
|
+
isRealMessage(message) &&
|
|
460
|
+
shouldIncrementChatUnread(message) &&
|
|
461
|
+
typeof chat?.unreadCount === "number" &&
|
|
462
|
+
chat.unreadCount > 0
|
|
463
|
+
) {
|
|
464
|
+
logger.debug({ chatId: chat.id }, "decrementing chat counter");
|
|
465
|
+
chat.unreadCount -= 1;
|
|
466
|
+
if (chat.unreadCount === 0) {
|
|
467
|
+
delete chat.unreadCount;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function consolidateEvents(data) {
|
|
473
|
+
const map = {};
|
|
474
|
+
if (!data.historySets.empty) {
|
|
475
|
+
map["messaging-history.set"] = {
|
|
476
|
+
chats: Object.values(data.historySets.chats),
|
|
477
|
+
messages: Object.values(data.historySets.messages),
|
|
478
|
+
contacts: Object.values(data.historySets.contacts),
|
|
479
|
+
syncType: data.historySets.syncType,
|
|
480
|
+
progress: data.historySets.progress,
|
|
481
|
+
isLatest: data.historySets.isLatest,
|
|
482
|
+
peerDataRequestSessionId: data.historySets.peerDataRequestSessionId,
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
const chatUpsertList = Object.values(data.chatUpserts);
|
|
486
|
+
if (chatUpsertList.length) {
|
|
487
|
+
map["chats.upsert"] = chatUpsertList;
|
|
488
|
+
}
|
|
489
|
+
const chatUpdateList = Object.values(data.chatUpdates);
|
|
490
|
+
if (chatUpdateList.length) {
|
|
491
|
+
map["chats.update"] = chatUpdateList;
|
|
492
|
+
}
|
|
493
|
+
const chatDeleteList = Array.from(data.chatDeletes);
|
|
494
|
+
if (chatDeleteList.length) {
|
|
495
|
+
map["chats.delete"] = chatDeleteList;
|
|
496
|
+
}
|
|
497
|
+
const messageUpsertList = Object.values(data.messageUpserts);
|
|
498
|
+
if (messageUpsertList.length) {
|
|
499
|
+
const type = messageUpsertList[0].type;
|
|
500
|
+
map["messages.upsert"] = {
|
|
501
|
+
messages: messageUpsertList.map((m) => m.message),
|
|
502
|
+
type: type,
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
const messageUpdateList = Object.values(data.messageUpdates);
|
|
506
|
+
if (messageUpdateList.length) {
|
|
507
|
+
map["messages.update"] = messageUpdateList;
|
|
508
|
+
}
|
|
509
|
+
const messageDeleteList = Object.values(data.messageDeletes);
|
|
510
|
+
if (messageDeleteList.length) {
|
|
511
|
+
map["messages.delete"] = { keys: messageDeleteList };
|
|
512
|
+
}
|
|
513
|
+
const messageReactionList = Object.values(data.messageReactions).flatMap(
|
|
514
|
+
({ key: key, reactions: reactions }) =>
|
|
515
|
+
reactions.flatMap((reaction) => ({ key: key, reaction: reaction }))
|
|
516
|
+
);
|
|
517
|
+
if (messageReactionList.length) {
|
|
518
|
+
map["messages.reaction"] = messageReactionList;
|
|
519
|
+
}
|
|
520
|
+
const messageReceiptList = Object.values(data.messageReceipts).flatMap(
|
|
521
|
+
({ key: key, userReceipt: userReceipt }) =>
|
|
522
|
+
userReceipt.flatMap((receipt) => ({ key: key, receipt: receipt }))
|
|
523
|
+
);
|
|
524
|
+
if (messageReceiptList.length) {
|
|
525
|
+
map["message-receipt.update"] = messageReceiptList;
|
|
526
|
+
}
|
|
527
|
+
const contactUpsertList = Object.values(data.contactUpserts);
|
|
528
|
+
if (contactUpsertList.length) {
|
|
529
|
+
map["contacts.upsert"] = contactUpsertList;
|
|
530
|
+
}
|
|
531
|
+
const contactUpdateList = Object.values(data.contactUpdates);
|
|
532
|
+
if (contactUpdateList.length) {
|
|
533
|
+
map["contacts.update"] = contactUpdateList;
|
|
534
|
+
}
|
|
535
|
+
const groupUpdateList = Object.values(data.groupUpdates);
|
|
536
|
+
if (groupUpdateList.length) {
|
|
537
|
+
map["groups.update"] = groupUpdateList;
|
|
538
|
+
}
|
|
539
|
+
return map;
|
|
540
|
+
}
|
|
541
|
+
function concatChats(a, b) {
|
|
542
|
+
if (b.unreadCount === null && a.unreadCount < 0) {
|
|
543
|
+
a.unreadCount = undefined;
|
|
544
|
+
b.unreadCount = undefined;
|
|
545
|
+
}
|
|
546
|
+
if (typeof a.unreadCount === "number" && typeof b.unreadCount === "number") {
|
|
547
|
+
b = { ...b };
|
|
548
|
+
if (b.unreadCount >= 0) {
|
|
549
|
+
b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return Object.assign(a, b);
|
|
553
|
+
}
|
|
554
|
+
const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? "1" : "0"}`;
|
|
555
|
+
module.exports = { makeEventBuffer: makeEventBuffer };
|