@baileys-md/baileys 11.0.3 → 11.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/WAProto/index.js +4182 -13516
- package/WASignalGroup/GroupProtocol.js +1697 -0
- package/WASignalGroup/ciphertext_message.js +16 -0
- package/WASignalGroup/group_cipher.js +120 -0
- package/WASignalGroup/group_session_builder.js +46 -0
- package/WASignalGroup/index.js +5 -0
- package/WASignalGroup/keyhelper.js +21 -0
- package/WASignalGroup/protobufs.js +3 -0
- package/WASignalGroup/queue_job.js +69 -0
- package/WASignalGroup/sender_chain_key.js +50 -0
- package/WASignalGroup/sender_key_distribution_message.js +78 -0
- package/WASignalGroup/sender_key_message.js +92 -0
- package/WASignalGroup/sender_key_name.js +70 -0
- package/WASignalGroup/sender_key_record.js +56 -0
- package/WASignalGroup/sender_key_state.js +129 -0
- package/{lib/Signal/Group/sender-message-key.js → WASignalGroup/sender_message_key.js} +16 -4
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.js +71 -52
- package/lib/Defaults/{phonenumber-mcc.js → phonenumber-mcc.json} +1 -1
- package/lib/Signal/libsignal.js +61 -41
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.js +19 -3
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/business.js +42 -37
- package/lib/Socket/chats.js +187 -194
- package/lib/Socket/groups.js +90 -87
- package/lib/Socket/index.js +8 -7
- package/lib/Socket/messages-recv.js +335 -360
- package/lib/Socket/messages-send.js +279 -156
- package/lib/Socket/newsletter.js +213 -144
- package/lib/Socket/registration.js +166 -0
- package/lib/Socket/socket.js +161 -128
- package/lib/Socket/usync.js +26 -19
- package/lib/Store/index.js +8 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/{Utils → Store}/make-in-memory-store.js +27 -19
- package/lib/Store/make-mongo-store.js +567 -0
- package/lib/{Utils → Store}/make-ordered-dictionary.js +5 -2
- package/lib/{Utils → Store}/object-repository.js +4 -1
- 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 -7
- package/lib/Types/Newsletter.js +17 -30
- 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 -2
- package/lib/Types/USync.js +2 -2
- package/lib/Types/index.js +31 -15
- package/lib/Utils/auth-utils.js +47 -31
- package/lib/Utils/baileys-event-stream.js +22 -15
- package/lib/Utils/business.js +69 -66
- package/lib/Utils/chat-utils.js +195 -200
- package/lib/Utils/crypto.js +85 -70
- package/lib/Utils/decode-wa-message.js +51 -46
- package/lib/Utils/event-buffer.js +46 -36
- package/lib/Utils/generics.js +188 -116
- package/lib/Utils/history.js +46 -37
- package/lib/Utils/index.js +33 -19
- package/lib/Utils/link-preview.js +55 -14
- package/lib/Utils/logger.js +7 -3
- package/lib/Utils/lt-hash.js +26 -23
- package/lib/Utils/make-mutex.js +10 -7
- package/lib/Utils/messages-media.js +368 -239
- package/lib/Utils/messages.js +510 -278
- package/lib/Utils/noise-handler.js +31 -22
- package/lib/Utils/process-message.js +148 -144
- package/lib/Utils/signal.js +64 -71
- package/lib/Utils/use-multi-file-auth-state.js +32 -14
- package/lib/Utils/validate-connection.js +115 -72
- package/lib/WABinary/constants.js +20 -1281
- package/lib/WABinary/decode.js +52 -15
- package/lib/WABinary/encode.js +48 -14
- package/lib/WABinary/generic-utils.js +39 -31
- package/lib/WABinary/index.js +21 -6
- package/lib/WABinary/jid-utils.js +40 -23
- package/lib/WABinary/types.js +2 -2
- package/lib/WAM/BinaryInfo.js +5 -2
- package/lib/WAM/constants.js +2366 -2257
- package/lib/WAM/encode.js +21 -17
- package/lib/WAM/index.js +19 -4
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +11 -8
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +14 -11
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +12 -9
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +13 -9
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +22 -20
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +6 -3
- package/lib/WAUSync/Protocols/index.js +20 -5
- package/lib/WAUSync/USyncQuery.js +32 -34
- package/lib/WAUSync/USyncUser.js +5 -2
- package/lib/WAUSync/index.js +19 -4
- package/lib/index.js +33 -11
- package/package.json +61 -21
- package/WAProto/GenerateStatics.sh +0 -3
- package/WAProto/WAProto.proto +0 -4633
- package/WAProto/fix-imports.js +0 -29
- package/lib/Defaults/baileys-version.js +0 -1
- package/lib/Signal/Group/ciphertext-message.js +0 -12
- package/lib/Signal/Group/group-session-builder.js +0 -30
- package/lib/Signal/Group/group_cipher.js +0 -94
- package/lib/Signal/Group/index.js +0 -12
- package/lib/Signal/Group/keyhelper.js +0 -19
- package/lib/Signal/Group/queue-job.js +0 -54
- package/lib/Signal/Group/sender-chain-key.js +0 -32
- package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
- package/lib/Signal/Group/sender-key-message.js +0 -67
- package/lib/Signal/Group/sender-key-name.js +0 -48
- package/lib/Signal/Group/sender-key-record.js +0 -50
- package/lib/Signal/Group/sender-key-state.js +0 -96
- package/lib/Socket/Client/types.js +0 -11
- package/lib/Socket/Client/websocket.js +0 -50
- package/lib/Socket/communities.js +0 -351
- package/lib/Socket/mex.js +0 -42
package/lib/Utils/chat-utils.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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");
|
|
9
12
|
const mutationKeys = async (keydata) => {
|
|
10
|
-
const expanded = await hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' });
|
|
13
|
+
const expanded = await (0, crypto_1.hkdf)(keydata, 160, { info: 'WhatsApp Mutation Keys' });
|
|
11
14
|
return {
|
|
12
15
|
indexKey: expanded.slice(0, 32),
|
|
13
16
|
valueEncryptionKey: expanded.slice(32, 64),
|
|
@@ -20,10 +23,10 @@ const generateMac = (operation, data, keyId, key) => {
|
|
|
20
23
|
const getKeyData = () => {
|
|
21
24
|
let r;
|
|
22
25
|
switch (operation) {
|
|
23
|
-
case proto.SyncdMutation.SyncdOperation.SET:
|
|
26
|
+
case WAProto_1.proto.SyncdMutation.SyncdOperation.SET:
|
|
24
27
|
r = 0x01;
|
|
25
28
|
break;
|
|
26
|
-
case proto.SyncdMutation.SyncdOperation.REMOVE:
|
|
29
|
+
case WAProto_1.proto.SyncdMutation.SyncdOperation.REMOVE:
|
|
27
30
|
r = 0x02;
|
|
28
31
|
break;
|
|
29
32
|
}
|
|
@@ -34,7 +37,7 @@ const generateMac = (operation, data, keyId, key) => {
|
|
|
34
37
|
const last = Buffer.alloc(8); // 8 bytes
|
|
35
38
|
last.set([keyData.length], last.length - 1);
|
|
36
39
|
const total = Buffer.concat([keyData, data, last]);
|
|
37
|
-
const hmac = hmacSign(total, key, 'sha512');
|
|
40
|
+
const hmac = (0, crypto_1.hmacSign)(total, key, 'sha512');
|
|
38
41
|
return hmac.slice(0, 32);
|
|
39
42
|
};
|
|
40
43
|
const to64BitNetworkOrder = (e) => {
|
|
@@ -50,9 +53,9 @@ const makeLtHashGenerator = ({ indexValueMap, hash }) => {
|
|
|
50
53
|
mix: ({ indexMac, valueMac, operation }) => {
|
|
51
54
|
const indexMacBase64 = Buffer.from(indexMac).toString('base64');
|
|
52
55
|
const prevOp = indexValueMap[indexMacBase64];
|
|
53
|
-
if (operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
|
|
56
|
+
if (operation === WAProto_1.proto.SyncdMutation.SyncdOperation.REMOVE) {
|
|
54
57
|
if (!prevOp) {
|
|
55
|
-
throw new Boom('tried remove, but no previous op', { data: { indexMac, valueMac } });
|
|
58
|
+
throw new boom_1.Boom('tried remove, but no previous op', { data: { indexMac, valueMac } });
|
|
56
59
|
}
|
|
57
60
|
// remove from index value mac, since this mutation is erased
|
|
58
61
|
delete indexValueMap[indexMacBase64];
|
|
@@ -68,7 +71,7 @@ const makeLtHashGenerator = ({ indexValueMap, hash }) => {
|
|
|
68
71
|
},
|
|
69
72
|
finish: async () => {
|
|
70
73
|
const hashArrayBuffer = new Uint8Array(hash).buffer;
|
|
71
|
-
const result = await LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs);
|
|
74
|
+
const result = await lt_hash_1.LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs);
|
|
72
75
|
const buffer = Buffer.from(result);
|
|
73
76
|
return {
|
|
74
77
|
hash: buffer,
|
|
@@ -78,33 +81,43 @@ const makeLtHashGenerator = ({ indexValueMap, hash }) => {
|
|
|
78
81
|
};
|
|
79
82
|
};
|
|
80
83
|
const generateSnapshotMac = (lthash, version, name, key) => {
|
|
81
|
-
const total = Buffer.concat([
|
|
82
|
-
|
|
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');
|
|
83
90
|
};
|
|
84
91
|
const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
|
|
85
|
-
const total = Buffer.concat([
|
|
86
|
-
|
|
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);
|
|
87
99
|
};
|
|
88
|
-
|
|
89
|
-
|
|
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) => {
|
|
90
103
|
const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined;
|
|
91
104
|
if (!key) {
|
|
92
|
-
throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 });
|
|
105
|
+
throw new boom_1.Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 });
|
|
93
106
|
}
|
|
94
107
|
const encKeyId = Buffer.from(myAppStateKeyId, 'base64');
|
|
95
108
|
state = { ...state, indexValueMap: { ...state.indexValueMap } };
|
|
96
109
|
const indexBuffer = Buffer.from(JSON.stringify(index));
|
|
97
|
-
const dataProto = proto.SyncActionData.fromObject({
|
|
110
|
+
const dataProto = WAProto_1.proto.SyncActionData.fromObject({
|
|
98
111
|
index: indexBuffer,
|
|
99
112
|
value: syncAction,
|
|
100
113
|
padding: new Uint8Array(0),
|
|
101
114
|
version: apiVersion
|
|
102
115
|
});
|
|
103
|
-
const encoded = proto.SyncActionData.encode(dataProto).finish();
|
|
116
|
+
const encoded = WAProto_1.proto.SyncActionData.encode(dataProto).finish();
|
|
104
117
|
const keyValue = await mutationKeys(key.keyData);
|
|
105
|
-
const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey);
|
|
118
|
+
const encValue = (0, crypto_1.aesEncrypt)(encoded, keyValue.valueEncryptionKey);
|
|
106
119
|
const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey);
|
|
107
|
-
const indexMac = hmacSign(indexBuffer, keyValue.indexKey);
|
|
120
|
+
const indexMac = (0, crypto_1.hmacSign)(indexBuffer, keyValue.indexKey);
|
|
108
121
|
// update LT hash
|
|
109
122
|
const generator = makeLtHashGenerator(state);
|
|
110
123
|
generator.mix({ indexMac, valueMac, operation });
|
|
@@ -134,7 +147,8 @@ export const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, op
|
|
|
134
147
|
state.indexValueMap[base64Index] = { valueMac };
|
|
135
148
|
return { patch, state };
|
|
136
149
|
};
|
|
137
|
-
|
|
150
|
+
exports.encodeSyncdPatch = encodeSyncdPatch;
|
|
151
|
+
const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
|
|
138
152
|
const ltGenerator = makeLtHashGenerator(initialState);
|
|
139
153
|
// indexKey used to HMAC sign record.index.blob
|
|
140
154
|
// valueEncryptionKey used to AES-256-CBC encrypt record.value.blob[0:-32]
|
|
@@ -142,8 +156,8 @@ export const decodeSyncdMutations = async (msgMutations, initialState, getAppSta
|
|
|
142
156
|
for (const msgMutation of msgMutations) {
|
|
143
157
|
// if it's a syncdmutation, get the operation property
|
|
144
158
|
// otherwise, if it's only a record -- it'll be a SET mutation
|
|
145
|
-
const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET;
|
|
146
|
-
const record = 'record' in msgMutation && !!msgMutation.record ? msgMutation.record : msgMutation;
|
|
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;
|
|
147
161
|
const key = await getKey(record.keyId.id);
|
|
148
162
|
const content = Buffer.from(record.value.blob);
|
|
149
163
|
const encContent = content.slice(0, -32);
|
|
@@ -151,15 +165,15 @@ export const decodeSyncdMutations = async (msgMutations, initialState, getAppSta
|
|
|
151
165
|
if (validateMacs) {
|
|
152
166
|
const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey);
|
|
153
167
|
if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
|
|
154
|
-
throw new Boom('HMAC content verification failed');
|
|
168
|
+
throw new boom_1.Boom('HMAC content verification failed');
|
|
155
169
|
}
|
|
156
170
|
}
|
|
157
|
-
const result = aesDecrypt(encContent, key.valueEncryptionKey);
|
|
158
|
-
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);
|
|
159
173
|
if (validateMacs) {
|
|
160
|
-
const hmac = hmacSign(syncAction.index, key.indexKey);
|
|
174
|
+
const hmac = (0, crypto_1.hmacSign)(syncAction.index, key.indexKey);
|
|
161
175
|
if (Buffer.compare(hmac, record.index.blob) !== 0) {
|
|
162
|
-
throw new Boom('HMAC index verification failed');
|
|
176
|
+
throw new boom_1.Boom('HMAC index verification failed');
|
|
163
177
|
}
|
|
164
178
|
}
|
|
165
179
|
const indexStr = Buffer.from(syncAction.index).toString();
|
|
@@ -175,39 +189,38 @@ export const decodeSyncdMutations = async (msgMutations, initialState, getAppSta
|
|
|
175
189
|
const base64Key = Buffer.from(keyId).toString('base64');
|
|
176
190
|
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
177
191
|
if (!keyEnc) {
|
|
178
|
-
throw new Boom(`failed to find key "${base64Key}" to decode mutation`, {
|
|
179
|
-
statusCode: 404,
|
|
180
|
-
data: { msgMutations }
|
|
181
|
-
});
|
|
192
|
+
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode mutation`, { statusCode: 404, data: { msgMutations } });
|
|
182
193
|
}
|
|
183
194
|
return mutationKeys(keyEnc.keyData);
|
|
184
195
|
}
|
|
185
196
|
};
|
|
186
|
-
|
|
197
|
+
exports.decodeSyncdMutations = decodeSyncdMutations;
|
|
198
|
+
const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onMutation, validateMacs) => {
|
|
187
199
|
if (validateMacs) {
|
|
188
200
|
const base64Key = Buffer.from(msg.keyId.id).toString('base64');
|
|
189
201
|
const mainKeyObj = await getAppStateSyncKey(base64Key);
|
|
190
202
|
if (!mainKeyObj) {
|
|
191
|
-
throw new Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } });
|
|
203
|
+
throw new boom_1.Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } });
|
|
192
204
|
}
|
|
193
205
|
const mainKey = await mutationKeys(mainKeyObj.keyData);
|
|
194
206
|
const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32));
|
|
195
|
-
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);
|
|
196
208
|
if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
|
|
197
|
-
throw new Boom('Invalid patch mac');
|
|
209
|
+
throw new boom_1.Boom('Invalid patch mac');
|
|
198
210
|
}
|
|
199
211
|
}
|
|
200
|
-
const result = await decodeSyncdMutations(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs);
|
|
212
|
+
const result = await (0, exports.decodeSyncdMutations)(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs);
|
|
201
213
|
return result;
|
|
202
214
|
};
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
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');
|
|
206
219
|
const final = {};
|
|
207
220
|
await Promise.all(collectionNodes.map(async (collectionNode) => {
|
|
208
|
-
const patchesNode = getBinaryNodeChild(collectionNode, 'patches');
|
|
209
|
-
const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch');
|
|
210
|
-
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');
|
|
211
224
|
const syncds = [];
|
|
212
225
|
const name = collectionNode.attrs.name;
|
|
213
226
|
const hasMorePatches = collectionNode.attrs.has_more_patches === 'true';
|
|
@@ -216,16 +229,16 @@ export const extractSyncdPatches = async (result, options) => {
|
|
|
216
229
|
if (!Buffer.isBuffer(snapshotNode)) {
|
|
217
230
|
snapshotNode.content = Buffer.from(Object.values(snapshotNode.content));
|
|
218
231
|
}
|
|
219
|
-
const blobRef = proto.ExternalBlobReference.decode(snapshotNode.content);
|
|
220
|
-
const data = await downloadExternalBlob(blobRef, options);
|
|
221
|
-
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);
|
|
222
235
|
}
|
|
223
236
|
for (let { content } of patches) {
|
|
224
237
|
if (content) {
|
|
225
238
|
if (!Buffer.isBuffer(content)) {
|
|
226
239
|
content = Buffer.from(Object.values(content));
|
|
227
240
|
}
|
|
228
|
-
const syncd = proto.SyncdPatch.decode(content);
|
|
241
|
+
const syncd = WAProto_1.proto.SyncdPatch.decode(content);
|
|
229
242
|
if (!syncd.version) {
|
|
230
243
|
syncd.version = { version: +collectionNode.attrs.version + 1 };
|
|
231
244
|
}
|
|
@@ -236,27 +249,32 @@ export const extractSyncdPatches = async (result, options) => {
|
|
|
236
249
|
}));
|
|
237
250
|
return final;
|
|
238
251
|
};
|
|
239
|
-
|
|
240
|
-
|
|
252
|
+
exports.extractSyncdPatches = extractSyncdPatches;
|
|
253
|
+
const downloadExternalBlob = async (blob, options) => {
|
|
254
|
+
const stream = await (0, messages_media_1.downloadContentFromMessage)(blob, 'md-app-state', { options });
|
|
241
255
|
const bufferArray = [];
|
|
242
256
|
for await (const chunk of stream) {
|
|
243
257
|
bufferArray.push(chunk);
|
|
244
258
|
}
|
|
245
259
|
return Buffer.concat(bufferArray);
|
|
246
260
|
};
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
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);
|
|
250
265
|
return syncData;
|
|
251
266
|
};
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
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);
|
|
255
271
|
const mutationMap = {};
|
|
256
|
-
const areMutationsRequired = typeof minimumVersionNumber === 'undefined'
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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();
|
|
260
278
|
mutationMap[index] = mutation;
|
|
261
279
|
}
|
|
262
280
|
: () => { }, validateMacs);
|
|
@@ -266,12 +284,12 @@ export const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, mi
|
|
|
266
284
|
const base64Key = Buffer.from(snapshot.keyId.id).toString('base64');
|
|
267
285
|
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
268
286
|
if (!keyEnc) {
|
|
269
|
-
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`);
|
|
270
288
|
}
|
|
271
289
|
const result = await mutationKeys(keyEnc.keyData);
|
|
272
290
|
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
|
|
273
291
|
if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
|
|
274
|
-
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`);
|
|
292
|
+
throw new boom_1.Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`);
|
|
275
293
|
}
|
|
276
294
|
}
|
|
277
295
|
return {
|
|
@@ -279,41 +297,45 @@ export const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, mi
|
|
|
279
297
|
mutationMap
|
|
280
298
|
};
|
|
281
299
|
};
|
|
282
|
-
|
|
300
|
+
exports.decodeSyncdSnapshot = decodeSyncdSnapshot;
|
|
301
|
+
const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options, minimumVersionNumber, logger, validateMacs = true) => {
|
|
302
|
+
var _a;
|
|
283
303
|
const newState = {
|
|
284
304
|
...initial,
|
|
285
305
|
indexValueMap: { ...initial.indexValueMap }
|
|
286
306
|
};
|
|
287
307
|
const mutationMap = {};
|
|
288
|
-
for (
|
|
308
|
+
for (let i = 0; i < syncds.length; i++) {
|
|
309
|
+
const syncd = syncds[i];
|
|
289
310
|
const { version, keyId, snapshotMac } = syncd;
|
|
290
311
|
if (syncd.externalMutations) {
|
|
291
|
-
logger
|
|
292
|
-
const ref = await downloadExternalPatch(syncd.externalMutations, options);
|
|
293
|
-
logger
|
|
294
|
-
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);
|
|
295
316
|
}
|
|
296
|
-
const patchVersion = toNumber(version.version);
|
|
317
|
+
const patchVersion = (0, generics_1.toNumber)(version.version);
|
|
297
318
|
newState.version = patchVersion;
|
|
298
319
|
const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber;
|
|
299
|
-
const decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
|
|
320
|
+
const decodeResult = await (0, exports.decodeSyncdPatch)(syncd, name, newState, getAppStateSyncKey, shouldMutate
|
|
300
321
|
? mutation => {
|
|
301
|
-
|
|
322
|
+
var _a;
|
|
323
|
+
const index = (_a = mutation.syncAction.index) === null || _a === void 0 ? void 0 : _a.toString();
|
|
302
324
|
mutationMap[index] = mutation;
|
|
303
325
|
}
|
|
304
|
-
: () => { }, true);
|
|
326
|
+
: (() => { }), true);
|
|
305
327
|
newState.hash = decodeResult.hash;
|
|
306
328
|
newState.indexValueMap = decodeResult.indexValueMap;
|
|
307
329
|
if (validateMacs) {
|
|
308
330
|
const base64Key = Buffer.from(keyId.id).toString('base64');
|
|
309
331
|
const keyEnc = await getAppStateSyncKey(base64Key);
|
|
310
332
|
if (!keyEnc) {
|
|
311
|
-
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`);
|
|
312
334
|
}
|
|
313
335
|
const result = await mutationKeys(keyEnc.keyData);
|
|
314
336
|
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey);
|
|
315
337
|
if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
|
|
316
|
-
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`);
|
|
338
|
+
throw new boom_1.Boom(`failed to verify LTHash at ${newState.version} of ${name}`);
|
|
317
339
|
}
|
|
318
340
|
}
|
|
319
341
|
// clear memory used up by the mutations
|
|
@@ -321,31 +343,31 @@ export const decodePatches = async (name, syncds, initial, getAppStateSyncKey, o
|
|
|
321
343
|
}
|
|
322
344
|
return { state: newState, mutationMap };
|
|
323
345
|
};
|
|
324
|
-
|
|
325
|
-
|
|
346
|
+
exports.decodePatches = decodePatches;
|
|
347
|
+
const chatModificationToAppPatch = (mod, jid) => {
|
|
348
|
+
const OP = WAProto_1.proto.SyncdMutation.SyncdOperation;
|
|
326
349
|
const getMessageRange = (lastMessages) => {
|
|
327
350
|
let messageRange;
|
|
328
351
|
if (Array.isArray(lastMessages)) {
|
|
329
352
|
const lastMsg = lastMessages[lastMessages.length - 1];
|
|
330
353
|
messageRange = {
|
|
331
|
-
lastMessageTimestamp: lastMsg
|
|
332
|
-
messages: lastMessages
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
: 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
|
|
349
371
|
};
|
|
350
372
|
}
|
|
351
373
|
else {
|
|
@@ -414,9 +436,7 @@ export const chatModificationToAppPatch = (mod, jid) => {
|
|
|
414
436
|
else if ('clear' in mod) {
|
|
415
437
|
patch = {
|
|
416
438
|
syncAction: {
|
|
417
|
-
clearChatAction: {
|
|
418
|
-
messageRange: getMessageRange(mod.lastMessages)
|
|
419
|
-
}
|
|
439
|
+
clearChatAction: {} // add message range later
|
|
420
440
|
},
|
|
421
441
|
index: ['clearChat', jid, '1' /*the option here is 0 when keep starred messages is enabled*/, '0'],
|
|
422
442
|
type: 'regular_high',
|
|
@@ -437,17 +457,6 @@ export const chatModificationToAppPatch = (mod, jid) => {
|
|
|
437
457
|
operation: OP.SET
|
|
438
458
|
};
|
|
439
459
|
}
|
|
440
|
-
else if ('contact' in mod) {
|
|
441
|
-
patch = {
|
|
442
|
-
syncAction: {
|
|
443
|
-
contactAction: mod.contact || {}
|
|
444
|
-
},
|
|
445
|
-
index: ['contact', jid],
|
|
446
|
-
type: 'critical_unblock_low',
|
|
447
|
-
apiVersion: 2,
|
|
448
|
-
operation: mod.contact ? OP.SET : OP.REMOVE
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
460
|
else if ('star' in mod) {
|
|
452
461
|
const key = mod.star.messages[0];
|
|
453
462
|
patch = {
|
|
@@ -466,7 +475,7 @@ export const chatModificationToAppPatch = (mod, jid) => {
|
|
|
466
475
|
patch = {
|
|
467
476
|
syncAction: {
|
|
468
477
|
deleteChatAction: {
|
|
469
|
-
messageRange: getMessageRange(mod.lastMessages)
|
|
478
|
+
messageRange: getMessageRange(mod.lastMessages),
|
|
470
479
|
}
|
|
471
480
|
},
|
|
472
481
|
index: ['deleteChat', jid, '1'],
|
|
@@ -485,73 +494,64 @@ export const chatModificationToAppPatch = (mod, jid) => {
|
|
|
485
494
|
index: ['setting_pushName'],
|
|
486
495
|
type: 'critical_block',
|
|
487
496
|
apiVersion: 1,
|
|
488
|
-
operation: OP.SET
|
|
489
|
-
};
|
|
490
|
-
}
|
|
491
|
-
else if ('addLabel' in mod) {
|
|
492
|
-
patch = {
|
|
493
|
-
syncAction: {
|
|
494
|
-
labelEditAction: {
|
|
495
|
-
name: mod.addLabel.name,
|
|
496
|
-
color: mod.addLabel.color,
|
|
497
|
-
predefinedId: mod.addLabel.predefinedId,
|
|
498
|
-
deleted: mod.addLabel.deleted
|
|
499
|
-
}
|
|
500
|
-
},
|
|
501
|
-
index: ['label_edit', mod.addLabel.id],
|
|
502
|
-
type: 'regular',
|
|
503
|
-
apiVersion: 3,
|
|
504
|
-
operation: OP.SET
|
|
497
|
+
operation: OP.SET,
|
|
505
498
|
};
|
|
506
499
|
}
|
|
507
500
|
else if ('addChatLabel' in mod) {
|
|
508
501
|
patch = {
|
|
509
502
|
syncAction: {
|
|
510
503
|
labelAssociationAction: {
|
|
511
|
-
labeled: true
|
|
504
|
+
labeled: true,
|
|
512
505
|
}
|
|
513
506
|
},
|
|
514
|
-
index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
|
|
507
|
+
index: [LabelAssociation_1.LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
|
|
515
508
|
type: 'regular',
|
|
516
509
|
apiVersion: 3,
|
|
517
|
-
operation: OP.SET
|
|
510
|
+
operation: OP.SET,
|
|
518
511
|
};
|
|
519
512
|
}
|
|
520
513
|
else if ('removeChatLabel' in mod) {
|
|
521
514
|
patch = {
|
|
522
515
|
syncAction: {
|
|
523
516
|
labelAssociationAction: {
|
|
524
|
-
labeled: false
|
|
517
|
+
labeled: false,
|
|
525
518
|
}
|
|
526
519
|
},
|
|
527
|
-
index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
|
|
520
|
+
index: [LabelAssociation_1.LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
|
|
528
521
|
type: 'regular',
|
|
529
522
|
apiVersion: 3,
|
|
530
|
-
operation: OP.SET
|
|
523
|
+
operation: OP.SET,
|
|
531
524
|
};
|
|
532
525
|
}
|
|
533
526
|
else if ('addMessageLabel' in mod) {
|
|
534
527
|
patch = {
|
|
535
528
|
syncAction: {
|
|
536
529
|
labelAssociationAction: {
|
|
537
|
-
labeled: true
|
|
530
|
+
labeled: true,
|
|
538
531
|
}
|
|
539
532
|
},
|
|
540
|
-
index: [
|
|
533
|
+
index: [
|
|
534
|
+
LabelAssociation_1.LabelAssociationType.Message,
|
|
535
|
+
mod.addMessageLabel.labelId,
|
|
536
|
+
jid,
|
|
537
|
+
mod.addMessageLabel.messageId,
|
|
538
|
+
'0',
|
|
539
|
+
'0'
|
|
540
|
+
],
|
|
541
541
|
type: 'regular',
|
|
542
542
|
apiVersion: 3,
|
|
543
|
-
operation: OP.SET
|
|
543
|
+
operation: OP.SET,
|
|
544
544
|
};
|
|
545
545
|
}
|
|
546
546
|
else if ('removeMessageLabel' in mod) {
|
|
547
547
|
patch = {
|
|
548
548
|
syncAction: {
|
|
549
549
|
labelAssociationAction: {
|
|
550
|
-
labeled: false
|
|
550
|
+
labeled: false,
|
|
551
551
|
}
|
|
552
552
|
},
|
|
553
553
|
index: [
|
|
554
|
-
LabelAssociationType.Message,
|
|
554
|
+
LabelAssociation_1.LabelAssociationType.Message,
|
|
555
555
|
mod.removeMessageLabel.labelId,
|
|
556
556
|
jid,
|
|
557
557
|
mod.removeMessageLabel.messageId,
|
|
@@ -560,30 +560,34 @@ export const chatModificationToAppPatch = (mod, jid) => {
|
|
|
560
560
|
],
|
|
561
561
|
type: 'regular',
|
|
562
562
|
apiVersion: 3,
|
|
563
|
-
operation: OP.SET
|
|
563
|
+
operation: OP.SET,
|
|
564
564
|
};
|
|
565
565
|
}
|
|
566
566
|
else {
|
|
567
|
-
throw new Boom('not supported');
|
|
567
|
+
throw new boom_1.Boom('not supported');
|
|
568
568
|
}
|
|
569
569
|
patch.syncAction.timestamp = Date.now();
|
|
570
570
|
return patch;
|
|
571
571
|
};
|
|
572
|
-
|
|
572
|
+
exports.chatModificationToAppPatch = chatModificationToAppPatch;
|
|
573
|
+
const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
574
|
+
var _a, _b, _c, _d;
|
|
573
575
|
const isInitialSync = !!initialSyncOpts;
|
|
574
|
-
const accountSettings = initialSyncOpts
|
|
575
|
-
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');
|
|
576
578
|
const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction;
|
|
577
|
-
if (action
|
|
579
|
+
if (action === null || action === void 0 ? void 0 : action.muteAction) {
|
|
578
580
|
ev.emit('chats.update', [
|
|
579
581
|
{
|
|
580
582
|
id,
|
|
581
|
-
muteEndTime:
|
|
583
|
+
muteEndTime: ((_a = action.muteAction) === null || _a === void 0 ? void 0 : _a.muted)
|
|
584
|
+
? (0, generics_1.toNumber)(action.muteAction.muteEndTimestamp)
|
|
585
|
+
: null,
|
|
582
586
|
conditional: getChatUpdateConditional(id, undefined)
|
|
583
587
|
}
|
|
584
588
|
]);
|
|
585
589
|
}
|
|
586
|
-
else if (action
|
|
590
|
+
else if ((action === null || action === void 0 ? void 0 : action.archiveChatAction) || type === 'archive' || type === 'unarchive') {
|
|
587
591
|
// okay so we've to do some annoying computation here
|
|
588
592
|
// when we're initially syncing the app state
|
|
589
593
|
// there are a few cases we need to handle
|
|
@@ -594,38 +598,36 @@ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) =
|
|
|
594
598
|
// we compare the timestamp of latest message from the other person to determine this
|
|
595
599
|
// 2. if the account unarchiveChats setting is false -- then it doesn't matter,
|
|
596
600
|
// it'll always take an app state action to mark in unarchived -- which we'll get anyway
|
|
597
|
-
const archiveAction = action
|
|
598
|
-
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';
|
|
599
605
|
// // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
|
|
600
606
|
// // this only applies for the initial sync
|
|
601
607
|
// if(isInitialSync && !isArchived) {
|
|
602
608
|
// isArchived = false
|
|
603
609
|
// }
|
|
604
|
-
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;
|
|
605
611
|
// logger?.debug({ chat: id, syncAction }, 'message range archive')
|
|
606
|
-
ev.emit('chats.update', [
|
|
607
|
-
{
|
|
612
|
+
ev.emit('chats.update', [{
|
|
608
613
|
id,
|
|
609
614
|
archived: isArchived,
|
|
610
615
|
conditional: getChatUpdateConditional(id, msgRange)
|
|
611
|
-
}
|
|
612
|
-
]);
|
|
616
|
+
}]);
|
|
613
617
|
}
|
|
614
|
-
else if (action
|
|
618
|
+
else if (action === null || action === void 0 ? void 0 : action.markChatAsReadAction) {
|
|
615
619
|
const markReadAction = action.markChatAsReadAction;
|
|
616
620
|
// basically we don't need to fire an "read" update if the chat is being marked as read
|
|
617
621
|
// because the chat is read by default
|
|
618
622
|
// this only applies for the initial sync
|
|
619
623
|
const isNullUpdate = isInitialSync && markReadAction.read;
|
|
620
|
-
ev.emit('chats.update', [
|
|
621
|
-
{
|
|
624
|
+
ev.emit('chats.update', [{
|
|
622
625
|
id,
|
|
623
|
-
unreadCount: isNullUpdate ? null : !!markReadAction
|
|
624
|
-
conditional: getChatUpdateConditional(id, markReadAction
|
|
625
|
-
}
|
|
626
|
-
]);
|
|
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
|
+
}]);
|
|
627
629
|
}
|
|
628
|
-
else if (action
|
|
630
|
+
else if ((action === null || action === void 0 ? void 0 : action.deleteMessageForMeAction) || type === 'deleteMessageForMe') {
|
|
629
631
|
ev.emit('messages.delete', {
|
|
630
632
|
keys: [
|
|
631
633
|
{
|
|
@@ -636,41 +638,32 @@ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) =
|
|
|
636
638
|
]
|
|
637
639
|
});
|
|
638
640
|
}
|
|
639
|
-
else if (action
|
|
640
|
-
ev.emit('contacts.upsert', [
|
|
641
|
-
{
|
|
642
|
-
id: id,
|
|
643
|
-
name: action.contactAction.fullName,
|
|
644
|
-
lid: action.contactAction.lidJid || undefined,
|
|
645
|
-
jid: isJidUser(id) ? id : undefined
|
|
646
|
-
}
|
|
647
|
-
]);
|
|
641
|
+
else if (action === null || action === void 0 ? void 0 : action.contactAction) {
|
|
642
|
+
ev.emit('contacts.upsert', [{ id, name: action.contactAction.fullName }]);
|
|
648
643
|
}
|
|
649
|
-
else if (action
|
|
650
|
-
const name = action
|
|
651
|
-
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) {
|
|
652
647
|
ev.emit('creds.update', { me: { ...me, name } });
|
|
653
648
|
}
|
|
654
649
|
}
|
|
655
|
-
else if (action
|
|
656
|
-
ev.emit('chats.update', [
|
|
657
|
-
{
|
|
650
|
+
else if (action === null || action === void 0 ? void 0 : action.pinAction) {
|
|
651
|
+
ev.emit('chats.update', [{
|
|
658
652
|
id,
|
|
659
|
-
pinned: action.pinAction
|
|
653
|
+
pinned: ((_c = action.pinAction) === null || _c === void 0 ? void 0 : _c.pinned) ? (0, generics_1.toNumber)(action.timestamp) : null,
|
|
660
654
|
conditional: getChatUpdateConditional(id, undefined)
|
|
661
|
-
}
|
|
662
|
-
]);
|
|
655
|
+
}]);
|
|
663
656
|
}
|
|
664
|
-
else if (action
|
|
657
|
+
else if (action === null || action === void 0 ? void 0 : action.unarchiveChatsSetting) {
|
|
665
658
|
const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats;
|
|
666
659
|
ev.emit('creds.update', { accountSettings: { unarchiveChats } });
|
|
667
|
-
logger
|
|
660
|
+
logger === null || logger === void 0 ? void 0 : logger.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`);
|
|
668
661
|
if (accountSettings) {
|
|
669
662
|
accountSettings.unarchiveChats = unarchiveChats;
|
|
670
663
|
}
|
|
671
664
|
}
|
|
672
|
-
else if (action
|
|
673
|
-
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;
|
|
674
667
|
if (typeof starred !== 'boolean') {
|
|
675
668
|
starred = syncAction.index[syncAction.index.length - 1] === '1';
|
|
676
669
|
}
|
|
@@ -681,32 +674,34 @@ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) =
|
|
|
681
674
|
}
|
|
682
675
|
]);
|
|
683
676
|
}
|
|
684
|
-
else if (action
|
|
677
|
+
else if ((action === null || action === void 0 ? void 0 : action.deleteChatAction) || type === 'deleteChat') {
|
|
685
678
|
if (!isInitialSync) {
|
|
686
679
|
ev.emit('chats.delete', [id]);
|
|
687
680
|
}
|
|
688
681
|
}
|
|
689
|
-
else if (action
|
|
682
|
+
else if (action === null || action === void 0 ? void 0 : action.labelEditAction) {
|
|
690
683
|
const { name, color, deleted, predefinedId } = action.labelEditAction;
|
|
691
684
|
ev.emit('labels.edit', {
|
|
692
|
-
id
|
|
685
|
+
id,
|
|
693
686
|
name: name,
|
|
694
687
|
color: color,
|
|
695
688
|
deleted: deleted,
|
|
696
689
|
predefinedId: predefinedId ? String(predefinedId) : undefined
|
|
697
690
|
});
|
|
698
691
|
}
|
|
699
|
-
else if (action
|
|
692
|
+
else if (action === null || action === void 0 ? void 0 : action.labelAssociationAction) {
|
|
700
693
|
ev.emit('labels.association', {
|
|
701
|
-
type: action.labelAssociationAction.labeled
|
|
702
|
-
|
|
694
|
+
type: action.labelAssociationAction.labeled
|
|
695
|
+
? 'add'
|
|
696
|
+
: 'remove',
|
|
697
|
+
association: type === LabelAssociation_1.LabelAssociationType.Chat
|
|
703
698
|
? {
|
|
704
|
-
type: LabelAssociationType.Chat,
|
|
699
|
+
type: LabelAssociation_1.LabelAssociationType.Chat,
|
|
705
700
|
chatId: syncAction.index[2],
|
|
706
701
|
labelId: syncAction.index[1]
|
|
707
702
|
}
|
|
708
703
|
: {
|
|
709
|
-
type: LabelAssociationType.Message,
|
|
704
|
+
type: LabelAssociation_1.LabelAssociationType.Message,
|
|
710
705
|
chatId: syncAction.index[2],
|
|
711
706
|
messageId: syncAction.index[3],
|
|
712
707
|
labelId: syncAction.index[1]
|
|
@@ -714,11 +709,11 @@ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) =
|
|
|
714
709
|
});
|
|
715
710
|
}
|
|
716
711
|
else {
|
|
717
|
-
logger
|
|
712
|
+
logger === null || logger === void 0 ? void 0 : logger.debug({ syncAction, id }, 'unprocessable update');
|
|
718
713
|
}
|
|
719
714
|
function getChatUpdateConditional(id, msgRange) {
|
|
720
715
|
return isInitialSync
|
|
721
|
-
? data => {
|
|
716
|
+
? (data) => {
|
|
722
717
|
const chat = data.historySets.chats[id] || data.chatUpserts[id];
|
|
723
718
|
if (chat) {
|
|
724
719
|
return msgRange ? isValidPatchBasedOnMessageRange(chat, msgRange) : true;
|
|
@@ -727,9 +722,9 @@ export const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) =
|
|
|
727
722
|
: undefined;
|
|
728
723
|
}
|
|
729
724
|
function isValidPatchBasedOnMessageRange(chat, msgRange) {
|
|
730
|
-
const lastMsgTimestamp = Number(msgRange
|
|
731
|
-
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);
|
|
732
727
|
return lastMsgTimestamp >= chatLastMsgTimestamp;
|
|
733
728
|
}
|
|
734
729
|
};
|
|
735
|
-
|
|
730
|
+
exports.processSyncAction = processSyncAction;
|