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