@fhynella/baileys 1.2.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/LICENSE +21 -0
- package/WAProto/GenerateStatics.sh +4 -0
- package/WAProto/WAProto.proto +4775 -0
- package/WAProto/index.js +169661 -0
- package/WAProto/p.html +1 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.js +118 -0
- package/lib/Defaults/wileys-version.json +3 -0
- package/lib/Signal/Group/ciphertext-message.js +15 -0
- package/lib/Signal/Group/group-session-builder.js +64 -0
- package/lib/Signal/Group/group_cipher.js +96 -0
- package/lib/Signal/Group/index.js +57 -0
- package/lib/Signal/Group/keyhelper.js +55 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.js +34 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +69 -0
- package/lib/Signal/Group/sender-key-name.js +51 -0
- package/lib/Signal/Group/sender-key-record.js +53 -0
- package/lib/Signal/Group/sender-key-state.js +99 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +174 -0
- package/lib/Socket/Client/index.js +18 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +111 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.js +915 -0
- package/lib/Socket/groups.js +332 -0
- package/lib/Socket/index.js +10 -0
- package/lib/Socket/messages-recv.js +1054 -0
- package/lib/Socket/messages-send.js +949 -0
- package/lib/Socket/newsletter.js +250 -0
- package/lib/Socket/socket.js +654 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +8 -0
- package/lib/Store/make-in-memory-store.js +439 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.js +7 -0
- package/lib/Types/Newsletter.js +18 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +42 -0
- package/lib/Utils/auth-utils.js +199 -0
- package/lib/Utils/browser-utils.js +35 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.js +730 -0
- package/lib/Utils/crypto.js +193 -0
- package/lib/Utils/decode-wa-message.js +207 -0
- package/lib/Utils/event-buffer.js +518 -0
- package/lib/Utils/generics.js +467 -0
- package/lib/Utils/history.js +94 -0
- package/lib/Utils/index.js +35 -0
- package/lib/Utils/link-preview.js +126 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.js +43 -0
- package/lib/Utils/message-retry-manager.js +128 -0
- package/lib/Utils/messages-media.js +957 -0
- package/lib/Utils/messages.js +1049 -0
- package/lib/Utils/noise-handler.js +150 -0
- package/lib/Utils/process-message.js +404 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/use-multi-file-auth-state.js +125 -0
- package/lib/Utils/validate-connection.js +229 -0
- package/lib/Utils/wileys-event-stream.js +63 -0
- package/lib/WABinary/constants.js +1303 -0
- package/lib/WABinary/decode.js +265 -0
- package/lib/WABinary/encode.js +250 -0
- package/lib/WABinary/generic-utils.js +110 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.js +85 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.js +89 -0
- package/lib/WAUSync/USyncUser.js +26 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.js +49 -0
- package/package.json +115 -0
|
@@ -0,0 +1,915 @@
|
|
|
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.makeChatsSocket = void 0;
|
|
7
|
+
const boom_1 = require("@hapi/boom");
|
|
8
|
+
const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
|
|
9
|
+
const WAProto_1 = require("../../WAProto");
|
|
10
|
+
const Defaults_1 = require("../Defaults");
|
|
11
|
+
const Types_1 = require("../Types");
|
|
12
|
+
const Utils_1 = require("../Utils");
|
|
13
|
+
const make_mutex_1 = require("../Utils/make-mutex");
|
|
14
|
+
const process_message_1 = __importDefault(require("../Utils/process-message"));
|
|
15
|
+
const WABinary_1 = require("../WABinary");
|
|
16
|
+
const WAUSync_1 = require("../WAUSync");
|
|
17
|
+
const usync_1 = require("./usync");
|
|
18
|
+
const chalk = require('chalk');
|
|
19
|
+
const MAX_SYNC_ATTEMPTS = 2;
|
|
20
|
+
const SyncState = {
|
|
21
|
+
Connecting: 'connecting',
|
|
22
|
+
AwaitingInitialSync: 'awaiting_initial_sync',
|
|
23
|
+
Syncing: 'syncing',
|
|
24
|
+
Online: 'online'
|
|
25
|
+
};
|
|
26
|
+
const makeChatsSocket = (config) => {
|
|
27
|
+
const { logger, markOnlineOnConnect, fireInitQueries, appStateMacVerification, shouldIgnoreJid, shouldSyncHistoryMessage, } = config;
|
|
28
|
+
const sock = (0, usync_1.makeUSyncSocket)(config);
|
|
29
|
+
const { ev, ws, authState, generateMessageTag, sendNode, query, signalRepository, onUnexpectedError, } = sock;
|
|
30
|
+
let privacySettings;
|
|
31
|
+
let syncState = SyncState.Connecting;
|
|
32
|
+
let needToFlushWithAppStateSync = false;
|
|
33
|
+
let pendingAppStateSync = false;
|
|
34
|
+
let awaitingSyncTimeout;
|
|
35
|
+
const processingMutex = (0, make_mutex_1.makeMutex)();
|
|
36
|
+
const placeholderResendCache = config.placeholderResendCache || new node_cache_1.default({
|
|
37
|
+
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.MSG_RETRY,
|
|
38
|
+
useClones: false
|
|
39
|
+
});
|
|
40
|
+
if (!config.placeholderResendCache) {
|
|
41
|
+
config.placeholderResendCache = placeholderResendCache;
|
|
42
|
+
}
|
|
43
|
+
const getAppStateSyncKey = async (keyId) => {
|
|
44
|
+
const { [keyId]: key } = await authState.keys.get('app-state-sync-key', [keyId]);
|
|
45
|
+
return key;
|
|
46
|
+
};
|
|
47
|
+
const fetchPrivacySettings = async (force = false) => {
|
|
48
|
+
if (!privacySettings || force) {
|
|
49
|
+
const { content } = await query({
|
|
50
|
+
tag: 'iq',
|
|
51
|
+
attrs: {
|
|
52
|
+
xmlns: 'privacy',
|
|
53
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
54
|
+
type: 'get'
|
|
55
|
+
},
|
|
56
|
+
content: [
|
|
57
|
+
{ tag: 'privacy', attrs: {} }
|
|
58
|
+
]
|
|
59
|
+
});
|
|
60
|
+
privacySettings = (0, WABinary_1.reduceBinaryNodeToDictionary)(content === null || content === void 0 ? void 0 : content[0], 'category');
|
|
61
|
+
}
|
|
62
|
+
return privacySettings;
|
|
63
|
+
};
|
|
64
|
+
const privacyQuery = async (name, value) => {
|
|
65
|
+
await query({
|
|
66
|
+
tag: 'iq',
|
|
67
|
+
attrs: {
|
|
68
|
+
xmlns: 'privacy',
|
|
69
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
70
|
+
type: 'set'
|
|
71
|
+
},
|
|
72
|
+
content: [{
|
|
73
|
+
tag: 'privacy',
|
|
74
|
+
attrs: {},
|
|
75
|
+
content: [
|
|
76
|
+
{
|
|
77
|
+
tag: 'category',
|
|
78
|
+
attrs: { name, value }
|
|
79
|
+
}
|
|
80
|
+
]
|
|
81
|
+
}]
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
const updateMessagesPrivacy = async (value) => {
|
|
85
|
+
await privacyQuery('messages', value);
|
|
86
|
+
};
|
|
87
|
+
const updateCallPrivacy = async (value) => {
|
|
88
|
+
await privacyQuery('calladd', value);
|
|
89
|
+
};
|
|
90
|
+
const updateLastSeenPrivacy = async (value) => {
|
|
91
|
+
await privacyQuery('last', value);
|
|
92
|
+
};
|
|
93
|
+
const updateOnlinePrivacy = async (value) => {
|
|
94
|
+
await privacyQuery('online', value);
|
|
95
|
+
};
|
|
96
|
+
const updateProfilePicturePrivacy = async (value) => {
|
|
97
|
+
await privacyQuery('profile', value);
|
|
98
|
+
};
|
|
99
|
+
const updateStatusPrivacy = async (value) => {
|
|
100
|
+
await privacyQuery('status', value);
|
|
101
|
+
};
|
|
102
|
+
const updateReadReceiptsPrivacy = async (value) => {
|
|
103
|
+
await privacyQuery('readreceipts', value);
|
|
104
|
+
};
|
|
105
|
+
const updateGroupsAddPrivacy = async (value) => {
|
|
106
|
+
await privacyQuery('groupadd', value);
|
|
107
|
+
};
|
|
108
|
+
const updateDisableLinkPreviewsPrivacy = async (isPreviewsDisabled) => {
|
|
109
|
+
return chatModify({ disableLinkPreviews: { isPreviewsDisabled } }, '');
|
|
110
|
+
};
|
|
111
|
+
const updateDefaultDisappearingMode = async (duration) => {
|
|
112
|
+
await query({
|
|
113
|
+
tag: 'iq',
|
|
114
|
+
attrs: {
|
|
115
|
+
xmlns: 'disappearing_mode',
|
|
116
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
117
|
+
type: 'set'
|
|
118
|
+
},
|
|
119
|
+
content: [{
|
|
120
|
+
tag: 'disappearing_mode',
|
|
121
|
+
attrs: {
|
|
122
|
+
duration: duration.toString()
|
|
123
|
+
}
|
|
124
|
+
}]
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
const getBotListV2 = async () => {
|
|
128
|
+
const resp = await query({
|
|
129
|
+
tag: 'iq',
|
|
130
|
+
attrs: {
|
|
131
|
+
xmlns: 'bot',
|
|
132
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
133
|
+
type: 'get'
|
|
134
|
+
},
|
|
135
|
+
content: [{
|
|
136
|
+
tag: 'bot',
|
|
137
|
+
attrs: {
|
|
138
|
+
v: '2'
|
|
139
|
+
}
|
|
140
|
+
}]
|
|
141
|
+
});
|
|
142
|
+
const botNode = (0, WABinary_1.getBinaryNodeChild)(resp, 'bot');
|
|
143
|
+
const botList = [];
|
|
144
|
+
for (const section of (0, WABinary_1.getBinaryNodeChildren)(botNode, 'section')) {
|
|
145
|
+
if (section.attrs.type === 'all') {
|
|
146
|
+
for (const bot of (0, WABinary_1.getBinaryNodeChildren)(section, 'bot')) {
|
|
147
|
+
botList.push({
|
|
148
|
+
jid: bot.attrs.jid,
|
|
149
|
+
personaId: bot.attrs['persona_id']
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return botList;
|
|
155
|
+
};
|
|
156
|
+
const onWhatsApp = async (...jids) => {
|
|
157
|
+
const usyncQuery = new WAUSync_1.USyncQuery()
|
|
158
|
+
.withContactProtocol()
|
|
159
|
+
.withLIDProtocol();
|
|
160
|
+
for (const jid of jids) {
|
|
161
|
+
const phone = `+${jid.replace('+', '').split('@')[0].split(':')[0]}`;
|
|
162
|
+
usyncQuery.withUser(new WAUSync_1.USyncUser().withPhone(phone));
|
|
163
|
+
}
|
|
164
|
+
const results = await sock.executeUSyncQuery(usyncQuery);
|
|
165
|
+
if (results) {
|
|
166
|
+
return results.list.filter((a) => !!a.contact).map(({ contact, id, lid }) => ({ jid: id, exists: contact, lid }));
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
const fetchStatus = async (...jids) => {
|
|
170
|
+
const usyncQuery = new WAUSync_1.USyncQuery()
|
|
171
|
+
.withStatusProtocol();
|
|
172
|
+
for (const jid of jids) {
|
|
173
|
+
usyncQuery.withUser(new WAUSync_1.USyncUser().withId(jid));
|
|
174
|
+
}
|
|
175
|
+
const result = await sock.executeUSyncQuery(usyncQuery);
|
|
176
|
+
if (result) {
|
|
177
|
+
return result.list;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
const fetchDisappearingDuration = async (...jids) => {
|
|
181
|
+
const usyncQuery = new WAUSync_1.USyncQuery()
|
|
182
|
+
.withDisappearingModeProtocol();
|
|
183
|
+
for (const jid of jids) {
|
|
184
|
+
usyncQuery.withUser(new WAUSync_1.USyncUser().withId(jid));
|
|
185
|
+
}
|
|
186
|
+
const result = await sock.executeUSyncQuery(usyncQuery);
|
|
187
|
+
if (result) {
|
|
188
|
+
return result.list;
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
const updateProfilePicture = async (jid, content, dimensions) => {
|
|
192
|
+
let targetJid;
|
|
193
|
+
if (!jid) {
|
|
194
|
+
throw new boom_1.Boom('Illegal no-jid profile update. Please specify either your ID or the ID of the chat you wish to update');
|
|
195
|
+
}
|
|
196
|
+
if ((0, WABinary_1.jidNormalizedUser)(jid) !== (0, WABinary_1.jidNormalizedUser)(authState.creds.me.id)) {
|
|
197
|
+
targetJid = (0, WABinary_1.jidNormalizedUser)(jid);
|
|
198
|
+
}
|
|
199
|
+
const { img } = await (0, Utils_1.generateProfilePicture)(content, dimensions);
|
|
200
|
+
await query({
|
|
201
|
+
tag: 'iq',
|
|
202
|
+
attrs: {
|
|
203
|
+
target: targetJid,
|
|
204
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
205
|
+
type: 'set',
|
|
206
|
+
xmlns: 'w:profile:picture'
|
|
207
|
+
},
|
|
208
|
+
content: [
|
|
209
|
+
{
|
|
210
|
+
tag: 'picture',
|
|
211
|
+
attrs: { type: 'image' },
|
|
212
|
+
content: img
|
|
213
|
+
}
|
|
214
|
+
]
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
const removeProfilePicture = async (jid) => {
|
|
218
|
+
let targetJid;
|
|
219
|
+
if (!jid) {
|
|
220
|
+
throw new boom_1.Boom('Illegal no-jid profile update. Please specify either your ID or the ID of the chat you wish to update');
|
|
221
|
+
}
|
|
222
|
+
if ((0, WABinary_1.jidNormalizedUser)(jid) !== (0, WABinary_1.jidNormalizedUser)(authState.creds.me.id)) {
|
|
223
|
+
targetJid = (0, WABinary_1.jidNormalizedUser)(jid);
|
|
224
|
+
}
|
|
225
|
+
await query({
|
|
226
|
+
tag: 'iq',
|
|
227
|
+
attrs: {
|
|
228
|
+
target: targetJid,
|
|
229
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
230
|
+
type: 'set',
|
|
231
|
+
xmlns: 'w:profile:picture'
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
};
|
|
235
|
+
const updateProfileStatus = async (status) => {
|
|
236
|
+
await query({
|
|
237
|
+
tag: 'iq',
|
|
238
|
+
attrs: {
|
|
239
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
240
|
+
type: 'set',
|
|
241
|
+
xmlns: 'status'
|
|
242
|
+
},
|
|
243
|
+
content: [
|
|
244
|
+
{
|
|
245
|
+
tag: 'status',
|
|
246
|
+
attrs: {},
|
|
247
|
+
content: Buffer.from(status, 'utf-8')
|
|
248
|
+
}
|
|
249
|
+
]
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
const updateProfileName = async (name) => {
|
|
253
|
+
await chatModify({ pushNameSetting: name }, '');
|
|
254
|
+
};
|
|
255
|
+
const fetchBlocklist = async () => {
|
|
256
|
+
const result = await query({
|
|
257
|
+
tag: 'iq',
|
|
258
|
+
attrs: {
|
|
259
|
+
xmlns: 'blocklist',
|
|
260
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
261
|
+
type: 'get'
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
const listNode = (0, WABinary_1.getBinaryNodeChild)(result, 'list');
|
|
265
|
+
return (0, WABinary_1.getBinaryNodeChildren)(listNode, 'item')
|
|
266
|
+
.map(n => n.attrs.jid);
|
|
267
|
+
};
|
|
268
|
+
const updateBlockStatus = async (jid, action) => {
|
|
269
|
+
await query({
|
|
270
|
+
tag: 'iq',
|
|
271
|
+
attrs: {
|
|
272
|
+
xmlns: 'blocklist',
|
|
273
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
274
|
+
type: 'set'
|
|
275
|
+
},
|
|
276
|
+
content: [
|
|
277
|
+
{
|
|
278
|
+
tag: 'item',
|
|
279
|
+
attrs: {
|
|
280
|
+
action,
|
|
281
|
+
jid
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
]
|
|
285
|
+
});
|
|
286
|
+
};
|
|
287
|
+
const getBusinessProfile = async (jid) => {
|
|
288
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
289
|
+
const results = await query({
|
|
290
|
+
tag: 'iq',
|
|
291
|
+
attrs: {
|
|
292
|
+
to: 's.whatsapp.net',
|
|
293
|
+
xmlns: 'w:biz',
|
|
294
|
+
type: 'get'
|
|
295
|
+
},
|
|
296
|
+
content: [{
|
|
297
|
+
tag: 'business_profile',
|
|
298
|
+
attrs: { v: '244' },
|
|
299
|
+
content: [{
|
|
300
|
+
tag: 'profile',
|
|
301
|
+
attrs: { jid }
|
|
302
|
+
}]
|
|
303
|
+
}]
|
|
304
|
+
});
|
|
305
|
+
const profileNode = (0, WABinary_1.getBinaryNodeChild)(results, 'business_profile');
|
|
306
|
+
const profiles = (0, WABinary_1.getBinaryNodeChild)(profileNode, 'profile');
|
|
307
|
+
if (profiles) {
|
|
308
|
+
const address = (0, WABinary_1.getBinaryNodeChild)(profiles, 'address');
|
|
309
|
+
const description = (0, WABinary_1.getBinaryNodeChild)(profiles, 'description');
|
|
310
|
+
const website = (0, WABinary_1.getBinaryNodeChild)(profiles, 'website');
|
|
311
|
+
const email = (0, WABinary_1.getBinaryNodeChild)(profiles, 'email');
|
|
312
|
+
const category = (0, WABinary_1.getBinaryNodeChild)((0, WABinary_1.getBinaryNodeChild)(profiles, 'categories'), 'category');
|
|
313
|
+
const businessHours = (0, WABinary_1.getBinaryNodeChild)(profiles, 'business_hours');
|
|
314
|
+
const businessHoursConfig = businessHours
|
|
315
|
+
? (0, WABinary_1.getBinaryNodeChildren)(businessHours, 'business_hours_config')
|
|
316
|
+
: undefined;
|
|
317
|
+
const websiteStr = (_a = website === null || website === void 0 ? void 0 : website.content) === null || _a === void 0 ? void 0 : _a.toString();
|
|
318
|
+
return {
|
|
319
|
+
wid: (_b = profiles.attrs) === null || _b === void 0 ? void 0 : _b.jid,
|
|
320
|
+
address: (_c = address === null || address === void 0 ? void 0 : address.content) === null || _c === void 0 ? void 0 : _c.toString(),
|
|
321
|
+
description: ((_d = description === null || description === void 0 ? void 0 : description.content) === null || _d === void 0 ? void 0 : _d.toString()) || '',
|
|
322
|
+
website: websiteStr ? [websiteStr] : [],
|
|
323
|
+
email: (_e = email === null || email === void 0 ? void 0 : email.content) === null || _e === void 0 ? void 0 : _e.toString(),
|
|
324
|
+
category: (_f = category === null || category === void 0 ? void 0 : category.content) === null || _f === void 0 ? void 0 : _f.toString(),
|
|
325
|
+
'business_hours': {
|
|
326
|
+
timezone: (_g = businessHours === null || businessHours === void 0 ? void 0 : businessHours.attrs) === null || _g === void 0 ? void 0 : _g.timezone,
|
|
327
|
+
'business_config': businessHoursConfig === null || businessHoursConfig === void 0 ? void 0 : businessHoursConfig.map(({ attrs }) => attrs)
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
const cleanDirtyBits = async (type, fromTimestamp) => {
|
|
333
|
+
logger.info({ fromTimestamp }, 'clean dirty bits ' + type);
|
|
334
|
+
await sendNode({
|
|
335
|
+
tag: 'iq',
|
|
336
|
+
attrs: {
|
|
337
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
338
|
+
type: 'set',
|
|
339
|
+
xmlns: 'urn:xmpp:whatsapp:dirty',
|
|
340
|
+
id: generateMessageTag(),
|
|
341
|
+
},
|
|
342
|
+
content: [
|
|
343
|
+
{
|
|
344
|
+
tag: 'clean',
|
|
345
|
+
attrs: {
|
|
346
|
+
type,
|
|
347
|
+
...(fromTimestamp ? { timestamp: fromTimestamp.toString() } : null),
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
]
|
|
351
|
+
});
|
|
352
|
+
};
|
|
353
|
+
const newAppStateChunkHandler = (isInitialSync) => {
|
|
354
|
+
return {
|
|
355
|
+
onMutation(mutation) {
|
|
356
|
+
(0, Utils_1.processSyncAction)(mutation, ev, authState.creds.me, isInitialSync ? { accountSettings: authState.creds.accountSettings } : undefined, logger);
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
};
|
|
360
|
+
const resyncAppState = ev.createBufferedFunction(async (collections, isInitialSync) => {
|
|
361
|
+
const initialVersionMap = {};
|
|
362
|
+
const globalMutationMap = {};
|
|
363
|
+
await authState.keys.transaction(async () => {
|
|
364
|
+
var _a;
|
|
365
|
+
const collectionsToHandle = new Set(collections);
|
|
366
|
+
const attemptsMap = {};
|
|
367
|
+
while (collectionsToHandle.size) {
|
|
368
|
+
const states = {};
|
|
369
|
+
const nodes = [];
|
|
370
|
+
for (const name of collectionsToHandle) {
|
|
371
|
+
const result = await authState.keys.get('app-state-sync-version', [name]);
|
|
372
|
+
let state = result[name];
|
|
373
|
+
if (state) {
|
|
374
|
+
if (typeof initialVersionMap[name] === 'undefined') {
|
|
375
|
+
initialVersionMap[name] = state.version;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
state = (0, Utils_1.newLTHashState)();
|
|
380
|
+
}
|
|
381
|
+
states[name] = state;
|
|
382
|
+
logger.info(`resyncing ${name} from v${state.version}`);
|
|
383
|
+
nodes.push({
|
|
384
|
+
tag: 'collection',
|
|
385
|
+
attrs: {
|
|
386
|
+
name,
|
|
387
|
+
version: state.version.toString(),
|
|
388
|
+
'return_snapshot': (!state.version).toString()
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
const result = await query({
|
|
393
|
+
tag: 'iq',
|
|
394
|
+
attrs: {
|
|
395
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
396
|
+
xmlns: 'w:sync:app:state',
|
|
397
|
+
type: 'set'
|
|
398
|
+
},
|
|
399
|
+
content: [
|
|
400
|
+
{
|
|
401
|
+
tag: 'sync',
|
|
402
|
+
attrs: {},
|
|
403
|
+
content: nodes
|
|
404
|
+
}
|
|
405
|
+
]
|
|
406
|
+
});
|
|
407
|
+
const decoded = await (0, Utils_1.extractSyncdPatches)(result, config === null || config === void 0 ? void 0 : config.options);
|
|
408
|
+
for (const key in decoded) {
|
|
409
|
+
const name = key;
|
|
410
|
+
const { patches, hasMorePatches, snapshot } = decoded[name];
|
|
411
|
+
try {
|
|
412
|
+
if (snapshot) {
|
|
413
|
+
const { state: newState, mutationMap } = await (0, Utils_1.decodeSyncdSnapshot)(name, snapshot, getAppStateSyncKey, initialVersionMap[name], appStateMacVerification.snapshot);
|
|
414
|
+
states[name] = newState;
|
|
415
|
+
Object.assign(globalMutationMap, mutationMap);
|
|
416
|
+
logger.info(`restored state of ${name} from snapshot to v${newState.version} with mutations`);
|
|
417
|
+
await authState.keys.set({ 'app-state-sync-version': { [name]: newState } });
|
|
418
|
+
}
|
|
419
|
+
if (patches.length) {
|
|
420
|
+
const { state: newState, mutationMap } = await (0, Utils_1.decodePatches)(name, patches, states[name], getAppStateSyncKey, config.options, initialVersionMap[name], logger, appStateMacVerification.patch);
|
|
421
|
+
await authState.keys.set({ 'app-state-sync-version': { [name]: newState } });
|
|
422
|
+
logger.info(`synced ${name} to v${newState.version}`);
|
|
423
|
+
initialVersionMap[name] = newState.version;
|
|
424
|
+
Object.assign(globalMutationMap, mutationMap);
|
|
425
|
+
}
|
|
426
|
+
if (hasMorePatches) {
|
|
427
|
+
logger.info(`${name} has more patches...`);
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
collectionsToHandle.delete(name);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
catch (error) {
|
|
434
|
+
const isIrrecoverableError = attemptsMap[name] >= MAX_SYNC_ATTEMPTS
|
|
435
|
+
|| ((_a = error.output) === null || _a === void 0 ? void 0 : _a.statusCode) === 404
|
|
436
|
+
|| error.name === 'TypeError';
|
|
437
|
+
logger.info({ name, error: error.stack }, `failed to sync state from version${isIrrecoverableError ? '' : ', removing and trying from scratch'}`);
|
|
438
|
+
await authState.keys.set({ 'app-state-sync-version': { [name]: null } });
|
|
439
|
+
attemptsMap[name] = (attemptsMap[name] || 0) + 1;
|
|
440
|
+
if (isIrrecoverableError) {
|
|
441
|
+
collectionsToHandle.delete(name);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
const { onMutation } = newAppStateChunkHandler(isInitialSync);
|
|
448
|
+
for (const key in globalMutationMap) {
|
|
449
|
+
onMutation(globalMutationMap[key]);
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
const profilePictureUrl = async (jid, type = 'preview', timeoutMs) => {
|
|
453
|
+
var _a;
|
|
454
|
+
jid = (0, WABinary_1.jidNormalizedUser)(jid);
|
|
455
|
+
try {
|
|
456
|
+
const result = await query({
|
|
457
|
+
tag: 'iq',
|
|
458
|
+
attrs: {
|
|
459
|
+
target: jid,
|
|
460
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
461
|
+
type: 'get',
|
|
462
|
+
xmlns: 'w:profile:picture'
|
|
463
|
+
},
|
|
464
|
+
content: [
|
|
465
|
+
{ tag: 'picture', attrs: { type, query: 'url' } }
|
|
466
|
+
]
|
|
467
|
+
}, timeoutMs);
|
|
468
|
+
const child = (0, WABinary_1.getBinaryNodeChild)(result, 'picture');
|
|
469
|
+
return (_a = child === null || child === void 0 ? void 0 : child.attrs) === null || _a === void 0 ? void 0 : _a.url;
|
|
470
|
+
} catch (error) {
|
|
471
|
+
if (error.message?.includes('item-not-found') ||
|
|
472
|
+
error.output?.payload?.statusCode === 404 ||
|
|
473
|
+
error.statusCode === 404) {
|
|
474
|
+
logger.info(chalk.gray(`[INFO] Profile pic not found for ${jid}, using fallback`));
|
|
475
|
+
return {
|
|
476
|
+
eurl: undefined,
|
|
477
|
+
id: jid.split('@')[0],
|
|
478
|
+
status: null,
|
|
479
|
+
img: null
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
throw error;
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
const createCallLink = async (type, event, timeoutMs) => {
|
|
486
|
+
const result = await query({
|
|
487
|
+
tag: 'call',
|
|
488
|
+
attrs: {
|
|
489
|
+
id: generateMessageTag(),
|
|
490
|
+
to: '@call'
|
|
491
|
+
},
|
|
492
|
+
content: [
|
|
493
|
+
{
|
|
494
|
+
tag: 'link_create',
|
|
495
|
+
attrs: { media: type },
|
|
496
|
+
content: event ? [{ tag: 'event', attrs: { start_time: String(event.startTime) } }] : undefined
|
|
497
|
+
}
|
|
498
|
+
]
|
|
499
|
+
}, timeoutMs);
|
|
500
|
+
const child = (0, WABinary_1.getBinaryNodeChild)(result, 'link_create');
|
|
501
|
+
return child?.attrs?.token;
|
|
502
|
+
};
|
|
503
|
+
const sendPresenceUpdate = async (type, toJid) => {
|
|
504
|
+
const me = authState.creds.me;
|
|
505
|
+
if (type === 'available' || type === 'unavailable') {
|
|
506
|
+
if (!me.name) {
|
|
507
|
+
logger.warn('no name present, ignoring presence update request...');
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
ev.emit('connection.update', { isOnline: type === 'available' });
|
|
511
|
+
await sendNode({
|
|
512
|
+
tag: 'presence',
|
|
513
|
+
attrs: {
|
|
514
|
+
name: me.name.replace(/@/g, ''),
|
|
515
|
+
type
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
const { server } = (0, WABinary_1.jidDecode)(toJid);
|
|
521
|
+
const isLid = server === 'lid';
|
|
522
|
+
await sendNode({
|
|
523
|
+
tag: 'chatstate',
|
|
524
|
+
attrs: {
|
|
525
|
+
from: isLid ? me.lid : me.id,
|
|
526
|
+
to: toJid,
|
|
527
|
+
},
|
|
528
|
+
content: [
|
|
529
|
+
{
|
|
530
|
+
tag: type === 'recording' ? 'composing' : type,
|
|
531
|
+
attrs: type === 'recording' ? { media: 'audio' } : {}
|
|
532
|
+
}
|
|
533
|
+
]
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
const presenceSubscribe = (toJid, tcToken) => (sendNode({
|
|
538
|
+
tag: 'presence',
|
|
539
|
+
attrs: {
|
|
540
|
+
to: toJid,
|
|
541
|
+
id: generateMessageTag(),
|
|
542
|
+
type: 'subscribe'
|
|
543
|
+
},
|
|
544
|
+
content: tcToken
|
|
545
|
+
? [
|
|
546
|
+
{
|
|
547
|
+
tag: 'tctoken',
|
|
548
|
+
attrs: {},
|
|
549
|
+
content: tcToken
|
|
550
|
+
}
|
|
551
|
+
]
|
|
552
|
+
: undefined
|
|
553
|
+
}));
|
|
554
|
+
const handlePresenceUpdate = ({ tag, attrs, content }) => {
|
|
555
|
+
var _a;
|
|
556
|
+
let presence;
|
|
557
|
+
const jid = attrs.from;
|
|
558
|
+
const participant = attrs.participant || attrs.from;
|
|
559
|
+
if (shouldIgnoreJid(jid) && jid != '@s.whatsapp.net') {
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
if (tag === 'presence') {
|
|
563
|
+
presence = {
|
|
564
|
+
lastKnownPresence: attrs.type === 'unavailable' ? 'unavailable' : 'available',
|
|
565
|
+
lastSeen: attrs.last && attrs.last !== 'deny' ? +attrs.last : undefined
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
else if (Array.isArray(content)) {
|
|
569
|
+
const [firstChild] = content;
|
|
570
|
+
let type = firstChild.tag;
|
|
571
|
+
if (type === 'paused') {
|
|
572
|
+
type = 'available';
|
|
573
|
+
}
|
|
574
|
+
if (((_a = firstChild.attrs) === null || _a === void 0 ? void 0 : _a.media) === 'audio') {
|
|
575
|
+
type = 'recording';
|
|
576
|
+
}
|
|
577
|
+
presence = { lastKnownPresence: type };
|
|
578
|
+
}
|
|
579
|
+
else {
|
|
580
|
+
logger.error({ tag, attrs, content }, 'recv invalid presence node');
|
|
581
|
+
}
|
|
582
|
+
if (presence) {
|
|
583
|
+
ev.emit('presence.update', { id: jid, presences: { [participant]: presence } });
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
const appPatch = async (patchCreate) => {
|
|
587
|
+
const name = patchCreate.type;
|
|
588
|
+
const myAppStateKeyId = authState.creds.myAppStateKeyId;
|
|
589
|
+
if (!myAppStateKeyId) {
|
|
590
|
+
throw new boom_1.Boom('App state key not present!', { statusCode: 400 });
|
|
591
|
+
}
|
|
592
|
+
let initial;
|
|
593
|
+
let encodeResult;
|
|
594
|
+
await processingMutex.mutex(async () => {
|
|
595
|
+
await authState.keys.transaction(async () => {
|
|
596
|
+
logger.debug({ patch: patchCreate }, 'applying app patch');
|
|
597
|
+
await resyncAppState([name], false);
|
|
598
|
+
const { [name]: currentSyncVersion } = await authState.keys.get('app-state-sync-version', [name]);
|
|
599
|
+
initial = currentSyncVersion || (0, Utils_1.newLTHashState)();
|
|
600
|
+
encodeResult = await (0, Utils_1.encodeSyncdPatch)(patchCreate, myAppStateKeyId, initial, getAppStateSyncKey);
|
|
601
|
+
const { patch, state } = encodeResult;
|
|
602
|
+
const node = {
|
|
603
|
+
tag: 'iq',
|
|
604
|
+
attrs: {
|
|
605
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
606
|
+
type: 'set',
|
|
607
|
+
xmlns: 'w:sync:app:state'
|
|
608
|
+
},
|
|
609
|
+
content: [
|
|
610
|
+
{
|
|
611
|
+
tag: 'sync',
|
|
612
|
+
attrs: {},
|
|
613
|
+
content: [
|
|
614
|
+
{
|
|
615
|
+
tag: 'collection',
|
|
616
|
+
attrs: {
|
|
617
|
+
name,
|
|
618
|
+
version: (state.version - 1).toString(),
|
|
619
|
+
'return_snapshot': 'false'
|
|
620
|
+
},
|
|
621
|
+
content: [
|
|
622
|
+
{
|
|
623
|
+
tag: 'patch',
|
|
624
|
+
attrs: {},
|
|
625
|
+
content: WAProto_1.proto.SyncdPatch.encode(patch).finish()
|
|
626
|
+
}
|
|
627
|
+
]
|
|
628
|
+
}
|
|
629
|
+
]
|
|
630
|
+
}
|
|
631
|
+
]
|
|
632
|
+
};
|
|
633
|
+
await query(node);
|
|
634
|
+
await authState.keys.set({ 'app-state-sync-version': { [name]: state } });
|
|
635
|
+
});
|
|
636
|
+
});
|
|
637
|
+
if (config.emitOwnEvents) {
|
|
638
|
+
const { onMutation } = newAppStateChunkHandler(false);
|
|
639
|
+
const { mutationMap } = await (0, Utils_1.decodePatches)(name, [{ ...encodeResult.patch, version: { version: encodeResult.state.version }, }], initial, getAppStateSyncKey, config.options, undefined, logger);
|
|
640
|
+
for (const key in mutationMap) {
|
|
641
|
+
onMutation(mutationMap[key]);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
const fetchProps = async () => {
|
|
646
|
+
var _a, _b, _c;
|
|
647
|
+
const resultNode = await query({
|
|
648
|
+
tag: 'iq',
|
|
649
|
+
attrs: {
|
|
650
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
651
|
+
xmlns: 'w',
|
|
652
|
+
type: 'get',
|
|
653
|
+
},
|
|
654
|
+
content: [
|
|
655
|
+
{ tag: 'props', attrs: {
|
|
656
|
+
protocol: '2',
|
|
657
|
+
hash: ((_a = authState === null || authState === void 0 ? void 0 : authState.creds) === null || _a === void 0 ? void 0 : _a.lastPropHash) || ''
|
|
658
|
+
} }
|
|
659
|
+
]
|
|
660
|
+
});
|
|
661
|
+
const propsNode = (0, WABinary_1.getBinaryNodeChild)(resultNode, 'props');
|
|
662
|
+
let props = {};
|
|
663
|
+
if (propsNode) {
|
|
664
|
+
if ((_b = propsNode.attrs) === null || _b === void 0 ? void 0 : _b.hash) {
|
|
665
|
+
authState.creds.lastPropHash = (_c = propsNode === null || propsNode === void 0 ? void 0 : propsNode.attrs) === null || _c === void 0 ? void 0 : _c.hash;
|
|
666
|
+
ev.emit('creds.update', authState.creds);
|
|
667
|
+
}
|
|
668
|
+
props = (0, WABinary_1.reduceBinaryNodeToDictionary)(propsNode, 'prop');
|
|
669
|
+
}
|
|
670
|
+
logger.debug('fetched props');
|
|
671
|
+
return props;
|
|
672
|
+
};
|
|
673
|
+
const chatModify = (mod, jid) => {
|
|
674
|
+
const patch = (0, Utils_1.chatModificationToAppPatch)(mod, jid);
|
|
675
|
+
return appPatch(patch);
|
|
676
|
+
};
|
|
677
|
+
const star = (jid, messages, star) => {
|
|
678
|
+
return chatModify({
|
|
679
|
+
star: {
|
|
680
|
+
messages,
|
|
681
|
+
star
|
|
682
|
+
}
|
|
683
|
+
}, jid);
|
|
684
|
+
};
|
|
685
|
+
const addOrEditContact = (jid, contact) => {
|
|
686
|
+
return chatModify({ contact }, jid);
|
|
687
|
+
};
|
|
688
|
+
const removeContact = (jid) => {
|
|
689
|
+
return chatModify({ contact: null }, jid);
|
|
690
|
+
};
|
|
691
|
+
const addLabel = (jid, labels) => {
|
|
692
|
+
return chatModify({ addLabel: { ...labels } }, jid);
|
|
693
|
+
};
|
|
694
|
+
const addChatLabel = (jid, labelId) => {
|
|
695
|
+
return chatModify({
|
|
696
|
+
addChatLabel: {
|
|
697
|
+
labelId
|
|
698
|
+
}
|
|
699
|
+
}, jid);
|
|
700
|
+
};
|
|
701
|
+
const removeChatLabel = (jid, labelId) => {
|
|
702
|
+
return chatModify({
|
|
703
|
+
removeChatLabel: {
|
|
704
|
+
labelId
|
|
705
|
+
}
|
|
706
|
+
}, jid);
|
|
707
|
+
};
|
|
708
|
+
const addMessageLabel = (jid, messageId, labelId) => {
|
|
709
|
+
return chatModify({
|
|
710
|
+
addMessageLabel: {
|
|
711
|
+
messageId,
|
|
712
|
+
labelId
|
|
713
|
+
}
|
|
714
|
+
}, jid);
|
|
715
|
+
};
|
|
716
|
+
const removeMessageLabel = (jid, messageId, labelId) => {
|
|
717
|
+
return chatModify({
|
|
718
|
+
removeMessageLabel: {
|
|
719
|
+
messageId,
|
|
720
|
+
labelId
|
|
721
|
+
}
|
|
722
|
+
}, jid);
|
|
723
|
+
};
|
|
724
|
+
const executeInitQueries = async () => {
|
|
725
|
+
await Promise.all([
|
|
726
|
+
fetchProps(),
|
|
727
|
+
fetchBlocklist(),
|
|
728
|
+
fetchPrivacySettings(),
|
|
729
|
+
]);
|
|
730
|
+
};
|
|
731
|
+
const upsertMessage = ev.createBufferedFunction(async (msg, type) => {
|
|
732
|
+
var _a, _b, _c;
|
|
733
|
+
ev.emit('messages.upsert', { messages: [msg], type });
|
|
734
|
+
if (!!msg.pushName) {
|
|
735
|
+
let jid = msg.key.fromMe ? authState.creds.me.id : (msg.key.participant || msg.key.remoteJid);
|
|
736
|
+
jid = (0, WABinary_1.jidNormalizedUser)(jid);
|
|
737
|
+
if (!msg.key.fromMe) {
|
|
738
|
+
ev.emit('contacts.update', [{ id: jid, notify: msg.pushName, verifiedName: msg.verifiedBizName }]);
|
|
739
|
+
}
|
|
740
|
+
if (msg.key.fromMe && msg.pushName && ((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.name) !== msg.pushName) {
|
|
741
|
+
ev.emit('creds.update', { me: { ...authState.creds.me, name: msg.pushName } });
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
const historyMsg = (0, Utils_1.getHistoryMsg)(msg.message);
|
|
745
|
+
const shouldProcessHistoryMsg = historyMsg
|
|
746
|
+
? (shouldSyncHistoryMessage(historyMsg)
|
|
747
|
+
&& Defaults_1.PROCESSABLE_HISTORY_TYPES.includes(historyMsg.syncType))
|
|
748
|
+
: false;
|
|
749
|
+
if (historyMsg && syncState === SyncState.AwaitingInitialSync) {
|
|
750
|
+
if (awaitingSyncTimeout) {
|
|
751
|
+
clearTimeout(awaitingSyncTimeout);
|
|
752
|
+
awaitingSyncTimeout = undefined;
|
|
753
|
+
}
|
|
754
|
+
if (shouldProcessHistoryMsg) {
|
|
755
|
+
syncState = SyncState.Syncing;
|
|
756
|
+
logger.info('Transitioned to Syncing state');
|
|
757
|
+
} else {
|
|
758
|
+
syncState = SyncState.Online;
|
|
759
|
+
logger.info('History sync skipped, transitioning to Online state and flushing buffer');
|
|
760
|
+
ev.flush();
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
const doAppStateSync = async () => {
|
|
764
|
+
if (syncState === SyncState.Syncing) {
|
|
765
|
+
logger.info('Doing app state sync');
|
|
766
|
+
await resyncAppState(Types_1.ALL_WA_PATCH_NAMES, true);
|
|
767
|
+
syncState = SyncState.Online;
|
|
768
|
+
logger.info('App state sync complete, transitioning to Online state and flushing buffer');
|
|
769
|
+
ev.flush();
|
|
770
|
+
const accountSyncCounter = (authState.creds.accountSyncCounter || 0) + 1;
|
|
771
|
+
ev.emit('creds.update', { accountSyncCounter });
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
if (historyMsg && !authState.creds.myAppStateKeyId) {
|
|
775
|
+
logger.warn('skipping app state sync, as myAppStateKeyId is not set');
|
|
776
|
+
pendingAppStateSync = true;
|
|
777
|
+
}
|
|
778
|
+
await Promise.all([
|
|
779
|
+
(async () => {
|
|
780
|
+
if (historyMsg
|
|
781
|
+
&& authState.creds.myAppStateKeyId) {
|
|
782
|
+
pendingAppStateSync = false;
|
|
783
|
+
await doAppStateSync();
|
|
784
|
+
}
|
|
785
|
+
})(),
|
|
786
|
+
(0, process_message_1.default)(msg, {
|
|
787
|
+
signalRepository,
|
|
788
|
+
shouldProcessHistoryMsg,
|
|
789
|
+
placeholderResendCache,
|
|
790
|
+
ev,
|
|
791
|
+
creds: authState.creds,
|
|
792
|
+
keyStore: authState.keys,
|
|
793
|
+
logger,
|
|
794
|
+
options: config.options,
|
|
795
|
+
getMessage: config.getMessage,
|
|
796
|
+
})
|
|
797
|
+
]);
|
|
798
|
+
if (((_c = (_b = msg.message) === null || _b === void 0 ? void 0 : _b.protocolMessage) === null || _c === void 0 ? void 0 : _c.appStateSyncKeyShare)
|
|
799
|
+
&& pendingAppStateSync) {
|
|
800
|
+
await doAppStateSync();
|
|
801
|
+
pendingAppStateSync = false;
|
|
802
|
+
}
|
|
803
|
+
});
|
|
804
|
+
ws.on('CB:presence', handlePresenceUpdate);
|
|
805
|
+
ws.on('CB:chatstate', handlePresenceUpdate);
|
|
806
|
+
ws.on('CB:ib,,dirty', async (node) => {
|
|
807
|
+
const { attrs } = (0, WABinary_1.getBinaryNodeChild)(node, 'dirty');
|
|
808
|
+
const type = attrs.type;
|
|
809
|
+
switch (type) {
|
|
810
|
+
case 'account_sync':
|
|
811
|
+
if (attrs.timestamp) {
|
|
812
|
+
let { lastAccountSyncTimestamp } = authState.creds;
|
|
813
|
+
if (lastAccountSyncTimestamp) {
|
|
814
|
+
await cleanDirtyBits('account_sync', lastAccountSyncTimestamp);
|
|
815
|
+
}
|
|
816
|
+
lastAccountSyncTimestamp = +attrs.timestamp;
|
|
817
|
+
ev.emit('creds.update', { lastAccountSyncTimestamp });
|
|
818
|
+
}
|
|
819
|
+
break;
|
|
820
|
+
case 'groups':
|
|
821
|
+
break;
|
|
822
|
+
default:
|
|
823
|
+
logger.info({ node }, 'received unknown sync');
|
|
824
|
+
break;
|
|
825
|
+
}
|
|
826
|
+
});
|
|
827
|
+
ev.on('connection.update', ({ connection, receivedPendingNotifications }) => {
|
|
828
|
+
var _a;
|
|
829
|
+
if (connection === 'open') {
|
|
830
|
+
if (fireInitQueries) {
|
|
831
|
+
executeInitQueries()
|
|
832
|
+
.catch(error => onUnexpectedError(error, 'init queries'));
|
|
833
|
+
}
|
|
834
|
+
sendPresenceUpdate(markOnlineOnConnect ? 'available' : 'unavailable')
|
|
835
|
+
.catch(error => onUnexpectedError(error, 'presence update requests'));
|
|
836
|
+
}
|
|
837
|
+
if (receivedPendingNotifications &&
|
|
838
|
+
!((_a = authState.creds) === null || _a === void 0 ? void 0 : _a.myAppStateKeyId)) {
|
|
839
|
+
ev.buffer();
|
|
840
|
+
needToFlushWithAppStateSync = true;
|
|
841
|
+
}
|
|
842
|
+
if (!receivedPendingNotifications || syncState !== SyncState.Connecting) {
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
syncState = SyncState.AwaitingInitialSync;
|
|
846
|
+
logger.info('Connection is now AwaitingInitialSync, buffering events');
|
|
847
|
+
ev.buffer();
|
|
848
|
+
const willSyncHistory = shouldSyncHistoryMessage(
|
|
849
|
+
WAProto_1.proto.Message.HistorySyncNotification.create({
|
|
850
|
+
syncType: WAProto_1.proto.HistorySync.HistorySyncType.RECENT
|
|
851
|
+
})
|
|
852
|
+
);
|
|
853
|
+
if (!willSyncHistory) {
|
|
854
|
+
logger.info('History sync is disabled by config, not waiting for notification. Transitioning to Online.');
|
|
855
|
+
syncState = SyncState.Online;
|
|
856
|
+
setTimeout(() => ev.flush(), 0);
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
logger.info('History sync is enabled, awaiting notification with a 20s timeout.');
|
|
860
|
+
if (awaitingSyncTimeout) {
|
|
861
|
+
clearTimeout(awaitingSyncTimeout);
|
|
862
|
+
}
|
|
863
|
+
awaitingSyncTimeout = setTimeout(() => {
|
|
864
|
+
if (syncState === SyncState.AwaitingInitialSync) {
|
|
865
|
+
logger.warn('Timeout in AwaitingInitialSync, forcing state to Online and flushing buffer');
|
|
866
|
+
syncState = SyncState.Online;
|
|
867
|
+
ev.flush();
|
|
868
|
+
}
|
|
869
|
+
}, 20000);
|
|
870
|
+
});
|
|
871
|
+
return {
|
|
872
|
+
...sock,
|
|
873
|
+
createCallLink,
|
|
874
|
+
getBotListV2,
|
|
875
|
+
processingMutex,
|
|
876
|
+
fetchPrivacySettings,
|
|
877
|
+
upsertMessage,
|
|
878
|
+
appPatch,
|
|
879
|
+
sendPresenceUpdate,
|
|
880
|
+
presenceSubscribe,
|
|
881
|
+
profilePictureUrl,
|
|
882
|
+
onWhatsApp,
|
|
883
|
+
fetchBlocklist,
|
|
884
|
+
fetchDisappearingDuration,
|
|
885
|
+
fetchStatus,
|
|
886
|
+
updateProfilePicture,
|
|
887
|
+
removeProfilePicture,
|
|
888
|
+
updateProfileStatus,
|
|
889
|
+
updateProfileName,
|
|
890
|
+
updateBlockStatus,
|
|
891
|
+
updateDisableLinkPreviewsPrivacy,
|
|
892
|
+
updateCallPrivacy,
|
|
893
|
+
updateMessagesPrivacy,
|
|
894
|
+
updateLastSeenPrivacy,
|
|
895
|
+
updateOnlinePrivacy,
|
|
896
|
+
updateProfilePicturePrivacy,
|
|
897
|
+
updateStatusPrivacy,
|
|
898
|
+
updateReadReceiptsPrivacy,
|
|
899
|
+
updateGroupsAddPrivacy,
|
|
900
|
+
updateDefaultDisappearingMode,
|
|
901
|
+
getBusinessProfile,
|
|
902
|
+
resyncAppState,
|
|
903
|
+
chatModify,
|
|
904
|
+
cleanDirtyBits,
|
|
905
|
+
addOrEditContact,
|
|
906
|
+
removeContact,
|
|
907
|
+
addLabel,
|
|
908
|
+
addChatLabel,
|
|
909
|
+
removeChatLabel,
|
|
910
|
+
addMessageLabel,
|
|
911
|
+
removeMessageLabel,
|
|
912
|
+
star
|
|
913
|
+
};
|
|
914
|
+
};
|
|
915
|
+
exports.makeChatsSocket = makeChatsSocket;
|