@d0v3riz/baileys 6.7.22 → 6.7.24

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 (356) hide show
  1. package/WAProto/GenerateStatics.sh +3 -2
  2. package/WAProto/fix-imports.js +29 -0
  3. package/WAProto/index.js +4136 -4138
  4. package/lib/Defaults/index.d.ts +3 -2
  5. package/lib/Defaults/index.d.ts.map +1 -0
  6. package/lib/Defaults/index.js +36 -40
  7. package/lib/Defaults/index.js.map +1 -0
  8. package/lib/Signal/Group/ciphertext-message.d.ts +1 -0
  9. package/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
  10. package/lib/Signal/Group/ciphertext-message.js +2 -5
  11. package/lib/Signal/Group/ciphertext-message.js.map +1 -0
  12. package/lib/Signal/Group/group-session-builder.d.ts +4 -3
  13. package/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
  14. package/lib/Signal/Group/group-session-builder.js +7 -41
  15. package/lib/Signal/Group/group-session-builder.js.map +1 -0
  16. package/lib/Signal/Group/group_cipher.d.ts +3 -2
  17. package/lib/Signal/Group/group_cipher.d.ts.map +1 -0
  18. package/lib/Signal/Group/group_cipher.js +14 -16
  19. package/lib/Signal/Group/group_cipher.js.map +1 -0
  20. package/lib/Signal/Group/index.d.ts +12 -11
  21. package/lib/Signal/Group/index.d.ts.map +1 -0
  22. package/lib/Signal/Group/index.js +12 -57
  23. package/lib/Signal/Group/index.js.map +1 -0
  24. package/lib/Signal/Group/keyhelper.d.ts +2 -1
  25. package/lib/Signal/Group/keyhelper.d.ts.map +1 -0
  26. package/lib/Signal/Group/keyhelper.js +8 -44
  27. package/lib/Signal/Group/keyhelper.js.map +1 -0
  28. package/lib/Signal/Group/queue-job.d.ts +1 -0
  29. package/lib/Signal/Group/queue-job.d.ts.map +1 -0
  30. package/lib/Signal/Group/queue-job.js +2 -5
  31. package/lib/Signal/Group/queue-job.js.map +1 -0
  32. package/lib/Signal/Group/sender-chain-key.d.ts +2 -1
  33. package/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
  34. package/lib/Signal/Group/sender-chain-key.js +7 -9
  35. package/lib/Signal/Group/sender-chain-key.js.map +1 -0
  36. package/lib/Signal/Group/sender-key-distribution-message.d.ts +2 -1
  37. package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
  38. package/lib/Signal/Group/sender-key-distribution-message.js +6 -9
  39. package/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
  40. package/lib/Signal/Group/sender-key-message.d.ts +2 -1
  41. package/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
  42. package/lib/Signal/Group/sender-key-message.js +10 -12
  43. package/lib/Signal/Group/sender-key-message.js.map +1 -0
  44. package/lib/Signal/Group/sender-key-name.d.ts +1 -0
  45. package/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
  46. package/lib/Signal/Group/sender-key-name.js +2 -5
  47. package/lib/Signal/Group/sender-key-name.js.map +1 -0
  48. package/lib/Signal/Group/sender-key-record.d.ts +2 -1
  49. package/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
  50. package/lib/Signal/Group/sender-key-record.js +9 -12
  51. package/lib/Signal/Group/sender-key-record.js.map +1 -0
  52. package/lib/Signal/Group/sender-key-state.d.ts +3 -2
  53. package/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
  54. package/lib/Signal/Group/sender-key-state.js +6 -9
  55. package/lib/Signal/Group/sender-key-state.js.map +1 -0
  56. package/lib/Signal/Group/sender-message-key.d.ts +1 -0
  57. package/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
  58. package/lib/Signal/Group/sender-message-key.js +5 -7
  59. package/lib/Signal/Group/sender-message-key.js.map +1 -0
  60. package/lib/Signal/libsignal.d.ts +3 -2
  61. package/lib/Signal/libsignal.d.ts.map +1 -0
  62. package/lib/Signal/libsignal.js +22 -55
  63. package/lib/Signal/libsignal.js.map +1 -0
  64. package/lib/Socket/Client/index.d.ts +3 -2
  65. package/lib/Socket/Client/index.d.ts.map +1 -0
  66. package/lib/Socket/Client/index.js +3 -18
  67. package/lib/Socket/Client/index.js.map +1 -0
  68. package/lib/Socket/Client/types.d.ts +2 -1
  69. package/lib/Socket/Client/types.d.ts.map +1 -0
  70. package/lib/Socket/Client/types.js +4 -6
  71. package/lib/Socket/Client/types.js.map +1 -0
  72. package/lib/Socket/Client/websocket.d.ts +2 -1
  73. package/lib/Socket/Client/websocket.d.ts.map +1 -0
  74. package/lib/Socket/Client/websocket.js +14 -26
  75. package/lib/Socket/Client/websocket.js.map +1 -0
  76. package/lib/Socket/business.d.ts +65 -63
  77. package/lib/Socket/business.d.ts.map +1 -0
  78. package/lib/Socket/business.js +32 -37
  79. package/lib/Socket/business.js.map +1 -0
  80. package/lib/Socket/chats.d.ts +20 -18
  81. package/lib/Socket/chats.d.ts.map +1 -0
  82. package/lib/Socket/chats.js +176 -141
  83. package/lib/Socket/chats.js.map +1 -0
  84. package/lib/Socket/communities.d.ts +219 -0
  85. package/lib/Socket/communities.d.ts.map +1 -0
  86. package/lib/Socket/communities.js +351 -0
  87. package/lib/Socket/communities.js.map +1 -0
  88. package/lib/Socket/groups.d.ts +36 -34
  89. package/lib/Socket/groups.d.ts.map +1 -0
  90. package/lib/Socket/groups.js +56 -63
  91. package/lib/Socket/groups.js.map +1 -0
  92. package/lib/Socket/index.d.ts +101 -69
  93. package/lib/Socket/index.d.ts.map +1 -0
  94. package/lib/Socket/index.js +16 -9
  95. package/lib/Socket/index.js.map +1 -0
  96. package/lib/Socket/messages-recv.d.ts +50 -48
  97. package/lib/Socket/messages-recv.d.ts.map +1 -0
  98. package/lib/Socket/messages-recv.js +180 -173
  99. package/lib/Socket/messages-recv.js.map +1 -0
  100. package/lib/Socket/messages-send.d.ts +47 -45
  101. package/lib/Socket/messages-send.d.ts.map +1 -0
  102. package/lib/Socket/messages-send.js +75 -84
  103. package/lib/Socket/messages-send.js.map +1 -0
  104. package/lib/Socket/mex.d.ts +2 -1
  105. package/lib/Socket/mex.d.ts.map +1 -0
  106. package/lib/Socket/mex.js +12 -16
  107. package/lib/Socket/mex.js.map +1 -0
  108. package/lib/Socket/newsletter.d.ts +45 -43
  109. package/lib/Socket/newsletter.d.ts.map +1 -0
  110. package/lib/Socket/newsletter.js +23 -27
  111. package/lib/Socket/newsletter.js.map +1 -0
  112. package/lib/Socket/socket.d.ts +12 -11
  113. package/lib/Socket/socket.d.ts.map +1 -0
  114. package/lib/Socket/socket.js +96 -105
  115. package/lib/Socket/socket.js.map +1 -0
  116. package/lib/Socket/usync.d.ts +14 -13
  117. package/lib/Socket/usync.d.ts.map +1 -0
  118. package/lib/Socket/usync.js +9 -11
  119. package/lib/Socket/usync.js.map +1 -0
  120. package/lib/Types/Auth.d.ts +4 -3
  121. package/lib/Types/Auth.d.ts.map +1 -0
  122. package/lib/Types/Auth.js +2 -2
  123. package/lib/Types/Auth.js.map +1 -0
  124. package/lib/Types/Call.d.ts +1 -0
  125. package/lib/Types/Call.d.ts.map +1 -0
  126. package/lib/Types/Call.js +2 -2
  127. package/lib/Types/Call.js.map +1 -0
  128. package/lib/Types/Chat.d.ts +10 -7
  129. package/lib/Types/Chat.d.ts.map +1 -0
  130. package/lib/Types/Chat.js +2 -4
  131. package/lib/Types/Chat.js.map +1 -0
  132. package/lib/Types/Contact.d.ts +1 -0
  133. package/lib/Types/Contact.d.ts.map +1 -0
  134. package/lib/Types/Contact.js +2 -2
  135. package/lib/Types/Contact.js.map +1 -0
  136. package/lib/Types/Events.d.ts +11 -10
  137. package/lib/Types/Events.d.ts.map +1 -0
  138. package/lib/Types/Events.js +2 -2
  139. package/lib/Types/Events.js.map +1 -0
  140. package/lib/Types/GroupMetadata.d.ts +3 -2
  141. package/lib/Types/GroupMetadata.d.ts.map +1 -0
  142. package/lib/Types/GroupMetadata.js +2 -2
  143. package/lib/Types/GroupMetadata.js.map +1 -0
  144. package/lib/Types/Label.d.ts +1 -0
  145. package/lib/Types/Label.d.ts.map +1 -0
  146. package/lib/Types/Label.js +3 -5
  147. package/lib/Types/Label.js.map +1 -0
  148. package/lib/Types/LabelAssociation.d.ts +1 -0
  149. package/lib/Types/LabelAssociation.d.ts.map +1 -0
  150. package/lib/Types/LabelAssociation.js +3 -5
  151. package/lib/Types/LabelAssociation.js.map +1 -0
  152. package/lib/Types/Message.d.ts +9 -7
  153. package/lib/Types/Message.d.ts.map +1 -0
  154. package/lib/Types/Message.js +7 -7
  155. package/lib/Types/Message.js.map +1 -0
  156. package/lib/Types/Newsletter.d.ts +1 -0
  157. package/lib/Types/Newsletter.d.ts.map +1 -0
  158. package/lib/Types/Newsletter.js +5 -7
  159. package/lib/Types/Newsletter.js.map +1 -0
  160. package/lib/Types/Product.d.ts +2 -1
  161. package/lib/Types/Product.d.ts.map +1 -0
  162. package/lib/Types/Product.js +2 -2
  163. package/lib/Types/Product.js.map +1 -0
  164. package/lib/Types/Signal.d.ts +2 -1
  165. package/lib/Types/Signal.d.ts.map +1 -0
  166. package/lib/Types/Signal.js +2 -2
  167. package/lib/Types/Signal.js.map +1 -0
  168. package/lib/Types/Socket.d.ts +8 -7
  169. package/lib/Types/Socket.d.ts.map +1 -0
  170. package/lib/Types/Socket.js +3 -2
  171. package/lib/Types/Socket.js.map +1 -0
  172. package/lib/Types/State.d.ts +14 -2
  173. package/lib/Types/State.d.ts.map +1 -0
  174. package/lib/Types/State.js +13 -2
  175. package/lib/Types/State.js.map +1 -0
  176. package/lib/Types/USync.d.ts +3 -2
  177. package/lib/Types/USync.d.ts.map +1 -0
  178. package/lib/Types/USync.js +2 -2
  179. package/lib/Types/USync.js.map +1 -0
  180. package/lib/Types/index.d.ts +15 -14
  181. package/lib/Types/index.d.ts.map +1 -0
  182. package/lib/Types/index.js +15 -31
  183. package/lib/Types/index.js.map +1 -0
  184. package/lib/Utils/auth-utils.d.ts +3 -2
  185. package/lib/Utils/auth-utils.d.ts.map +1 -0
  186. package/lib/Utils/auth-utils.js +25 -34
  187. package/lib/Utils/auth-utils.js.map +1 -0
  188. package/lib/Utils/baileys-event-stream.d.ts +2 -1
  189. package/lib/Utils/baileys-event-stream.d.ts.map +1 -0
  190. package/lib/Utils/baileys-event-stream.js +15 -22
  191. package/lib/Utils/baileys-event-stream.js.map +1 -0
  192. package/lib/Utils/business.d.ts +3 -2
  193. package/lib/Utils/business.d.ts.map +1 -0
  194. package/lib/Utils/business.js +61 -70
  195. package/lib/Utils/business.js.map +1 -0
  196. package/lib/Utils/chat-utils.d.ts +8 -7
  197. package/lib/Utils/chat-utils.d.ts.map +1 -0
  198. package/lib/Utils/chat-utils.js +130 -137
  199. package/lib/Utils/chat-utils.js.map +1 -0
  200. package/lib/Utils/crypto.d.ts +2 -1
  201. package/lib/Utils/crypto.d.ts.map +1 -0
  202. package/lib/Utils/crypto.js +37 -86
  203. package/lib/Utils/crypto.js.map +1 -0
  204. package/lib/Utils/decode-wa-message.d.ts +5 -4
  205. package/lib/Utils/decode-wa-message.d.ts.map +1 -0
  206. package/lib/Utils/decode-wa-message.js +38 -44
  207. package/lib/Utils/decode-wa-message.js.map +1 -0
  208. package/lib/Utils/event-buffer.d.ts +4 -4
  209. package/lib/Utils/event-buffer.d.ts.map +1 -0
  210. package/lib/Utils/event-buffer.js +37 -50
  211. package/lib/Utils/event-buffer.js.map +1 -0
  212. package/lib/Utils/generics.d.ts +9 -8
  213. package/lib/Utils/generics.d.ts.map +1 -0
  214. package/lib/Utils/generics.js +87 -114
  215. package/lib/Utils/generics.js.map +1 -0
  216. package/lib/Utils/history.d.ts +4 -3
  217. package/lib/Utils/history.d.ts.map +1 -0
  218. package/lib/Utils/history.js +32 -40
  219. package/lib/Utils/history.js.map +1 -0
  220. package/lib/Utils/index.d.ts +18 -17
  221. package/lib/Utils/index.d.ts.map +1 -0
  222. package/lib/Utils/index.js +18 -33
  223. package/lib/Utils/index.js.map +1 -0
  224. package/lib/Utils/link-preview.d.ts +4 -3
  225. package/lib/Utils/link-preview.d.ts.map +1 -0
  226. package/lib/Utils/link-preview.js +10 -47
  227. package/lib/Utils/link-preview.js.map +1 -0
  228. package/lib/Utils/logger.d.ts +8 -6
  229. package/lib/Utils/logger.d.ts.map +1 -0
  230. package/lib/Utils/logger.js +3 -7
  231. package/lib/Utils/logger.js.map +1 -0
  232. package/lib/Utils/lt-hash.d.ts +9 -8
  233. package/lib/Utils/lt-hash.d.ts.map +1 -0
  234. package/lib/Utils/lt-hash.js +23 -26
  235. package/lib/Utils/lt-hash.js.map +1 -0
  236. package/lib/Utils/make-mutex.d.ts +1 -0
  237. package/lib/Utils/make-mutex.d.ts.map +1 -0
  238. package/lib/Utils/make-mutex.js +5 -9
  239. package/lib/Utils/make-mutex.js.map +1 -0
  240. package/lib/Utils/messages-media.d.ts +11 -10
  241. package/lib/Utils/messages-media.d.ts.map +1 -0
  242. package/lib/Utils/messages-media.js +141 -203
  243. package/lib/Utils/messages-media.js.map +1 -0
  244. package/lib/Utils/messages.d.ts +7 -6
  245. package/lib/Utils/messages.d.ts.map +1 -0
  246. package/lib/Utils/messages.js +189 -198
  247. package/lib/Utils/messages.js.map +1 -0
  248. package/lib/Utils/noise-handler.d.ts +5 -4
  249. package/lib/Utils/noise-handler.d.ts.map +1 -0
  250. package/lib/Utils/noise-handler.js +22 -26
  251. package/lib/Utils/noise-handler.js.map +1 -0
  252. package/lib/Utils/process-message.d.ts +5 -4
  253. package/lib/Utils/process-message.d.ts.map +1 -0
  254. package/lib/Utils/process-message.js +95 -104
  255. package/lib/Utils/process-message.js.map +1 -0
  256. package/lib/Utils/signal.d.ts +5 -4
  257. package/lib/Utils/signal.d.ts.map +1 -0
  258. package/lib/Utils/signal.js +47 -52
  259. package/lib/Utils/signal.js.map +1 -0
  260. package/lib/Utils/use-multi-file-auth-state.d.ts +2 -1
  261. package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
  262. package/lib/Utils/use-multi-file-auth-state.js +23 -26
  263. package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
  264. package/lib/Utils/validate-connection.d.ts +4 -3
  265. package/lib/Utils/validate-connection.d.ts.map +1 -0
  266. package/lib/Utils/validate-connection.js +52 -58
  267. package/lib/Utils/validate-connection.js.map +1 -0
  268. package/lib/WABinary/constants.d.ts +1 -0
  269. package/lib/WABinary/constants.d.ts.map +1 -0
  270. package/lib/WABinary/constants.js +9 -11
  271. package/lib/WABinary/constants.js.map +1 -0
  272. package/lib/WABinary/decode.d.ts +2 -1
  273. package/lib/WABinary/decode.d.ts.map +1 -0
  274. package/lib/WABinary/decode.js +13 -51
  275. package/lib/WABinary/decode.js.map +1 -0
  276. package/lib/WABinary/encode.d.ts +2 -1
  277. package/lib/WABinary/encode.d.ts.map +1 -0
  278. package/lib/WABinary/encode.js +5 -41
  279. package/lib/WABinary/encode.js.map +1 -0
  280. package/lib/WABinary/generic-utils.d.ts +4 -3
  281. package/lib/WABinary/generic-utils.d.ts.map +1 -0
  282. package/lib/WABinary/generic-utils.js +30 -38
  283. package/lib/WABinary/generic-utils.js.map +1 -0
  284. package/lib/WABinary/index.d.ts +6 -5
  285. package/lib/WABinary/index.d.ts.map +1 -0
  286. package/lib/WABinary/index.js +6 -21
  287. package/lib/WABinary/index.js.map +1 -0
  288. package/lib/WABinary/jid-utils.d.ts +1 -0
  289. package/lib/WABinary/jid-utils.d.ts.map +1 -0
  290. package/lib/WABinary/jid-utils.js +21 -35
  291. package/lib/WABinary/jid-utils.js.map +1 -0
  292. package/lib/WABinary/types.d.ts +2 -1
  293. package/lib/WABinary/types.d.ts.map +1 -0
  294. package/lib/WABinary/types.js +2 -2
  295. package/lib/WABinary/types.js.map +1 -0
  296. package/lib/WAM/BinaryInfo.d.ts +2 -1
  297. package/lib/WAM/BinaryInfo.d.ts.map +1 -0
  298. package/lib/WAM/BinaryInfo.js +2 -5
  299. package/lib/WAM/BinaryInfo.js.map +1 -0
  300. package/lib/WAM/constants.d.ts +1 -0
  301. package/lib/WAM/constants.d.ts.map +1 -0
  302. package/lib/WAM/constants.js +4 -6
  303. package/lib/WAM/constants.js.map +1 -0
  304. package/lib/WAM/encode.d.ts +2 -1
  305. package/lib/WAM/encode.d.ts.map +1 -0
  306. package/lib/WAM/encode.js +12 -14
  307. package/lib/WAM/encode.js.map +1 -0
  308. package/lib/WAM/index.d.ts +4 -3
  309. package/lib/WAM/index.d.ts.map +1 -0
  310. package/lib/WAM/index.js +4 -19
  311. package/lib/WAM/index.js.map +1 -0
  312. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +4 -3
  313. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
  314. package/lib/WAUSync/Protocols/USyncContactProtocol.js +6 -9
  315. package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
  316. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +3 -2
  317. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
  318. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +9 -12
  319. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
  320. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +3 -2
  321. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
  322. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +6 -9
  323. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
  324. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +3 -2
  325. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
  326. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +7 -11
  327. package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
  328. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +4 -3
  329. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
  330. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +19 -21
  331. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
  332. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +3 -2
  333. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
  334. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +2 -5
  335. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
  336. package/lib/WAUSync/Protocols/index.d.ts +5 -4
  337. package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
  338. package/lib/WAUSync/Protocols/index.js +5 -20
  339. package/lib/WAUSync/Protocols/index.js.map +1 -0
  340. package/lib/WAUSync/USyncQuery.d.ts +4 -3
  341. package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
  342. package/lib/WAUSync/USyncQuery.js +18 -20
  343. package/lib/WAUSync/USyncQuery.js.map +1 -0
  344. package/lib/WAUSync/USyncUser.d.ts +6 -5
  345. package/lib/WAUSync/USyncUser.d.ts.map +1 -0
  346. package/lib/WAUSync/USyncUser.js +2 -5
  347. package/lib/WAUSync/USyncUser.js.map +1 -0
  348. package/lib/WAUSync/index.d.ts +4 -3
  349. package/lib/WAUSync/index.d.ts.map +1 -0
  350. package/lib/WAUSync/index.js +4 -19
  351. package/lib/WAUSync/index.js.map +1 -0
  352. package/lib/index.d.ts +9 -8
  353. package/lib/index.d.ts.map +1 -0
  354. package/lib/index.js +11 -30
  355. package/lib/index.js.map +1 -0
  356. package/package.json +25 -24
@@ -1,33 +1,29 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.makeChatsSocket = void 0;
7
- const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
8
- const boom_1 = require("@hapi/boom");
9
- const WAProto_1 = require("../../WAProto");
10
- const Defaults_1 = require("../Defaults");
11
- const Types_1 = require("../Types");
12
- const Utils_1 = require("../Utils");
13
- const make_mutex_1 = require("../Utils/make-mutex");
14
- const process_message_1 = __importDefault(require("../Utils/process-message"));
15
- const WABinary_1 = require("../WABinary");
16
- const WAUSync_1 = require("../WAUSync");
17
- const usync_1 = require("./usync");
1
+ import NodeCache from '@cacheable/node-cache';
2
+ import { Boom } from '@hapi/boom';
3
+ import { proto } from '../../WAProto/index.js';
4
+ import { DEFAULT_CACHE_TTLS, PROCESSABLE_HISTORY_TYPES } from '../Defaults/index.js';
5
+ import { ALL_WA_PATCH_NAMES } from '../Types/index.js';
6
+ import { SyncState } from '../Types/State.js';
7
+ import { chatModificationToAppPatch, decodePatches, decodeSyncdSnapshot, encodeSyncdPatch, extractSyncdPatches, generateProfilePicture, getHistoryMsg, newLTHashState, processSyncAction } from '../Utils/index.js';
8
+ import { makeMutex } from '../Utils/make-mutex.js';
9
+ import processMessage from '../Utils/process-message.js';
10
+ import { getBinaryNodeChild, getBinaryNodeChildren, jidDecode, jidNormalizedUser, reduceBinaryNodeToDictionary, S_WHATSAPP_NET } from '../WABinary/index.js';
11
+ import { USyncQuery, USyncUser } from '../WAUSync/index.js';
12
+ import { makeUSyncSocket } from './usync.js';
18
13
  const MAX_SYNC_ATTEMPTS = 2;
19
- const makeChatsSocket = (config) => {
14
+ export const makeChatsSocket = (config) => {
20
15
  const { logger, markOnlineOnConnect, fireInitQueries, appStateMacVerification, shouldIgnoreJid, shouldSyncHistoryMessage } = config;
21
- const sock = (0, usync_1.makeUSyncSocket)(config);
16
+ const sock = makeUSyncSocket(config);
22
17
  const { ev, ws, authState, generateMessageTag, sendNode, query, onUnexpectedError } = sock;
23
18
  let privacySettings;
24
- let needToFlushWithAppStateSync = false;
25
- let pendingAppStateSync = false;
19
+ let syncState = SyncState.Connecting;
26
20
  /** this mutex ensures that the notifications (receipts, messages etc.) are processed in order */
27
- const processingMutex = (0, make_mutex_1.makeMutex)();
21
+ const processingMutex = makeMutex();
22
+ // Timeout for AwaitingInitialSync state
23
+ let awaitingSyncTimeout;
28
24
  const placeholderResendCache = config.placeholderResendCache ||
29
- new node_cache_1.default({
30
- stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.MSG_RETRY, // 1 hour
25
+ new NodeCache({
26
+ stdTTL: DEFAULT_CACHE_TTLS.MSG_RETRY, // 1 hour
31
27
  useClones: false
32
28
  });
33
29
  if (!config.placeholderResendCache) {
@@ -44,12 +40,12 @@ const makeChatsSocket = (config) => {
44
40
  tag: 'iq',
45
41
  attrs: {
46
42
  xmlns: 'privacy',
47
- to: WABinary_1.S_WHATSAPP_NET,
43
+ to: S_WHATSAPP_NET,
48
44
  type: 'get'
49
45
  },
50
46
  content: [{ tag: 'privacy', attrs: {} }]
51
47
  });
52
- privacySettings = (0, WABinary_1.reduceBinaryNodeToDictionary)(content === null || content === void 0 ? void 0 : content[0], 'category');
48
+ privacySettings = reduceBinaryNodeToDictionary(content?.[0], 'category');
53
49
  }
54
50
  return privacySettings;
55
51
  };
@@ -59,7 +55,7 @@ const makeChatsSocket = (config) => {
59
55
  tag: 'iq',
60
56
  attrs: {
61
57
  xmlns: 'privacy',
62
- to: WABinary_1.S_WHATSAPP_NET,
58
+ to: S_WHATSAPP_NET,
63
59
  type: 'set'
64
60
  },
65
61
  content: [
@@ -105,7 +101,7 @@ const makeChatsSocket = (config) => {
105
101
  tag: 'iq',
106
102
  attrs: {
107
103
  xmlns: 'disappearing_mode',
108
- to: WABinary_1.S_WHATSAPP_NET,
104
+ to: S_WHATSAPP_NET,
109
105
  type: 'set'
110
106
  },
111
107
  content: [
@@ -123,7 +119,7 @@ const makeChatsSocket = (config) => {
123
119
  tag: 'iq',
124
120
  attrs: {
125
121
  xmlns: 'bot',
126
- to: WABinary_1.S_WHATSAPP_NET,
122
+ to: S_WHATSAPP_NET,
127
123
  type: 'get'
128
124
  },
129
125
  content: [
@@ -135,11 +131,11 @@ const makeChatsSocket = (config) => {
135
131
  }
136
132
  ]
137
133
  });
138
- const botNode = (0, WABinary_1.getBinaryNodeChild)(resp, 'bot');
134
+ const botNode = getBinaryNodeChild(resp, 'bot');
139
135
  const botList = [];
140
- for (const section of (0, WABinary_1.getBinaryNodeChildren)(botNode, 'section')) {
136
+ for (const section of getBinaryNodeChildren(botNode, 'section')) {
141
137
  if (section.attrs.type === 'all') {
142
- for (const bot of (0, WABinary_1.getBinaryNodeChildren)(section, 'bot')) {
138
+ for (const bot of getBinaryNodeChildren(section, 'bot')) {
143
139
  botList.push({
144
140
  jid: bot.attrs.jid,
145
141
  personaId: bot.attrs['persona_id']
@@ -150,10 +146,10 @@ const makeChatsSocket = (config) => {
150
146
  return botList;
151
147
  };
152
148
  const onWhatsApp = async (...jids) => {
153
- const usyncQuery = new WAUSync_1.USyncQuery().withContactProtocol().withLIDProtocol();
149
+ const usyncQuery = new USyncQuery().withContactProtocol().withLIDProtocol();
154
150
  for (const jid of jids) {
155
- const phone = `+${jid.replace('+', '').split('@')[0].split(':')[0]}`;
156
- usyncQuery.withUser(new WAUSync_1.USyncUser().withPhone(phone));
151
+ const phone = `+${jid.replace('+', '').split('@')[0]?.split(':')[0]}`;
152
+ usyncQuery.withUser(new USyncUser().withPhone(phone));
157
153
  }
158
154
  const results = await sock.executeUSyncQuery(usyncQuery);
159
155
  if (results) {
@@ -161,9 +157,9 @@ const makeChatsSocket = (config) => {
161
157
  }
162
158
  };
163
159
  const fetchStatus = async (...jids) => {
164
- const usyncQuery = new WAUSync_1.USyncQuery().withStatusProtocol();
160
+ const usyncQuery = new USyncQuery().withStatusProtocol();
165
161
  for (const jid of jids) {
166
- usyncQuery.withUser(new WAUSync_1.USyncUser().withId(jid));
162
+ usyncQuery.withUser(new USyncUser().withId(jid));
167
163
  }
168
164
  const result = await sock.executeUSyncQuery(usyncQuery);
169
165
  if (result) {
@@ -171,9 +167,9 @@ const makeChatsSocket = (config) => {
171
167
  }
172
168
  };
173
169
  const fetchDisappearingDuration = async (...jids) => {
174
- const usyncQuery = new WAUSync_1.USyncQuery().withDisappearingModeProtocol();
170
+ const usyncQuery = new USyncQuery().withDisappearingModeProtocol();
175
171
  for (const jid of jids) {
176
- usyncQuery.withUser(new WAUSync_1.USyncUser().withId(jid));
172
+ usyncQuery.withUser(new USyncUser().withId(jid));
177
173
  }
178
174
  const result = await sock.executeUSyncQuery(usyncQuery);
179
175
  if (result) {
@@ -184,19 +180,22 @@ const makeChatsSocket = (config) => {
184
180
  const updateProfilePicture = async (jid, content, dimensions) => {
185
181
  let targetJid;
186
182
  if (!jid) {
187
- throw new boom_1.Boom('Illegal no-jid profile update. Please specify either your ID or the ID of the chat you wish to update');
183
+ throw new Boom('Illegal no-jid profile update. Please specify either your ID or the ID of the chat you wish to update');
184
+ }
185
+ if (jidNormalizedUser(jid) !== jidNormalizedUser(authState.creds.me.id)) {
186
+ targetJid = jidNormalizedUser(jid); // in case it is someone other than us
188
187
  }
189
- if ((0, WABinary_1.jidNormalizedUser)(jid) !== (0, WABinary_1.jidNormalizedUser)(authState.creds.me.id)) {
190
- targetJid = (0, WABinary_1.jidNormalizedUser)(jid); // in case it is someone other than us
188
+ else {
189
+ targetJid = undefined;
191
190
  }
192
- const { img } = await (0, Utils_1.generateProfilePicture)(content, dimensions);
191
+ const { img } = await generateProfilePicture(content, dimensions);
193
192
  await query({
194
193
  tag: 'iq',
195
194
  attrs: {
196
- target: targetJid,
197
- to: WABinary_1.S_WHATSAPP_NET,
195
+ to: S_WHATSAPP_NET,
198
196
  type: 'set',
199
- xmlns: 'w:profile:picture'
197
+ xmlns: 'w:profile:picture',
198
+ ...(targetJid ? { target: targetJid } : {})
200
199
  },
201
200
  content: [
202
201
  {
@@ -211,18 +210,21 @@ const makeChatsSocket = (config) => {
211
210
  const removeProfilePicture = async (jid) => {
212
211
  let targetJid;
213
212
  if (!jid) {
214
- throw new boom_1.Boom('Illegal no-jid profile update. Please specify either your ID or the ID of the chat you wish to update');
213
+ throw new Boom('Illegal no-jid profile update. Please specify either your ID or the ID of the chat you wish to update');
215
214
  }
216
- if ((0, WABinary_1.jidNormalizedUser)(jid) !== (0, WABinary_1.jidNormalizedUser)(authState.creds.me.id)) {
217
- targetJid = (0, WABinary_1.jidNormalizedUser)(jid); // in case it is someone other than us
215
+ if (jidNormalizedUser(jid) !== jidNormalizedUser(authState.creds.me.id)) {
216
+ targetJid = jidNormalizedUser(jid); // in case it is someone other than us
217
+ }
218
+ else {
219
+ targetJid = undefined;
218
220
  }
219
221
  await query({
220
222
  tag: 'iq',
221
223
  attrs: {
222
- target: targetJid,
223
- to: WABinary_1.S_WHATSAPP_NET,
224
+ to: S_WHATSAPP_NET,
224
225
  type: 'set',
225
- xmlns: 'w:profile:picture'
226
+ xmlns: 'w:profile:picture',
227
+ ...(targetJid ? { target: targetJid } : {})
226
228
  }
227
229
  });
228
230
  };
@@ -231,7 +233,7 @@ const makeChatsSocket = (config) => {
231
233
  await query({
232
234
  tag: 'iq',
233
235
  attrs: {
234
- to: WABinary_1.S_WHATSAPP_NET,
236
+ to: S_WHATSAPP_NET,
235
237
  type: 'set',
236
238
  xmlns: 'status'
237
239
  },
@@ -252,19 +254,19 @@ const makeChatsSocket = (config) => {
252
254
  tag: 'iq',
253
255
  attrs: {
254
256
  xmlns: 'blocklist',
255
- to: WABinary_1.S_WHATSAPP_NET,
257
+ to: S_WHATSAPP_NET,
256
258
  type: 'get'
257
259
  }
258
260
  });
259
- const listNode = (0, WABinary_1.getBinaryNodeChild)(result, 'list');
260
- return (0, WABinary_1.getBinaryNodeChildren)(listNode, 'item').map(n => n.attrs.jid);
261
+ const listNode = getBinaryNodeChild(result, 'list');
262
+ return getBinaryNodeChildren(listNode, 'item').map(n => n.attrs.jid);
261
263
  };
262
264
  const updateBlockStatus = async (jid, action) => {
263
265
  await query({
264
266
  tag: 'iq',
265
267
  attrs: {
266
268
  xmlns: 'blocklist',
267
- to: WABinary_1.S_WHATSAPP_NET,
269
+ to: S_WHATSAPP_NET,
268
270
  type: 'set'
269
271
  },
270
272
  content: [
@@ -279,7 +281,6 @@ const makeChatsSocket = (config) => {
279
281
  });
280
282
  };
281
283
  const getBusinessProfile = async (jid) => {
282
- var _a, _b, _c, _d, _e, _f, _g;
283
284
  const results = await query({
284
285
  tag: 'iq',
285
286
  attrs: {
@@ -300,29 +301,29 @@ const makeChatsSocket = (config) => {
300
301
  }
301
302
  ]
302
303
  });
303
- const profileNode = (0, WABinary_1.getBinaryNodeChild)(results, 'business_profile');
304
- const profiles = (0, WABinary_1.getBinaryNodeChild)(profileNode, 'profile');
304
+ const profileNode = getBinaryNodeChild(results, 'business_profile');
305
+ const profiles = getBinaryNodeChild(profileNode, 'profile');
305
306
  if (profiles) {
306
- const address = (0, WABinary_1.getBinaryNodeChild)(profiles, 'address');
307
- const description = (0, WABinary_1.getBinaryNodeChild)(profiles, 'description');
308
- const website = (0, WABinary_1.getBinaryNodeChild)(profiles, 'website');
309
- const email = (0, WABinary_1.getBinaryNodeChild)(profiles, 'email');
310
- const category = (0, WABinary_1.getBinaryNodeChild)((0, WABinary_1.getBinaryNodeChild)(profiles, 'categories'), 'category');
311
- const businessHours = (0, WABinary_1.getBinaryNodeChild)(profiles, 'business_hours');
307
+ const address = getBinaryNodeChild(profiles, 'address');
308
+ const description = getBinaryNodeChild(profiles, 'description');
309
+ const website = getBinaryNodeChild(profiles, 'website');
310
+ const email = getBinaryNodeChild(profiles, 'email');
311
+ const category = getBinaryNodeChild(getBinaryNodeChild(profiles, 'categories'), 'category');
312
+ const businessHours = getBinaryNodeChild(profiles, 'business_hours');
312
313
  const businessHoursConfig = businessHours
313
- ? (0, WABinary_1.getBinaryNodeChildren)(businessHours, 'business_hours_config')
314
+ ? getBinaryNodeChildren(businessHours, 'business_hours_config')
314
315
  : undefined;
315
- const websiteStr = (_a = website === null || website === void 0 ? void 0 : website.content) === null || _a === void 0 ? void 0 : _a.toString();
316
+ const websiteStr = website?.content?.toString();
316
317
  return {
317
- wid: (_b = profiles.attrs) === null || _b === void 0 ? void 0 : _b.jid,
318
- address: (_c = address === null || address === void 0 ? void 0 : address.content) === null || _c === void 0 ? void 0 : _c.toString(),
319
- description: ((_d = description === null || description === void 0 ? void 0 : description.content) === null || _d === void 0 ? void 0 : _d.toString()) || '',
318
+ wid: profiles.attrs?.jid,
319
+ address: address?.content?.toString(),
320
+ description: description?.content?.toString() || '',
320
321
  website: websiteStr ? [websiteStr] : [],
321
- email: (_e = email === null || email === void 0 ? void 0 : email.content) === null || _e === void 0 ? void 0 : _e.toString(),
322
- category: (_f = category === null || category === void 0 ? void 0 : category.content) === null || _f === void 0 ? void 0 : _f.toString(),
322
+ email: email?.content?.toString(),
323
+ category: category?.content?.toString(),
323
324
  business_hours: {
324
- timezone: (_g = businessHours === null || businessHours === void 0 ? void 0 : businessHours.attrs) === null || _g === void 0 ? void 0 : _g.timezone,
325
- business_config: businessHoursConfig === null || businessHoursConfig === void 0 ? void 0 : businessHoursConfig.map(({ attrs }) => attrs)
325
+ timezone: businessHours?.attrs?.timezone,
326
+ business_config: businessHoursConfig?.map(({ attrs }) => attrs)
326
327
  }
327
328
  };
328
329
  }
@@ -332,7 +333,7 @@ const makeChatsSocket = (config) => {
332
333
  await sendNode({
333
334
  tag: 'iq',
334
335
  attrs: {
335
- to: WABinary_1.S_WHATSAPP_NET,
336
+ to: S_WHATSAPP_NET,
336
337
  type: 'set',
337
338
  xmlns: 'urn:xmpp:whatsapp:dirty',
338
339
  id: generateMessageTag()
@@ -351,7 +352,7 @@ const makeChatsSocket = (config) => {
351
352
  const newAppStateChunkHandler = (isInitialSync) => {
352
353
  return {
353
354
  onMutation(mutation) {
354
- (0, Utils_1.processSyncAction)(mutation, ev, authState.creds.me, isInitialSync ? { accountSettings: authState.creds.accountSettings } : undefined, logger);
355
+ processSyncAction(mutation, ev, authState.creds.me, isInitialSync ? { accountSettings: authState.creds.accountSettings } : undefined, logger);
355
356
  }
356
357
  };
357
358
  };
@@ -361,7 +362,6 @@ const makeChatsSocket = (config) => {
361
362
  const initialVersionMap = {};
362
363
  const globalMutationMap = {};
363
364
  await authState.keys.transaction(async () => {
364
- var _a;
365
365
  const collectionsToHandle = new Set(collections);
366
366
  // in case something goes wrong -- ensure we don't enter a loop that cannot be exited from
367
367
  const attemptsMap = {};
@@ -380,7 +380,7 @@ const makeChatsSocket = (config) => {
380
380
  }
381
381
  }
382
382
  else {
383
- state = (0, Utils_1.newLTHashState)();
383
+ state = newLTHashState();
384
384
  }
385
385
  states[name] = state;
386
386
  logger.info(`resyncing ${name} from v${state.version}`);
@@ -397,7 +397,7 @@ const makeChatsSocket = (config) => {
397
397
  const result = await query({
398
398
  tag: 'iq',
399
399
  attrs: {
400
- to: WABinary_1.S_WHATSAPP_NET,
400
+ to: S_WHATSAPP_NET,
401
401
  xmlns: 'w:sync:app:state',
402
402
  type: 'set'
403
403
  },
@@ -410,13 +410,13 @@ const makeChatsSocket = (config) => {
410
410
  ]
411
411
  });
412
412
  // extract from binary node
413
- const decoded = await (0, Utils_1.extractSyncdPatches)(result, config === null || config === void 0 ? void 0 : config.options);
413
+ const decoded = await extractSyncdPatches(result, config?.options);
414
414
  for (const key in decoded) {
415
415
  const name = key;
416
416
  const { patches, hasMorePatches, snapshot } = decoded[name];
417
417
  try {
418
418
  if (snapshot) {
419
- const { state: newState, mutationMap } = await (0, Utils_1.decodeSyncdSnapshot)(name, snapshot, getAppStateSyncKey, initialVersionMap[name], appStateMacVerification.snapshot);
419
+ const { state: newState, mutationMap } = await decodeSyncdSnapshot(name, snapshot, getAppStateSyncKey, initialVersionMap[name], appStateMacVerification.snapshot);
420
420
  states[name] = newState;
421
421
  Object.assign(globalMutationMap, mutationMap);
422
422
  logger.info(`restored state of ${name} from snapshot to v${newState.version} with mutations`);
@@ -424,7 +424,7 @@ const makeChatsSocket = (config) => {
424
424
  }
425
425
  // only process if there are syncd patches
426
426
  if (patches.length) {
427
- const { state: newState, mutationMap } = await (0, Utils_1.decodePatches)(name, patches, states[name], getAppStateSyncKey, config.options, initialVersionMap[name], logger, appStateMacVerification.patch);
427
+ const { state: newState, mutationMap } = await decodePatches(name, patches, states[name], getAppStateSyncKey, config.options, initialVersionMap[name], logger, appStateMacVerification.patch);
428
428
  await authState.keys.set({ 'app-state-sync-version': { [name]: newState } });
429
429
  logger.info(`synced ${name} to v${newState.version}`);
430
430
  initialVersionMap[name] = newState.version;
@@ -442,7 +442,7 @@ const makeChatsSocket = (config) => {
442
442
  // if retry attempts overshoot
443
443
  // or key not found
444
444
  const isIrrecoverableError = attemptsMap[name] >= MAX_SYNC_ATTEMPTS ||
445
- ((_a = error.output) === null || _a === void 0 ? void 0 : _a.statusCode) === 404 ||
445
+ error.output?.statusCode === 404 ||
446
446
  error.name === 'TypeError';
447
447
  logger.info({ name, error: error.stack }, `failed to sync state from version${isIrrecoverableError ? '' : ', removing and trying from scratch'}`);
448
448
  await authState.keys.set({ 'app-state-sync-version': { [name]: null } });
@@ -467,20 +467,19 @@ const makeChatsSocket = (config) => {
467
467
  * type = "image for the high res picture"
468
468
  */
469
469
  const profilePictureUrl = async (jid, type = 'preview', timeoutMs) => {
470
- var _a;
471
- jid = (0, WABinary_1.jidNormalizedUser)(jid);
470
+ jid = jidNormalizedUser(jid);
472
471
  const result = await query({
473
472
  tag: 'iq',
474
473
  attrs: {
475
474
  target: jid,
476
- to: WABinary_1.S_WHATSAPP_NET,
475
+ to: S_WHATSAPP_NET,
477
476
  type: 'get',
478
477
  xmlns: 'w:profile:picture'
479
478
  },
480
479
  content: [{ tag: 'picture', attrs: { type, query: 'url' } }]
481
480
  }, timeoutMs);
482
- const child = (0, WABinary_1.getBinaryNodeChild)(result, 'picture');
483
- return (_a = child === null || child === void 0 ? void 0 : child.attrs) === null || _a === void 0 ? void 0 : _a.url;
481
+ const child = getBinaryNodeChild(result, 'picture');
482
+ return child?.attrs?.url;
484
483
  };
485
484
  const sendPresenceUpdate = async (type, toJid) => {
486
485
  const me = authState.creds.me;
@@ -499,7 +498,7 @@ const makeChatsSocket = (config) => {
499
498
  });
500
499
  }
501
500
  else {
502
- const { server } = (0, WABinary_1.jidDecode)(toJid);
501
+ const { server } = jidDecode(toJid);
503
502
  const isLid = server === 'lid';
504
503
  await sendNode({
505
504
  tag: 'chatstate',
@@ -538,7 +537,6 @@ const makeChatsSocket = (config) => {
538
537
  : undefined
539
538
  });
540
539
  const handlePresenceUpdate = ({ tag, attrs, content }) => {
541
- var _a;
542
540
  let presence;
543
541
  const jid = attrs.from;
544
542
  const participant = attrs.participant || attrs.from;
@@ -557,7 +555,7 @@ const makeChatsSocket = (config) => {
557
555
  if (type === 'paused') {
558
556
  type = 'available';
559
557
  }
560
- if (((_a = firstChild.attrs) === null || _a === void 0 ? void 0 : _a.media) === 'audio') {
558
+ if (firstChild.attrs?.media === 'audio') {
561
559
  type = 'recording';
562
560
  }
563
561
  presence = { lastKnownPresence: type };
@@ -573,7 +571,7 @@ const makeChatsSocket = (config) => {
573
571
  const name = patchCreate.type;
574
572
  const myAppStateKeyId = authState.creds.myAppStateKeyId;
575
573
  if (!myAppStateKeyId) {
576
- throw new boom_1.Boom('App state key not present!', { statusCode: 400 });
574
+ throw new Boom('App state key not present!', { statusCode: 400 });
577
575
  }
578
576
  let initial;
579
577
  let encodeResult;
@@ -582,13 +580,13 @@ const makeChatsSocket = (config) => {
582
580
  logger.debug({ patch: patchCreate }, 'applying app patch');
583
581
  await resyncAppState([name], false);
584
582
  const { [name]: currentSyncVersion } = await authState.keys.get('app-state-sync-version', [name]);
585
- initial = currentSyncVersion || (0, Utils_1.newLTHashState)();
586
- encodeResult = await (0, Utils_1.encodeSyncdPatch)(patchCreate, myAppStateKeyId, initial, getAppStateSyncKey);
583
+ initial = currentSyncVersion || newLTHashState();
584
+ encodeResult = await encodeSyncdPatch(patchCreate, myAppStateKeyId, initial, getAppStateSyncKey);
587
585
  const { patch, state } = encodeResult;
588
586
  const node = {
589
587
  tag: 'iq',
590
588
  attrs: {
591
- to: WABinary_1.S_WHATSAPP_NET,
589
+ to: S_WHATSAPP_NET,
592
590
  type: 'set',
593
591
  xmlns: 'w:sync:app:state'
594
592
  },
@@ -608,7 +606,7 @@ const makeChatsSocket = (config) => {
608
606
  {
609
607
  tag: 'patch',
610
608
  attrs: {},
611
- content: WAProto_1.proto.SyncdPatch.encode(patch).finish()
609
+ content: proto.SyncdPatch.encode(patch).finish()
612
610
  }
613
611
  ]
614
612
  }
@@ -622,7 +620,7 @@ const makeChatsSocket = (config) => {
622
620
  });
623
621
  if (config.emitOwnEvents) {
624
622
  const { onMutation } = newAppStateChunkHandler(false);
625
- const { mutationMap } = await (0, Utils_1.decodePatches)(name, [{ ...encodeResult.patch, version: { version: encodeResult.state.version } }], initial, getAppStateSyncKey, config.options, undefined, logger);
623
+ const { mutationMap } = await decodePatches(name, [{ ...encodeResult.patch, version: { version: encodeResult.state.version } }], initial, getAppStateSyncKey, config.options, undefined, logger);
626
624
  for (const key in mutationMap) {
627
625
  onMutation(mutationMap[key]);
628
626
  }
@@ -630,11 +628,10 @@ const makeChatsSocket = (config) => {
630
628
  };
631
629
  /** sending non-abt props may fix QR scan fail if server expects */
632
630
  const fetchProps = async () => {
633
- var _a, _b, _c;
634
631
  const resultNode = await query({
635
632
  tag: 'iq',
636
633
  attrs: {
637
- to: WABinary_1.S_WHATSAPP_NET,
634
+ to: S_WHATSAPP_NET,
638
635
  xmlns: 'w',
639
636
  type: 'get'
640
637
  },
@@ -643,20 +640,20 @@ const makeChatsSocket = (config) => {
643
640
  tag: 'props',
644
641
  attrs: {
645
642
  protocol: '2',
646
- hash: ((_a = authState === null || authState === void 0 ? void 0 : authState.creds) === null || _a === void 0 ? void 0 : _a.lastPropHash) || ''
643
+ hash: authState?.creds?.lastPropHash || ''
647
644
  }
648
645
  }
649
646
  ]
650
647
  });
651
- const propsNode = (0, WABinary_1.getBinaryNodeChild)(resultNode, 'props');
648
+ const propsNode = getBinaryNodeChild(resultNode, 'props');
652
649
  let props = {};
653
650
  if (propsNode) {
654
- if ((_b = propsNode.attrs) === null || _b === void 0 ? void 0 : _b.hash) {
651
+ if (propsNode.attrs?.hash) {
655
652
  // on some clients, the hash is returning as undefined
656
- authState.creds.lastPropHash = (_c = propsNode === null || propsNode === void 0 ? void 0 : propsNode.attrs) === null || _c === void 0 ? void 0 : _c.hash;
653
+ authState.creds.lastPropHash = propsNode?.attrs?.hash;
657
654
  ev.emit('creds.update', authState.creds);
658
655
  }
659
- props = (0, WABinary_1.reduceBinaryNodeToDictionary)(propsNode, 'prop');
656
+ props = reduceBinaryNodeToDictionary(propsNode, 'prop');
660
657
  }
661
658
  logger.debug('fetched props');
662
659
  return props;
@@ -667,9 +664,17 @@ const makeChatsSocket = (config) => {
667
664
  * requires the last messages till the last message received; required for archive & unread
668
665
  */
669
666
  const chatModify = (mod, jid) => {
670
- const patch = (0, Utils_1.chatModificationToAppPatch)(mod, jid);
667
+ const patch = chatModificationToAppPatch(mod, jid);
671
668
  return appPatch(patch);
672
669
  };
670
+ /**
671
+ * Enable/Disable link preview privacy, not related to baileys link preview generation
672
+ */
673
+ const updateDisableLinkPreviewsPrivacy = (isPreviewsDisabled) => {
674
+ return chatModify({
675
+ disableLinkPreviews: { isPreviewsDisabled }
676
+ }, '');
677
+ };
673
678
  /**
674
679
  * Star or Unstar a message
675
680
  */
@@ -757,35 +762,58 @@ const makeChatsSocket = (config) => {
757
762
  await Promise.all([fetchProps(), fetchBlocklist(), fetchPrivacySettings()]);
758
763
  };
759
764
  const upsertMessage = ev.createBufferedFunction(async (msg, type) => {
760
- var _a, _b, _c;
761
765
  ev.emit('messages.upsert', { messages: [msg], type });
762
766
  if (!!msg.pushName) {
763
767
  let jid = msg.key.fromMe ? authState.creds.me.id : msg.key.participant || msg.key.remoteJid;
764
- jid = (0, WABinary_1.jidNormalizedUser)(jid);
768
+ jid = jidNormalizedUser(jid);
765
769
  if (!msg.key.fromMe) {
766
770
  ev.emit('contacts.update', [{ id: jid, notify: msg.pushName, verifiedName: msg.verifiedBizName }]);
767
771
  }
768
772
  // update our pushname too
769
- if (msg.key.fromMe && msg.pushName && ((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.name) !== msg.pushName) {
773
+ if (msg.key.fromMe && msg.pushName && authState.creds.me?.name !== msg.pushName) {
770
774
  ev.emit('creds.update', { me: { ...authState.creds.me, name: msg.pushName } });
771
775
  }
772
776
  }
773
- const historyMsg = (0, Utils_1.getHistoryMsg)(msg.message);
777
+ const historyMsg = getHistoryMsg(msg.message);
774
778
  const shouldProcessHistoryMsg = historyMsg
775
- ? shouldSyncHistoryMessage(historyMsg) && Defaults_1.PROCESSABLE_HISTORY_TYPES.includes(historyMsg.syncType)
779
+ ? shouldSyncHistoryMessage(historyMsg) && PROCESSABLE_HISTORY_TYPES.includes(historyMsg.syncType)
776
780
  : false;
777
- if (historyMsg && !authState.creds.myAppStateKeyId) {
778
- logger.warn('skipping app state sync, as myAppStateKeyId is not set');
779
- pendingAppStateSync = true;
781
+ // State machine: decide on sync and flush
782
+ if (historyMsg && syncState === SyncState.AwaitingInitialSync) {
783
+ if (awaitingSyncTimeout) {
784
+ clearTimeout(awaitingSyncTimeout);
785
+ awaitingSyncTimeout = undefined;
786
+ }
787
+ if (shouldProcessHistoryMsg) {
788
+ syncState = SyncState.Syncing;
789
+ logger.info('Transitioned to Syncing state');
790
+ // Let doAppStateSync handle the final flush after it's done
791
+ }
792
+ else {
793
+ syncState = SyncState.Online;
794
+ logger.info('History sync skipped, transitioning to Online state and flushing buffer');
795
+ ev.flush();
796
+ }
780
797
  }
798
+ const doAppStateSync = async () => {
799
+ if (syncState === SyncState.Syncing) {
800
+ logger.info('Doing app state sync');
801
+ await resyncAppState(ALL_WA_PATCH_NAMES, true);
802
+ // Sync is complete, go online and flush everything
803
+ syncState = SyncState.Online;
804
+ logger.info('App state sync complete, transitioning to Online state and flushing buffer');
805
+ ev.flush();
806
+ const accountSyncCounter = (authState.creds.accountSyncCounter || 0) + 1;
807
+ ev.emit('creds.update', { accountSyncCounter });
808
+ }
809
+ };
781
810
  await Promise.all([
782
811
  (async () => {
783
- if (historyMsg && authState.creds.myAppStateKeyId) {
784
- pendingAppStateSync = false;
812
+ if (shouldProcessHistoryMsg) {
785
813
  await doAppStateSync();
786
814
  }
787
815
  })(),
788
- (0, process_message_1.default)(msg, {
816
+ processMessage(msg, {
789
817
  shouldProcessHistoryMsg,
790
818
  placeholderResendCache,
791
819
  ev,
@@ -795,27 +823,16 @@ const makeChatsSocket = (config) => {
795
823
  options: config.options
796
824
  })
797
825
  ]);
798
- if (((_c = (_b = msg.message) === null || _b === void 0 ? void 0 : _b.protocolMessage) === null || _c === void 0 ? void 0 : _c.appStateSyncKeyShare) && pendingAppStateSync) {
826
+ // If the app state key arrives and we are waiting to sync, trigger the sync now.
827
+ if (msg.message?.protocolMessage?.appStateSyncKeyShare && syncState === SyncState.Syncing) {
828
+ logger.info('App state sync key arrived, triggering app state sync');
799
829
  await doAppStateSync();
800
- pendingAppStateSync = false;
801
- }
802
- async function doAppStateSync() {
803
- if (!authState.creds.accountSyncCounter) {
804
- logger.info('doing initial app state sync');
805
- await resyncAppState(Types_1.ALL_WA_PATCH_NAMES, true);
806
- const accountSyncCounter = (authState.creds.accountSyncCounter || 0) + 1;
807
- ev.emit('creds.update', { accountSyncCounter });
808
- if (needToFlushWithAppStateSync) {
809
- logger.debug('flushing with app state sync');
810
- ev.flush();
811
- }
812
- }
813
830
  }
814
831
  });
815
832
  ws.on('CB:presence', handlePresenceUpdate);
816
833
  ws.on('CB:chatstate', handlePresenceUpdate);
817
834
  ws.on('CB:ib,,dirty', async (node) => {
818
- const { attrs } = (0, WABinary_1.getBinaryNodeChild)(node, 'dirty');
835
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
819
836
  const type = attrs.type;
820
837
  switch (type) {
821
838
  case 'account_sync':
@@ -837,21 +854,38 @@ const makeChatsSocket = (config) => {
837
854
  }
838
855
  });
839
856
  ev.on('connection.update', ({ connection, receivedPendingNotifications }) => {
840
- var _a;
841
857
  if (connection === 'open') {
842
858
  if (fireInitQueries) {
843
859
  executeInitQueries().catch(error => onUnexpectedError(error, 'init queries'));
844
860
  }
845
861
  sendPresenceUpdate(markOnlineOnConnect ? 'available' : 'unavailable').catch(error => onUnexpectedError(error, 'presence update requests'));
846
862
  }
847
- if (receivedPendingNotifications && // if we don't have the app state key
848
- // we keep buffering events until we finally have
849
- // the key and can sync the messages
850
- // todo scrutinize
851
- !((_a = authState.creds) === null || _a === void 0 ? void 0 : _a.myAppStateKeyId)) {
852
- ev.buffer();
853
- needToFlushWithAppStateSync = true;
863
+ if (!receivedPendingNotifications || syncState !== SyncState.Connecting) {
864
+ return;
854
865
  }
866
+ syncState = SyncState.AwaitingInitialSync;
867
+ logger.info('Connection is now AwaitingInitialSync, buffering events');
868
+ ev.buffer();
869
+ const willSyncHistory = shouldSyncHistoryMessage(proto.Message.HistorySyncNotification.fromObject({
870
+ syncType: proto.HistorySync.HistorySyncType.RECENT
871
+ }));
872
+ if (!willSyncHistory) {
873
+ logger.info('History sync is disabled by config, not waiting for notification. Transitioning to Online.');
874
+ syncState = SyncState.Online;
875
+ setTimeout(() => ev.flush(), 0);
876
+ return;
877
+ }
878
+ logger.info('History sync is enabled, awaiting notification with a 20s timeout.');
879
+ if (awaitingSyncTimeout) {
880
+ clearTimeout(awaitingSyncTimeout);
881
+ }
882
+ awaitingSyncTimeout = setTimeout(() => {
883
+ if (syncState === SyncState.AwaitingInitialSync) {
884
+ logger.warn('Timeout in AwaitingInitialSync, forcing state to Online and flushing buffer');
885
+ syncState = SyncState.Online;
886
+ ev.flush();
887
+ }
888
+ }, 20000);
855
889
  });
856
890
  return {
857
891
  ...sock,
@@ -872,6 +906,7 @@ const makeChatsSocket = (config) => {
872
906
  updateProfileStatus,
873
907
  updateProfileName,
874
908
  updateBlockStatus,
909
+ updateDisableLinkPreviewsPrivacy,
875
910
  updateCallPrivacy,
876
911
  updateMessagesPrivacy,
877
912
  updateLastSeenPrivacy,
@@ -895,4 +930,4 @@ const makeChatsSocket = (config) => {
895
930
  star
896
931
  };
897
932
  };
898
- exports.makeChatsSocket = makeChatsSocket;
933
+ //# sourceMappingURL=chats.js.map