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