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