@hansaka02/baileys 7.3.2 → 7.3.6
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/lib/Defaults/baileys-version.json +2 -2
- package/lib/Defaults/connection.js +51 -0
- package/lib/Defaults/constants.js +74 -0
- package/lib/Defaults/history.js +19 -0
- package/lib/Defaults/index.js +36 -142
- package/lib/Defaults/media.js +48 -0
- package/lib/Defaults/prefix.js +18 -0
- package/lib/Signal/Group/group-session-builder.js +10 -42
- package/lib/Signal/Group/group_cipher.js +9 -6
- package/lib/Signal/Group/index.js +39 -53
- package/lib/Signal/Group/keyhelper.js +8 -41
- package/lib/Signal/Group/sender-chain-key.js +5 -18
- package/lib/Signal/Group/sender-key-distribution-message.js +7 -7
- package/lib/Signal/Group/sender-key-message.js +12 -8
- package/lib/Signal/Group/sender-key-record.js +7 -16
- package/lib/Signal/Group/sender-key-state.js +15 -61
- package/lib/Signal/Group/sender-message-key.js +2 -2
- package/lib/Signal/libsignal.js +237 -177
- package/lib/Signal/lid-mapping.js +128 -71
- package/lib/Socket/Client/types.js +2 -2
- package/lib/Socket/Client/websocket.js +25 -16
- package/lib/Socket/business.js +46 -33
- package/lib/Socket/chats.js +286 -170
- package/lib/Socket/community.js +215 -77
- package/lib/Socket/groups.js +77 -61
- package/lib/Socket/index.js +4 -4
- package/lib/Socket/messages-recv.js +629 -457
- package/lib/Socket/messages-send.js +645 -656
- package/lib/Socket/mex.js +61 -0
- package/lib/Socket/newsletter.js +166 -245
- package/lib/Socket/socket.js +396 -170
- package/lib/Store/index.js +27 -11
- package/lib/Store/make-cache-manager-store.js +14 -15
- package/lib/Store/make-in-memory-store.js +28 -24
- package/lib/Types/LabelAssociation.js +2 -2
- package/lib/Types/Message.js +6 -6
- package/lib/Types/MexUpdates.js +5 -5
- package/lib/Types/Newsletter.js +32 -25
- package/lib/Types/State.js +4 -4
- package/lib/Types/index.js +28 -12
- package/lib/Utils/auth-utils.js +212 -375
- package/lib/Utils/baileys-event-stream.js +68 -69
- package/lib/Utils/browser-utils.js +43 -0
- package/lib/Utils/business.js +63 -53
- package/lib/Utils/chat-utils.js +241 -106
- package/lib/Utils/crypto.js +25 -45
- package/lib/Utils/decode-wa-message.js +361 -311
- package/lib/Utils/event-buffer.js +97 -42
- package/lib/Utils/generics.js +90 -207
- package/lib/Utils/history.js +29 -27
- package/lib/Utils/index.js +28 -14
- package/lib/Utils/link-preview.js +24 -62
- package/lib/Utils/logger.js +5 -5
- package/lib/Utils/lt-hash.js +29 -23
- package/lib/Utils/make-mutex.js +26 -28
- package/lib/Utils/message-retry-manager.js +55 -7
- package/lib/Utils/messages-media.js +434 -247
- package/lib/Utils/messages.js +963 -917
- package/lib/Utils/noise-handler.js +60 -20
- package/lib/Utils/pre-key-manager.js +126 -0
- package/lib/Utils/process-message.js +216 -141
- package/lib/Utils/signal.js +75 -37
- package/lib/Utils/use-multi-file-auth-state.js +18 -22
- package/lib/Utils/validate-connection.js +96 -66
- package/lib/WABinary/constants.js +1268 -1268
- package/lib/WABinary/decode.js +62 -34
- package/lib/WABinary/encode.js +57 -36
- package/lib/WABinary/generic-utils.js +4 -4
- package/lib/WABinary/index.js +27 -11
- package/lib/WABinary/jid-utils.js +58 -11
- package/lib/WAM/constants.js +19064 -11563
- package/lib/WAM/encode.js +71 -14
- package/lib/WAM/index.js +27 -11
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +20 -16
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +2 -2
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +7 -4
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +2 -2
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -2
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +2 -2
- package/lib/WAUSync/Protocols/index.js +27 -11
- package/lib/WAUSync/USyncQuery.js +51 -28
- package/lib/WAUSync/index.js +27 -11
- package/lib/index.js +60 -31
- package/package.json +12 -17
- package/WAProto/AICommon/AICommon.d.ts +0 -11702
- package/WAProto/Adv/Adv.d.ts +0 -643
- package/WAProto/BotMetadata/BotMetadata.d.ts +0 -5654
- package/WAProto/Cert/Cert.d.ts +0 -613
- package/WAProto/ChatLockSettings/ChatLockSettings.d.ts +0 -476
- package/WAProto/CompanionReg/CompanionReg.d.ts +0 -1361
- package/WAProto/DeviceCapabilities/DeviceCapabilities.d.ts +0 -577
- package/WAProto/E2E/E2E.d.ts +0 -41724
- package/WAProto/Ephemeral/Ephemeral.d.ts +0 -114
- package/WAProto/HistorySync/HistorySync.d.ts +0 -51700
- package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.d.ts +0 -229
- package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.d.ts +0 -583
- package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.d.ts +0 -42897
- package/WAProto/MmsRetry/MmsRetry.d.ts +0 -243
- package/WAProto/Protocol/Protocol.d.ts +0 -270
- package/WAProto/Reporting/Reporting.d.ts +0 -371
- package/WAProto/ServerSync/ServerSync.d.ts +0 -1285
- package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.d.ts +0 -1868
- package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.d.ts +0 -767
- package/WAProto/StatusAttributions/StatusAttributions.d.ts +0 -1027
- package/WAProto/SyncAction/SyncAction.d.ts +0 -11193
- package/WAProto/UserPassword/UserPassword.d.ts +0 -363
- package/WAProto/VnameCert/VnameCert.d.ts +0 -821
- package/WAProto/Wa6/Wa6.d.ts +0 -2128
- package/WAProto/Web/Web.d.ts +0 -46383
- package/WAProto/index.d.ts +0 -55
- package/lib/Defaults/index.d.ts +0 -77
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
- package/lib/Signal/Group/group-session-builder.d.ts +0 -17
- package/lib/Signal/Group/group_cipher.d.ts +0 -19
- package/lib/Signal/Group/index.d.ts +0 -11
- package/lib/Signal/Group/keyhelper.d.ts +0 -16
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
- package/lib/Signal/Group/sender-key-message.d.ts +0 -19
- package/lib/Signal/Group/sender-key-name.d.ts +0 -19
- package/lib/Signal/Group/sender-key-record.d.ts +0 -32
- package/lib/Signal/Group/sender-key-state.d.ts +0 -44
- package/lib/Signal/Group/sender-message-key.d.ts +0 -11
- package/lib/Signal/libsignal.d.ts +0 -8
- package/lib/Signal/lid-mapping.d.ts +0 -28
- package/lib/Socket/Client/index.d.ts +0 -2
- package/lib/Socket/Client/types.d.ts +0 -16
- package/lib/Socket/Client/websocket.d.ts +0 -13
- package/lib/Socket/business.d.ts +0 -187
- package/lib/Socket/chats.d.ts +0 -97
- package/lib/Socket/community.d.ts +0 -129
- package/lib/Socket/groups.d.ts +0 -129
- package/lib/Socket/index.d.ts +0 -191
- package/lib/Socket/messages-recv.d.ts +0 -174
- package/lib/Socket/messages-send.d.ts +0 -165
- package/lib/Socket/newsletter.d.ts +0 -145
- package/lib/Socket/socket.d.ts +0 -45
- package/lib/Socket/usync.d.ts +0 -37
- package/lib/Socket/usync.js +0 -83
- package/lib/Store/index.d.ts +0 -4
- package/lib/Store/make-cache-manager-store.d.ts +0 -14
- package/lib/Store/make-in-memory-store.d.ts +0 -123
- package/lib/Store/make-ordered-dictionary.d.ts +0 -12
- package/lib/Store/object-repository.d.ts +0 -10
- package/lib/Types/Auth.d.ts +0 -121
- package/lib/Types/Bussiness.d.ts +0 -28
- package/lib/Types/Call.d.ts +0 -14
- package/lib/Types/Chat.d.ts +0 -143
- package/lib/Types/Contact.d.ts +0 -23
- package/lib/Types/Events.d.ts +0 -226
- package/lib/Types/GroupMetadata.d.ts +0 -66
- package/lib/Types/Label.d.ts +0 -48
- package/lib/Types/LabelAssociation.d.ts +0 -35
- package/lib/Types/Message.d.ts +0 -484
- package/lib/Types/MexUpdates.d.ts +0 -9
- package/lib/Types/Newsletter.d.ts +0 -109
- package/lib/Types/Product.d.ts +0 -92
- package/lib/Types/Signal.d.ts +0 -98
- package/lib/Types/Socket.d.ts +0 -141
- package/lib/Types/State.d.ts +0 -41
- package/lib/Types/USync.d.ts +0 -26
- package/lib/Types/index.d.ts +0 -80
- package/lib/Utils/auth-utils.d.ts +0 -21
- package/lib/Utils/baileys-event-stream.d.ts +0 -18
- package/lib/Utils/business.d.ts +0 -29
- package/lib/Utils/chat-utils.d.ts +0 -82
- package/lib/Utils/crypto.d.ts +0 -56
- package/lib/Utils/decode-wa-message.d.ts +0 -53
- package/lib/Utils/event-buffer.d.ts +0 -39
- package/lib/Utils/generics.d.ts +0 -117
- package/lib/Utils/history.d.ts +0 -23
- package/lib/Utils/index.d.ts +0 -20
- package/lib/Utils/link-preview.d.ts +0 -23
- package/lib/Utils/logger.d.ts +0 -13
- package/lib/Utils/lt-hash.d.ts +0 -14
- package/lib/Utils/make-mutex.d.ts +0 -9
- package/lib/Utils/message-retry-manager.d.ts +0 -88
- package/lib/Utils/messages-media.d.ts +0 -135
- package/lib/Utils/messages.d.ts +0 -105
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/process-message.d.ts +0 -49
- package/lib/Utils/signal.d.ts +0 -42
- package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
- package/lib/Utils/use-mongo-file-auth-state.js +0 -84
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
- package/lib/Utils/use-single-file-auth-state.js +0 -80
- package/lib/Utils/validate-connection.d.ts +0 -13
- package/lib/WABinary/constants.d.ts +0 -30
- package/lib/WABinary/decode.d.ts +0 -9
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -28
- package/lib/WABinary/index.d.ts +0 -5
- package/lib/WABinary/jid-utils.d.ts +0 -58
- package/lib/WABinary/types.d.ts +0 -22
- package/lib/WAM/BinaryInfo.d.ts +0 -16
- package/lib/WAM/constants.d.ts +0 -47
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -3
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +0 -28
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -26
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -14
- package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -14
- package/lib/WAUSync/Protocols/index.d.ts +0 -6
- package/lib/WAUSync/USyncQuery.d.ts +0 -31
- package/lib/WAUSync/USyncUser.d.ts +0 -12
- package/lib/WAUSync/index.d.ts +0 -3
- package/lib/index.d.ts +0 -13
package/lib/Utils/chat-utils.js
CHANGED
|
@@ -2,17 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true })
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
const { Boom } = require("@hapi/boom")
|
|
6
|
+
const { proto } = require("../../WAProto")
|
|
7
|
+
const { LabelAssociationType } = require("../Types/LabelAssociation")
|
|
8
|
+
const {
|
|
9
|
+
getBinaryNodeChild,
|
|
10
|
+
getBinaryNodeChildren,
|
|
11
|
+
isJidGroup,
|
|
12
|
+
jidNormalizedUser
|
|
13
|
+
} = require("../WABinary")
|
|
14
|
+
const {
|
|
15
|
+
hkdf,
|
|
16
|
+
hmacSign,
|
|
17
|
+
aesEncrypt,
|
|
18
|
+
aesDecrypt
|
|
19
|
+
} = require("./crypto")
|
|
20
|
+
const { toNumber } = require("./generics")
|
|
21
|
+
const { LT_HASH_ANTI_TAMPERING } = require("./lt-hash")
|
|
22
|
+
const { downloadContentFromMessage } = require("./messages-media")
|
|
13
23
|
|
|
14
24
|
const mutationKeys = async (keydata) => {
|
|
15
|
-
const expanded = await
|
|
25
|
+
const expanded = await hkdf(keydata, 160, { info: 'WhatsApp Mutation Keys' })
|
|
16
26
|
return {
|
|
17
27
|
indexKey: expanded.slice(0, 32),
|
|
18
28
|
valueEncryptionKey: expanded.slice(32, 64),
|
|
@@ -26,10 +36,10 @@ const generateMac = (operation, data, keyId, key) => {
|
|
|
26
36
|
const getKeyData = () => {
|
|
27
37
|
let r
|
|
28
38
|
switch (operation) {
|
|
29
|
-
case
|
|
39
|
+
case proto.SyncdMutation.SyncdOperation.SET:
|
|
30
40
|
r = 0x01
|
|
31
41
|
break
|
|
32
|
-
case
|
|
42
|
+
case proto.SyncdMutation.SyncdOperation.REMOVE:
|
|
33
43
|
r = 0x02
|
|
34
44
|
break
|
|
35
45
|
}
|
|
@@ -40,7 +50,7 @@ const generateMac = (operation, data, keyId, key) => {
|
|
|
40
50
|
const last = Buffer.alloc(8) // 8 bytes
|
|
41
51
|
last.set([keyData.length], last.length - 1)
|
|
42
52
|
const total = Buffer.concat([keyData, data, last])
|
|
43
|
-
const hmac =
|
|
53
|
+
const hmac = hmacSign(total, key, 'sha512')
|
|
44
54
|
return hmac.slice(0, 32)
|
|
45
55
|
}
|
|
46
56
|
|
|
@@ -58,9 +68,9 @@ const makeLtHashGenerator = ({ indexValueMap, hash }) => {
|
|
|
58
68
|
mix: ({ indexMac, valueMac, operation }) => {
|
|
59
69
|
const indexMacBase64 = Buffer.from(indexMac).toString('base64')
|
|
60
70
|
const prevOp = indexValueMap[indexMacBase64]
|
|
61
|
-
if (operation ===
|
|
71
|
+
if (operation === proto.SyncdMutation.SyncdOperation.REMOVE) {
|
|
62
72
|
if (!prevOp) {
|
|
63
|
-
throw new
|
|
73
|
+
throw new Boom('tried remove, but no previous op', { data: { indexMac, valueMac } })
|
|
64
74
|
}
|
|
65
75
|
// remove from index value mac, since this mutation is erased
|
|
66
76
|
delete indexValueMap[indexMacBase64]
|
|
@@ -76,7 +86,7 @@ const makeLtHashGenerator = ({ indexValueMap, hash }) => {
|
|
|
76
86
|
},
|
|
77
87
|
finish: async () => {
|
|
78
88
|
const hashArrayBuffer = new Uint8Array(hash).buffer
|
|
79
|
-
const result = await
|
|
89
|
+
const result = await LT_HASH_ANTI_TAMPERING.subtractThenAdd(hashArrayBuffer, addBuffs, subBuffs)
|
|
80
90
|
const buffer = Buffer.from(result)
|
|
81
91
|
return {
|
|
82
92
|
hash: buffer,
|
|
@@ -92,7 +102,7 @@ const generateSnapshotMac = (lthash, version, name, key) => {
|
|
|
92
102
|
to64BitNetworkOrder(version),
|
|
93
103
|
Buffer.from(name, 'utf-8')
|
|
94
104
|
])
|
|
95
|
-
return
|
|
105
|
+
return hmacSign(total, key, 'sha256')
|
|
96
106
|
}
|
|
97
107
|
|
|
98
108
|
const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
|
|
@@ -102,7 +112,7 @@ const generatePatchMac = (snapshotMac, valueMacs, version, type, key) => {
|
|
|
102
112
|
to64BitNetworkOrder(version),
|
|
103
113
|
Buffer.from(type, 'utf-8')
|
|
104
114
|
])
|
|
105
|
-
return
|
|
115
|
+
return hmacSign(total, key)
|
|
106
116
|
}
|
|
107
117
|
|
|
108
118
|
const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} })
|
|
@@ -110,22 +120,22 @@ const newLTHashState = () => ({ version: 0, hash: Buffer.alloc(128), indexValueM
|
|
|
110
120
|
const encodeSyncdPatch = async ({ type, index, syncAction, apiVersion, operation }, myAppStateKeyId, state, getAppStateSyncKey) => {
|
|
111
121
|
const key = !!myAppStateKeyId ? await getAppStateSyncKey(myAppStateKeyId) : undefined
|
|
112
122
|
if (!key) {
|
|
113
|
-
throw new
|
|
123
|
+
throw new Boom(`myAppStateKey ("${myAppStateKeyId}") not present`, { statusCode: 404 })
|
|
114
124
|
}
|
|
115
125
|
const encKeyId = Buffer.from(myAppStateKeyId, 'base64')
|
|
116
126
|
state = { ...state, indexValueMap: { ...state.indexValueMap } }
|
|
117
127
|
const indexBuffer = Buffer.from(JSON.stringify(index))
|
|
118
|
-
const dataProto =
|
|
128
|
+
const dataProto = proto.SyncActionData.fromObject({
|
|
119
129
|
index: indexBuffer,
|
|
120
130
|
value: syncAction,
|
|
121
131
|
padding: new Uint8Array(0),
|
|
122
132
|
version: apiVersion
|
|
123
133
|
})
|
|
124
|
-
const encoded =
|
|
134
|
+
const encoded = proto.SyncActionData.encode(dataProto).finish()
|
|
125
135
|
const keyValue = await mutationKeys(key.keyData)
|
|
126
|
-
const encValue =
|
|
136
|
+
const encValue = aesEncrypt(encoded, keyValue.valueEncryptionKey)
|
|
127
137
|
const valueMac = generateMac(operation, encValue, encKeyId, keyValue.valueMacKey)
|
|
128
|
-
const indexMac =
|
|
138
|
+
const indexMac = hmacSign(indexBuffer, keyValue.indexKey)
|
|
129
139
|
// update LT hash
|
|
130
140
|
const generator = makeLtHashGenerator(state)
|
|
131
141
|
generator.mix({ indexMac, valueMac, operation })
|
|
@@ -164,8 +174,8 @@ const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncK
|
|
|
164
174
|
for (const msgMutation of msgMutations) {
|
|
165
175
|
// if it's a syncdmutation, get the operation property
|
|
166
176
|
// otherwise, if it's only a record -- it'll be a SET mutation
|
|
167
|
-
const operation = 'operation' in msgMutation ? msgMutation.operation :
|
|
168
|
-
const record =
|
|
177
|
+
const operation = 'operation' in msgMutation ? msgMutation.operation : proto.SyncdMutation.SyncdOperation.SET
|
|
178
|
+
const record = 'record' in msgMutation && !!msgMutation.record ? msgMutation.record : msgMutation
|
|
169
179
|
const key = await getKey(record.keyId.id)
|
|
170
180
|
const content = Buffer.from(record.value.blob)
|
|
171
181
|
const encContent = content.slice(0, -32)
|
|
@@ -173,15 +183,15 @@ const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncK
|
|
|
173
183
|
if (validateMacs) {
|
|
174
184
|
const contentHmac = generateMac(operation, encContent, record.keyId.id, key.valueMacKey)
|
|
175
185
|
if (Buffer.compare(contentHmac, ogValueMac) !== 0) {
|
|
176
|
-
throw new
|
|
186
|
+
throw new Boom('HMAC content verification failed')
|
|
177
187
|
}
|
|
178
188
|
}
|
|
179
|
-
const result =
|
|
180
|
-
const syncAction =
|
|
189
|
+
const result = aesDecrypt(encContent, key.valueEncryptionKey)
|
|
190
|
+
const syncAction = proto.SyncActionData.decode(result)
|
|
181
191
|
if (validateMacs) {
|
|
182
|
-
const hmac =
|
|
192
|
+
const hmac = hmacSign(syncAction.index, key.indexKey)
|
|
183
193
|
if (Buffer.compare(hmac, record.index.blob) !== 0) {
|
|
184
|
-
throw new
|
|
194
|
+
throw new Boom('HMAC index verification failed')
|
|
185
195
|
}
|
|
186
196
|
}
|
|
187
197
|
const indexStr = Buffer.from(syncAction.index).toString()
|
|
@@ -197,7 +207,7 @@ const decodeSyncdMutations = async (msgMutations, initialState, getAppStateSyncK
|
|
|
197
207
|
const base64Key = Buffer.from(keyId).toString('base64')
|
|
198
208
|
const keyEnc = await getAppStateSyncKey(base64Key)
|
|
199
209
|
if (!keyEnc) {
|
|
200
|
-
throw new
|
|
210
|
+
throw new Boom(`failed to find key "${base64Key}" to decode mutation`, { statusCode: 404, data: { msgMutations } })
|
|
201
211
|
}
|
|
202
212
|
return mutationKeys(keyEnc.keyData)
|
|
203
213
|
}
|
|
@@ -208,13 +218,13 @@ const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onM
|
|
|
208
218
|
const base64Key = Buffer.from(msg.keyId.id).toString('base64')
|
|
209
219
|
const mainKeyObj = await getAppStateSyncKey(base64Key)
|
|
210
220
|
if (!mainKeyObj) {
|
|
211
|
-
throw new
|
|
221
|
+
throw new Boom(`failed to find key "${base64Key}" to decode patch`, { statusCode: 404, data: { msg } })
|
|
212
222
|
}
|
|
213
223
|
const mainKey = await mutationKeys(mainKeyObj.keyData)
|
|
214
224
|
const mutationmacs = msg.mutations.map(mutation => mutation.record.value.blob.slice(-32))
|
|
215
|
-
const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs,
|
|
225
|
+
const patchMac = generatePatchMac(msg.snapshotMac, mutationmacs, toNumber(msg.version.version), name, mainKey.patchMacKey)
|
|
216
226
|
if (Buffer.compare(patchMac, msg.patchMac) !== 0) {
|
|
217
|
-
throw new
|
|
227
|
+
throw new Boom('Invalid patch mac')
|
|
218
228
|
}
|
|
219
229
|
}
|
|
220
230
|
const result = await decodeSyncdMutations(msg.mutations, initialState, getAppStateSyncKey, onMutation, validateMacs)
|
|
@@ -222,13 +232,13 @@ const decodeSyncdPatch = async (msg, name, initialState, getAppStateSyncKey, onM
|
|
|
222
232
|
}
|
|
223
233
|
|
|
224
234
|
const extractSyncdPatches = async (result, options) => {
|
|
225
|
-
const syncNode =
|
|
226
|
-
const collectionNodes =
|
|
235
|
+
const syncNode = getBinaryNodeChild(result, 'sync')
|
|
236
|
+
const collectionNodes = getBinaryNodeChildren(syncNode, 'collection')
|
|
227
237
|
const final = {}
|
|
228
238
|
await Promise.all(collectionNodes.map(async (collectionNode) => {
|
|
229
|
-
const patchesNode =
|
|
230
|
-
const patches =
|
|
231
|
-
const snapshotNode =
|
|
239
|
+
const patchesNode = getBinaryNodeChild(collectionNode, 'patches')
|
|
240
|
+
const patches = getBinaryNodeChildren(patchesNode || collectionNode, 'patch')
|
|
241
|
+
const snapshotNode = getBinaryNodeChild(collectionNode, 'snapshot')
|
|
232
242
|
const syncds = []
|
|
233
243
|
const name = collectionNode.attrs.name
|
|
234
244
|
const hasMorePatches = collectionNode.attrs.has_more_patches === 'true'
|
|
@@ -237,16 +247,16 @@ const extractSyncdPatches = async (result, options) => {
|
|
|
237
247
|
if (!Buffer.isBuffer(snapshotNode)) {
|
|
238
248
|
snapshotNode.content = Buffer.from(Object.values(snapshotNode.content))
|
|
239
249
|
}
|
|
240
|
-
const blobRef =
|
|
250
|
+
const blobRef = proto.ExternalBlobReference.decode(snapshotNode.content)
|
|
241
251
|
const data = await downloadExternalBlob(blobRef, options)
|
|
242
|
-
snapshot =
|
|
252
|
+
snapshot = proto.SyncdSnapshot.decode(data)
|
|
243
253
|
}
|
|
244
254
|
for (let { content } of patches) {
|
|
245
255
|
if (content) {
|
|
246
256
|
if (!Buffer.isBuffer(content)) {
|
|
247
257
|
content = Buffer.from(Object.values(content))
|
|
248
258
|
}
|
|
249
|
-
const syncd =
|
|
259
|
+
const syncd = proto.SyncdPatch.decode(content)
|
|
250
260
|
if (!syncd.version) {
|
|
251
261
|
syncd.version = { version: +collectionNode.attrs.version + 1 }
|
|
252
262
|
}
|
|
@@ -259,7 +269,7 @@ const extractSyncdPatches = async (result, options) => {
|
|
|
259
269
|
}
|
|
260
270
|
|
|
261
271
|
const downloadExternalBlob = async (blob, options) => {
|
|
262
|
-
const stream = await
|
|
272
|
+
const stream = await downloadContentFromMessage(blob, 'md-app-state', { options })
|
|
263
273
|
const bufferArray = []
|
|
264
274
|
for await (const chunk of stream) {
|
|
265
275
|
bufferArray.push(chunk)
|
|
@@ -269,13 +279,13 @@ const downloadExternalBlob = async (blob, options) => {
|
|
|
269
279
|
|
|
270
280
|
const downloadExternalPatch = async (blob, options) => {
|
|
271
281
|
const buffer = await downloadExternalBlob(blob, options)
|
|
272
|
-
const syncData =
|
|
282
|
+
const syncData = proto.SyncdMutations.decode(buffer)
|
|
273
283
|
return syncData
|
|
274
284
|
}
|
|
275
285
|
|
|
276
286
|
const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVersionNumber, validateMacs = true) => {
|
|
277
287
|
const newState = newLTHashState()
|
|
278
|
-
newState.version =
|
|
288
|
+
newState.version = toNumber(snapshot.version.version)
|
|
279
289
|
const mutationMap = {}
|
|
280
290
|
const areMutationsRequired = typeof minimumVersionNumber === 'undefined'
|
|
281
291
|
|| newState.version > minimumVersionNumber
|
|
@@ -291,12 +301,12 @@ const decodeSyncdSnapshot = async (name, snapshot, getAppStateSyncKey, minimumVe
|
|
|
291
301
|
const base64Key = Buffer.from(snapshot.keyId.id).toString('base64')
|
|
292
302
|
const keyEnc = await getAppStateSyncKey(base64Key)
|
|
293
303
|
if (!keyEnc) {
|
|
294
|
-
throw new
|
|
304
|
+
throw new Boom(`failed to find key "${base64Key}" to decode mutation`)
|
|
295
305
|
}
|
|
296
306
|
const result = await mutationKeys(keyEnc.keyData)
|
|
297
307
|
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
|
|
298
308
|
if (Buffer.compare(snapshot.mac, computedSnapshotMac) !== 0) {
|
|
299
|
-
throw new
|
|
309
|
+
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name} from snapshot`)
|
|
300
310
|
}
|
|
301
311
|
}
|
|
302
312
|
return {
|
|
@@ -310,39 +320,52 @@ const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options,
|
|
|
310
320
|
...initial,
|
|
311
321
|
indexValueMap: { ...initial.indexValueMap }
|
|
312
322
|
}
|
|
323
|
+
|
|
313
324
|
const mutationMap = {}
|
|
325
|
+
|
|
314
326
|
for (const syncd of syncds) {
|
|
315
327
|
const { version, keyId, snapshotMac } = syncd
|
|
328
|
+
|
|
316
329
|
if (syncd.externalMutations) {
|
|
317
330
|
logger?.trace({ name, version }, 'downloading external patch')
|
|
331
|
+
|
|
318
332
|
const ref = await downloadExternalPatch(syncd.externalMutations, options)
|
|
333
|
+
|
|
319
334
|
logger?.debug({ name, version, mutations: ref.mutations.length }, 'downloaded external patch')
|
|
320
|
-
syncd.mutations?.push(...ref.mutations)
|
|
335
|
+
syncd.mutations?.push(...ref.mutations);
|
|
321
336
|
}
|
|
322
|
-
|
|
337
|
+
|
|
338
|
+
const patchVersion = toNumber(version.version)
|
|
339
|
+
|
|
323
340
|
newState.version = patchVersion
|
|
341
|
+
|
|
324
342
|
const shouldMutate = typeof minimumVersionNumber === 'undefined' || patchVersion > minimumVersionNumber
|
|
325
343
|
const decodeResult = await decodeSyncdPatch(syncd, name, newState, getAppStateSyncKey, shouldMutate
|
|
326
344
|
? mutation => {
|
|
327
|
-
var _a
|
|
328
345
|
const index = mutation.syncAction.index?.toString()
|
|
329
346
|
mutationMap[index] = mutation
|
|
330
347
|
}
|
|
331
|
-
: (
|
|
348
|
+
: () => { }, true)
|
|
349
|
+
|
|
332
350
|
newState.hash = decodeResult.hash
|
|
333
351
|
newState.indexValueMap = decodeResult.indexValueMap
|
|
352
|
+
|
|
334
353
|
if (validateMacs) {
|
|
335
354
|
const base64Key = Buffer.from(keyId.id).toString('base64')
|
|
336
355
|
const keyEnc = await getAppStateSyncKey(base64Key)
|
|
356
|
+
|
|
337
357
|
if (!keyEnc) {
|
|
338
|
-
throw new
|
|
358
|
+
throw new Boom(`failed to find key "${base64Key}" to decode mutation`)
|
|
339
359
|
}
|
|
360
|
+
|
|
340
361
|
const result = await mutationKeys(keyEnc.keyData)
|
|
341
362
|
const computedSnapshotMac = generateSnapshotMac(newState.hash, newState.version, name, result.snapshotMacKey)
|
|
363
|
+
|
|
342
364
|
if (Buffer.compare(snapshotMac, computedSnapshotMac) !== 0) {
|
|
343
|
-
throw new
|
|
365
|
+
throw new Boom(`failed to verify LTHash at ${newState.version} of ${name}`)
|
|
344
366
|
}
|
|
345
367
|
}
|
|
368
|
+
|
|
346
369
|
// clear memory used up by the mutations
|
|
347
370
|
syncd.mutations = []
|
|
348
371
|
}
|
|
@@ -350,36 +373,47 @@ const decodePatches = async (name, syncds, initial, getAppStateSyncKey, options,
|
|
|
350
373
|
}
|
|
351
374
|
|
|
352
375
|
const chatModificationToAppPatch = (mod, jid) => {
|
|
353
|
-
const OP =
|
|
376
|
+
const OP = proto.SyncdMutation.SyncdOperation
|
|
354
377
|
const getMessageRange = (lastMessages) => {
|
|
355
378
|
let messageRange
|
|
379
|
+
|
|
356
380
|
if (Array.isArray(lastMessages)) {
|
|
357
381
|
const lastMsg = lastMessages[lastMessages.length - 1]
|
|
358
382
|
messageRange = {
|
|
359
383
|
lastMessageTimestamp: lastMsg?.messageTimestamp,
|
|
360
|
-
messages: lastMessages?.length
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
384
|
+
messages: lastMessages?.length
|
|
385
|
+
? lastMessages.map(m => {
|
|
386
|
+
if (!m.key?.id || !m.key?.remoteJid) {
|
|
387
|
+
throw new Boom('Incomplete key', { statusCode: 400, data: m })
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) {
|
|
391
|
+
throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m })
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (!m.messageTimestamp || !toNumber(m.messageTimestamp)) {
|
|
395
|
+
throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m })
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (m.key.participant) {
|
|
399
|
+
m.key.participant = jidNormalizedUser(m.key.participant)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return m
|
|
403
|
+
})
|
|
404
|
+
: undefined
|
|
375
405
|
}
|
|
376
406
|
}
|
|
407
|
+
|
|
377
408
|
else {
|
|
378
409
|
messageRange = lastMessages
|
|
379
410
|
}
|
|
411
|
+
|
|
380
412
|
return messageRange
|
|
381
413
|
}
|
|
414
|
+
|
|
382
415
|
let patch
|
|
416
|
+
|
|
383
417
|
if ('mute' in mod) {
|
|
384
418
|
patch = {
|
|
385
419
|
syncAction: {
|
|
@@ -394,6 +428,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
394
428
|
operation: OP.SET
|
|
395
429
|
}
|
|
396
430
|
}
|
|
431
|
+
|
|
397
432
|
else if ('archive' in mod) {
|
|
398
433
|
patch = {
|
|
399
434
|
syncAction: {
|
|
@@ -408,6 +443,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
408
443
|
operation: OP.SET
|
|
409
444
|
}
|
|
410
445
|
}
|
|
446
|
+
|
|
411
447
|
else if ('markRead' in mod) {
|
|
412
448
|
patch = {
|
|
413
449
|
syncAction: {
|
|
@@ -422,6 +458,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
422
458
|
operation: OP.SET
|
|
423
459
|
}
|
|
424
460
|
}
|
|
461
|
+
|
|
425
462
|
else if ('deleteForMe' in mod) {
|
|
426
463
|
const { timestamp, key, deleteMedia } = mod.deleteForMe
|
|
427
464
|
patch = {
|
|
@@ -437,6 +474,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
437
474
|
operation: OP.SET
|
|
438
475
|
}
|
|
439
476
|
}
|
|
477
|
+
|
|
440
478
|
else if ('clear' in mod) {
|
|
441
479
|
patch = {
|
|
442
480
|
syncAction: {
|
|
@@ -450,6 +488,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
450
488
|
operation: OP.SET
|
|
451
489
|
}
|
|
452
490
|
}
|
|
491
|
+
|
|
453
492
|
else if ('pin' in mod) {
|
|
454
493
|
patch = {
|
|
455
494
|
syncAction: {
|
|
@@ -463,17 +502,19 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
463
502
|
operation: OP.SET
|
|
464
503
|
}
|
|
465
504
|
}
|
|
505
|
+
|
|
466
506
|
else if ('contact' in mod) {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
},
|
|
471
|
-
index: ['contact', jid],
|
|
472
|
-
type: 'critical_unblock_low',
|
|
507
|
+
patch = {
|
|
508
|
+
syncAction: {
|
|
509
|
+
contactAction: mod.contact || {}
|
|
510
|
+
},
|
|
511
|
+
index: ['contact', jid],
|
|
512
|
+
type: 'critical_unblock_low',
|
|
473
513
|
apiVersion: 2,
|
|
474
514
|
operation: mod.contact ? OP.SET : OP.REMOVE
|
|
475
515
|
}
|
|
476
516
|
}
|
|
517
|
+
|
|
477
518
|
else if ('disableLinkPreviews' in mod) {
|
|
478
519
|
patch = {
|
|
479
520
|
syncAction: {
|
|
@@ -485,6 +526,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
485
526
|
operation: OP.SET
|
|
486
527
|
}
|
|
487
528
|
}
|
|
529
|
+
|
|
488
530
|
else if ('star' in mod) {
|
|
489
531
|
const key = mod.star.messages[0]
|
|
490
532
|
patch = {
|
|
@@ -499,6 +541,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
499
541
|
operation: OP.SET
|
|
500
542
|
}
|
|
501
543
|
}
|
|
544
|
+
|
|
502
545
|
else if ('delete' in mod) {
|
|
503
546
|
patch = {
|
|
504
547
|
syncAction: {
|
|
@@ -512,6 +555,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
512
555
|
operation: OP.SET
|
|
513
556
|
}
|
|
514
557
|
}
|
|
558
|
+
|
|
515
559
|
else if ('pushNameSetting' in mod) {
|
|
516
560
|
patch = {
|
|
517
561
|
syncAction: {
|
|
@@ -522,9 +566,10 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
522
566
|
index: ['setting_pushName'],
|
|
523
567
|
type: 'critical_block',
|
|
524
568
|
apiVersion: 1,
|
|
525
|
-
operation: OP.SET
|
|
569
|
+
operation: OP.SET
|
|
526
570
|
}
|
|
527
571
|
}
|
|
572
|
+
|
|
528
573
|
else if ('quickReply' in mod) {
|
|
529
574
|
patch = {
|
|
530
575
|
syncAction: {
|
|
@@ -542,6 +587,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
542
587
|
operation: OP.SET
|
|
543
588
|
}
|
|
544
589
|
}
|
|
590
|
+
|
|
545
591
|
else if ('addLabel' in mod) {
|
|
546
592
|
patch = {
|
|
547
593
|
syncAction: {
|
|
@@ -555,9 +601,10 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
555
601
|
index: ['label_edit', mod.addLabel.id],
|
|
556
602
|
type: 'regular',
|
|
557
603
|
apiVersion: 3,
|
|
558
|
-
operation: OP.SET
|
|
604
|
+
operation: OP.SET
|
|
559
605
|
}
|
|
560
606
|
}
|
|
607
|
+
|
|
561
608
|
else if ('addChatLabel' in mod) {
|
|
562
609
|
patch = {
|
|
563
610
|
syncAction: {
|
|
@@ -565,25 +612,27 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
565
612
|
labeled: true,
|
|
566
613
|
}
|
|
567
614
|
},
|
|
568
|
-
index: [
|
|
615
|
+
index: [LabelAssociationType.Chat, mod.addChatLabel.labelId, jid],
|
|
569
616
|
type: 'regular',
|
|
570
617
|
apiVersion: 3,
|
|
571
|
-
operation: OP.SET
|
|
618
|
+
operation: OP.SET
|
|
572
619
|
}
|
|
573
620
|
}
|
|
621
|
+
|
|
574
622
|
else if ('removeChatLabel' in mod) {
|
|
575
623
|
patch = {
|
|
576
624
|
syncAction: {
|
|
577
625
|
labelAssociationAction: {
|
|
578
|
-
labeled: false
|
|
626
|
+
labeled: false
|
|
579
627
|
}
|
|
580
628
|
},
|
|
581
|
-
index: [
|
|
629
|
+
index: [LabelAssociationType.Chat, mod.removeChatLabel.labelId, jid],
|
|
582
630
|
type: 'regular',
|
|
583
631
|
apiVersion: 3,
|
|
584
|
-
operation: OP.SET
|
|
632
|
+
operation: OP.SET
|
|
585
633
|
}
|
|
586
634
|
}
|
|
635
|
+
|
|
587
636
|
else if ('addMessageLabel' in mod) {
|
|
588
637
|
patch = {
|
|
589
638
|
syncAction: {
|
|
@@ -592,7 +641,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
592
641
|
}
|
|
593
642
|
},
|
|
594
643
|
index: [
|
|
595
|
-
|
|
644
|
+
LabelAssociationType.Message,
|
|
596
645
|
mod.addMessageLabel.labelId,
|
|
597
646
|
jid,
|
|
598
647
|
mod.addMessageLabel.messageId,
|
|
@@ -601,9 +650,10 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
601
650
|
],
|
|
602
651
|
type: 'regular',
|
|
603
652
|
apiVersion: 3,
|
|
604
|
-
operation: OP.SET
|
|
653
|
+
operation: OP.SET
|
|
605
654
|
}
|
|
606
655
|
}
|
|
656
|
+
|
|
607
657
|
else if ('removeMessageLabel' in mod) {
|
|
608
658
|
patch = {
|
|
609
659
|
syncAction: {
|
|
@@ -612,7 +662,7 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
612
662
|
}
|
|
613
663
|
},
|
|
614
664
|
index: [
|
|
615
|
-
|
|
665
|
+
LabelAssociationType.Message,
|
|
616
666
|
mod.removeMessageLabel.labelId,
|
|
617
667
|
jid,
|
|
618
668
|
mod.removeMessageLabel.messageId,
|
|
@@ -621,32 +671,39 @@ const chatModificationToAppPatch = (mod, jid) => {
|
|
|
621
671
|
],
|
|
622
672
|
type: 'regular',
|
|
623
673
|
apiVersion: 3,
|
|
624
|
-
operation: OP.SET
|
|
674
|
+
operation: OP.SET
|
|
625
675
|
}
|
|
626
676
|
}
|
|
677
|
+
|
|
627
678
|
else {
|
|
628
|
-
throw new
|
|
679
|
+
throw new Boom('not supported')
|
|
629
680
|
}
|
|
681
|
+
|
|
630
682
|
patch.syncAction.timestamp = Date.now()
|
|
683
|
+
|
|
631
684
|
return patch
|
|
632
685
|
}
|
|
633
686
|
|
|
634
687
|
const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
635
688
|
const isInitialSync = !!initialSyncOpts
|
|
636
689
|
const accountSettings = initialSyncOpts?.accountSettings
|
|
690
|
+
|
|
637
691
|
logger?.trace({ syncAction, initialSync: !!initialSyncOpts }, 'processing sync action')
|
|
692
|
+
|
|
638
693
|
const { syncAction: { value: action }, index: [type, id, msgId, fromMe] } = syncAction
|
|
694
|
+
|
|
639
695
|
if (action?.muteAction) {
|
|
640
696
|
ev.emit('chats.update', [
|
|
641
697
|
{
|
|
642
698
|
id,
|
|
643
699
|
muteEndTime: action.muteAction?.muted
|
|
644
|
-
?
|
|
700
|
+
? toNumber(action.muteAction.muteEndTimestamp)
|
|
645
701
|
: null,
|
|
646
702
|
conditional: getChatUpdateConditional(id, undefined)
|
|
647
703
|
}
|
|
648
704
|
])
|
|
649
705
|
}
|
|
706
|
+
|
|
650
707
|
else if (action?.archiveChatAction || type === 'archive' || type === 'unarchive') {
|
|
651
708
|
// okay so we've to do some annoying computation here
|
|
652
709
|
// when we're initially syncing the app state
|
|
@@ -654,27 +711,28 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
|
654
711
|
// 1. if the account unarchiveChats setting is true
|
|
655
712
|
// a. if the chat is archived, and no further messages have been received -- simple, keep archived
|
|
656
713
|
// b. if the chat was archived, and the user received messages from the other person afterwards
|
|
657
|
-
//
|
|
658
|
-
//
|
|
714
|
+
// then the chat should be marked unarchved --
|
|
715
|
+
// we compare the timestamp of latest message from the other person to determine this
|
|
659
716
|
// 2. if the account unarchiveChats setting is false -- then it doesn't matter,
|
|
660
|
-
//
|
|
717
|
+
// it'll always take an app state action to mark in unarchived -- which we'll get anyway
|
|
661
718
|
const archiveAction = action?.archiveChatAction
|
|
662
|
-
const isArchived = archiveAction
|
|
663
|
-
? archiveAction.archived
|
|
664
|
-
: type === 'archive'
|
|
719
|
+
const isArchived = archiveAction ? archiveAction.archived : type === 'archive'
|
|
665
720
|
// // basically we don't need to fire an "archive" update if the chat is being marked unarchvied
|
|
666
721
|
// // this only applies for the initial sync
|
|
667
722
|
// if(isInitialSync && !isArchived) {
|
|
668
|
-
//
|
|
723
|
+
// isArchived = false
|
|
669
724
|
// }
|
|
670
725
|
const msgRange = !accountSettings?.unarchiveChats ? undefined : archiveAction?.messageRange
|
|
671
726
|
// logger?.debug({ chat: id, syncAction }, 'message range archive')
|
|
672
|
-
ev.emit('chats.update', [
|
|
727
|
+
ev.emit('chats.update', [
|
|
728
|
+
{
|
|
673
729
|
id,
|
|
674
730
|
archived: isArchived,
|
|
675
731
|
conditional: getChatUpdateConditional(id, msgRange)
|
|
676
|
-
}
|
|
732
|
+
}
|
|
733
|
+
])
|
|
677
734
|
}
|
|
735
|
+
|
|
678
736
|
else if (action?.markChatAsReadAction) {
|
|
679
737
|
const markReadAction = action.markChatAsReadAction
|
|
680
738
|
// basically we don't need to fire an "read" update if the chat is being marked as read
|
|
@@ -687,6 +745,7 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
|
687
745
|
conditional: getChatUpdateConditional(id, markReadAction?.messageRange)
|
|
688
746
|
}])
|
|
689
747
|
}
|
|
748
|
+
|
|
690
749
|
else if (action?.deleteMessageForMeAction || type === 'deleteMessageForMe') {
|
|
691
750
|
ev.emit('messages.delete', {
|
|
692
751
|
keys: [
|
|
@@ -698,35 +757,44 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
|
698
757
|
]
|
|
699
758
|
})
|
|
700
759
|
}
|
|
760
|
+
|
|
701
761
|
else if (action?.contactAction) {
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
762
|
+
ev.emit('contacts.upsert', [
|
|
763
|
+
{
|
|
764
|
+
id: id,
|
|
765
|
+
name: action.contactAction.fullName,
|
|
766
|
+
lid: action.contactAction.lidJid || undefined,
|
|
767
|
+
phoneNumber: action.contactAction.pnJid || undefined
|
|
768
|
+
}
|
|
769
|
+
])
|
|
708
770
|
}
|
|
771
|
+
|
|
709
772
|
else if (action?.pushNameSetting) {
|
|
710
773
|
const name = action?.pushNameSetting?.name
|
|
711
774
|
if (name && me?.name !== name) {
|
|
712
775
|
ev.emit('creds.update', { me: { ...me, name } })
|
|
713
776
|
}
|
|
714
777
|
}
|
|
778
|
+
|
|
715
779
|
else if (action?.pinAction) {
|
|
716
780
|
ev.emit('chats.update', [{
|
|
717
781
|
id,
|
|
718
|
-
pinned: action.pinAction?.pinned ?
|
|
782
|
+
pinned: action.pinAction?.pinned ? toNumber(action.timestamp) : null,
|
|
719
783
|
conditional: getChatUpdateConditional(id, undefined)
|
|
720
784
|
}])
|
|
721
785
|
}
|
|
786
|
+
|
|
722
787
|
else if (action?.unarchiveChatsSetting) {
|
|
723
788
|
const unarchiveChats = !!action.unarchiveChatsSetting.unarchiveChats
|
|
789
|
+
|
|
724
790
|
ev.emit('creds.update', { accountSettings: { unarchiveChats } })
|
|
725
791
|
logger?.info(`archive setting updated => '${action.unarchiveChatsSetting.unarchiveChats}'`)
|
|
792
|
+
|
|
726
793
|
if (accountSettings) {
|
|
727
794
|
accountSettings.unarchiveChats = unarchiveChats
|
|
728
795
|
}
|
|
729
796
|
}
|
|
797
|
+
|
|
730
798
|
else if (action?.starAction || type === 'star') {
|
|
731
799
|
let starred = action?.starAction?.starred
|
|
732
800
|
if (typeof starred !== 'boolean') {
|
|
@@ -739,11 +807,13 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
|
739
807
|
}
|
|
740
808
|
])
|
|
741
809
|
}
|
|
810
|
+
|
|
742
811
|
else if (action?.deleteChatAction || type === 'deleteChat') {
|
|
743
812
|
if (!isInitialSync) {
|
|
744
813
|
ev.emit('chats.delete', [id])
|
|
745
814
|
}
|
|
746
815
|
}
|
|
816
|
+
|
|
747
817
|
else if (action?.labelEditAction) {
|
|
748
818
|
const { name, color, deleted, predefinedId } = action.labelEditAction
|
|
749
819
|
ev.emit('labels.edit', {
|
|
@@ -754,28 +824,92 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
|
754
824
|
predefinedId: predefinedId ? String(predefinedId) : undefined
|
|
755
825
|
})
|
|
756
826
|
}
|
|
827
|
+
|
|
757
828
|
else if (action?.labelAssociationAction) {
|
|
758
829
|
ev.emit('labels.association', {
|
|
759
830
|
type: action.labelAssociationAction.labeled
|
|
760
831
|
? 'add'
|
|
761
832
|
: 'remove',
|
|
762
|
-
association: type ===
|
|
833
|
+
association: type === LabelAssociationType.Chat
|
|
763
834
|
? {
|
|
764
|
-
type:
|
|
835
|
+
type: LabelAssociationType.Chat,
|
|
765
836
|
chatId: syncAction.index[2],
|
|
766
837
|
labelId: syncAction.index[1]
|
|
767
838
|
}
|
|
768
839
|
: {
|
|
769
|
-
type:
|
|
840
|
+
type: LabelAssociationType.Message,
|
|
770
841
|
chatId: syncAction.index[2],
|
|
771
842
|
messageId: syncAction.index[3],
|
|
772
843
|
labelId: syncAction.index[1]
|
|
773
844
|
}
|
|
774
845
|
})
|
|
775
846
|
}
|
|
847
|
+
|
|
848
|
+
else if (action?.localeSetting?.locale) {
|
|
849
|
+
ev.emit('settings.update', { setting: 'locale', value: action.localeSetting.locale })
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
else if (action?.timeFormatAction) {
|
|
853
|
+
ev.emit('settings.update', { setting: 'timeFormat', value: action.timeFormatAction })
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
else if (action?.pnForLidChatAction) {
|
|
857
|
+
if (action.pnForLidChatAction.pnJid) {
|
|
858
|
+
ev.emit('lid-mapping.update', { lid: id, pn: action.pnForLidChatAction.pnJid })
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
else if (action?.privacySettingRelayAllCalls) {
|
|
863
|
+
ev.emit('settings.update', {
|
|
864
|
+
setting: 'privacySettingRelayAllCalls',
|
|
865
|
+
value: action.privacySettingRelayAllCalls
|
|
866
|
+
})
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
else if (action?.statusPrivacy) {
|
|
870
|
+
ev.emit('settings.update', { setting: 'statusPrivacy', value: action.statusPrivacy })
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
else if (action?.lockChatAction) {
|
|
874
|
+
ev.emit('chats.lock', { id: id, locked: !!action.lockChatAction.locked })
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
else if (action?.privacySettingDisableLinkPreviewsAction) {
|
|
878
|
+
ev.emit('settings.update', {
|
|
879
|
+
setting: 'disableLinkPreviews',
|
|
880
|
+
value: action.privacySettingDisableLinkPreviewsAction
|
|
881
|
+
})
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
else if (action?.notificationActivitySettingAction?.notificationActivitySetting) {
|
|
885
|
+
ev.emit('settings.update', {
|
|
886
|
+
setting: 'notificationActivitySetting',
|
|
887
|
+
value: action.notificationActivitySettingAction.notificationActivitySetting
|
|
888
|
+
})
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
else if (action?.lidContactAction) {
|
|
892
|
+
ev.emit('contacts.upsert', [
|
|
893
|
+
{
|
|
894
|
+
id: id,
|
|
895
|
+
name: action.lidContactAction.fullName,
|
|
896
|
+
lid: id,
|
|
897
|
+
phoneNumber: undefined
|
|
898
|
+
}
|
|
899
|
+
])
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
else if (action?.privacySettingChannelsPersonalisedRecommendationAction) {
|
|
903
|
+
ev.emit('settings.update', {
|
|
904
|
+
setting: 'channelsPersonalisedRecommendation',
|
|
905
|
+
value: action.privacySettingChannelsPersonalisedRecommendationAction
|
|
906
|
+
})
|
|
907
|
+
}
|
|
908
|
+
|
|
776
909
|
else {
|
|
777
910
|
logger?.debug({ syncAction, id }, 'unprocessable update')
|
|
778
911
|
}
|
|
912
|
+
|
|
779
913
|
function getChatUpdateConditional(id, msgRange) {
|
|
780
914
|
return isInitialSync
|
|
781
915
|
? (data) => {
|
|
@@ -786,6 +920,7 @@ const processSyncAction = (syncAction, ev, me, initialSyncOpts, logger) => {
|
|
|
786
920
|
}
|
|
787
921
|
: undefined
|
|
788
922
|
}
|
|
923
|
+
|
|
789
924
|
function isValidPatchBasedOnMessageRange(chat, msgRange) {
|
|
790
925
|
const lastMsgTimestamp = Number(msgRange?.lastMessageTimestamp || msgRange?.lastSystemMessageTimestamp || 0)
|
|
791
926
|
const chatLastMsgTimestamp = Number(chat?.lastMessageRecvTimestamp || 0)
|