@skyzopedia/baileys-mod 5.0.8 → 6.0.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/WAProto/index.js +133384 -57814
- package/engine-requirements.js +10 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.d.ts +53 -0
- package/lib/Defaults/index.js +141 -117
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
- package/lib/Signal/Group/ciphertext-message.js +14 -12
- package/lib/Signal/Group/group-session-builder.d.ts +14 -0
- package/lib/Signal/Group/group-session-builder.js +42 -10
- package/lib/Signal/Group/group_cipher.d.ts +17 -0
- package/lib/Signal/Group/group_cipher.js +87 -75
- package/lib/Signal/Group/index.d.ts +11 -0
- package/lib/Signal/Group/index.js +57 -13
- package/lib/Signal/Group/keyhelper.d.ts +10 -0
- package/lib/Signal/Group/keyhelper.js +52 -17
- package/lib/Signal/Group/queue-job.d.ts +1 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
- package/lib/Signal/Group/sender-chain-key.js +33 -27
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -62
- package/lib/Signal/Group/sender-key-message.d.ts +18 -0
- package/lib/Signal/Group/sender-key-message.js +66 -65
- package/lib/Signal/Group/sender-key-name.d.ts +17 -0
- package/lib/Signal/Group/sender-key-name.js +44 -45
- package/lib/Signal/Group/sender-key-record.d.ts +30 -0
- package/lib/Signal/Group/sender-key-record.js +49 -39
- package/lib/Signal/Group/sender-key-state.d.ts +38 -0
- package/lib/Signal/Group/sender-key-state.js +93 -80
- package/lib/Signal/Group/sender-message-key.d.ts +11 -0
- package/lib/Signal/Group/sender-message-key.js +28 -27
- package/lib/Signal/libsignal.d.ts +3 -0
- package/lib/Signal/libsignal.js +163 -313
- package/lib/Socket/Client/abstract-socket-client.d.ts +17 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.js +19 -4
- package/lib/Socket/Client/mobile-socket-client.d.ts +13 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/web-socket-client.d.ts +12 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/business.d.ts +171 -0
- package/lib/Socket/business.js +242 -359
- package/lib/Socket/chats.d.ts +267 -0
- package/lib/Socket/chats.js +935 -846
- package/lib/Socket/dugong.d.ts +254 -0
- package/lib/Socket/dugong.js +484 -0
- package/lib/Socket/groups.d.ts +115 -0
- package/lib/Socket/groups.js +309 -304
- package/lib/Socket/index.d.ts +173 -0
- package/lib/Socket/index.js +10 -15
- package/lib/Socket/messages-recv.d.ts +161 -0
- package/lib/Socket/messages-recv.js +1054 -1107
- package/lib/Socket/messages-send.d.ts +149 -0
- package/lib/Socket/messages-send.js +447 -706
- package/lib/Socket/newsletter.d.ts +134 -0
- package/lib/Socket/newsletter.js +314 -199
- package/lib/Socket/registration.d.ts +267 -0
- package/lib/Socket/registration.js +166 -0
- package/lib/Socket/socket.d.ts +43 -0
- package/lib/Socket/socket.js +650 -777
- package/lib/Socket/usync.d.ts +36 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +10 -6
- package/lib/Store/make-cache-manager-store.d.ts +13 -0
- package/lib/Store/make-cache-manager-store.js +81 -73
- package/lib/Store/make-in-memory-store.d.ts +118 -0
- package/lib/Store/make-in-memory-store.js +423 -286
- package/lib/Store/make-ordered-dictionary.d.ts +13 -0
- package/lib/Store/make-ordered-dictionary.js +79 -77
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +26 -24
- package/lib/Types/Auth.d.ts +110 -0
- package/lib/Types/Auth.js +2 -3
- package/lib/Types/Call.d.ts +13 -0
- package/lib/Types/Call.js +2 -3
- package/lib/Types/Chat.d.ts +102 -0
- package/lib/Types/Chat.js +4 -9
- package/lib/Types/Contact.d.ts +19 -0
- package/lib/Types/Contact.js +2 -3
- package/lib/Types/Events.d.ts +157 -0
- package/lib/Types/Events.js +2 -3
- package/lib/Types/GroupMetadata.d.ts +55 -0
- package/lib/Types/GroupMetadata.js +2 -3
- package/lib/Types/Label.d.ts +35 -0
- package/lib/Types/Label.js +26 -24
- package/lib/Types/LabelAssociation.d.ts +29 -0
- package/lib/Types/LabelAssociation.js +8 -6
- package/lib/Types/Message.d.ts +273 -0
- package/lib/Types/Message.js +9 -12
- package/lib/Types/Newsletter.d.ts +103 -0
- package/lib/Types/Newsletter.js +38 -33
- package/lib/Types/Product.d.ts +78 -0
- package/lib/Types/Product.js +2 -3
- package/lib/Types/Signal.d.ts +57 -0
- package/lib/Types/Signal.js +2 -3
- package/lib/Types/Socket.d.ts +111 -0
- package/lib/Types/Socket.js +2 -4
- package/lib/Types/State.d.ts +27 -0
- package/lib/Types/State.js +2 -11
- package/lib/Types/USync.d.ts +25 -0
- package/lib/Types/USync.js +2 -3
- package/lib/Types/index.d.ts +57 -0
- package/lib/Types/index.js +41 -27
- package/lib/Utils/auth-utils.d.ts +18 -0
- package/lib/Utils/auth-utils.js +198 -211
- package/lib/Utils/baileys-event-stream.d.ts +16 -0
- package/lib/Utils/baileys-event-stream.js +61 -42
- package/lib/Utils/business.d.ts +22 -0
- package/lib/Utils/business.js +214 -213
- package/lib/Utils/chat-utils.d.ts +71 -0
- package/lib/Utils/chat-utils.js +687 -710
- package/lib/Utils/crypto.d.ts +41 -0
- package/lib/Utils/crypto.js +133 -112
- package/lib/Utils/decode-wa-message.d.ts +19 -0
- package/lib/Utils/decode-wa-message.js +183 -252
- package/lib/Utils/event-buffer.d.ts +35 -0
- package/lib/Utils/event-buffer.js +496 -510
- package/lib/Utils/generics.d.ts +92 -0
- package/lib/Utils/generics.js +387 -319
- package/lib/Utils/history.d.ts +15 -0
- package/lib/Utils/history.js +92 -83
- package/lib/Utils/index.d.ts +17 -0
- package/lib/Utils/index.js +33 -21
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.js +83 -71
- package/lib/Utils/logger.d.ts +4 -0
- package/lib/Utils/logger.js +7 -5
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +46 -40
- package/lib/Utils/make-mutex.d.ts +7 -0
- package/lib/Utils/make-mutex.js +41 -34
- package/lib/Utils/messages-media.d.ts +116 -0
- package/lib/Utils/messages-media.js +768 -550
- package/lib/Utils/messages.d.ts +77 -0
- package/lib/Utils/messages.js +263 -362
- package/lib/Utils/noise-handler.d.ts +21 -0
- package/lib/Utils/noise-handler.js +149 -138
- package/lib/Utils/process-message.d.ts +41 -0
- package/lib/Utils/process-message.js +303 -323
- package/lib/Utils/signal.d.ts +32 -0
- package/lib/Utils/signal.js +141 -149
- package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
- package/lib/Utils/use-multi-file-auth-state.js +103 -95
- package/lib/Utils/validate-connection.d.ts +11 -0
- package/lib/Utils/validate-connection.js +220 -183
- package/lib/WABinary/constants.d.ts +30 -0
- package/lib/WABinary/constants.js +35 -1298
- package/lib/WABinary/decode.d.ts +7 -0
- package/lib/WABinary/decode.js +249 -237
- package/lib/WABinary/encode.d.ts +3 -0
- package/lib/WABinary/encode.js +260 -213
- package/lib/WABinary/generic-utils.d.ts +17 -0
- package/lib/WABinary/generic-utils.js +65 -56
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +21 -7
- package/lib/WABinary/jid-utils.d.ts +31 -0
- package/lib/WABinary/jid-utils.js +58 -89
- package/lib/WABinary/types.d.ts +18 -0
- package/lib/WABinary/types.js +2 -3
- package/lib/WAM/BinaryInfo.d.ts +17 -0
- package/lib/WAM/BinaryInfo.js +12 -10
- package/lib/WAM/constants.d.ts +38 -0
- package/lib/WAM/constants.js +15348 -22851
- package/lib/WAM/encode.d.ts +3 -0
- package/lib/WAM/encode.js +136 -135
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +19 -5
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +30 -28
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -49
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +28 -27
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -36
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +20 -26
- package/lib/WAUSync/Protocols/index.d.ts +4 -0
- package/lib/WAUSync/Protocols/index.js +20 -6
- package/lib/WAUSync/USyncQuery.d.ts +28 -0
- package/lib/WAUSync/USyncQuery.js +85 -86
- package/lib/WAUSync/USyncUser.d.ts +12 -0
- package/lib/WAUSync/USyncUser.js +25 -23
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +19 -5
- package/lib/index.d.ts +12 -0
- package/lib/index.js +36 -24
- package/package.json +106 -98
- package/LICENSE +0 -21
- package/WAProto/WAProto.proto +0 -5311
- package/lib/KeyDB/BinarySearch.js +0 -20
- package/lib/KeyDB/KeyedDB.js +0 -167
- package/lib/KeyDB/index.js +0 -4
- package/lib/Signal/lid-mapping.js +0 -155
- package/lib/Socket/Client/types.js +0 -13
- package/lib/Socket/Client/websocket.js +0 -52
- package/lib/Socket/Client/websocket.js.bak +0 -53
- package/lib/Socket/communities.js +0 -413
- package/lib/Socket/mex.js +0 -45
- package/lib/Types/Bussines.js +0 -3
- package/lib/Types/Newsletter.js.bak +0 -33
- package/lib/Utils/browser-utils.js +0 -25
- package/lib/Utils/message-retry-manager.js +0 -113
- package/lib/Utils/messages.js.bak +0 -907
- package/lib/Utils/pre-key-manager.js +0 -85
package/lib/Utils/chat-utils.js
CHANGED
|
@@ -1,752 +1,729 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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 LabelAssociation_1 = require("../Types/LabelAssociation");
|
|
7
|
+
const WABinary_1 = require("../WABinary");
|
|
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 = (keydata) => {
|
|
13
|
+
const expanded = (0, crypto_1.hkdf)(keydata, 160, { info: 'WhatsApp Mutation Keys' });
|
|
14
|
+
return {
|
|
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)
|
|
20
|
+
};
|
|
20
21
|
};
|
|
21
|
-
//=======================================================//
|
|
22
22
|
const generateMac = (operation, data, keyId, key) => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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);
|
|
42
42
|
};
|
|
43
|
-
//=======================================================//
|
|
44
43
|
const to64BitNetworkOrder = (e) => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
const buff = Buffer.alloc(8);
|
|
45
|
+
buff.writeUint32BE(e, 4);
|
|
46
|
+
return buff;
|
|
48
47
|
};
|
|
49
|
-
//=======================================================//
|
|
50
48
|
const makeLtHashGenerator = ({ indexValueMap, hash }) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
49
|
+
indexValueMap = { ...indexValueMap };
|
|
50
|
+
const addBuffs = [];
|
|
51
|
+
const subBuffs = [];
|
|
52
|
+
return {
|
|
53
|
+
mix: ({ indexMac, valueMac, operation }) => {
|
|
54
|
+
const indexMacBase64 = Buffer.from(indexMac).toString('base64');
|
|
55
|
+
const prevOp = indexValueMap[indexMacBase64];
|
|
56
|
+
if (operation === WAProto_1.proto.SyncdMutation.SyncdOperation.REMOVE) {
|
|
57
|
+
if (!prevOp) {
|
|
58
|
+
throw new boom_1.Boom('tried remove, but no previous op', { data: { indexMac, valueMac } });
|
|
59
|
+
}
|
|
60
|
+
// remove from index value mac, since this mutation is erased
|
|
61
|
+
delete indexValueMap[indexMacBase64];
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
addBuffs.push(new Uint8Array(valueMac).buffer);
|
|
65
|
+
// add this index into the history map
|
|
66
|
+
indexValueMap[indexMacBase64] = { valueMac };
|
|
67
|
+
}
|
|
68
|
+
if (prevOp) {
|
|
69
|
+
subBuffs.push(new Uint8Array(prevOp.valueMac).buffer);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
finish: () => {
|
|
73
|
+
const hashArrayBuffer = new Uint8Array(hash).buffer;
|
|
74
|
+
const result = lt_hash_1.LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs);
|
|
75
|
+
const buffer = Buffer.from(result);
|
|
76
|
+
return {
|
|
77
|
+
hash: buffer,
|
|
78
|
+
indexValueMap
|
|
79
|
+
};
|
|
61
80
|
}
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
addBuffs.push(new Uint8Array(valueMac).buffer);
|
|
66
|
-
indexValueMap[indexMacBase64] = { valueMac };
|
|
67
|
-
}
|
|
68
|
-
if (prevOp) {
|
|
69
|
-
subBuffs.push(new Uint8Array(prevOp.valueMac).buffer);
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
finish: async () => {
|
|
73
|
-
const hashArrayBuffer = new Uint8Array(hash).buffer;
|
|
74
|
-
const result = await LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs);
|
|
75
|
-
const buffer = Buffer.from(result);
|
|
76
|
-
return {
|
|
77
|
-
hash: buffer,
|
|
78
|
-
indexValueMap
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
};
|
|
81
|
+
};
|
|
82
82
|
};
|
|
83
|
-
//=======================================================//
|
|
84
83
|
const generateSnapshotMac = (lthash, version, name, key) => {
|
|
85
|
-
|
|
86
|
-
|
|
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');
|
|
87
90
|
};
|
|
88
|
-
//=======================================================//
|
|
89
91
|
const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (!key) {
|
|
98
|
-
throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 });
|
|
99
|
-
}
|
|
100
|
-
const encKeyId = Buffer.from(myAppStateKeyId, "base64");
|
|
101
|
-
state = { ...state, indexValueMap: { ...state.indexValueMap } };
|
|
102
|
-
const indexBuffer = Buffer.from(JSON.stringify(index));
|
|
103
|
-
const dataProto = proto.SyncActionData.fromObject({
|
|
104
|
-
index: indexBuffer,
|
|
105
|
-
value: syncAction,
|
|
106
|
-
padding: new Uint8Array(0),
|
|
107
|
-
version: apiVersion
|
|
108
|
-
});
|
|
109
|
-
const encoded = proto.SyncActionData.encode(dataProto).finish();
|
|
110
|
-
const keyValue = await mutationKeys(key.keyData);
|
|
111
|
-
const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey);
|
|
112
|
-
const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey);
|
|
113
|
-
const indexMac = hmacSign(indexBuffer, keyValue.indexKey);
|
|
114
|
-
const generator = makeLtHashGenerator(state);
|
|
115
|
-
generator.mix({ indexMac, valueMac, operation });
|
|
116
|
-
Object.assign(state, await generator.finish());
|
|
117
|
-
state.version += 1;
|
|
118
|
-
const snapshotMac = generateSnapshotMac(state.hash, state.version, type, keyValue.snapshotMacKey);
|
|
119
|
-
const patch = {
|
|
120
|
-
patchMac: generatePatchMac(snapshotMac, [valueMac], state.version, type, keyValue.patchMacKey),
|
|
121
|
-
snapshotMac: snapshotMac,
|
|
122
|
-
keyId: { id: encKeyId },
|
|
123
|
-
mutations: [
|
|
124
|
-
{
|
|
125
|
-
operation: operation,
|
|
126
|
-
record: {
|
|
127
|
-
index: {
|
|
128
|
-
blob: indexMac
|
|
129
|
-
},
|
|
130
|
-
value: {
|
|
131
|
-
blob: Buffer.concat([encValue, valueMac])
|
|
132
|
-
},
|
|
133
|
-
keyId: { id: encKeyId }
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
]
|
|
137
|
-
};
|
|
138
|
-
const base64Index = indexMac.toString("base64");
|
|
139
|
-
state.indexValueMap[base64Index] = { valueMac };
|
|
140
|
-
return { patch, 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);
|
|
141
99
|
};
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const key = await getKey(record.keyId.id);
|
|
149
|
-
const content = Buffer.from(record.value.blob);
|
|
150
|
-
const encContent = content.slice(0, -32);
|
|
151
|
-
const ogValueMac = content.slice(-32);
|
|
152
|
-
if (validateMacs) {
|
|
153
|
-
const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey);
|
|
154
|
-
if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
|
|
155
|
-
throw new Boom("HMAC content verification failed");
|
|
156
|
-
}
|
|
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) => {
|
|
103
|
+
const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined;
|
|
104
|
+
if (!key) {
|
|
105
|
+
throw new boom_1.Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 });
|
|
157
106
|
}
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const indexStr = Buffer.from(syncAction.index).toString();
|
|
167
|
-
onMutation({ syncAction, index: JSON.parse(indexStr) });
|
|
168
|
-
ltGenerator.mix({
|
|
169
|
-
indexMac: record.index.blob,
|
|
170
|
-
valueMac: ogValueMac,
|
|
171
|
-
operation: operation
|
|
107
|
+
const encKeyId = Buffer.from(myAppStateKeyId, 'base64');
|
|
108
|
+
state = { ...state, indexValueMap: { ...state.indexValueMap } };
|
|
109
|
+
const indexBuffer = Buffer.from(JSON.stringify(index));
|
|
110
|
+
const dataProto = WAProto_1.proto.SyncActionData.fromObject({
|
|
111
|
+
index: indexBuffer,
|
|
112
|
+
value: syncAction,
|
|
113
|
+
padding: new Uint8Array(0),
|
|
114
|
+
version: apiVersion
|
|
172
115
|
});
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
116
|
+
const encoded = WAProto_1.proto.SyncActionData.encode(dataProto).finish();
|
|
117
|
+
const keyValue = mutationKeys(key.keyData);
|
|
118
|
+
const encValue = (0, crypto_1.aesEncrypt)(encoded, keyValue.valueEncryptionKey);
|
|
119
|
+
const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey);
|
|
120
|
+
const indexMac = (0, crypto_1.hmacSign)(indexBuffer, keyValue.indexKey);
|
|
121
|
+
// update LT hash
|
|
122
|
+
const generator = makeLtHashGenerator(state);
|
|
123
|
+
generator.mix({ indexMac, valueMac, operation });
|
|
124
|
+
Object.assign(state, generator.finish());
|
|
125
|
+
state.version += 1;
|
|
126
|
+
const snapshotMac = generateSnapshotMac(state.hash, state.version, type, keyValue.snapshotMacKey);
|
|
127
|
+
const patch = {
|
|
128
|
+
patchMac: generatePatchMac(snapshotMac, [valueMac], state.version, type, keyValue.patchMacKey),
|
|
129
|
+
snapshotMac: snapshotMac,
|
|
130
|
+
keyId: { id: encKeyId },
|
|
131
|
+
mutations: [
|
|
132
|
+
{
|
|
133
|
+
operation: operation,
|
|
134
|
+
record: {
|
|
135
|
+
index: {
|
|
136
|
+
blob: indexMac
|
|
137
|
+
},
|
|
138
|
+
value: {
|
|
139
|
+
blob: Buffer.concat([encValue, valueMac])
|
|
140
|
+
},
|
|
141
|
+
keyId: { id: encKeyId }
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
]
|
|
145
|
+
};
|
|
146
|
+
const base64Index = indexMac.toString('base64');
|
|
147
|
+
state.indexValueMap[base64Index] = { valueMac };
|
|
148
|
+
return { patch, state };
|
|
204
149
|
};
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
for (let { content } of patches) {
|
|
227
|
-
if (content) {
|
|
228
|
-
if (!Buffer.isBuffer(content)) {
|
|
229
|
-
content = Buffer.from(Object.values(content));
|
|
150
|
+
exports.encodeSyncdPatch = encodeSyncdPatch;
|
|
151
|
+
const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
|
|
152
|
+
const ltGenerator = makeLtHashGenerator(initialState);
|
|
153
|
+
// indexKey used to HMAC sign record.index.blob
|
|
154
|
+
// valueEncryptionKey used to AES-256-CBC encrypt record.value.blob[0:-32]
|
|
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
|
|
156
|
+
for (const msgMutation of msgMutations) {
|
|
157
|
+
// if it's a syncdmutation, get the operation property
|
|
158
|
+
// otherwise, if it's only a record -- it'll be a SET mutation
|
|
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);
|
|
165
|
+
if (validateMacs) {
|
|
166
|
+
const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey);
|
|
167
|
+
if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
|
|
168
|
+
throw new boom_1.Boom('HMAC content verification failed');
|
|
169
|
+
}
|
|
230
170
|
}
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
171
|
+
const result = (0, crypto_1.aesDecrypt)(encContent, key.valueEncryptionKey);
|
|
172
|
+
const syncAction = WAProto_1.proto.SyncActionData.decode(result);
|
|
173
|
+
if (validateMacs) {
|
|
174
|
+
const hmac = (0, crypto_1.hmacSign)(syncAction.index, key.indexKey);
|
|
175
|
+
if (Buffer.compare(hmac, record.index.blob) !== 0) {
|
|
176
|
+
throw new boom_1.Boom('HMAC index verification failed');
|
|
177
|
+
}
|
|
234
178
|
}
|
|
235
|
-
|
|
236
|
-
|
|
179
|
+
const indexStr = Buffer.from(syncAction.index).toString();
|
|
180
|
+
onMutation({ syncAction, index: JSON.parse(indexStr) });
|
|
181
|
+
ltGenerator.mix({
|
|
182
|
+
indexMac: record.index.blob,
|
|
183
|
+
valueMac: ogValueMac,
|
|
184
|
+
operation: operation
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return ltGenerator.finish();
|
|
188
|
+
async function getKey(keyId) {
|
|
189
|
+
const base64Key = Buffer.from(keyId).toString('base64');
|
|
190
|
+
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
191
|
+
if (!keyEnc) {
|
|
192
|
+
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`, { statusCode: 404, data: { msgMutations } });
|
|
193
|
+
}
|
|
194
|
+
return mutationKeys(keyEnc.keyData);
|
|
237
195
|
}
|
|
238
|
-
final[name] = { patches: syncds, hasMorePatches, snapshot };
|
|
239
|
-
}));
|
|
240
|
-
return final;
|
|
241
|
-
};
|
|
242
|
-
//=======================================================//
|
|
243
|
-
export const downloadExternalBlob = async (blob, options) => {
|
|
244
|
-
const stream = await downloadContentFromMessage(blob, "md-app-state", { options });
|
|
245
|
-
const bufferArray = [];
|
|
246
|
-
for await (const chunk of stream) {
|
|
247
|
-
bufferArray.push(chunk);
|
|
248
|
-
}
|
|
249
|
-
return Buffer.concat(bufferArray);
|
|
250
|
-
};
|
|
251
|
-
//=======================================================//
|
|
252
|
-
export const downloadExternalPatch = async (blob, options) => {
|
|
253
|
-
const buffer = await downloadExternalBlob(blob, options);
|
|
254
|
-
const syncData = proto.SyncdMutations.decode(buffer);
|
|
255
|
-
return syncData;
|
|
256
|
-
};
|
|
257
|
-
//=======================================================//
|
|
258
|
-
export const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVersionNumber, validateMacs = true) => {
|
|
259
|
-
const newState = newLTHashState();
|
|
260
|
-
newState.version = toNumber(snapshot.version.version);
|
|
261
|
-
const mutationMap = {};
|
|
262
|
-
const areMutationsRequired = typeof minimumVersionNumber === "undefined" || newState.version > minimumVersionNumber;
|
|
263
|
-
const { hash, indexValueMap } = await decodeSyncdMutations(snapshot.records, newState, getAppStateSyncKey, areMutationsRequired
|
|
264
|
-
? mutation => {
|
|
265
|
-
const index = mutation.syncAction.index?.toString();
|
|
266
|
-
mutationMap[index] = mutation;
|
|
267
|
-
}
|
|
268
|
-
: () => { }, validateMacs);
|
|
269
|
-
newState.hash = hash;
|
|
270
|
-
newState.indexValueMap = indexValueMap;
|
|
271
|
-
if (validateMacs) {
|
|
272
|
-
const base64Key = Buffer.from(snapshot.keyId.id).toString("base64");
|
|
273
|
-
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
274
|
-
if (!keyEnc) {
|
|
275
|
-
throw new Boom(`failed to find key "${base64Key}" to decode mutation`);
|
|
276
|
-
}
|
|
277
|
-
const result = await mutationKeys(keyEnc.keyData);
|
|
278
|
-
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
|
|
279
|
-
if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
|
|
280
|
-
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
return {
|
|
284
|
-
state: newState,
|
|
285
|
-
mutationMap
|
|
286
|
-
};
|
|
287
196
|
};
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
const newState = {
|
|
291
|
-
...initial,
|
|
292
|
-
indexValueMap: { ...initial.indexValueMap }
|
|
293
|
-
};
|
|
294
|
-
const mutationMap = {};
|
|
295
|
-
for (const syncd of syncds) {
|
|
296
|
-
const { version, keyId, snapshotMac } = syncd;
|
|
297
|
-
if (syncd.externalMutations) {
|
|
298
|
-
logger?.trace({ name, version }, "downloading external patch");
|
|
299
|
-
const ref = await downloadExternalPatch(syncd.externalMutations, options);
|
|
300
|
-
logger?.debug({ name, version, mutations: ref.mutations.length }, "downloaded external patch");
|
|
301
|
-
syncd.mutations?.push(...ref.mutations);
|
|
302
|
-
}
|
|
303
|
-
const patchVersion = toNumber(version.version);
|
|
304
|
-
newState.version = patchVersion;
|
|
305
|
-
const shouldMutate = typeof minimumVersionNumber === "undefined" || patchVersion > minimumVersionNumber;
|
|
306
|
-
const decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
|
|
307
|
-
? mutation => {
|
|
308
|
-
const index = mutation.syncAction.index?.toString();
|
|
309
|
-
mutationMap[index] = mutation;
|
|
310
|
-
}
|
|
311
|
-
: () => { }, true);
|
|
312
|
-
newState.hash = decodeResult.hash;
|
|
313
|
-
newState.indexValueMap = decodeResult.indexValueMap;
|
|
197
|
+
exports.decodeSyncdMutations = decodeSyncdMutations;
|
|
198
|
+
const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
|
|
314
199
|
if (validateMacs) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
320
|
-
const result = await mutationKeys(keyEnc.keyData);
|
|
321
|
-
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
|
|
322
|
-
if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
|
|
323
|
-
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
syncd.mutations = [];
|
|
327
|
-
}
|
|
328
|
-
return { state: newState, mutationMap };
|
|
329
|
-
};
|
|
330
|
-
//=======================================================//
|
|
331
|
-
export const chatModificationToAppPatch = (mod, jid) => {
|
|
332
|
-
const OP = proto.SyncdMutation.SyncdOperation;
|
|
333
|
-
const getMessageRange = (lastMessages) => {
|
|
334
|
-
let messageRange;
|
|
335
|
-
if (Array.isArray(lastMessages)) {
|
|
336
|
-
const lastMsg = lastMessages[lastMessages.length - 1];
|
|
337
|
-
messageRange = {
|
|
338
|
-
lastMessageTimestamp: lastMsg?.messageTimestamp,
|
|
339
|
-
messages: lastMessages?.length
|
|
340
|
-
? lastMessages.map(m => {
|
|
341
|
-
if (!m.key?.id || !m.key?.remoteJid) {
|
|
342
|
-
throw new Boom("Incomplete key", { statusCode: 400, data: m });
|
|
343
|
-
}
|
|
344
|
-
if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
|
|
345
|
-
throw new Boom("Expected not from me message to have participant", { statusCode: 400, data: m });
|
|
346
|
-
}
|
|
347
|
-
if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
|
|
348
|
-
throw new Boom("Missing timestamp in last message list", { statusCode: 400, data: m });
|
|
349
|
-
}
|
|
350
|
-
if (m.key.participant) {
|
|
351
|
-
m.key.participant = jidNormalizedUser(m.key.participant);
|
|
352
|
-
}
|
|
353
|
-
return m;
|
|
354
|
-
})
|
|
355
|
-
: undefined
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
else {
|
|
359
|
-
messageRange = lastMessages;
|
|
360
|
-
}
|
|
361
|
-
return messageRange;
|
|
362
|
-
};
|
|
363
|
-
let patch;
|
|
364
|
-
if ("mute" in mod) {
|
|
365
|
-
patch = {
|
|
366
|
-
syncAction: {
|
|
367
|
-
muteAction: {
|
|
368
|
-
muted: !!mod.mute,
|
|
369
|
-
muteEndTimestamp: mod.mute || undefined
|
|
200
|
+
const base64Key = Buffer.from(msg.keyId.id).toString('base64');
|
|
201
|
+
const mainKeyObj = await getAppStateSyncKey(base64Key);
|
|
202
|
+
if (!mainKeyObj) {
|
|
203
|
+
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } });
|
|
370
204
|
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
};
|
|
377
|
-
}
|
|
378
|
-
else if ("archive" in mod) {
|
|
379
|
-
patch = {
|
|
380
|
-
syncAction: {
|
|
381
|
-
archiveChatAction: {
|
|
382
|
-
archived: !!mod.archive,
|
|
383
|
-
messageRange: getMessageRange(mod.lastMessages)
|
|
384
|
-
}
|
|
385
|
-
},
|
|
386
|
-
index: ["archive", jid],
|
|
387
|
-
type: "regular_low",
|
|
388
|
-
apiVersion: 3,
|
|
389
|
-
operation: OP.SET
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
else if ("markRead" in mod) {
|
|
393
|
-
patch = {
|
|
394
|
-
syncAction: {
|
|
395
|
-
markChatAsReadAction: {
|
|
396
|
-
read: mod.markRead,
|
|
397
|
-
messageRange: getMessageRange(mod.lastMessages)
|
|
205
|
+
const mainKey = mutationKeys(mainKeyObj.keyData);
|
|
206
|
+
const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32));
|
|
207
|
+
const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, (0, generics_1.toNumber)(msg.version.version), name, mainKey.patchMacKey);
|
|
208
|
+
if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
|
|
209
|
+
throw new boom_1.Boom('Invalid patch mac');
|
|
398
210
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
211
|
+
}
|
|
212
|
+
const result = await (0, exports.decodeSyncdMutations)(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs);
|
|
213
|
+
return result;
|
|
214
|
+
};
|
|
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');
|
|
219
|
+
const final = {};
|
|
220
|
+
await Promise.all(collectionNodes.map(async (collectionNode) => {
|
|
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');
|
|
224
|
+
const syncds = [];
|
|
225
|
+
const name = collectionNode.attrs.name;
|
|
226
|
+
const hasMorePatches = collectionNode.attrs.has_more_patches === 'true';
|
|
227
|
+
let snapshot = undefined;
|
|
228
|
+
if (snapshotNode && !!snapshotNode.content) {
|
|
229
|
+
if (!Buffer.isBuffer(snapshotNode)) {
|
|
230
|
+
snapshotNode.content = Buffer.from(Object.values(snapshotNode.content));
|
|
231
|
+
}
|
|
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);
|
|
413
235
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
messageRange: getMessageRange(mod.lastMessages)
|
|
236
|
+
for (let { content } of patches) {
|
|
237
|
+
if (content) {
|
|
238
|
+
if (!Buffer.isBuffer(content)) {
|
|
239
|
+
content = Buffer.from(Object.values(content));
|
|
240
|
+
}
|
|
241
|
+
const syncd = WAProto_1.proto.SyncdPatch.decode(content);
|
|
242
|
+
if (!syncd.version) {
|
|
243
|
+
syncd.version = { version: +collectionNode.attrs.version + 1 };
|
|
244
|
+
}
|
|
245
|
+
syncds.push(syncd);
|
|
246
|
+
}
|
|
426
247
|
}
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
248
|
+
final[name] = { patches: syncds, hasMorePatches, snapshot };
|
|
249
|
+
}));
|
|
250
|
+
return final;
|
|
251
|
+
};
|
|
252
|
+
exports.extractSyncdPatches = extractSyncdPatches;
|
|
253
|
+
const downloadExternalBlob = async (blob, options) => {
|
|
254
|
+
const stream = await (0, messages_media_1.downloadContentFromMessage)(blob, 'md-app-state', { options });
|
|
255
|
+
const bufferArray = [];
|
|
256
|
+
for await (const chunk of stream) {
|
|
257
|
+
bufferArray.push(chunk);
|
|
258
|
+
}
|
|
259
|
+
return Buffer.concat(bufferArray);
|
|
260
|
+
};
|
|
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);
|
|
265
|
+
return syncData;
|
|
266
|
+
};
|
|
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);
|
|
271
|
+
const mutationMap = {};
|
|
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();
|
|
278
|
+
mutationMap[index] = mutation;
|
|
439
279
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
patch = {
|
|
449
|
-
syncAction: {
|
|
450
|
-
contactAction: mod.contact || {}
|
|
451
|
-
},
|
|
452
|
-
index: ["contact", jid],
|
|
453
|
-
type: "critical_unblock_low",
|
|
454
|
-
apiVersion: 2,
|
|
455
|
-
operation: mod.contact ? OP.SET : OP.REMOVE
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
else if ("disableLinkPreviews" in mod) {
|
|
459
|
-
patch = {
|
|
460
|
-
syncAction: {
|
|
461
|
-
privacySettingDisableLinkPreviewsAction: mod.disableLinkPreviews || {}
|
|
462
|
-
},
|
|
463
|
-
index: ["setting_disableLinkPreviews"],
|
|
464
|
-
type: "regular",
|
|
465
|
-
apiVersion: 8,
|
|
466
|
-
operation: OP.SET
|
|
467
|
-
};
|
|
468
|
-
}
|
|
469
|
-
else if ("star" in mod) {
|
|
470
|
-
const key = mod.star.messages[0];
|
|
471
|
-
patch = {
|
|
472
|
-
syncAction: {
|
|
473
|
-
starAction: {
|
|
474
|
-
starred: !!mod.star.star
|
|
280
|
+
: () => { }, validateMacs);
|
|
281
|
+
newState.hash = hash;
|
|
282
|
+
newState.indexValueMap = indexValueMap;
|
|
283
|
+
if (validateMacs) {
|
|
284
|
+
const base64Key = Buffer.from(snapshot.keyId.id).toString('base64');
|
|
285
|
+
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
286
|
+
if (!keyEnc) {
|
|
287
|
+
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`);
|
|
475
288
|
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
operation: OP.SET
|
|
481
|
-
};
|
|
482
|
-
}
|
|
483
|
-
else if ("delete" in mod) {
|
|
484
|
-
patch = {
|
|
485
|
-
syncAction: {
|
|
486
|
-
deleteChatAction: {
|
|
487
|
-
messageRange: getMessageRange(mod.lastMessages)
|
|
289
|
+
const result = mutationKeys(keyEnc.keyData);
|
|
290
|
+
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
|
|
291
|
+
if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
|
|
292
|
+
throw new boom_1.Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`);
|
|
488
293
|
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
operation: OP.SET
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
state: newState,
|
|
297
|
+
mutationMap
|
|
494
298
|
};
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
}
|
|
502
|
-
},
|
|
503
|
-
index: ["setting_pushName"],
|
|
504
|
-
type: "critical_block",
|
|
505
|
-
apiVersion: 1,
|
|
506
|
-
operation: OP.SET
|
|
299
|
+
};
|
|
300
|
+
exports.decodeSyncdSnapshot = decodeSyncdSnapshot;
|
|
301
|
+
const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options, minimumVersionNumber, logger, validateMacs = true) => {
|
|
302
|
+
var _a;
|
|
303
|
+
const newState = {
|
|
304
|
+
...initial,
|
|
305
|
+
indexValueMap: { ...initial.indexValueMap }
|
|
507
306
|
};
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
shortcut: mod.quickReply.shortcut || ""
|
|
307
|
+
const mutationMap = {};
|
|
308
|
+
for (let i = 0; i < syncds.length; i++) {
|
|
309
|
+
const syncd = syncds[i];
|
|
310
|
+
const { version, keyId, snapshotMac } = syncd;
|
|
311
|
+
if (syncd.externalMutations) {
|
|
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);
|
|
518
316
|
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
317
|
+
const patchVersion = (0, generics_1.toNumber)(version.version);
|
|
318
|
+
newState.version = patchVersion;
|
|
319
|
+
const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber;
|
|
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);
|
|
327
|
+
newState.hash = decodeResult.hash;
|
|
328
|
+
newState.indexValueMap = decodeResult.indexValueMap;
|
|
329
|
+
if (validateMacs) {
|
|
330
|
+
const base64Key = Buffer.from(keyId.id).toString('base64');
|
|
331
|
+
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
332
|
+
if (!keyEnc) {
|
|
333
|
+
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`);
|
|
334
|
+
}
|
|
335
|
+
const result = mutationKeys(keyEnc.keyData);
|
|
336
|
+
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
|
|
337
|
+
if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
|
|
338
|
+
throw new boom_1.Boom(`failed to verify LTHash at ${newState.version} of ${name}`);
|
|
339
|
+
}
|
|
534
340
|
}
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
341
|
+
// clear memory used up by the mutations
|
|
342
|
+
syncd.mutations = [];
|
|
343
|
+
}
|
|
344
|
+
return { state: newState, mutationMap };
|
|
345
|
+
};
|
|
346
|
+
exports.decodePatches = decodePatches;
|
|
347
|
+
const chatModificationToAppPatch = (mod, jid) => {
|
|
348
|
+
const OP = WAProto_1.proto.SyncdMutation.SyncdOperation;
|
|
349
|
+
const getMessageRange = (lastMessages) => {
|
|
350
|
+
let messageRange;
|
|
351
|
+
if (Array.isArray(lastMessages)) {
|
|
352
|
+
const lastMsg = lastMessages[lastMessages.length - 1];
|
|
353
|
+
messageRange = {
|
|
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
|
|
371
|
+
};
|
|
547
372
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
type: "regular",
|
|
551
|
-
apiVersion: 3,
|
|
552
|
-
operation: OP.SET
|
|
553
|
-
};
|
|
554
|
-
}
|
|
555
|
-
else if ("removeChatLabel" in mod) {
|
|
556
|
-
patch = {
|
|
557
|
-
syncAction: {
|
|
558
|
-
labelAssociationAction: {
|
|
559
|
-
labeled: false
|
|
373
|
+
else {
|
|
374
|
+
messageRange = lastMessages;
|
|
560
375
|
}
|
|
561
|
-
|
|
562
|
-
index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
|
|
563
|
-
type: "regular",
|
|
564
|
-
apiVersion: 3,
|
|
565
|
-
operation: OP.SET
|
|
376
|
+
return messageRange;
|
|
566
377
|
};
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
378
|
+
let patch;
|
|
379
|
+
if ('mute' in mod) {
|
|
380
|
+
patch = {
|
|
381
|
+
syncAction: {
|
|
382
|
+
muteAction: {
|
|
383
|
+
muted: !!mod.mute,
|
|
384
|
+
muteEndTimestamp: mod.mute || undefined
|
|
385
|
+
}
|
|
386
|
+
},
|
|
387
|
+
index: ['mute', jid],
|
|
388
|
+
type: 'regular_high',
|
|
389
|
+
apiVersion: 2,
|
|
390
|
+
operation: OP.SET
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
else if ('archive' in mod) {
|
|
394
|
+
patch = {
|
|
395
|
+
syncAction: {
|
|
396
|
+
archiveChatAction: {
|
|
397
|
+
archived: !!mod.archive,
|
|
398
|
+
messageRange: getMessageRange(mod.lastMessages)
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
index: ['archive', jid],
|
|
402
|
+
type: 'regular_low',
|
|
403
|
+
apiVersion: 3,
|
|
404
|
+
operation: OP.SET
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
else if ('markRead' in mod) {
|
|
408
|
+
patch = {
|
|
409
|
+
syncAction: {
|
|
410
|
+
markChatAsReadAction: {
|
|
411
|
+
read: mod.markRead,
|
|
412
|
+
messageRange: getMessageRange(mod.lastMessages)
|
|
413
|
+
}
|
|
414
|
+
},
|
|
415
|
+
index: ['markChatAsRead', jid],
|
|
416
|
+
type: 'regular_low',
|
|
417
|
+
apiVersion: 3,
|
|
418
|
+
operation: OP.SET
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
else if ('clear' in mod) {
|
|
422
|
+
if (mod.clear === 'all') {
|
|
423
|
+
throw new boom_1.Boom('not supported');
|
|
573
424
|
}
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
425
|
+
else {
|
|
426
|
+
const key = mod.clear.messages[0];
|
|
427
|
+
patch = {
|
|
428
|
+
syncAction: {
|
|
429
|
+
deleteMessageForMeAction: {
|
|
430
|
+
deleteMedia: false,
|
|
431
|
+
messageTimestamp: key.timestamp
|
|
432
|
+
}
|
|
433
|
+
},
|
|
434
|
+
index: ['deleteMessageForMe', jid, key.id, key.fromMe ? '1' : '0', '0'],
|
|
435
|
+
type: 'regular_high',
|
|
436
|
+
apiVersion: 3,
|
|
437
|
+
operation: OP.SET
|
|
438
|
+
};
|
|
586
439
|
}
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
440
|
+
}
|
|
441
|
+
else if ('pin' in mod) {
|
|
442
|
+
patch = {
|
|
443
|
+
syncAction: {
|
|
444
|
+
pinAction: {
|
|
445
|
+
pinned: !!mod.pin
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
index: ['pin_v1', jid],
|
|
449
|
+
type: 'regular_low',
|
|
450
|
+
apiVersion: 5,
|
|
451
|
+
operation: OP.SET
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
else if ('star' in mod) {
|
|
455
|
+
const key = mod.star.messages[0];
|
|
456
|
+
patch = {
|
|
457
|
+
syncAction: {
|
|
458
|
+
starAction: {
|
|
459
|
+
starred: !!mod.star.star
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
index: ['star', jid, key.id, key.fromMe ? '1' : '0', '0'],
|
|
463
|
+
type: 'regular_low',
|
|
464
|
+
apiVersion: 2,
|
|
465
|
+
operation: OP.SET
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
else if ('delete' in mod) {
|
|
469
|
+
patch = {
|
|
470
|
+
syncAction: {
|
|
471
|
+
deleteChatAction: {
|
|
472
|
+
messageRange: getMessageRange(mod.lastMessages),
|
|
473
|
+
}
|
|
474
|
+
},
|
|
475
|
+
index: ['deleteChat', jid, '1'],
|
|
476
|
+
type: 'regular_high',
|
|
477
|
+
apiVersion: 6,
|
|
478
|
+
operation: OP.SET
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
else if ('pushNameSetting' in mod) {
|
|
482
|
+
patch = {
|
|
483
|
+
syncAction: {
|
|
484
|
+
pushNameSetting: {
|
|
485
|
+
name: mod.pushNameSetting
|
|
486
|
+
}
|
|
487
|
+
},
|
|
488
|
+
index: ['setting_pushName'],
|
|
489
|
+
type: 'critical_block',
|
|
490
|
+
apiVersion: 1,
|
|
491
|
+
operation: OP.SET,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
else if ('addChatLabel' in mod) {
|
|
495
|
+
patch = {
|
|
496
|
+
syncAction: {
|
|
497
|
+
labelAssociationAction: {
|
|
498
|
+
labeled: true,
|
|
499
|
+
}
|
|
500
|
+
},
|
|
501
|
+
index: [LabelAssociation_1.LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
|
|
502
|
+
type: 'regular',
|
|
503
|
+
apiVersion: 3,
|
|
504
|
+
operation: OP.SET,
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
else if ('removeChatLabel' in mod) {
|
|
508
|
+
patch = {
|
|
509
|
+
syncAction: {
|
|
510
|
+
labelAssociationAction: {
|
|
511
|
+
labeled: false,
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
index: [LabelAssociation_1.LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
|
|
515
|
+
type: 'regular',
|
|
516
|
+
apiVersion: 3,
|
|
517
|
+
operation: OP.SET,
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
else if ('addMessageLabel' in mod) {
|
|
521
|
+
patch = {
|
|
522
|
+
syncAction: {
|
|
523
|
+
labelAssociationAction: {
|
|
524
|
+
labeled: true,
|
|
525
|
+
}
|
|
526
|
+
},
|
|
527
|
+
index: [
|
|
528
|
+
LabelAssociation_1.LabelAssociationType.Message,
|
|
529
|
+
mod.addMessageLabel.labelId,
|
|
530
|
+
jid,
|
|
531
|
+
mod.addMessageLabel.messageId,
|
|
532
|
+
'0',
|
|
533
|
+
'0'
|
|
534
|
+
],
|
|
535
|
+
type: 'regular',
|
|
536
|
+
apiVersion: 3,
|
|
537
|
+
operation: OP.SET,
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
else if ('removeMessageLabel' in mod) {
|
|
541
|
+
patch = {
|
|
542
|
+
syncAction: {
|
|
543
|
+
labelAssociationAction: {
|
|
544
|
+
labeled: false,
|
|
545
|
+
}
|
|
546
|
+
},
|
|
547
|
+
index: [
|
|
548
|
+
LabelAssociation_1.LabelAssociationType.Message,
|
|
549
|
+
mod.removeMessageLabel.labelId,
|
|
550
|
+
jid,
|
|
551
|
+
mod.removeMessageLabel.messageId,
|
|
552
|
+
'0',
|
|
553
|
+
'0'
|
|
554
|
+
],
|
|
555
|
+
type: 'regular',
|
|
556
|
+
apiVersion: 3,
|
|
557
|
+
operation: OP.SET,
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
else {
|
|
561
|
+
throw new boom_1.Boom('not supported');
|
|
562
|
+
}
|
|
563
|
+
patch.syncAction.timestamp = Date.now();
|
|
564
|
+
return patch;
|
|
606
565
|
};
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
566
|
+
exports.chatModificationToAppPatch = chatModificationToAppPatch;
|
|
567
|
+
const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
568
|
+
var _a, _b, _c, _d;
|
|
569
|
+
const isInitialSync = !!initialSyncOpts;
|
|
570
|
+
const accountSettings = initialSyncOpts === null || initialSyncOpts === void 0 ? void 0 : initialSyncOpts.accountSettings;
|
|
571
|
+
logger === null || logger === void 0 ? void 0 : logger.trace({ syncAction, initialSync: !!initialSyncOpts }, 'processing sync action');
|
|
572
|
+
const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction;
|
|
573
|
+
if (action === null || action === void 0 ? void 0 : action.muteAction) {
|
|
574
|
+
ev.emit('chats.update', [
|
|
575
|
+
{
|
|
576
|
+
id,
|
|
577
|
+
muteEndTime: ((_a = action.muteAction) === null || _a === void 0 ? void 0 : _a.muted)
|
|
578
|
+
? (0, generics_1.toNumber)(action.muteAction.muteEndTimestamp)
|
|
579
|
+
: null,
|
|
580
|
+
conditional: getChatUpdateConditional(id, undefined)
|
|
581
|
+
}
|
|
582
|
+
]);
|
|
583
|
+
}
|
|
584
|
+
else if ((action === null || action === void 0 ? void 0 : action.archiveChatAction) || type === 'archive' || type === 'unarchive') {
|
|
585
|
+
// okay so we've to do some annoying computation here
|
|
586
|
+
// when we're initially syncing the app state
|
|
587
|
+
// there are a few cases we need to handle
|
|
588
|
+
// 1. if the account unarchiveChats setting is true
|
|
589
|
+
// a. if the chat is archived, and no further messages have been received -- simple, keep archived
|
|
590
|
+
// b. if the chat was archived, and the user received messages from the other person afterwards
|
|
591
|
+
// then the chat should be marked unarchved --
|
|
592
|
+
// we compare the timestamp of latest message from the other person to determine this
|
|
593
|
+
// 2. if the account unarchiveChats setting is false -- then it doesn't matter,
|
|
594
|
+
// it'll always take an app state action to mark in unarchived -- which we'll get anyway
|
|
595
|
+
const archiveAction = action === null || action === void 0 ? void 0 : action.archiveChatAction;
|
|
596
|
+
const isArchived = archiveAction
|
|
597
|
+
? archiveAction.archived
|
|
598
|
+
: type === 'archive';
|
|
599
|
+
// // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
|
|
600
|
+
// // this only applies for the initial sync
|
|
601
|
+
// if(isInitialSync && !isArchived) {
|
|
602
|
+
// isArchived = false
|
|
603
|
+
// }
|
|
604
|
+
const msgRange = !(accountSettings === null || accountSettings === void 0 ? void 0 : accountSettings.unarchiveChats) ? undefined : archiveAction === null || archiveAction === void 0 ? void 0 : archiveAction.messageRange;
|
|
605
|
+
// logger?.debug({ chat: id, syncAction }, 'message range archive')
|
|
606
|
+
ev.emit('chats.update', [{
|
|
607
|
+
id,
|
|
608
|
+
archived: isArchived,
|
|
609
|
+
conditional: getChatUpdateConditional(id, msgRange)
|
|
610
|
+
}]);
|
|
611
|
+
}
|
|
612
|
+
else if (action === null || action === void 0 ? void 0 : action.markChatAsReadAction) {
|
|
613
|
+
const markReadAction = action.markChatAsReadAction;
|
|
614
|
+
// basically we don't need to fire an "read" update if the chat is being marked as read
|
|
615
|
+
// because the chat is read by default
|
|
616
|
+
// this only applies for the initial sync
|
|
617
|
+
const isNullUpdate = isInitialSync && markReadAction.read;
|
|
618
|
+
ev.emit('chats.update', [{
|
|
619
|
+
id,
|
|
620
|
+
unreadCount: isNullUpdate ? null : !!(markReadAction === null || markReadAction === void 0 ? void 0 : markReadAction.read) ? 0 : -1,
|
|
621
|
+
conditional: getChatUpdateConditional(id, markReadAction === null || markReadAction === void 0 ? void 0 : markReadAction.messageRange)
|
|
622
|
+
}]);
|
|
623
|
+
}
|
|
624
|
+
else if ((action === null || action === void 0 ? void 0 : action.deleteMessageForMeAction) || type === 'deleteMessageForMe') {
|
|
625
|
+
ev.emit('messages.delete', {
|
|
626
|
+
keys: [
|
|
627
|
+
{
|
|
628
|
+
remoteJid: id,
|
|
629
|
+
id: msgId,
|
|
630
|
+
fromMe: fromMe === '1'
|
|
631
|
+
}
|
|
632
|
+
]
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
else if (action === null || action === void 0 ? void 0 : action.contactAction) {
|
|
636
|
+
ev.emit('contacts.upsert', [{
|
|
637
|
+
id: id,
|
|
638
|
+
name: action.contactAction.fullName,
|
|
639
|
+
lid: action.contactAction.lidJid || undefined,
|
|
640
|
+
jid: (0, WABinary_1.isJidUser)(id) ? id : undefined
|
|
641
|
+
}]);
|
|
642
|
+
}
|
|
643
|
+
else if (action === null || action === void 0 ? void 0 : action.pushNameSetting) {
|
|
644
|
+
const name = (_b = action === null || action === void 0 ? void 0 : action.pushNameSetting) === null || _b === void 0 ? void 0 : _b.name;
|
|
645
|
+
if (name && (me === null || me === void 0 ? void 0 : me.name) !== name) {
|
|
646
|
+
ev.emit('creds.update', { me: { ...me, name } });
|
|
652
647
|
}
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
const name = action?.pushNameSetting?.name;
|
|
668
|
-
if (name && me?.name !== name) {
|
|
669
|
-
ev.emit("creds.update", { me: { ...me, name } });
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
else if (action?.pinAction) {
|
|
673
|
-
ev.emit("chats.update", [
|
|
674
|
-
{
|
|
675
|
-
id,
|
|
676
|
-
pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
|
|
677
|
-
conditional: getChatUpdateConditional(id, undefined)
|
|
678
|
-
}
|
|
679
|
-
]);
|
|
680
|
-
}
|
|
681
|
-
else if (action?.unarchiveChatsSetting) {
|
|
682
|
-
const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats;
|
|
683
|
-
ev.emit("creds.update", { accountSettings: { unarchiveChats } });
|
|
684
|
-
logger?.info(`archive setting updated => "${action.unarchiveChatsSetting.unarchiveChats}"`);
|
|
685
|
-
if (accountSettings) {
|
|
686
|
-
accountSettings.unarchiveChats = unarchiveChats;
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
else if (action?.starAction || type === "star") {
|
|
690
|
-
let starred = action?.starAction?.starred;
|
|
691
|
-
if (typeof starred !== "boolean") {
|
|
692
|
-
starred = syncAction.index[syncAction.index.length - 1] === "1";
|
|
693
|
-
}
|
|
694
|
-
ev.emit("messages.update", [
|
|
695
|
-
{
|
|
696
|
-
key: { remoteJid: id, id: msgId, fromMe: fromMe === "1" },
|
|
697
|
-
update: { starred }
|
|
698
|
-
}
|
|
699
|
-
]);
|
|
700
|
-
}
|
|
701
|
-
else if (action?.deleteChatAction || type === "deleteChat") {
|
|
702
|
-
if (!isInitialSync) {
|
|
703
|
-
ev.emit("chats.delete", [id]);
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
else if (action?.labelEditAction) {
|
|
707
|
-
const { name, color, deleted, predefinedId } = action.labelEditAction;
|
|
708
|
-
ev.emit("labels.edit", {
|
|
709
|
-
id: id,
|
|
710
|
-
name: name,
|
|
711
|
-
color: color,
|
|
712
|
-
deleted: deleted,
|
|
713
|
-
predefinedId: predefinedId ? String(predefinedId) : undefined
|
|
714
|
-
});
|
|
715
|
-
}
|
|
716
|
-
else if (action?.labelAssociationAction) {
|
|
717
|
-
ev.emit("labels.association", {
|
|
718
|
-
type: action.labelAssociationAction.labeled ? "add" : "remove",
|
|
719
|
-
association: type === LabelAssociationType.Chat
|
|
720
|
-
? {
|
|
721
|
-
type: LabelAssociationType.Chat,
|
|
722
|
-
chatId: syncAction.index[2],
|
|
723
|
-
labelId: syncAction.index[1]
|
|
648
|
+
}
|
|
649
|
+
else if (action === null || action === void 0 ? void 0 : action.pinAction) {
|
|
650
|
+
ev.emit('chats.update', [{
|
|
651
|
+
id,
|
|
652
|
+
pinned: ((_c = action.pinAction) === null || _c === void 0 ? void 0 : _c.pinned) ? (0, generics_1.toNumber)(action.timestamp) : null,
|
|
653
|
+
conditional: getChatUpdateConditional(id, undefined)
|
|
654
|
+
}]);
|
|
655
|
+
}
|
|
656
|
+
else if (action === null || action === void 0 ? void 0 : action.unarchiveChatsSetting) {
|
|
657
|
+
const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats;
|
|
658
|
+
ev.emit('creds.update', { accountSettings: { unarchiveChats } });
|
|
659
|
+
logger === null || logger === void 0 ? void 0 : logger.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`);
|
|
660
|
+
if (accountSettings) {
|
|
661
|
+
accountSettings.unarchiveChats = unarchiveChats;
|
|
724
662
|
}
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
663
|
+
}
|
|
664
|
+
else if ((action === null || action === void 0 ? void 0 : action.starAction) || type === 'star') {
|
|
665
|
+
let starred = (_d = action === null || action === void 0 ? void 0 : action.starAction) === null || _d === void 0 ? void 0 : _d.starred;
|
|
666
|
+
if (typeof starred !== 'boolean') {
|
|
667
|
+
starred = syncAction.index[syncAction.index.length - 1] === '1';
|
|
730
668
|
}
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true;
|
|
669
|
+
ev.emit('messages.update', [
|
|
670
|
+
{
|
|
671
|
+
key: { remoteJid: id, id: msgId, fromMe: fromMe === '1' },
|
|
672
|
+
update: { starred }
|
|
673
|
+
}
|
|
674
|
+
]);
|
|
675
|
+
}
|
|
676
|
+
else if ((action === null || action === void 0 ? void 0 : action.deleteChatAction) || type === 'deleteChat') {
|
|
677
|
+
if (!isInitialSync) {
|
|
678
|
+
ev.emit('chats.delete', [id]);
|
|
742
679
|
}
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
680
|
+
}
|
|
681
|
+
else if (action === null || action === void 0 ? void 0 : action.labelEditAction) {
|
|
682
|
+
const { name, color, deleted, predefinedId } = action.labelEditAction;
|
|
683
|
+
ev.emit('labels.edit', {
|
|
684
|
+
id,
|
|
685
|
+
name: name,
|
|
686
|
+
color: color,
|
|
687
|
+
deleted: deleted,
|
|
688
|
+
predefinedId: predefinedId ? String(predefinedId) : undefined
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
else if (action === null || action === void 0 ? void 0 : action.labelAssociationAction) {
|
|
692
|
+
ev.emit('labels.association', {
|
|
693
|
+
type: action.labelAssociationAction.labeled
|
|
694
|
+
? 'add'
|
|
695
|
+
: 'remove',
|
|
696
|
+
association: type === LabelAssociation_1.LabelAssociationType.Chat
|
|
697
|
+
? {
|
|
698
|
+
type: LabelAssociation_1.LabelAssociationType.Chat,
|
|
699
|
+
chatId: syncAction.index[2],
|
|
700
|
+
labelId: syncAction.index[1]
|
|
701
|
+
}
|
|
702
|
+
: {
|
|
703
|
+
type: LabelAssociation_1.LabelAssociationType.Message,
|
|
704
|
+
chatId: syncAction.index[2],
|
|
705
|
+
messageId: syncAction.index[3],
|
|
706
|
+
labelId: syncAction.index[1]
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
else {
|
|
711
|
+
logger === null || logger === void 0 ? void 0 : logger.debug({ syncAction, id }, 'unprocessable update');
|
|
712
|
+
}
|
|
713
|
+
function getChatUpdateConditional(id, msgRange) {
|
|
714
|
+
return isInitialSync
|
|
715
|
+
? (data) => {
|
|
716
|
+
const chat = data.historySets.chats[id] || data.chatUpserts[id];
|
|
717
|
+
if (chat) {
|
|
718
|
+
return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
: undefined;
|
|
722
|
+
}
|
|
723
|
+
function isValidPatchBasedOnMessageRange(chat, msgRange) {
|
|
724
|
+
const lastMsgTimestamp = Number((msgRange === null || msgRange === void 0 ? void 0 : msgRange.lastMessageTimestamp) || (msgRange === null || msgRange === void 0 ? void 0 : msgRange.lastSystemMessageTimestamp) || 0);
|
|
725
|
+
const chatLastMsgTimestamp = Number((chat === null || chat === void 0 ? void 0 : chat.lastMessageRecvTimestamp) || 0);
|
|
726
|
+
return lastMsgTimestamp >= chatLastMsgTimestamp;
|
|
727
|
+
}
|
|
751
728
|
};
|
|
752
|
-
|
|
729
|
+
exports.processSyncAction = processSyncAction;
|