@ubedsockets/baileys 1.6.6 → 1.6.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/WAProto/GenerateStatics.sh +4 -3
- package/WAProto/WAProto.proto +511 -1215
- package/WAProto/index.js +131318 -59331
- package/lib/Defaults/index.js +92 -76
- package/lib/Signal/Group/ciphertext-message.js +5 -2
- package/lib/Signal/Group/group-session-builder.js +41 -7
- package/lib/Signal/Group/group_cipher.js +51 -37
- package/lib/Signal/Group/index.js +57 -12
- package/lib/Signal/Group/keyhelper.js +44 -7
- package/lib/Signal/Group/sender-chain-key.js +15 -7
- package/lib/Signal/Group/sender-key-distribution-message.js +11 -8
- package/lib/Signal/Group/sender-key-message.js +12 -9
- package/lib/Signal/Group/sender-key-name.js +5 -2
- package/lib/Signal/Group/sender-key-record.js +21 -9
- package/lib/Signal/Group/sender-key-state.js +42 -27
- package/lib/Signal/Group/sender-message-key.js +7 -4
- package/lib/Signal/libsignal.js +90 -347
- package/lib/Socket/Client/index.js +18 -3
- package/lib/Socket/Client/types.js +6 -4
- package/lib/Socket/Client/websocket.js +78 -21
- package/lib/Socket/business.js +43 -162
- package/lib/Socket/chats.js +249 -571
- package/lib/Socket/groups.js +99 -114
- package/lib/Socket/index.js +9 -11
- package/lib/Socket/messages-recv.js +559 -1277
- package/lib/Socket/messages-send.js +669 -674
- package/lib/Socket/newsletter.js +286 -149
- package/lib/Socket/socket.js +235 -548
- package/lib/Types/Auth.js +2 -2
- package/lib/Types/Call.js +2 -2
- package/lib/Types/Chat.js +4 -8
- package/lib/Types/Contact.js +2 -2
- package/lib/Types/Events.js +2 -2
- package/lib/Types/GroupMetadata.js +2 -2
- package/lib/Types/Label.js +5 -3
- package/lib/Types/LabelAssociation.js +5 -3
- package/lib/Types/Message.js +7 -11
- package/lib/Types/Product.js +2 -2
- package/lib/Types/Signal.js +2 -2
- package/lib/Types/Socket.js +2 -3
- package/lib/Types/State.js +2 -56
- package/lib/Types/USync.js +2 -2
- package/lib/Types/index.js +31 -15
- package/lib/Utils/auth-utils.js +136 -239
- package/lib/Utils/business.js +69 -66
- package/lib/Utils/chat-utils.js +238 -380
- package/lib/Utils/crypto.js +127 -56
- package/lib/Utils/decode-wa-message.js +63 -173
- package/lib/Utils/event-buffer.js +70 -174
- package/lib/Utils/generics.js +207 -159
- package/lib/Utils/history.js +53 -93
- package/lib/Utils/index.js +33 -22
- package/lib/Utils/link-preview.js +58 -17
- package/lib/Utils/logger.js +7 -3
- package/lib/Utils/lt-hash.js +46 -3
- package/lib/Utils/make-mutex.js +34 -24
- package/lib/Utils/messages-media.js +744 -483
- package/lib/Utils/messages.js +621 -411
- package/lib/Utils/noise-handler.js +99 -149
- package/lib/Utils/process-message.js +150 -459
- package/lib/Utils/signal.js +73 -112
- package/lib/Utils/use-multi-file-auth-state.js +27 -23
- package/lib/Utils/validate-connection.js +104 -74
- package/lib/WABinary/constants.js +11 -9
- package/lib/WABinary/decode.js +52 -49
- package/lib/WABinary/encode.js +49 -19
- package/lib/WABinary/generic-utils.js +43 -46
- package/lib/WABinary/index.js +21 -6
- package/lib/WABinary/jid-utils.js +45 -72
- package/lib/WABinary/types.js +2 -2
- package/lib/WAM/BinaryInfo.js +5 -2
- package/lib/WAM/constants.js +11958 -19461
- package/lib/WAM/encode.js +22 -17
- package/lib/WAM/index.js +19 -4
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +13 -33
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +14 -11
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +11 -8
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +13 -9
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +22 -20
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +8 -13
- package/lib/WAUSync/Protocols/index.js +20 -6
- package/lib/WAUSync/USyncQuery.js +35 -44
- package/lib/WAUSync/USyncUser.js +5 -10
- package/lib/WAUSync/index.js +19 -4
- package/lib/index.js +70 -11
- package/package.json +60 -100
- package/engine-requirements.js +0 -10
package/lib/Utils/chat-utils.js
CHANGED
|
@@ -1,44 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processSyncAction = exports.chatModificationToAppPatch = exports.decodePatches = exports.decodeSyncdSnapshot = exports.downloadExternalPatch = exports.downloadExternalBlob = exports.extractSyncdPatches = exports.decodeSyncdPatch = exports.decodeSyncdMutations = exports.encodeSyncdPatch = exports.newLTHashState = void 0;
|
|
4
|
+
const boom_1 = require("@hapi/boom");
|
|
5
|
+
const WAProto_1 = require("../../WAProto");
|
|
6
|
+
const WABinary_1 = require("../WABinary");
|
|
7
|
+
const LabelAssociation_1 = require("../Types/LabelAssociation");
|
|
8
|
+
const crypto_1 = require("./crypto");
|
|
9
|
+
const generics_1 = require("./generics");
|
|
10
|
+
const lt_hash_1 = require("./lt-hash");
|
|
11
|
+
const messages_media_1 = require("./messages-media");
|
|
12
|
+
const mutationKeys = async (keydata) => {
|
|
13
|
+
const expanded = await (0, crypto_1.hkdf)(keydata, 160, { info: 'WhatsApp Mutation Keys' });
|
|
13
14
|
return {
|
|
14
|
-
indexKey:
|
|
15
|
-
valueEncryptionKey:
|
|
16
|
-
valueMacKey:
|
|
17
|
-
snapshotMacKey:
|
|
18
|
-
patchMacKey:
|
|
15
|
+
indexKey: expanded.slice(0, 32),
|
|
16
|
+
valueEncryptionKey: expanded.slice(32, 64),
|
|
17
|
+
valueMacKey: expanded.slice(64, 96),
|
|
18
|
+
snapshotMacKey: expanded.slice(96, 128),
|
|
19
|
+
patchMacKey: expanded.slice(128, 160)
|
|
19
20
|
};
|
|
20
21
|
};
|
|
21
22
|
const generateMac = (operation, data, keyId, key) => {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
23
|
+
const getKeyData = () => {
|
|
24
|
+
let r;
|
|
25
|
+
switch (operation) {
|
|
26
|
+
case WAProto_1.proto.SyncdMutation.SyncdOperation.SET:
|
|
27
|
+
r = 0x01;
|
|
28
|
+
break;
|
|
29
|
+
case WAProto_1.proto.SyncdMutation.SyncdOperation.REMOVE:
|
|
30
|
+
r = 0x02;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
const buff = Buffer.from([r]);
|
|
34
|
+
return Buffer.concat([buff, Buffer.from(keyId, 'base64')]);
|
|
35
|
+
};
|
|
36
|
+
const keyData = getKeyData();
|
|
37
|
+
const last = Buffer.alloc(8); // 8 bytes
|
|
38
|
+
last.set([keyData.length], last.length - 1);
|
|
39
|
+
const total = Buffer.concat([keyData, data, last]);
|
|
40
|
+
const hmac = (0, crypto_1.hmacSign)(total, key, 'sha512');
|
|
41
|
+
return hmac.slice(0, 32);
|
|
35
42
|
};
|
|
36
43
|
const to64BitNetworkOrder = (e) => {
|
|
37
44
|
const buff = Buffer.alloc(8);
|
|
38
45
|
buff.writeUint32BE(e, 4);
|
|
39
46
|
return buff;
|
|
40
47
|
};
|
|
41
|
-
|
|
48
|
+
const makeLtHashGenerator = ({ indexValueMap, hash }) => {
|
|
42
49
|
indexValueMap = { ...indexValueMap };
|
|
43
50
|
const addBuffs = [];
|
|
44
51
|
const subBuffs = [];
|
|
@@ -46,89 +53,75 @@ export const makeLtHashGenerator = ({ indexValueMap, hash }) => {
|
|
|
46
53
|
mix: ({ indexMac, valueMac, operation }) => {
|
|
47
54
|
const indexMacBase64 = Buffer.from(indexMac).toString('base64');
|
|
48
55
|
const prevOp = indexValueMap[indexMacBase64];
|
|
49
|
-
if (operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
|
|
56
|
+
if (operation === WAProto_1.proto.SyncdMutation.SyncdOperation.REMOVE) {
|
|
50
57
|
if (!prevOp) {
|
|
51
|
-
|
|
52
|
-
// The missing REMOVE will cause an LTHash mismatch, which is handled
|
|
53
|
-
// by the MAC validation layer (snapshot recovery or retry).
|
|
54
|
-
return;
|
|
58
|
+
throw new boom_1.Boom('tried remove, but no previous op', { data: { indexMac, valueMac } });
|
|
55
59
|
}
|
|
56
60
|
// remove from index value mac, since this mutation is erased
|
|
57
61
|
delete indexValueMap[indexMacBase64];
|
|
58
62
|
}
|
|
59
63
|
else {
|
|
60
|
-
addBuffs.push(valueMac);
|
|
64
|
+
addBuffs.push(new Uint8Array(valueMac).buffer);
|
|
61
65
|
// add this index into the history map
|
|
62
66
|
indexValueMap[indexMacBase64] = { valueMac };
|
|
63
67
|
}
|
|
64
68
|
if (prevOp) {
|
|
65
|
-
subBuffs.push(prevOp.valueMac);
|
|
69
|
+
subBuffs.push(new Uint8Array(prevOp.valueMac).buffer);
|
|
66
70
|
}
|
|
67
71
|
},
|
|
68
|
-
finish: () => {
|
|
69
|
-
const
|
|
72
|
+
finish: async () => {
|
|
73
|
+
const hashArrayBuffer = new Uint8Array(hash).buffer;
|
|
74
|
+
const result = await lt_hash_1.LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs);
|
|
75
|
+
const buffer = Buffer.from(result);
|
|
70
76
|
return {
|
|
71
|
-
hash:
|
|
77
|
+
hash: buffer,
|
|
72
78
|
indexValueMap
|
|
73
79
|
};
|
|
74
80
|
}
|
|
75
81
|
};
|
|
76
82
|
};
|
|
77
83
|
const generateSnapshotMac = (lthash, version, name, key) => {
|
|
78
|
-
const total = Buffer.concat([
|
|
79
|
-
|
|
84
|
+
const total = Buffer.concat([
|
|
85
|
+
lthash,
|
|
86
|
+
to64BitNetworkOrder(version),
|
|
87
|
+
Buffer.from(name, 'utf-8')
|
|
88
|
+
]);
|
|
89
|
+
return (0, crypto_1.hmacSign)(total, key, 'sha256');
|
|
80
90
|
};
|
|
81
91
|
const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
|
|
82
|
-
const total = Buffer.concat([
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
return state;
|
|
92
|
+
const total = Buffer.concat([
|
|
93
|
+
snapshotMac,
|
|
94
|
+
...valueMacs,
|
|
95
|
+
to64BitNetworkOrder(version),
|
|
96
|
+
Buffer.from(type, 'utf-8')
|
|
97
|
+
]);
|
|
98
|
+
return (0, crypto_1.hmacSign)(total, key);
|
|
91
99
|
};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
* WA Web treats these as "Blocked" (waits for key arrival), not fatal.
|
|
96
|
-
* In Baileys we retry with a snapshot which may use a different key.
|
|
97
|
-
*/
|
|
98
|
-
export const isMissingKeyError = (error) => {
|
|
99
|
-
return error?.data?.isMissingKey === true;
|
|
100
|
-
};
|
|
101
|
-
/**
|
|
102
|
-
* Determines if an app state sync error is unrecoverable.
|
|
103
|
-
* TypeError indicates a WASM crash; otherwise we give up after MAX_SYNC_ATTEMPTS.
|
|
104
|
-
* Missing keys are NOT checked here — they are handled separately as "Blocked".
|
|
105
|
-
*/
|
|
106
|
-
export const isAppStateSyncIrrecoverable = (error, attempts) => {
|
|
107
|
-
return attempts >= MAX_SYNC_ATTEMPTS || error?.name === 'TypeError';
|
|
108
|
-
};
|
|
109
|
-
export const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, operation }, myAppStateKeyId, state, getAppStateSyncKey) => {
|
|
100
|
+
const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} });
|
|
101
|
+
exports.newLTHashState = newLTHashState;
|
|
102
|
+
const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, operation }, myAppStateKeyId, state, getAppStateSyncKey) => {
|
|
110
103
|
const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined;
|
|
111
104
|
if (!key) {
|
|
112
|
-
throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, {
|
|
105
|
+
throw new boom_1.Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 });
|
|
113
106
|
}
|
|
114
107
|
const encKeyId = Buffer.from(myAppStateKeyId, 'base64');
|
|
115
108
|
state = { ...state, indexValueMap: { ...state.indexValueMap } };
|
|
116
109
|
const indexBuffer = Buffer.from(JSON.stringify(index));
|
|
117
|
-
const dataProto = proto.SyncActionData.fromObject({
|
|
110
|
+
const dataProto = WAProto_1.proto.SyncActionData.fromObject({
|
|
118
111
|
index: indexBuffer,
|
|
119
112
|
value: syncAction,
|
|
120
113
|
padding: new Uint8Array(0),
|
|
121
114
|
version: apiVersion
|
|
122
115
|
});
|
|
123
|
-
const encoded = proto.SyncActionData.encode(dataProto).finish();
|
|
124
|
-
const keyValue = mutationKeys(key.keyData);
|
|
125
|
-
const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey);
|
|
116
|
+
const encoded = WAProto_1.proto.SyncActionData.encode(dataProto).finish();
|
|
117
|
+
const keyValue = await mutationKeys(key.keyData);
|
|
118
|
+
const encValue = (0, crypto_1.aesEncrypt)(encoded, keyValue.valueEncryptionKey);
|
|
126
119
|
const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey);
|
|
127
|
-
const indexMac = hmacSign(indexBuffer, keyValue.indexKey);
|
|
120
|
+
const indexMac = (0, crypto_1.hmacSign)(indexBuffer, keyValue.indexKey);
|
|
128
121
|
// update LT hash
|
|
129
122
|
const generator = makeLtHashGenerator(state);
|
|
130
123
|
generator.mix({ indexMac, valueMac, operation });
|
|
131
|
-
Object.assign(state, generator.finish());
|
|
124
|
+
Object.assign(state, await generator.finish());
|
|
132
125
|
state.version += 1;
|
|
133
126
|
const snapshotMac = generateSnapshotMac(state.hash, state.version, type, keyValue.snapshotMacKey);
|
|
134
127
|
const patch = {
|
|
@@ -154,52 +147,33 @@ export const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, op
|
|
|
154
147
|
state.indexValueMap[base64Index] = { valueMac };
|
|
155
148
|
return { patch, state };
|
|
156
149
|
};
|
|
157
|
-
|
|
150
|
+
exports.encodeSyncdPatch = encodeSyncdPatch;
|
|
151
|
+
const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
|
|
158
152
|
const ltGenerator = makeLtHashGenerator(initialState);
|
|
159
|
-
const derivedKeyCache = new Map();
|
|
160
153
|
// indexKey used to HMAC sign record.index.blob
|
|
161
154
|
// valueEncryptionKey used to AES-256-CBC encrypt record.value.blob[0:-32]
|
|
162
155
|
// the remaining record.value.blob[0:-32] is the mac, it the HMAC sign of key.keyId + decoded proto data + length of bytes in keyId
|
|
163
156
|
for (const msgMutation of msgMutations) {
|
|
164
157
|
// if it's a syncdmutation, get the operation property
|
|
165
158
|
// otherwise, if it's only a record -- it'll be a SET mutation
|
|
166
|
-
const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET;
|
|
167
|
-
const record = 'record' in msgMutation && !!msgMutation.record ? msgMutation.record : msgMutation;
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
catch (err) {
|
|
173
|
-
// Missing-key errors must propagate so the orchestrator can park the
|
|
174
|
-
// collection (Blocked) and retry when APP_STATE_SYNC_KEY_SHARE arrives.
|
|
175
|
-
// Other errors → individual record corruption, skip and keep going.
|
|
176
|
-
if (isMissingKeyError(err))
|
|
177
|
-
throw err;
|
|
178
|
-
continue;
|
|
179
|
-
}
|
|
180
|
-
const content = record.value.blob;
|
|
181
|
-
const encContent = content.subarray(0, -32);
|
|
182
|
-
const ogValueMac = content.subarray(-32);
|
|
159
|
+
const operation = 'operation' in msgMutation ? msgMutation.operation : WAProto_1.proto.SyncdMutation.SyncdOperation.SET;
|
|
160
|
+
const record = ('record' in msgMutation && !!msgMutation.record) ? msgMutation.record : msgMutation;
|
|
161
|
+
const key = await getKey(record.keyId.id);
|
|
162
|
+
const content = Buffer.from(record.value.blob);
|
|
163
|
+
const encContent = content.slice(0, -32);
|
|
164
|
+
const ogValueMac = content.slice(-32);
|
|
183
165
|
if (validateMacs) {
|
|
184
166
|
const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey);
|
|
185
167
|
if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
|
|
186
|
-
|
|
187
|
-
continue;
|
|
168
|
+
throw new boom_1.Boom('HMAC content verification failed');
|
|
188
169
|
}
|
|
189
170
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
result = aesDecrypt(encContent, key.valueEncryptionKey);
|
|
193
|
-
}
|
|
194
|
-
catch {
|
|
195
|
-
// decrypt failed — skip this record instead of aborting
|
|
196
|
-
continue;
|
|
197
|
-
}
|
|
198
|
-
const syncAction = proto.SyncActionData.decode(result);
|
|
171
|
+
const result = (0, crypto_1.aesDecrypt)(encContent, key.valueEncryptionKey);
|
|
172
|
+
const syncAction = WAProto_1.proto.SyncActionData.decode(result);
|
|
199
173
|
if (validateMacs) {
|
|
200
|
-
const hmac = hmacSign(syncAction.index, key.indexKey);
|
|
174
|
+
const hmac = (0, crypto_1.hmacSign)(syncAction.index, key.indexKey);
|
|
201
175
|
if (Buffer.compare(hmac, record.index.blob) !== 0) {
|
|
202
|
-
throw new Boom('HMAC index verification failed');
|
|
176
|
+
throw new boom_1.Boom('HMAC index verification failed');
|
|
203
177
|
}
|
|
204
178
|
}
|
|
205
179
|
const indexStr = Buffer.from(syncAction.index).toString();
|
|
@@ -210,49 +184,43 @@ export const decodeSyncdMutations = async (msgMutations, initialState, getAppSta
|
|
|
210
184
|
operation: operation
|
|
211
185
|
});
|
|
212
186
|
}
|
|
213
|
-
return ltGenerator.finish();
|
|
187
|
+
return await ltGenerator.finish();
|
|
214
188
|
async function getKey(keyId) {
|
|
215
189
|
const base64Key = Buffer.from(keyId).toString('base64');
|
|
216
|
-
const cached = derivedKeyCache.get(base64Key);
|
|
217
|
-
if (cached) {
|
|
218
|
-
return cached;
|
|
219
|
-
}
|
|
220
190
|
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
221
191
|
if (!keyEnc) {
|
|
222
|
-
throw new Boom(`failed to find key "${base64Key}" to decode mutation`, {
|
|
223
|
-
data: { isMissingKey: true, msgMutations }
|
|
224
|
-
});
|
|
192
|
+
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`, { statusCode: 404, data: { msgMutations } });
|
|
225
193
|
}
|
|
226
|
-
|
|
227
|
-
derivedKeyCache.set(base64Key, keys);
|
|
228
|
-
return keys;
|
|
194
|
+
return mutationKeys(keyEnc.keyData);
|
|
229
195
|
}
|
|
230
196
|
};
|
|
231
|
-
|
|
197
|
+
exports.decodeSyncdMutations = decodeSyncdMutations;
|
|
198
|
+
const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
|
|
232
199
|
if (validateMacs) {
|
|
233
200
|
const base64Key = Buffer.from(msg.keyId.id).toString('base64');
|
|
234
201
|
const mainKeyObj = await getAppStateSyncKey(base64Key);
|
|
235
202
|
if (!mainKeyObj) {
|
|
236
|
-
throw new Boom(`failed to find key "${base64Key}" to decode patch`, {
|
|
203
|
+
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } });
|
|
237
204
|
}
|
|
238
|
-
const mainKey = mutationKeys(mainKeyObj.keyData);
|
|
205
|
+
const mainKey = await mutationKeys(mainKeyObj.keyData);
|
|
239
206
|
const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32));
|
|
240
|
-
const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, toNumber(msg.version.version), name, mainKey.patchMacKey);
|
|
207
|
+
const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, (0, generics_1.toNumber)(msg.version.version), name, mainKey.patchMacKey);
|
|
241
208
|
if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
|
|
242
|
-
throw new Boom('Invalid patch mac');
|
|
209
|
+
throw new boom_1.Boom('Invalid patch mac');
|
|
243
210
|
}
|
|
244
211
|
}
|
|
245
|
-
const result = await decodeSyncdMutations(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs);
|
|
212
|
+
const result = await (0, exports.decodeSyncdMutations)(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs);
|
|
246
213
|
return result;
|
|
247
214
|
};
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
const
|
|
215
|
+
exports.decodeSyncdPatch = decodeSyncdPatch;
|
|
216
|
+
const extractSyncdPatches = async (result, options) => {
|
|
217
|
+
const syncNode = (0, WABinary_1.getBinaryNodeChild)(result, 'sync');
|
|
218
|
+
const collectionNodes = (0, WABinary_1.getBinaryNodeChildren)(syncNode, 'collection');
|
|
251
219
|
const final = {};
|
|
252
220
|
await Promise.all(collectionNodes.map(async (collectionNode) => {
|
|
253
|
-
const patchesNode = getBinaryNodeChild(collectionNode, 'patches');
|
|
254
|
-
const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch');
|
|
255
|
-
const snapshotNode = getBinaryNodeChild(collectionNode, 'snapshot');
|
|
221
|
+
const patchesNode = (0, WABinary_1.getBinaryNodeChild)(collectionNode, 'patches');
|
|
222
|
+
const patches = (0, WABinary_1.getBinaryNodeChildren)(patchesNode || collectionNode, 'patch');
|
|
223
|
+
const snapshotNode = (0, WABinary_1.getBinaryNodeChild)(collectionNode, 'snapshot');
|
|
256
224
|
const syncds = [];
|
|
257
225
|
const name = collectionNode.attrs.name;
|
|
258
226
|
const hasMorePatches = collectionNode.attrs.has_more_patches === 'true';
|
|
@@ -261,16 +229,16 @@ export const extractSyncdPatches = async (result, options) => {
|
|
|
261
229
|
if (!Buffer.isBuffer(snapshotNode)) {
|
|
262
230
|
snapshotNode.content = Buffer.from(Object.values(snapshotNode.content));
|
|
263
231
|
}
|
|
264
|
-
const blobRef = proto.ExternalBlobReference.decode(snapshotNode.content);
|
|
265
|
-
const data = await downloadExternalBlob(blobRef, options);
|
|
266
|
-
snapshot = proto.SyncdSnapshot.decode(data);
|
|
232
|
+
const blobRef = WAProto_1.proto.ExternalBlobReference.decode(snapshotNode.content);
|
|
233
|
+
const data = await (0, exports.downloadExternalBlob)(blobRef, options);
|
|
234
|
+
snapshot = WAProto_1.proto.SyncdSnapshot.decode(data);
|
|
267
235
|
}
|
|
268
236
|
for (let { content } of patches) {
|
|
269
237
|
if (content) {
|
|
270
238
|
if (!Buffer.isBuffer(content)) {
|
|
271
239
|
content = Buffer.from(Object.values(content));
|
|
272
240
|
}
|
|
273
|
-
const syncd = proto.SyncdPatch.decode(content);
|
|
241
|
+
const syncd = WAProto_1.proto.SyncdPatch.decode(content);
|
|
274
242
|
if (!syncd.version) {
|
|
275
243
|
syncd.version = { version: +collectionNode.attrs.version + 1 };
|
|
276
244
|
}
|
|
@@ -281,27 +249,32 @@ export const extractSyncdPatches = async (result, options) => {
|
|
|
281
249
|
}));
|
|
282
250
|
return final;
|
|
283
251
|
};
|
|
284
|
-
|
|
285
|
-
|
|
252
|
+
exports.extractSyncdPatches = extractSyncdPatches;
|
|
253
|
+
const downloadExternalBlob = async (blob, options) => {
|
|
254
|
+
const stream = await (0, messages_media_1.downloadContentFromMessage)(blob, 'md-app-state', { options });
|
|
286
255
|
const bufferArray = [];
|
|
287
256
|
for await (const chunk of stream) {
|
|
288
257
|
bufferArray.push(chunk);
|
|
289
258
|
}
|
|
290
259
|
return Buffer.concat(bufferArray);
|
|
291
260
|
};
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
const
|
|
261
|
+
exports.downloadExternalBlob = downloadExternalBlob;
|
|
262
|
+
const downloadExternalPatch = async (blob, options) => {
|
|
263
|
+
const buffer = await (0, exports.downloadExternalBlob)(blob, options);
|
|
264
|
+
const syncData = WAProto_1.proto.SyncdMutations.decode(buffer);
|
|
295
265
|
return syncData;
|
|
296
266
|
};
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
newState
|
|
267
|
+
exports.downloadExternalPatch = downloadExternalPatch;
|
|
268
|
+
const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVersionNumber, validateMacs = true) => {
|
|
269
|
+
const newState = (0, exports.newLTHashState)();
|
|
270
|
+
newState.version = (0, generics_1.toNumber)(snapshot.version.version);
|
|
300
271
|
const mutationMap = {};
|
|
301
|
-
const areMutationsRequired = typeof minimumVersionNumber === 'undefined'
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
272
|
+
const areMutationsRequired = typeof minimumVersionNumber === 'undefined'
|
|
273
|
+
|| newState.version > minimumVersionNumber;
|
|
274
|
+
const { hash, indexValueMap } = await (0, exports.decodeSyncdMutations)(snapshot.records, newState, getAppStateSyncKey, areMutationsRequired
|
|
275
|
+
? (mutation) => {
|
|
276
|
+
var _a;
|
|
277
|
+
const index = (_a = mutation.syncAction.index) === null || _a === void 0 ? void 0 : _a.toString();
|
|
305
278
|
mutationMap[index] = mutation;
|
|
306
279
|
}
|
|
307
280
|
: () => { }, validateMacs);
|
|
@@ -311,17 +284,12 @@ export const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, mi
|
|
|
311
284
|
const base64Key = Buffer.from(snapshot.keyId.id).toString('base64');
|
|
312
285
|
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
313
286
|
if (!keyEnc) {
|
|
314
|
-
throw new Boom(`failed to find key "${base64Key}" to decode mutation
|
|
287
|
+
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`);
|
|
315
288
|
}
|
|
316
|
-
const result = mutationKeys(keyEnc.keyData);
|
|
289
|
+
const result = await mutationKeys(keyEnc.keyData);
|
|
317
290
|
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
|
|
318
291
|
if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
|
|
319
|
-
|
|
320
|
-
// records (poisoned server-side snapshot); the aggregate client hash diverges
|
|
321
|
-
// from the server-computed mac. Fall through with a warning so the session stays
|
|
322
|
-
// alive with partial state, symmetric to how decodePatches handles its own
|
|
323
|
-
// LTHash mismatch a few lines below.
|
|
324
|
-
logger?.warn({ name, version: newState.version }, 'LTHash verification failed on snapshot, continuing with partial state');
|
|
292
|
+
throw new boom_1.Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`);
|
|
325
293
|
}
|
|
326
294
|
}
|
|
327
295
|
return {
|
|
@@ -329,51 +297,45 @@ export const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, mi
|
|
|
329
297
|
mutationMap
|
|
330
298
|
};
|
|
331
299
|
};
|
|
332
|
-
|
|
300
|
+
exports.decodeSyncdSnapshot = decodeSyncdSnapshot;
|
|
301
|
+
const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options, minimumVersionNumber, logger, validateMacs = true) => {
|
|
302
|
+
var _a;
|
|
333
303
|
const newState = {
|
|
334
304
|
...initial,
|
|
335
305
|
indexValueMap: { ...initial.indexValueMap }
|
|
336
306
|
};
|
|
337
307
|
const mutationMap = {};
|
|
338
|
-
for (
|
|
308
|
+
for (let i = 0; i < syncds.length; i++) {
|
|
309
|
+
const syncd = syncds[i];
|
|
339
310
|
const { version, keyId, snapshotMac } = syncd;
|
|
340
311
|
if (syncd.externalMutations) {
|
|
341
|
-
logger
|
|
342
|
-
const ref = await downloadExternalPatch(syncd.externalMutations, options);
|
|
343
|
-
logger
|
|
344
|
-
syncd.mutations
|
|
312
|
+
logger === null || logger === void 0 ? void 0 : logger.trace({ name, version }, 'downloading external patch');
|
|
313
|
+
const ref = await (0, exports.downloadExternalPatch)(syncd.externalMutations, options);
|
|
314
|
+
logger === null || logger === void 0 ? void 0 : logger.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch');
|
|
315
|
+
(_a = syncd.mutations) === null || _a === void 0 ? void 0 : _a.push(...ref.mutations);
|
|
345
316
|
}
|
|
346
|
-
const patchVersion = toNumber(version.version);
|
|
317
|
+
const patchVersion = (0, generics_1.toNumber)(version.version);
|
|
347
318
|
newState.version = patchVersion;
|
|
348
319
|
const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber;
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
: () => { }, validateMacs);
|
|
357
|
-
}
|
|
358
|
-
catch (err) {
|
|
359
|
-
if (isMissingKeyError(err))
|
|
360
|
-
throw err;
|
|
361
|
-
logger?.warn({ name, version: patchVersion, error: err.message }, 'failed to decode patch, skipping');
|
|
362
|
-
continue;
|
|
363
|
-
}
|
|
320
|
+
const decodeResult = await (0, exports.decodeSyncdPatch)(syncd, name, newState, getAppStateSyncKey, shouldMutate
|
|
321
|
+
? mutation => {
|
|
322
|
+
var _a;
|
|
323
|
+
const index = (_a = mutation.syncAction.index) === null || _a === void 0 ? void 0 : _a.toString();
|
|
324
|
+
mutationMap[index] = mutation;
|
|
325
|
+
}
|
|
326
|
+
: (() => { }), true);
|
|
364
327
|
newState.hash = decodeResult.hash;
|
|
365
328
|
newState.indexValueMap = decodeResult.indexValueMap;
|
|
366
329
|
if (validateMacs) {
|
|
367
330
|
const base64Key = Buffer.from(keyId.id).toString('base64');
|
|
368
331
|
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
369
332
|
if (!keyEnc) {
|
|
370
|
-
throw new Boom(`failed to find key "${base64Key}" to decode mutation
|
|
333
|
+
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`);
|
|
371
334
|
}
|
|
372
|
-
const result = mutationKeys(keyEnc.keyData);
|
|
335
|
+
const result = await mutationKeys(keyEnc.keyData);
|
|
373
336
|
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
|
|
374
337
|
if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
|
|
375
|
-
|
|
376
|
-
break;
|
|
338
|
+
throw new boom_1.Boom(`failed to verify LTHash at ${newState.version} of ${name}`);
|
|
377
339
|
}
|
|
378
340
|
}
|
|
379
341
|
// clear memory used up by the mutations
|
|
@@ -381,31 +343,31 @@ export const decodePatches = async (name, syncds, initial, getAppStateSyncKey, o
|
|
|
381
343
|
}
|
|
382
344
|
return { state: newState, mutationMap };
|
|
383
345
|
};
|
|
384
|
-
|
|
385
|
-
|
|
346
|
+
exports.decodePatches = decodePatches;
|
|
347
|
+
const chatModificationToAppPatch = (mod, jid) => {
|
|
348
|
+
const OP = WAProto_1.proto.SyncdMutation.SyncdOperation;
|
|
386
349
|
const getMessageRange = (lastMessages) => {
|
|
387
350
|
let messageRange;
|
|
388
351
|
if (Array.isArray(lastMessages)) {
|
|
389
352
|
const lastMsg = lastMessages[lastMessages.length - 1];
|
|
390
353
|
messageRange = {
|
|
391
|
-
lastMessageTimestamp: lastMsg
|
|
392
|
-
messages: lastMessages
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
: undefined
|
|
354
|
+
lastMessageTimestamp: lastMsg === null || lastMsg === void 0 ? void 0 : lastMsg.messageTimestamp,
|
|
355
|
+
messages: (lastMessages === null || lastMessages === void 0 ? void 0 : lastMessages.length) ? lastMessages.map(m => {
|
|
356
|
+
var _a, _b;
|
|
357
|
+
if (!((_a = m.key) === null || _a === void 0 ? void 0 : _a.id) || !((_b = m.key) === null || _b === void 0 ? void 0 : _b.remoteJid)) {
|
|
358
|
+
throw new boom_1.Boom('Incomplete key', { statusCode: 400, data: m });
|
|
359
|
+
}
|
|
360
|
+
if ((0, WABinary_1.isJidGroup)(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
|
|
361
|
+
throw new boom_1.Boom('Expected not from me message to have participant', { statusCode: 400, data: m });
|
|
362
|
+
}
|
|
363
|
+
if (!m.messageTimestamp || !(0, generics_1.toNumber)(m.messageTimestamp)) {
|
|
364
|
+
throw new boom_1.Boom('Missing timestamp in last message list', { statusCode: 400, data: m });
|
|
365
|
+
}
|
|
366
|
+
if (m.key.participant) {
|
|
367
|
+
m.key.participant = (0, WABinary_1.jidNormalizedUser)(m.key.participant);
|
|
368
|
+
}
|
|
369
|
+
return m;
|
|
370
|
+
}) : undefined
|
|
409
371
|
};
|
|
410
372
|
}
|
|
411
373
|
else {
|
|
@@ -474,9 +436,7 @@ export const chatModificationToAppPatch = (mod, jid) => {
|
|
|
474
436
|
else if ('clear' in mod) {
|
|
475
437
|
patch = {
|
|
476
438
|
syncAction: {
|
|
477
|
-
clearChatAction: {
|
|
478
|
-
messageRange: getMessageRange(mod.lastMessages)
|
|
479
|
-
}
|
|
439
|
+
clearChatAction: {} // add message range later
|
|
480
440
|
},
|
|
481
441
|
index: ['clearChat', jid, '1' /*the option here is 0 when keep starred messages is enabled*/, '0'],
|
|
482
442
|
type: 'regular_high',
|
|
@@ -497,28 +457,6 @@ export const chatModificationToAppPatch = (mod, jid) => {
|
|
|
497
457
|
operation: OP.SET
|
|
498
458
|
};
|
|
499
459
|
}
|
|
500
|
-
else if ('contact' in mod) {
|
|
501
|
-
patch = {
|
|
502
|
-
syncAction: {
|
|
503
|
-
contactAction: mod.contact || {}
|
|
504
|
-
},
|
|
505
|
-
index: ['contact', jid],
|
|
506
|
-
type: 'critical_unblock_low',
|
|
507
|
-
apiVersion: 2,
|
|
508
|
-
operation: mod.contact ? OP.SET : OP.REMOVE
|
|
509
|
-
};
|
|
510
|
-
}
|
|
511
|
-
else if ('disableLinkPreviews' in mod) {
|
|
512
|
-
patch = {
|
|
513
|
-
syncAction: {
|
|
514
|
-
privacySettingDisableLinkPreviewsAction: mod.disableLinkPreviews || {}
|
|
515
|
-
},
|
|
516
|
-
index: ['setting_disableLinkPreviews'],
|
|
517
|
-
type: 'regular',
|
|
518
|
-
apiVersion: 8,
|
|
519
|
-
operation: OP.SET
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
460
|
else if ('star' in mod) {
|
|
523
461
|
const key = mod.star.messages[0];
|
|
524
462
|
patch = {
|
|
@@ -537,7 +475,7 @@ export const chatModificationToAppPatch = (mod, jid) => {
|
|
|
537
475
|
patch = {
|
|
538
476
|
syncAction: {
|
|
539
477
|
deleteChatAction: {
|
|
540
|
-
messageRange: getMessageRange(mod.lastMessages)
|
|
478
|
+
messageRange: getMessageRange(mod.lastMessages),
|
|
541
479
|
}
|
|
542
480
|
},
|
|
543
481
|
index: ['deleteChat', jid, '1'],
|
|
@@ -556,90 +494,64 @@ export const chatModificationToAppPatch = (mod, jid) => {
|
|
|
556
494
|
index: ['setting_pushName'],
|
|
557
495
|
type: 'critical_block',
|
|
558
496
|
apiVersion: 1,
|
|
559
|
-
operation: OP.SET
|
|
560
|
-
};
|
|
561
|
-
}
|
|
562
|
-
else if ('quickReply' in mod) {
|
|
563
|
-
patch = {
|
|
564
|
-
syncAction: {
|
|
565
|
-
quickReplyAction: {
|
|
566
|
-
count: 0,
|
|
567
|
-
deleted: mod.quickReply.deleted || false,
|
|
568
|
-
keywords: [],
|
|
569
|
-
message: mod.quickReply.message || '',
|
|
570
|
-
shortcut: mod.quickReply.shortcut || ''
|
|
571
|
-
}
|
|
572
|
-
},
|
|
573
|
-
index: ['quick_reply', mod.quickReply.timestamp || String(Math.floor(Date.now() / 1000))],
|
|
574
|
-
type: 'regular',
|
|
575
|
-
apiVersion: 2,
|
|
576
|
-
operation: OP.SET
|
|
577
|
-
};
|
|
578
|
-
}
|
|
579
|
-
else if ('addLabel' in mod) {
|
|
580
|
-
patch = {
|
|
581
|
-
syncAction: {
|
|
582
|
-
labelEditAction: {
|
|
583
|
-
name: mod.addLabel.name,
|
|
584
|
-
color: mod.addLabel.color,
|
|
585
|
-
predefinedId: mod.addLabel.predefinedId,
|
|
586
|
-
deleted: mod.addLabel.deleted
|
|
587
|
-
}
|
|
588
|
-
},
|
|
589
|
-
index: ['label_edit', mod.addLabel.id],
|
|
590
|
-
type: 'regular',
|
|
591
|
-
apiVersion: 3,
|
|
592
|
-
operation: OP.SET
|
|
497
|
+
operation: OP.SET,
|
|
593
498
|
};
|
|
594
499
|
}
|
|
595
500
|
else if ('addChatLabel' in mod) {
|
|
596
501
|
patch = {
|
|
597
502
|
syncAction: {
|
|
598
503
|
labelAssociationAction: {
|
|
599
|
-
labeled: true
|
|
504
|
+
labeled: true,
|
|
600
505
|
}
|
|
601
506
|
},
|
|
602
|
-
index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
|
|
507
|
+
index: [LabelAssociation_1.LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
|
|
603
508
|
type: 'regular',
|
|
604
509
|
apiVersion: 3,
|
|
605
|
-
operation: OP.SET
|
|
510
|
+
operation: OP.SET,
|
|
606
511
|
};
|
|
607
512
|
}
|
|
608
513
|
else if ('removeChatLabel' in mod) {
|
|
609
514
|
patch = {
|
|
610
515
|
syncAction: {
|
|
611
516
|
labelAssociationAction: {
|
|
612
|
-
labeled: false
|
|
517
|
+
labeled: false,
|
|
613
518
|
}
|
|
614
519
|
},
|
|
615
|
-
index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
|
|
520
|
+
index: [LabelAssociation_1.LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
|
|
616
521
|
type: 'regular',
|
|
617
522
|
apiVersion: 3,
|
|
618
|
-
operation: OP.SET
|
|
523
|
+
operation: OP.SET,
|
|
619
524
|
};
|
|
620
525
|
}
|
|
621
526
|
else if ('addMessageLabel' in mod) {
|
|
622
527
|
patch = {
|
|
623
528
|
syncAction: {
|
|
624
529
|
labelAssociationAction: {
|
|
625
|
-
labeled: true
|
|
530
|
+
labeled: true,
|
|
626
531
|
}
|
|
627
532
|
},
|
|
628
|
-
index: [
|
|
533
|
+
index: [
|
|
534
|
+
LabelAssociation_1.LabelAssociationType.Message,
|
|
535
|
+
mod.addMessageLabel.labelId,
|
|
536
|
+
jid,
|
|
537
|
+
mod.addMessageLabel.messageId,
|
|
538
|
+
'0',
|
|
539
|
+
'0'
|
|
540
|
+
],
|
|
629
541
|
type: 'regular',
|
|
630
542
|
apiVersion: 3,
|
|
631
|
-
operation: OP.SET
|
|
543
|
+
operation: OP.SET,
|
|
632
544
|
};
|
|
633
545
|
}
|
|
634
546
|
else if ('removeMessageLabel' in mod) {
|
|
635
547
|
patch = {
|
|
636
548
|
syncAction: {
|
|
637
549
|
labelAssociationAction: {
|
|
638
|
-
labeled: false
|
|
550
|
+
labeled: false,
|
|
639
551
|
}
|
|
640
552
|
},
|
|
641
553
|
index: [
|
|
642
|
-
LabelAssociationType.Message,
|
|
554
|
+
LabelAssociation_1.LabelAssociationType.Message,
|
|
643
555
|
mod.removeMessageLabel.labelId,
|
|
644
556
|
jid,
|
|
645
557
|
mod.removeMessageLabel.messageId,
|
|
@@ -648,30 +560,34 @@ export const chatModificationToAppPatch = (mod, jid) => {
|
|
|
648
560
|
],
|
|
649
561
|
type: 'regular',
|
|
650
562
|
apiVersion: 3,
|
|
651
|
-
operation: OP.SET
|
|
563
|
+
operation: OP.SET,
|
|
652
564
|
};
|
|
653
565
|
}
|
|
654
566
|
else {
|
|
655
|
-
throw new Boom('not supported');
|
|
567
|
+
throw new boom_1.Boom('not supported');
|
|
656
568
|
}
|
|
657
569
|
patch.syncAction.timestamp = Date.now();
|
|
658
570
|
return patch;
|
|
659
571
|
};
|
|
660
|
-
|
|
572
|
+
exports.chatModificationToAppPatch = chatModificationToAppPatch;
|
|
573
|
+
const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
574
|
+
var _a, _b, _c, _d;
|
|
661
575
|
const isInitialSync = !!initialSyncOpts;
|
|
662
|
-
const accountSettings = initialSyncOpts
|
|
663
|
-
logger
|
|
576
|
+
const accountSettings = initialSyncOpts === null || initialSyncOpts === void 0 ? void 0 : initialSyncOpts.accountSettings;
|
|
577
|
+
logger === null || logger === void 0 ? void 0 : logger.trace({ syncAction, initialSync: !!initialSyncOpts }, 'processing sync action');
|
|
664
578
|
const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction;
|
|
665
|
-
if (action
|
|
579
|
+
if (action === null || action === void 0 ? void 0 : action.muteAction) {
|
|
666
580
|
ev.emit('chats.update', [
|
|
667
581
|
{
|
|
668
582
|
id,
|
|
669
|
-
muteEndTime:
|
|
583
|
+
muteEndTime: ((_a = action.muteAction) === null || _a === void 0 ? void 0 : _a.muted)
|
|
584
|
+
? (0, generics_1.toNumber)(action.muteAction.muteEndTimestamp)
|
|
585
|
+
: null,
|
|
670
586
|
conditional: getChatUpdateConditional(id, undefined)
|
|
671
587
|
}
|
|
672
588
|
]);
|
|
673
589
|
}
|
|
674
|
-
else if (action
|
|
590
|
+
else if ((action === null || action === void 0 ? void 0 : action.archiveChatAction) || type === 'archive' || type === 'unarchive') {
|
|
675
591
|
// okay so we've to do some annoying computation here
|
|
676
592
|
// when we're initially syncing the app state
|
|
677
593
|
// there are a few cases we need to handle
|
|
@@ -682,38 +598,36 @@ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) =
|
|
|
682
598
|
// we compare the timestamp of latest message from the other person to determine this
|
|
683
599
|
// 2. if the account unarchiveChats setting is false -- then it doesn't matter,
|
|
684
600
|
// it'll always take an app state action to mark in unarchived -- which we'll get anyway
|
|
685
|
-
const archiveAction = action
|
|
686
|
-
const isArchived = archiveAction
|
|
601
|
+
const archiveAction = action === null || action === void 0 ? void 0 : action.archiveChatAction;
|
|
602
|
+
const isArchived = archiveAction
|
|
603
|
+
? archiveAction.archived
|
|
604
|
+
: type === 'archive';
|
|
687
605
|
// // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
|
|
688
606
|
// // this only applies for the initial sync
|
|
689
607
|
// if(isInitialSync && !isArchived) {
|
|
690
608
|
// isArchived = false
|
|
691
609
|
// }
|
|
692
|
-
const msgRange = !accountSettings
|
|
610
|
+
const msgRange = !(accountSettings === null || accountSettings === void 0 ? void 0 : accountSettings.unarchiveChats) ? undefined : archiveAction === null || archiveAction === void 0 ? void 0 : archiveAction.messageRange;
|
|
693
611
|
// logger?.debug({ chat: id, syncAction }, 'message range archive')
|
|
694
|
-
ev.emit('chats.update', [
|
|
695
|
-
{
|
|
612
|
+
ev.emit('chats.update', [{
|
|
696
613
|
id,
|
|
697
614
|
archived: isArchived,
|
|
698
615
|
conditional: getChatUpdateConditional(id, msgRange)
|
|
699
|
-
}
|
|
700
|
-
]);
|
|
616
|
+
}]);
|
|
701
617
|
}
|
|
702
|
-
else if (action
|
|
618
|
+
else if (action === null || action === void 0 ? void 0 : action.markChatAsReadAction) {
|
|
703
619
|
const markReadAction = action.markChatAsReadAction;
|
|
704
620
|
// basically we don't need to fire an "read" update if the chat is being marked as read
|
|
705
621
|
// because the chat is read by default
|
|
706
622
|
// this only applies for the initial sync
|
|
707
623
|
const isNullUpdate = isInitialSync && markReadAction.read;
|
|
708
|
-
ev.emit('chats.update', [
|
|
709
|
-
{
|
|
624
|
+
ev.emit('chats.update', [{
|
|
710
625
|
id,
|
|
711
|
-
unreadCount: isNullUpdate ? null : !!markReadAction
|
|
712
|
-
conditional: getChatUpdateConditional(id, markReadAction
|
|
713
|
-
}
|
|
714
|
-
]);
|
|
626
|
+
unreadCount: isNullUpdate ? null : !!(markReadAction === null || markReadAction === void 0 ? void 0 : markReadAction.read) ? 0 : -1,
|
|
627
|
+
conditional: getChatUpdateConditional(id, markReadAction === null || markReadAction === void 0 ? void 0 : markReadAction.messageRange)
|
|
628
|
+
}]);
|
|
715
629
|
}
|
|
716
|
-
else if (action
|
|
630
|
+
else if ((action === null || action === void 0 ? void 0 : action.deleteMessageForMeAction) || type === 'deleteMessageForMe') {
|
|
717
631
|
ev.emit('messages.delete', {
|
|
718
632
|
keys: [
|
|
719
633
|
{
|
|
@@ -724,35 +638,32 @@ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) =
|
|
|
724
638
|
]
|
|
725
639
|
});
|
|
726
640
|
}
|
|
727
|
-
else if (action
|
|
728
|
-
|
|
729
|
-
emitSyncActionResults(ev, results);
|
|
641
|
+
else if (action === null || action === void 0 ? void 0 : action.contactAction) {
|
|
642
|
+
ev.emit('contacts.upsert', [{ id, name: action.contactAction.fullName, lid: action.contactAction.lidJid || undefined, jid: (0, WABinary_1.isJidUser)(id) ? id : action.contactAction.pnJid }]);
|
|
730
643
|
}
|
|
731
|
-
else if (action
|
|
732
|
-
const name = action
|
|
733
|
-
if (name && me
|
|
644
|
+
else if (action === null || action === void 0 ? void 0 : action.pushNameSetting) {
|
|
645
|
+
const name = (_b = action === null || action === void 0 ? void 0 : action.pushNameSetting) === null || _b === void 0 ? void 0 : _b.name;
|
|
646
|
+
if (name && (me === null || me === void 0 ? void 0 : me.name) !== name) {
|
|
734
647
|
ev.emit('creds.update', { me: { ...me, name } });
|
|
735
648
|
}
|
|
736
649
|
}
|
|
737
|
-
else if (action
|
|
738
|
-
ev.emit('chats.update', [
|
|
739
|
-
{
|
|
650
|
+
else if (action === null || action === void 0 ? void 0 : action.pinAction) {
|
|
651
|
+
ev.emit('chats.update', [{
|
|
740
652
|
id,
|
|
741
|
-
pinned: action.pinAction
|
|
653
|
+
pinned: ((_c = action.pinAction) === null || _c === void 0 ? void 0 : _c.pinned) ? (0, generics_1.toNumber)(action.timestamp) : null,
|
|
742
654
|
conditional: getChatUpdateConditional(id, undefined)
|
|
743
|
-
}
|
|
744
|
-
]);
|
|
655
|
+
}]);
|
|
745
656
|
}
|
|
746
|
-
else if (action
|
|
657
|
+
else if (action === null || action === void 0 ? void 0 : action.unarchiveChatsSetting) {
|
|
747
658
|
const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats;
|
|
748
659
|
ev.emit('creds.update', { accountSettings: { unarchiveChats } });
|
|
749
|
-
logger
|
|
660
|
+
logger === null || logger === void 0 ? void 0 : logger.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`);
|
|
750
661
|
if (accountSettings) {
|
|
751
662
|
accountSettings.unarchiveChats = unarchiveChats;
|
|
752
663
|
}
|
|
753
664
|
}
|
|
754
|
-
else if (action
|
|
755
|
-
let starred = action
|
|
665
|
+
else if ((action === null || action === void 0 ? void 0 : action.starAction) || type === 'star') {
|
|
666
|
+
let starred = (_d = action === null || action === void 0 ? void 0 : action.starAction) === null || _d === void 0 ? void 0 : _d.starred;
|
|
756
667
|
if (typeof starred !== 'boolean') {
|
|
757
668
|
starred = syncAction.index[syncAction.index.length - 1] === '1';
|
|
758
669
|
}
|
|
@@ -763,99 +674,46 @@ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) =
|
|
|
763
674
|
}
|
|
764
675
|
]);
|
|
765
676
|
}
|
|
766
|
-
else if (action
|
|
677
|
+
else if ((action === null || action === void 0 ? void 0 : action.deleteChatAction) || type === 'deleteChat') {
|
|
767
678
|
if (!isInitialSync) {
|
|
768
679
|
ev.emit('chats.delete', [id]);
|
|
769
680
|
}
|
|
770
681
|
}
|
|
771
|
-
else if (action
|
|
682
|
+
else if (action === null || action === void 0 ? void 0 : action.labelEditAction) {
|
|
772
683
|
const { name, color, deleted, predefinedId } = action.labelEditAction;
|
|
773
684
|
ev.emit('labels.edit', {
|
|
774
|
-
id
|
|
685
|
+
id,
|
|
775
686
|
name: name,
|
|
776
687
|
color: color,
|
|
777
688
|
deleted: deleted,
|
|
778
689
|
predefinedId: predefinedId ? String(predefinedId) : undefined
|
|
779
690
|
});
|
|
780
691
|
}
|
|
781
|
-
else if (action
|
|
692
|
+
else if (action === null || action === void 0 ? void 0 : action.labelAssociationAction) {
|
|
782
693
|
ev.emit('labels.association', {
|
|
783
|
-
type: action.labelAssociationAction.labeled
|
|
784
|
-
|
|
694
|
+
type: action.labelAssociationAction.labeled
|
|
695
|
+
? 'add'
|
|
696
|
+
: 'remove',
|
|
697
|
+
association: type === LabelAssociation_1.LabelAssociationType.Chat
|
|
785
698
|
? {
|
|
786
|
-
type: LabelAssociationType.Chat,
|
|
699
|
+
type: LabelAssociation_1.LabelAssociationType.Chat,
|
|
787
700
|
chatId: syncAction.index[2],
|
|
788
701
|
labelId: syncAction.index[1]
|
|
789
702
|
}
|
|
790
703
|
: {
|
|
791
|
-
type: LabelAssociationType.Message,
|
|
704
|
+
type: LabelAssociation_1.LabelAssociationType.Message,
|
|
792
705
|
chatId: syncAction.index[2],
|
|
793
706
|
messageId: syncAction.index[3],
|
|
794
707
|
labelId: syncAction.index[1]
|
|
795
708
|
}
|
|
796
709
|
});
|
|
797
710
|
}
|
|
798
|
-
else if (action?.localeSetting?.locale) {
|
|
799
|
-
ev.emit('settings.update', { setting: 'locale', value: action.localeSetting.locale });
|
|
800
|
-
}
|
|
801
|
-
else if (action?.timeFormatAction) {
|
|
802
|
-
ev.emit('settings.update', { setting: 'timeFormat', value: action.timeFormatAction });
|
|
803
|
-
}
|
|
804
|
-
else if (action?.pnForLidChatAction) {
|
|
805
|
-
if (action.pnForLidChatAction.pnJid) {
|
|
806
|
-
ev.emit('lid-mapping.update', { lid: id, pn: action.pnForLidChatAction.pnJid });
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
else if (action?.privacySettingRelayAllCalls) {
|
|
810
|
-
ev.emit('settings.update', {
|
|
811
|
-
setting: 'privacySettingRelayAllCalls',
|
|
812
|
-
value: action.privacySettingRelayAllCalls
|
|
813
|
-
});
|
|
814
|
-
}
|
|
815
|
-
else if (action?.statusPrivacy) {
|
|
816
|
-
ev.emit('settings.update', { setting: 'statusPrivacy', value: action.statusPrivacy });
|
|
817
|
-
}
|
|
818
|
-
else if (action?.lockChatAction) {
|
|
819
|
-
ev.emit('chats.lock', { id: id, locked: !!action.lockChatAction.locked });
|
|
820
|
-
}
|
|
821
|
-
else if (action?.privacySettingDisableLinkPreviewsAction) {
|
|
822
|
-
ev.emit('settings.update', {
|
|
823
|
-
setting: 'disableLinkPreviews',
|
|
824
|
-
value: action.privacySettingDisableLinkPreviewsAction
|
|
825
|
-
});
|
|
826
|
-
}
|
|
827
|
-
else if (action?.notificationActivitySettingAction?.notificationActivitySetting) {
|
|
828
|
-
ev.emit('settings.update', {
|
|
829
|
-
setting: 'notificationActivitySetting',
|
|
830
|
-
value: action.notificationActivitySettingAction.notificationActivitySetting
|
|
831
|
-
});
|
|
832
|
-
}
|
|
833
|
-
else if (action?.lidContactAction) {
|
|
834
|
-
ev.emit('contacts.upsert', [
|
|
835
|
-
{
|
|
836
|
-
id: id,
|
|
837
|
-
name: action.lidContactAction.fullName ||
|
|
838
|
-
action.lidContactAction.firstName ||
|
|
839
|
-
action.lidContactAction.username ||
|
|
840
|
-
undefined,
|
|
841
|
-
username: action.lidContactAction.username || undefined,
|
|
842
|
-
lid: id,
|
|
843
|
-
phoneNumber: undefined
|
|
844
|
-
}
|
|
845
|
-
]);
|
|
846
|
-
}
|
|
847
|
-
else if (action?.privacySettingChannelsPersonalisedRecommendationAction) {
|
|
848
|
-
ev.emit('settings.update', {
|
|
849
|
-
setting: 'channelsPersonalisedRecommendation',
|
|
850
|
-
value: action.privacySettingChannelsPersonalisedRecommendationAction
|
|
851
|
-
});
|
|
852
|
-
}
|
|
853
711
|
else {
|
|
854
|
-
logger
|
|
712
|
+
logger === null || logger === void 0 ? void 0 : logger.debug({ syncAction, id }, 'unprocessable update');
|
|
855
713
|
}
|
|
856
714
|
function getChatUpdateConditional(id, msgRange) {
|
|
857
715
|
return isInitialSync
|
|
858
|
-
? data => {
|
|
716
|
+
? (data) => {
|
|
859
717
|
const chat = data.historySets.chats[id] || data.chatUpserts[id];
|
|
860
718
|
if (chat) {
|
|
861
719
|
return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true;
|
|
@@ -864,9 +722,9 @@ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) =
|
|
|
864
722
|
: undefined;
|
|
865
723
|
}
|
|
866
724
|
function isValidPatchBasedOnMessageRange(chat, msgRange) {
|
|
867
|
-
const lastMsgTimestamp = Number(msgRange
|
|
868
|
-
const chatLastMsgTimestamp = Number(chat
|
|
725
|
+
const lastMsgTimestamp = Number((msgRange === null || msgRange === void 0 ? void 0 : msgRange.lastMessageTimestamp) || (msgRange === null || msgRange === void 0 ? void 0 : msgRange.lastSystemMessageTimestamp) || 0);
|
|
726
|
+
const chatLastMsgTimestamp = Number((chat === null || chat === void 0 ? void 0 : chat.lastMessageRecvTimestamp) || 0);
|
|
869
727
|
return lastMsgTimestamp >= chatLastMsgTimestamp;
|
|
870
728
|
}
|
|
871
729
|
};
|
|
872
|
-
|
|
730
|
+
exports.processSyncAction = processSyncAction;
|