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