@badzz88/baileys 8.5.4 → 8.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (269) hide show
  1. package/README.md +2 -2
  2. package/WAProto/fix-imports.js +69 -79
  3. package/WAProto/index.d.ts +15305 -87935
  4. package/WAProto/index.js +41109 -194076
  5. package/engine-requirements.js +15 -8
  6. package/package.json +23 -19
  7. package/src/Defaults/index.js +186 -0
  8. package/src/Signal/Group/ciphertext-message.js +15 -0
  9. package/src/Signal/Group/group-session-builder.js +86 -0
  10. package/src/Signal/Group/group_cipher.js +82 -0
  11. package/src/Signal/Group/index.js +140 -0
  12. package/src/Signal/Group/keyhelper.js +77 -0
  13. package/src/Signal/Group/sender-chain-key.js +29 -0
  14. package/src/Signal/Group/sender-key-distribution-message.js +66 -0
  15. package/src/Signal/Group/sender-key-message.js +69 -0
  16. package/src/Signal/Group/sender-key-name.js +51 -0
  17. package/src/Signal/Group/sender-key-record.js +44 -0
  18. package/src/Signal/Group/sender-key-state.js +87 -0
  19. package/src/Signal/Group/sender-message-key.js +29 -0
  20. package/src/Signal/libsignal.js +455 -0
  21. package/src/Signal/lid-mapping.js +273 -0
  22. package/src/Socket/Client/index.js +31 -0
  23. package/src/Socket/Client/types.js +13 -0
  24. package/src/Socket/Client/websocket.js +61 -0
  25. package/src/Socket/aigroups.js +221 -0
  26. package/src/Socket/business.js +411 -0
  27. package/src/Socket/chats.js +1449 -0
  28. package/src/Socket/communities.js +463 -0
  29. package/src/Socket/graphql.js +523 -0
  30. package/src/Socket/groups.js +516 -0
  31. package/src/Socket/index.js +31 -0
  32. package/src/Socket/interactive-handler.js +527 -0
  33. package/src/Socket/interop.js +339 -0
  34. package/src/Socket/luxu.js +349 -0
  35. package/src/Socket/managed-account.js +98 -0
  36. package/src/Socket/messages-recv.js +2685 -0
  37. package/src/Socket/messages-send.js +2047 -0
  38. package/src/Socket/mex.js +57 -0
  39. package/src/Socket/newsletter.js +455 -0
  40. package/src/Socket/privacy.js +125 -0
  41. package/src/Socket/registration.js +236 -0
  42. package/src/Socket/socket.js +983 -0
  43. package/src/Socket/text-router.js +65 -0
  44. package/src/Socket/username.js +130 -0
  45. package/src/Store/index.js +33 -0
  46. package/src/Store/keyed-db.js +118 -0
  47. package/src/Store/make-cache-manager-store.js +84 -0
  48. package/src/Store/make-in-memory-store.js +551 -0
  49. package/src/Store/make-ordered-dictionary.js +81 -0
  50. package/src/Store/object-repository.js +26 -0
  51. package/src/Types/Auth.js +31 -0
  52. package/src/Types/Bussines.js +2 -0
  53. package/src/Types/Call.js +2 -0
  54. package/src/Types/Chat.js +4 -0
  55. package/src/Types/Contact.js +2 -0
  56. package/src/Types/Events.js +2 -0
  57. package/src/Types/GroupMetadata.js +2 -0
  58. package/src/Types/Label.js +26 -0
  59. package/src/Types/LabelAssociation.js +8 -0
  60. package/src/Types/Message.js +93 -0
  61. package/src/Types/Mex.js +112 -0
  62. package/src/Types/Newsletter.js +109 -0
  63. package/src/Types/Product.js +2 -0
  64. package/src/Types/Signal.js +2 -0
  65. package/src/Types/Socket.js +2 -0
  66. package/src/Types/State.js +62 -0
  67. package/src/Types/USync.js +2 -0
  68. package/src/Types/index.js +56 -0
  69. package/src/Utils/auth-utils.js +254 -0
  70. package/src/Utils/browser-utils.js +112 -0
  71. package/src/Utils/business.js +240 -0
  72. package/src/Utils/chat-utils.js +1230 -0
  73. package/src/Utils/command-loader.d.ts +27 -0
  74. package/src/Utils/command-loader.js +89 -0
  75. package/src/Utils/companion-reg-client-utils.js +40 -0
  76. package/src/Utils/consumer-application.js +105 -0
  77. package/src/Utils/crypto.js +118 -0
  78. package/src/Utils/curve25519-js.js +242 -0
  79. package/src/Utils/decode-wa-message.js +529 -0
  80. package/src/Utils/event-buffer.js +580 -0
  81. package/src/Utils/generics.js +483 -0
  82. package/src/Utils/group-history.js +44 -0
  83. package/src/Utils/history.js +232 -0
  84. package/src/Utils/identity-change-handler.js +52 -0
  85. package/src/Utils/index.js +58 -0
  86. package/src/Utils/jid-display-normalization.js +195 -0
  87. package/src/Utils/link-preview.js +135 -0
  88. package/src/Utils/logger.js +8 -0
  89. package/src/Utils/lt-hash.js +25 -0
  90. package/src/Utils/make-mutex.js +36 -0
  91. package/src/Utils/message-composer.js +471 -0
  92. package/src/Utils/message-retry-manager.js +217 -0
  93. package/src/Utils/messages-media.js +843 -0
  94. package/src/Utils/messages.js +2817 -0
  95. package/src/Utils/meta-ai-msmsg.js +222 -0
  96. package/src/Utils/native-bridge.js +68 -0
  97. package/src/Utils/noise-handler.js +169 -0
  98. package/src/Utils/offline-node-processor.js +34 -0
  99. package/src/Utils/pre-key-manager.js +90 -0
  100. package/src/Utils/process-message.js +950 -0
  101. package/src/Utils/reporting-utils.js +261 -0
  102. package/src/Utils/session-pool.d.ts +14 -0
  103. package/src/Utils/session-pool.js +70 -0
  104. package/src/Utils/signal.js +217 -0
  105. package/src/Utils/stanza-ack.js +30 -0
  106. package/src/Utils/sticker.d.ts +13 -0
  107. package/src/Utils/sticker.js +123 -0
  108. package/src/Utils/sync-action-utils.js +45 -0
  109. package/src/Utils/tc-token-utils.js +158 -0
  110. package/src/Utils/use-multi-file-auth-state.js +111 -0
  111. package/src/Utils/validate-connection.js +209 -0
  112. package/src/Utils/view-once-cache.d.ts +12 -0
  113. package/src/Utils/view-once-cache.js +63 -0
  114. package/src/Utils/voip-rekey.js +22 -0
  115. package/src/WABinary/constants.js +1304 -0
  116. package/src/WABinary/decode.js +342 -0
  117. package/src/WABinary/encode.js +225 -0
  118. package/src/WABinary/generic-utils.js +238 -0
  119. package/src/WABinary/index.js +34 -0
  120. package/src/WABinary/jid-utils.js +351 -0
  121. package/src/WABinary/types.js +2 -0
  122. package/src/WAM/BinaryInfo.js +13 -0
  123. package/src/WAM/constants.js +39484 -0
  124. package/src/WAM/encode.js +149 -0
  125. package/src/WAM/index.js +32 -0
  126. package/src/WAUSync/Protocols/USyncBotProfileProtocol.js +53 -0
  127. package/src/WAUSync/Protocols/USyncBusinessProtocol.js +44 -0
  128. package/src/WAUSync/Protocols/USyncContactProtocol.js +55 -0
  129. package/src/WAUSync/Protocols/USyncDeviceProtocol.js +55 -0
  130. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +31 -0
  131. package/src/WAUSync/Protocols/USyncFeatureProtocol.js +54 -0
  132. package/src/WAUSync/Protocols/USyncLIDProtocol.js +32 -0
  133. package/src/WAUSync/Protocols/USyncNewsletterProtocol.js +473 -0
  134. package/src/WAUSync/Protocols/USyncPictureProtocol.js +34 -0
  135. package/src/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
  136. package/src/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  137. package/src/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
  138. package/src/WAUSync/Protocols/USyncUsernameProtocol.js +28 -0
  139. package/src/WAUSync/Protocols/index.js +40 -0
  140. package/src/WAUSync/USyncQuery.js +126 -0
  141. package/src/WAUSync/USyncUser.js +50 -0
  142. package/src/WAUSync/index.js +32 -0
  143. package/src/antiban.js +4152 -0
  144. package/{lib → src}/index.js +51 -49
  145. package/lib/Defaults/index.js +0 -195
  146. package/lib/Signal/Group/ciphertext-message.js +0 -15
  147. package/lib/Signal/Group/group-session-builder.js +0 -92
  148. package/lib/Signal/Group/group_cipher.js +0 -89
  149. package/lib/Signal/Group/index.js +0 -136
  150. package/lib/Signal/Group/keyhelper.js +0 -73
  151. package/lib/Signal/Group/sender-chain-key.js +0 -32
  152. package/lib/Signal/Group/sender-key-distribution-message.js +0 -66
  153. package/lib/Signal/Group/sender-key-message.js +0 -69
  154. package/lib/Signal/Group/sender-key-name.js +0 -50
  155. package/lib/Signal/Group/sender-key-record.js +0 -44
  156. package/lib/Signal/Group/sender-key-state.js +0 -97
  157. package/lib/Signal/Group/sender-message-key.js +0 -30
  158. package/lib/Signal/libsignal.js +0 -470
  159. package/lib/Signal/lid-mapping.js +0 -280
  160. package/lib/Socket/Client/index.js +0 -30
  161. package/lib/Socket/Client/types.js +0 -13
  162. package/lib/Socket/Client/websocket.js +0 -62
  163. package/lib/Socket/aigroups.js +0 -240
  164. package/lib/Socket/business.js +0 -414
  165. package/lib/Socket/chats.js +0 -2165
  166. package/lib/Socket/communities.js +0 -545
  167. package/lib/Socket/graphql.js +0 -863
  168. package/lib/Socket/groups.js +0 -685
  169. package/lib/Socket/index.js +0 -41
  170. package/lib/Socket/interactive-handler.js +0 -579
  171. package/lib/Socket/interop.js +0 -430
  172. package/lib/Socket/managed-account.js +0 -214
  173. package/lib/Socket/messages-recv.js +0 -3000
  174. package/lib/Socket/messages-send.js +0 -2155
  175. package/lib/Socket/mex.js +0 -47
  176. package/lib/Socket/newsletter.js +0 -814
  177. package/lib/Socket/privacy.js +0 -261
  178. package/lib/Socket/registration.js +0 -434
  179. package/lib/Socket/socket.js +0 -1074
  180. package/lib/Socket/username.js +0 -160
  181. package/lib/Store/index.js +0 -36
  182. package/lib/Store/make-cache-manager-store.js +0 -90
  183. package/lib/Store/make-in-memory-store.js +0 -488
  184. package/lib/Store/make-ordered-dictionary.js +0 -81
  185. package/lib/Store/object-repository.js +0 -29
  186. package/lib/Types/Auth.js +0 -38
  187. package/lib/Types/Bussines.js +0 -2
  188. package/lib/Types/Call.js +0 -2
  189. package/lib/Types/Chat.js +0 -4
  190. package/lib/Types/Contact.js +0 -2
  191. package/lib/Types/Events.js +0 -2
  192. package/lib/Types/GroupMetadata.js +0 -2
  193. package/lib/Types/Label.js +0 -27
  194. package/lib/Types/LabelAssociation.js +0 -9
  195. package/lib/Types/Message.js +0 -95
  196. package/lib/Types/Newsletter.js +0 -152
  197. package/lib/Types/Product.js +0 -2
  198. package/lib/Types/Signal.js +0 -2
  199. package/lib/Types/Socket.js +0 -2
  200. package/lib/Types/State.js +0 -70
  201. package/lib/Types/USync.js +0 -2
  202. package/lib/Types/index.js +0 -55
  203. package/lib/Utils/auth-utils.js +0 -301
  204. package/lib/Utils/browser-utils.js +0 -114
  205. package/lib/Utils/business.js +0 -243
  206. package/lib/Utils/chat-utils.js +0 -1272
  207. package/lib/Utils/consumer-application.js +0 -107
  208. package/lib/Utils/crypto.js +0 -125
  209. package/lib/Utils/decode-wa-message.js +0 -793
  210. package/lib/Utils/event-buffer.js +0 -583
  211. package/lib/Utils/generics.js +0 -617
  212. package/lib/Utils/group-history.js +0 -51
  213. package/lib/Utils/history.js +0 -244
  214. package/lib/Utils/identity-change-handler.js +0 -52
  215. package/lib/Utils/index.js +0 -53
  216. package/lib/Utils/jid-display-normalization.js +0 -218
  217. package/lib/Utils/link-preview.js +0 -138
  218. package/lib/Utils/logger.js +0 -9
  219. package/lib/Utils/lt-hash.js +0 -6
  220. package/lib/Utils/make-mutex.js +0 -36
  221. package/lib/Utils/message-composer.js +0 -479
  222. package/lib/Utils/message-inspect.js +0 -393
  223. package/lib/Utils/message-retry-manager.js +0 -205
  224. package/lib/Utils/messages-media.js +0 -925
  225. package/lib/Utils/messages.js +0 -2482
  226. package/lib/Utils/meta-ai-msmsg.js +0 -122
  227. package/lib/Utils/noise-handler.js +0 -194
  228. package/lib/Utils/offline-node-processor.js +0 -37
  229. package/lib/Utils/pre-key-manager.js +0 -97
  230. package/lib/Utils/process-message.js +0 -1139
  231. package/lib/Utils/reporting-utils.js +0 -262
  232. package/lib/Utils/signal.js +0 -189
  233. package/lib/Utils/stanza-ack.js +0 -67
  234. package/lib/Utils/sync-action-utils.js +0 -51
  235. package/lib/Utils/tc-token-utils.js +0 -156
  236. package/lib/Utils/use-multi-file-auth-state.js +0 -137
  237. package/lib/Utils/validate-connection.js +0 -259
  238. package/lib/Utils/voip-rekey.js +0 -22
  239. package/lib/WABinary/constants.js +0 -1304
  240. package/lib/WABinary/decode.js +0 -377
  241. package/lib/WABinary/encode.js +0 -58
  242. package/lib/WABinary/generic-utils.js +0 -150
  243. package/lib/WABinary/index.js +0 -33
  244. package/lib/WABinary/jid-utils.js +0 -374
  245. package/lib/WABinary/types.js +0 -2
  246. package/lib/WAM/BinaryInfo.js +0 -13
  247. package/lib/WAM/constants.js +0 -39486
  248. package/lib/WAM/encode.js +0 -142
  249. package/lib/WAM/index.js +0 -31
  250. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +0 -55
  251. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +0 -100
  252. package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -60
  253. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -65
  254. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -27
  255. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +0 -72
  256. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -31
  257. package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -865
  258. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +0 -32
  259. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +0 -29
  260. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -44
  261. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +0 -38
  262. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +0 -28
  263. package/lib/WAUSync/Protocols/index.js +0 -40
  264. package/lib/WAUSync/USyncBackoff.js +0 -26
  265. package/lib/WAUSync/USyncQuery.js +0 -204
  266. package/lib/WAUSync/USyncUser.js +0 -58
  267. package/lib/WAUSync/index.js +0 -32
  268. package/lib/antiban.js +0 -4391
  269. /package/{lib → src}/Defaults/phonenumber-mcc.json +0 -0
@@ -1,1139 +0,0 @@
1
- 'use strict'
2
- Object.defineProperty(exports, '__esModule', { value: true })
3
- exports.getChatId = exports.shouldIncrementChatUnread = exports.isRealMessage = exports.cleanMessage = void 0
4
- exports.decryptPollVote = decryptPollVote
5
- exports.decryptEventResponse = decryptEventResponse
6
- const index_js_1 = require('../../WAProto/index.js')
7
- const Types_1 = require('../Types')
8
- const messages_1 = require('../Utils/messages')
9
- const WABinary_1 = require('../WABinary')
10
- const crypto_1 = require('./crypto')
11
- const generics_1 = require('./generics')
12
- const history_1 = require('./history')
13
- const tc_token_utils_1 = require('./tc-token-utils')
14
- async function storeTcTokensFromHistorySync(chats, signalRepository, keyStore, logger) {
15
- const getLIDForPN = signalRepository.lidMapping.getLIDForPN.bind(signalRepository.lidMapping)
16
- const candidates = []
17
- for (const chat of chats) {
18
- const ts = chat.tcTokenTimestamp ? (0, generics_1.toNumber)(chat.tcTokenTimestamp) : 0
19
- if (chat.tcToken?.length && ts > 0) {
20
- const jid = (0, WABinary_1.jidNormalizedUser)(chat.id)
21
- const storageJid = await (0, tc_token_utils_1.resolveTcTokenJid)(jid, getLIDForPN)
22
- candidates.push({
23
- storageJid,
24
- token: Buffer.from(chat.tcToken),
25
- ts,
26
- senderTs: chat.tcTokenSenderTimestamp ? (0, generics_1.toNumber)(chat.tcTokenSenderTimestamp) : undefined
27
- })
28
- }
29
- }
30
- if (!candidates.length) return
31
- const jids = candidates.map(c => c.storageJid)
32
- const existing = await keyStore.get('tctoken', jids)
33
- const entries = {}
34
- for (const c of candidates) {
35
- const existingEntry = existing[c.storageJid]
36
- const existingTs = existingEntry?.timestamp ? Number(existingEntry.timestamp) : 0
37
- if (existingTs > 0 && existingTs >= c.ts) continue
38
- entries[c.storageJid] = {
39
- ...existingEntry,
40
- token: c.token,
41
- timestamp: String(c.ts),
42
- ...(c.senderTs !== undefined ? { senderTimestamp: c.senderTs } : {})
43
- }
44
- }
45
- if (Object.keys(entries).length) {
46
- logger?.debug({ count: Object.keys(entries).length }, 'storing tctokens from history sync')
47
- try {
48
- const indexWrite = await (0, tc_token_utils_1.buildMergedTcTokenIndexWrite)(keyStore, Object.keys(entries))
49
- await keyStore.set({ tctoken: { ...entries, ...indexWrite } })
50
- } catch (err) {
51
- logger?.warn({ err }, 'failed to store tctokens from history sync')
52
- }
53
- }
54
- }
55
- const REAL_MSG_STUB_TYPES = new Set([
56
- Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
57
- Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE,
58
- Types_1.WAMessageStubType.CALL_MISSED_VIDEO,
59
- Types_1.WAMessageStubType.CALL_MISSED_VOICE
60
- ])
61
- const REAL_MSG_REQ_ME_STUB_TYPES = new Set([Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD])
62
-
63
- const cleanMessage = (message, meId, meLid) => {
64
-
65
- if ((0, WABinary_1.isHostedPnUser)(message.key.remoteJid) || (0, WABinary_1.isHostedLidUser)(message.key.remoteJid)) {
66
- message.key.remoteJid = (0, WABinary_1.jidEncode)(
67
- (0, WABinary_1.jidDecode)(message.key?.remoteJid)?.user,
68
- (0, WABinary_1.isHostedPnUser)(message.key.remoteJid) ? 's.whatsapp.net' : 'lid'
69
- )
70
- } else {
71
- message.key.remoteJid = (0, WABinary_1.jidNormalizedUser)(message.key.remoteJid)
72
- }
73
- if (
74
- (0, WABinary_1.isHostedPnUser)(message.key.participant) ||
75
- (0, WABinary_1.isHostedLidUser)(message.key.participant)
76
- ) {
77
- message.key.participant = (0, WABinary_1.jidEncode)(
78
- (0, WABinary_1.jidDecode)(message.key.participant)?.user,
79
- (0, WABinary_1.isHostedPnUser)(message.key.participant) ? 's.whatsapp.net' : 'lid'
80
- )
81
- } else {
82
- message.key.participant = (0, WABinary_1.jidNormalizedUser)(message.key.participant)
83
- }
84
- const content = (0, messages_1.normalizeMessageContent)(message.message)
85
-
86
- if (content?.reactionMessage) {
87
- normaliseKey(content.reactionMessage.key)
88
- }
89
- if (content?.pollUpdateMessage) {
90
- normaliseKey(content.pollUpdateMessage.pollCreationMessageKey)
91
- }
92
- function normaliseKey(msgKey) {
93
-
94
-
95
- if (!message.key.fromMe) {
96
-
97
-
98
- msgKey.fromMe = !msgKey.fromMe
99
- ? (0, WABinary_1.areJidsSameUser)(msgKey.participant || msgKey.remoteJid, meId) ||
100
- (0, WABinary_1.areJidsSameUser)(msgKey.participant || msgKey.remoteJid, meLid)
101
- :
102
-
103
- false
104
-
105
-
106
- msgKey.remoteJid = message.key.remoteJid
107
-
108
- msgKey.participant = msgKey.participant || message.key.participant
109
- }
110
- }
111
- }
112
- exports.cleanMessage = cleanMessage
113
- const isRealMessage = message => {
114
-
115
-
116
-
117
- if (REAL_MSG_STUB_TYPES.has(message.messageStubType) || REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)) {
118
- return true
119
- }
120
- const normalizedContent = (0, messages_1.normalizeMessageContent)(message.message)
121
- const hasSomeContent = !!(0, messages_1.getContentType)(normalizedContent)
122
- return (
123
- !!normalizedContent &&
124
- hasSomeContent &&
125
- !normalizedContent?.protocolMessage &&
126
- !normalizedContent?.reactionMessage &&
127
- !normalizedContent?.pollUpdateMessage &&
128
- !normalizedContent?.secretEncryptedMessage
129
- )
130
- }
131
- exports.isRealMessage = isRealMessage
132
- const shouldIncrementChatUnread = message => !message.key.fromMe && !message.messageStubType
133
- exports.shouldIncrementChatUnread = shouldIncrementChatUnread
134
-
135
- const getChatId = ({ remoteJid, participant, fromMe }) => {
136
- if ((0, WABinary_1.isJidBroadcast)(remoteJid) && !(0, WABinary_1.isJidStatusBroadcast)(remoteJid) && !fromMe) {
137
- return participant
138
- }
139
- return remoteJid
140
- }
141
- exports.getChatId = getChatId
142
-
143
- function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid }) {
144
- const sign = Buffer.concat([
145
- toBinary(pollMsgId),
146
- toBinary(pollCreatorJid),
147
- toBinary(voterJid),
148
- toBinary('Poll Vote'),
149
- new Uint8Array([1])
150
- ])
151
- const key0 = (0, crypto_1.hmacSign)(pollEncKey, new Uint8Array(32), 'sha256')
152
- const decKey = (0, crypto_1.hmacSign)(sign, key0, 'sha256')
153
- const aad = toBinary(`${pollMsgId}\u0000${voterJid}`)
154
- const decrypted = (0, crypto_1.aesDecryptGCM)(encPayload, decKey, encIv, aad)
155
- return index_js_1.proto.Message.PollVoteMessage.decode(decrypted)
156
- function toBinary(txt) {
157
- return Buffer.from(txt)
158
- }
159
- }
160
-
161
- function decryptEventResponse({ encPayload, encIv }, { eventCreatorJid, eventMsgId, eventEncKey, responderJid }) {
162
- const sign = Buffer.concat([
163
- toBinary(eventMsgId),
164
- toBinary(eventCreatorJid),
165
- toBinary(responderJid),
166
- toBinary('Event Response'),
167
- new Uint8Array([1])
168
- ])
169
- const key0 = (0, crypto_1.hmacSign)(eventEncKey, new Uint8Array(32), 'sha256')
170
- const decKey = (0, crypto_1.hmacSign)(sign, key0, 'sha256')
171
- const aad = toBinary(`${eventMsgId}\u0000${responderJid}`)
172
- const decrypted = (0, crypto_1.aesDecryptGCM)(encPayload, decKey, encIv, aad)
173
- return index_js_1.proto.Message.EventResponseMessage.decode(decrypted)
174
- function toBinary(txt) {
175
- return Buffer.from(txt)
176
- }
177
- }
178
-
179
- function decryptMessageEdit({ encPayload, encIv }, { origMsgId, origMsgSenderJid, editorJid, msgEncKey }) {
180
- const sign = Buffer.concat([
181
- toBinary(origMsgId),
182
- toBinary(origMsgSenderJid),
183
- toBinary(editorJid),
184
- toBinary('Message Edit'),
185
- new Uint8Array([1])
186
- ])
187
- const key0 = (0, crypto_1.hmacSign)(msgEncKey, new Uint8Array(32), 'sha256')
188
- const decKey = (0, crypto_1.hmacSign)(sign, key0, 'sha256')
189
-
190
- const decrypted = (0, crypto_1.aesDecryptGCM)(encPayload, decKey, encIv, new Uint8Array(0))
191
- return index_js_1.proto.Message.decode(decrypted)
192
- function toBinary(txt) {
193
- return Buffer.from(txt)
194
- }
195
- }
196
- exports.decryptMessageEdit = decryptMessageEdit
197
-
198
- const unwrapSecretEncryptedMessage = async (message, { creds, getMessage, logger }) => {
199
- const content = (0, messages_1.normalizeMessageContent)(message.message)
200
- const secretEnc = content?.secretEncryptedMessage
201
- if (
202
- !secretEnc?.encPayload ||
203
- !secretEnc.encIv ||
204
- secretEnc.secretEncType !== index_js_1.proto.Message.SecretEncryptedMessage.SecretEncType.MESSAGE_EDIT
205
- ) {
206
- return
207
- }
208
- const targetKey = secretEnc.targetMessageKey
209
- if (!targetKey?.id) {
210
- return
211
- }
212
- try {
213
-
214
-
215
- const origMsg = await getMessage({
216
- ...targetKey,
217
- remoteJid: message.key.remoteJid,
218
- fromMe: message.key.fromMe,
219
- participant: message.key.participant
220
- })
221
- let msgEncKey = origMsg?.messageContextInfo?.messageSecret
222
- if (typeof msgEncKey === 'string') {
223
-
224
- msgEncKey = Buffer.from(msgEncKey, 'base64')
225
- }
226
- if (!msgEncKey?.length) {
227
- logger?.warn({ targetKey }, 'message edit: missing messageSecret for decryption')
228
- return
229
- }
230
-
231
-
232
- const meIdNormalised = (0, WABinary_1.jidNormalizedUser)(creds.me.id)
233
- const candidates = [
234
- ...(message.key.fromMe
235
- ? [meIdNormalised, creds.me?.lid]
236
- : [message.key.participant || message.key.remoteJid, message.key.participantAlt || message.key.remoteJidAlt]),
237
- targetKey.remoteJid
238
- ]
239
- .filter(jid => !!jid)
240
- .map(jid => (0, WABinary_1.jidNormalizedUser)(jid))
241
- .filter((jid, i, arr) => arr.indexOf(jid) === i)
242
- let decoded
243
- let lastError
244
- for (const authorJid of candidates) {
245
- try {
246
- decoded = decryptMessageEdit(secretEnc, {
247
- origMsgId: targetKey.id,
248
- origMsgSenderJid: authorJid,
249
- editorJid: authorJid,
250
- msgEncKey
251
- })
252
- break
253
- } catch (err) {
254
- lastError = err
255
- }
256
- }
257
- if (!decoded) {
258
- throw lastError
259
- }
260
- if (!decoded.protocolMessage) {
261
-
262
- decoded = index_js_1.proto.Message.fromObject({
263
- protocolMessage: {
264
- key: targetKey,
265
- type: index_js_1.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT,
266
- editedMessage: decoded
267
- }
268
- })
269
- }
270
- if (!decoded.messageContextInfo && content?.messageContextInfo) {
271
- decoded.messageContextInfo = content.messageContextInfo
272
- }
273
- message.message = decoded
274
- logger?.debug({ targetKey }, 'decrypted secretEncryptedMessage edit')
275
- } catch (err) {
276
- logger?.warn({ err, targetKey }, 'failed to decrypt secretEncryptedMessage edit')
277
- }
278
- }
279
- exports.unwrapSecretEncryptedMessage = unwrapSecretEncryptedMessage
280
- const processMessage = async (
281
- message,
282
- {
283
- shouldProcessHistoryMsg,
284
- placeholderResendCache,
285
- ev,
286
- creds,
287
- signalRepository,
288
- keyStore,
289
- logger,
290
- options,
291
- getMessage
292
- }
293
- ) => {
294
- const meId = creds.me.id
295
- const { accountSettings } = creds
296
- const chat = { id: (0, WABinary_1.jidNormalizedUser)((0, exports.getChatId)(message.key)) }
297
- const isRealMsg = (0, exports.isRealMessage)(message)
298
- if (isRealMsg) {
299
- chat.messages = [{ message }]
300
- chat.conversationTimestamp = (0, generics_1.toNumber)(message.messageTimestamp)
301
-
302
- if ((0, exports.shouldIncrementChatUnread)(message)) {
303
- chat.unreadCount = (chat.unreadCount || 0) + 1
304
- }
305
- }
306
- if (message.statusPsa) {
307
- ev.emit('status.psa', {
308
- key: message.key,
309
- psa: message.statusPsa,
310
- chatId: chat.id
311
- })
312
- }
313
- if (message.quarantinedMessage) {
314
- ev.emit('message.quarantined', {
315
- key: message.key,
316
- quarantineInfo: message.quarantinedMessage,
317
- chatId: chat.id
318
- })
319
- }
320
- if (message.interactiveMessageAdditionalMetadata?.isGalaxyFlowCompleted) {
321
- ev.emit('galaxy.flow.completed', {
322
- key: message.key,
323
- chatId: chat.id
324
- })
325
- }
326
- if (message.ephemeralExpirationTimestamp) {
327
- Object.assign(chat, {
328
- ephemeralExpirationTimestamp: (0, generics_1.toNumber)(message.ephemeralExpirationTimestamp)
329
- })
330
- }
331
- const content = (0, messages_1.normalizeMessageContent)(message.message)
332
-
333
-
334
- if ((isRealMsg || content?.reactionMessage?.key?.fromMe) && accountSettings?.unarchiveChats) {
335
- chat.archived = false
336
- chat.readOnly = false
337
- }
338
- const protocolMsg = content?.protocolMessage
339
- if (protocolMsg) {
340
- switch (protocolMsg.type) {
341
- case index_js_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
342
- const histNotification = protocolMsg.historySyncNotification
343
- const process = shouldProcessHistoryMsg
344
- const isLatest = !creds.processedHistoryMessages?.length
345
- logger?.info(
346
- {
347
- histNotification,
348
- process,
349
- id: message.key.id,
350
- isLatest
351
- },
352
- 'got history notification'
353
- )
354
- if (process) {
355
-
356
- if (histNotification.syncType !== index_js_1.proto.HistorySync.HistorySyncType.ON_DEMAND) {
357
- ev.emit('creds.update', {
358
- processedHistoryMessages: [
359
- ...(creds.processedHistoryMessages || []),
360
- { key: message.key, messageTimestamp: message.messageTimestamp }
361
- ]
362
- })
363
- }
364
- const data = await (0, history_1.downloadAndProcessHistorySyncNotification)(histNotification, options, logger)
365
- if (data.lidPnMappings?.length) {
366
- logger?.debug({ count: data.lidPnMappings.length }, 'processing LID-PN mappings from history sync')
367
- await signalRepository.lidMapping
368
- .storeLIDPNMappings(data.lidPnMappings)
369
- .catch(err => logger?.warn({ err }, 'failed to store LID-PN mappings from history sync'))
370
- }
371
- await storeTcTokensFromHistorySync(data.chats, signalRepository, keyStore, logger)
372
- ev.emit('messaging-history.set', {
373
- ...data,
374
- isLatest:
375
- histNotification.syncType !== index_js_1.proto.HistorySync.HistorySyncType.ON_DEMAND
376
- ? isLatest
377
- : undefined,
378
- peerDataRequestSessionId: histNotification.peerDataRequestSessionId
379
- })
380
- }
381
- break
382
- case index_js_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
383
- const keys = protocolMsg.appStateSyncKeyShare.keys
384
- if (keys?.length) {
385
- let newAppStateSyncKeyId = ''
386
- await keyStore.transaction(async () => {
387
- const newKeys = []
388
- for (const { keyData, keyId } of keys) {
389
- const strKeyId = Buffer.from(keyId.keyId).toString('base64')
390
- newKeys.push(strKeyId)
391
- await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } })
392
- newAppStateSyncKeyId = strKeyId
393
- }
394
- logger?.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys')
395
- }, meId)
396
- ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId })
397
- } else {
398
- logger?.info({ protocolMsg }, 'recv app state sync with 0 keys')
399
- }
400
- break
401
- case index_js_1.proto.Message.ProtocolMessage.Type.REVOKE:
402
- ev.emit('messages.update', [
403
- {
404
- key: {
405
- ...message.key,
406
- id: protocolMsg.key.id
407
- },
408
- update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }
409
- }
410
- ])
411
- break
412
- case index_js_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
413
- Object.assign(chat, {
414
- ephemeralSettingTimestamp: (0, generics_1.toNumber)(message.messageTimestamp),
415
- ephemeralExpiration: protocolMsg.ephemeralExpiration || null
416
- })
417
- break
418
- case index_js_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
419
- const response = protocolMsg.peerDataOperationRequestResponseMessage
420
- if (response) {
421
-
422
- const peerDataOperationResult = response.peerDataOperationResult || []
423
- for (const result of peerDataOperationResult) {
424
- const retryResponse = result?.placeholderMessageResendResponse
425
-
426
- if (!retryResponse?.webMessageInfoBytes) {
427
- continue
428
- }
429
-
430
- try {
431
- const webMessageInfo = index_js_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes)
432
- const msgId = webMessageInfo.key?.id
433
-
434
-
435
- const cachedData = msgId ? await placeholderResendCache?.get(msgId) : undefined
436
-
437
- if (msgId) {
438
- await placeholderResendCache?.del(msgId)
439
- }
440
- let finalMsg
441
-
442
- if (cachedData && typeof cachedData === 'object') {
443
-
444
- cachedData.message = webMessageInfo.message
445
-
446
- if (webMessageInfo.messageTimestamp) {
447
- cachedData.messageTimestamp = webMessageInfo.messageTimestamp
448
- }
449
- finalMsg = cachedData
450
- } else {
451
- finalMsg = webMessageInfo
452
- }
453
- logger?.debug({ msgId, requestId: response.stanzaId }, 'received placeholder resend')
454
- ev.emit('messages.upsert', {
455
- messages: [finalMsg],
456
- type: 'notify',
457
- requestId: response.stanzaId
458
- })
459
- } catch (err) {
460
- logger?.warn({ err, stanzaId: response.stanzaId }, 'failed to decode placeholder resend response')
461
- }
462
- }
463
- }
464
- break
465
- case index_js_1.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
466
- ev.emit('messages.update', [
467
- {
468
-
469
- key: { ...message.key, id: protocolMsg.key?.id },
470
- update: {
471
- message: {
472
- editedMessage: {
473
- message: protocolMsg.editedMessage
474
- }
475
- },
476
- messageTimestamp: protocolMsg.timestampMs
477
- ? Math.floor((0, generics_1.toNumber)(protocolMsg.timestampMs) / 1000)
478
- : message.messageTimestamp
479
- }
480
- }
481
- ])
482
- break
483
- case index_js_1.proto.Message.ProtocolMessage.Type.GROUP_MEMBER_LABEL_CHANGE:
484
- const labelAssociationMsg = protocolMsg.memberLabel
485
- if (labelAssociationMsg) {
486
-
487
-
488
- ev.emit('group.member-tag.update', {
489
- groupId: chat.id,
490
- label: labelAssociationMsg.label || '',
491
- participant: message.key.participant,
492
- participantAlt: message.key.participantAlt,
493
- messageTimestamp: Number(message.messageTimestamp)
494
- })
495
- }
496
- break
497
- case index_js_1.proto.Message.ProtocolMessage.Type.BOT_FEEDBACK_MESSAGE:
498
- if (protocolMsg.botFeedbackMessage) {
499
- ev.emit('bot.feedback', {
500
- key: message.key,
501
- botFeedback: protocolMsg.botFeedbackMessage,
502
- targetKey: protocolMsg.key
503
- })
504
- }
505
- break
506
- case index_js_1.proto.Message.ProtocolMessage.Type.CLOUD_API_THREAD_CONTROL_NOTIFICATION:
507
- if (protocolMsg.cloudAPIThreadControlNotification) {
508
- ev.emit('cloud.thread.control', {
509
- key: message.key,
510
- notification: protocolMsg.cloudAPIThreadControlNotification,
511
- chatId: chat.id
512
- })
513
- }
514
- break
515
- case index_js_1.proto.Message.ProtocolMessage.Type.CHAT_THEME_SETTING:
516
- if (protocolMsg.chatThemeSetting) {
517
- ev.emit('chats.update', [
518
- {
519
- id: chat.id,
520
- chatThemeSetting: protocolMsg.chatThemeSetting
521
- }
522
- ])
523
- }
524
- break
525
- case index_js_1.proto.Message.ProtocolMessage.Type.STOP_GENERATION_MESSAGE:
526
- ev.emit('bot.stop-generation', {
527
- key: message.key,
528
- targetKey: protocolMsg.key,
529
- chatId: chat.id
530
- })
531
- break
532
- case index_js_1.proto.Message.ProtocolMessage.Type.MEDIA_NOTIFY_MESSAGE:
533
- if (protocolMsg.mediaNotifyMessage) {
534
- ev.emit('media.notify', {
535
- key: message.key,
536
- mediaNotify: protocolMsg.mediaNotifyMessage
537
- })
538
- }
539
- break
540
- case index_js_1.proto.Message.ProtocolMessage.Type.REQUEST_WELCOME_MESSAGE:
541
- ev.emit('bot.welcome-request', {
542
- key: message.key,
543
- chatId: chat.id,
544
- timestamp: message.messageTimestamp
545
- })
546
- break
547
- case index_js_1.proto.Message.ProtocolMessage.Type.BOT_MEMU_ONBOARDING_MESSAGE:
548
- ev.emit('bot.memu-onboarding', {
549
- key: message.key,
550
- chatId: chat.id
551
- })
552
- break
553
- case index_js_1.proto.Message.ProtocolMessage.Type.STATUS_MENTION_MESSAGE:
554
- if (protocolMsg.statusMentionMessage) {
555
- ev.emit('status.mention', {
556
- key: message.key,
557
- statusMention: protocolMsg.statusMentionMessage,
558
- chatId: chat.id
559
- })
560
- }
561
- break
562
- case index_js_1.proto.Message.ProtocolMessage.Type.AI_PSI_METADATA:
563
- if (protocolMsg.aiPsiMetadata) {
564
- ev.emit('bot.psi-metadata', {
565
- key: message.key,
566
- psiMetadata: protocolMsg.aiPsiMetadata,
567
- chatId: chat.id
568
- })
569
- }
570
- break
571
- case index_js_1.proto.Message.ProtocolMessage.Type.AI_QUERY_FANOUT:
572
- if (protocolMsg.aiQueryFanout) {
573
- ev.emit('bot.query-fanout', {
574
- key: message.key,
575
- queryFanout: protocolMsg.aiQueryFanout,
576
- chatId: chat.id
577
- })
578
- }
579
- break
580
- case index_js_1.proto.Message.ProtocolMessage.Type.AI_MEDIA_COLLECTION_MESSAGE:
581
- if (protocolMsg.aiMediaCollectionMessage) {
582
- ev.emit('bot.media-collection', {
583
- key: message.key,
584
- collection: protocolMsg.aiMediaCollectionMessage,
585
- chatId: chat.id
586
- })
587
- }
588
- break
589
- case index_js_1.proto.Message.ProtocolMessage.Type.REMINDER_MESSAGE:
590
- ev.emit('reminder.update', {
591
- key: message.key,
592
- chatId: chat.id,
593
- timestamp: message.messageTimestamp
594
- })
595
- break
596
- case index_js_1.proto.Message.ProtocolMessage.Type.MESSAGE_UNSCHEDULE:
597
- if (protocolMsg.key) {
598
- ev.emit('messages.update', [
599
- {
600
- key: { ...message.key, id: protocolMsg.key.id },
601
- update: { scheduledMessageMetadata: null }
602
- }
603
- ])
604
- }
605
- break
606
- case index_js_1.proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
607
- const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload
608
- const { pnToLidMappings, chatDbMigrationTimestamp } =
609
- index_js_1.proto.LIDMigrationMappingSyncPayload.decode(encodedPayload)
610
- logger?.debug({ pnToLidMappings, chatDbMigrationTimestamp }, 'got lid mappings and chat db migration timestamp')
611
- const pairs = []
612
- for (const { pn, latestLid, assignedLid } of pnToLidMappings) {
613
- const lid = latestLid || assignedLid
614
- pairs.push({ lid: `${lid}@lid`, pn: `${pn}@s.whatsapp.net` })
615
- }
616
- await signalRepository.lidMapping.storeLIDPNMappings(pairs)
617
- if (pairs.length) {
618
- for (const { pn, lid } of pairs) {
619
- await signalRepository.migrateSession(pn, lid)
620
- }
621
- }
622
- }
623
- } else if (content?.reactionMessage) {
624
- const reaction = {
625
- ...content.reactionMessage,
626
- key: message.key
627
- }
628
- ev.emit('messages.reaction', [
629
- {
630
- reaction,
631
- key: content.reactionMessage?.key
632
- }
633
- ])
634
- } else if (content?.keepInChatMessage) {
635
- const kic = content.keepInChatMessage
636
- if (kic.key) {
637
- ev.emit('messages.update', [
638
- {
639
- key: kic.key,
640
- update: { keepInChat: { keepType: kic.keepType, serverTimestamp: message.messageTimestamp, key: kic.key } }
641
- }
642
- ])
643
- }
644
- } else if (content?.encReactionMessage) {
645
- ev.emit('messages.update', [
646
- {
647
- key: content.encReactionMessage.targetMessageKey,
648
- update: { encReactionMessage: content.encReactionMessage }
649
- }
650
- ])
651
- } else if (content?.commentMessage) {
652
- if (content.commentMessage.targetMessageKey) {
653
- ev.emit('message.comment', {
654
- comment: content.commentMessage,
655
- commentKey: message.key,
656
- targetKey: content.commentMessage.targetMessageKey
657
- })
658
- }
659
- } else if (content?.encCommentMessage) {
660
- ev.emit('message.comment', {
661
- comment: content.encCommentMessage,
662
- commentKey: message.key,
663
- targetKey: content.encCommentMessage.targetMessageKey,
664
- encrypted: true
665
- })
666
- } else if (content?.bcallMessage) {
667
- ev.emit('call', [
668
- {
669
- id: content.bcallMessage.sessionId || message.key.id,
670
- from: message.key.participant || message.key.remoteJid,
671
- chatId: chat.id,
672
- isVideo: content.bcallMessage.mediaType === index_js_1.proto.Message.BCallMessage.MediaType.VIDEO,
673
- isBroadcast: true,
674
- caption: content.bcallMessage.caption
675
- }
676
- ])
677
- } else if (content?.pollAddOptionMessage) {
678
- const pao = content.pollAddOptionMessage
679
- if (pao.pollCreationMessageKey) {
680
- ev.emit('poll.add-option', {
681
- pollKey: pao.pollCreationMessageKey,
682
- addedOption: pao.addOption,
683
- addedBy: message.key.participant || message.key.remoteJid,
684
- messageKey: message.key
685
- })
686
- }
687
- } else if (content?.scheduledCallCreationMessage) {
688
- ev.emit('call.scheduled', {
689
- messageKey: message.key,
690
- scheduledCall: content.scheduledCallCreationMessage,
691
- chatId: chat.id
692
- })
693
- } else if (content?.scheduledCallEditMessage) {
694
- ev.emit('call.schedule-cancelled', {
695
- messageKey: message.key,
696
- editedCallKey: content.scheduledCallEditMessage.key,
697
- chatId: chat.id
698
- })
699
- } else if (content?.splitPaymentMessage) {
700
- ev.emit('payment.split', {
701
- messageKey: message.key,
702
- splitPayment: content.splitPaymentMessage,
703
- chatId: chat.id
704
- })
705
- } else if (content?.p2PPaymentReminderNotification) {
706
- ev.emit('payment.reminder', {
707
- messageKey: message.key,
708
- reminder: content.p2PPaymentReminderNotification,
709
- chatId: chat.id
710
- })
711
- } else if (content?.encEventResponseMessage) {
712
- const encEventResponse = content.encEventResponseMessage
713
- const creationMsgKey = encEventResponse.eventCreationMessageKey
714
-
715
- const eventMsg = await getMessage(creationMsgKey)
716
- if (eventMsg) {
717
- try {
718
- const meIdNormalised = (0, WABinary_1.jidNormalizedUser)(meId)
719
-
720
- const eventCreatorKey = creationMsgKey.participant || creationMsgKey.remoteJid
721
- const eventCreatorPn = (0, WABinary_1.isLidUser)(eventCreatorKey)
722
- ? await signalRepository.lidMapping.getPNForLID(eventCreatorKey)
723
- : eventCreatorKey
724
- const eventCreatorJid = (0, generics_1.getKeyAuthor)(
725
- { remoteJid: (0, WABinary_1.jidNormalizedUser)(eventCreatorPn), fromMe: meIdNormalised === eventCreatorPn },
726
- meIdNormalised
727
- )
728
- const responderJid = (0, generics_1.getKeyAuthor)(message.key, meIdNormalised)
729
- const eventEncKey = eventMsg?.messageContextInfo?.messageSecret
730
- if (!eventEncKey) {
731
- logger?.warn({ creationMsgKey }, 'event response: missing messageSecret for decryption')
732
- } else {
733
- const responseMsg = decryptEventResponse(encEventResponse, {
734
- eventEncKey,
735
- eventCreatorJid,
736
- eventMsgId: creationMsgKey.id,
737
- responderJid
738
- })
739
- const eventResponse = {
740
- eventResponseMessageKey: message.key,
741
- senderTimestampMs: responseMsg.timestampMs,
742
- response: responseMsg
743
- }
744
- ev.emit('messages.update', [
745
- {
746
- key: creationMsgKey,
747
- update: {
748
- eventResponses: [eventResponse]
749
- }
750
- }
751
- ])
752
- }
753
- } catch (err) {
754
- logger?.warn({ err, creationMsgKey }, 'failed to decrypt event response')
755
- }
756
- } else {
757
- logger?.warn({ creationMsgKey }, 'event creation message not found, cannot decrypt response')
758
- }
759
- } else if (message.messageStubType) {
760
- const jid = message.key?.remoteJid
761
-
762
- let participants
763
- const emitParticipantsUpdate = action =>
764
- ev.emit('group-participants.update', {
765
- id: jid,
766
- author: message.key.participant,
767
- authorPn: message.key.participantAlt,
768
- authorUsername: message.key.participantUsername,
769
- participants,
770
- action
771
- })
772
- const emitGroupUpdate = update => {
773
- ev.emit('groups.update', [
774
- {
775
- id: jid,
776
- ...update,
777
- author: message.key.participant,
778
- authorPn: message.key.participantAlt,
779
- authorUsername: message.key.participantUsername
780
- }
781
- ])
782
- }
783
- const emitGroupRequestJoin = (participant, action, method) => {
784
- ev.emit('group.join-request', {
785
- id: jid,
786
- author: message.key.participant,
787
- authorPn: message.key.participantAlt,
788
- authorUsername: message.key.participantUsername,
789
- participant: participant.lid,
790
- participantPn: participant.pn,
791
- action,
792
- method: method
793
- })
794
- }
795
- const participantsIncludesMe = () =>
796
- participants.find(jid => (0, WABinary_1.areJidsSameUser)(meId, jid.phoneNumber))
797
- switch (message.messageStubType) {
798
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
799
- participants = message.messageStubParameters.map(a => JSON.parse(a)) || []
800
- emitParticipantsUpdate('modify')
801
- break
802
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
803
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
804
- participants = message.messageStubParameters.map(a => JSON.parse(a)) || []
805
- emitParticipantsUpdate('remove')
806
-
807
- if (participantsIncludesMe()) {
808
- chat.readOnly = true
809
- }
810
- break
811
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:
812
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
813
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
814
- participants = message.messageStubParameters.map(a => JSON.parse(a)) || []
815
- if (participantsIncludesMe()) {
816
- chat.readOnly = false
817
- }
818
- emitParticipantsUpdate('add')
819
- break
820
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
821
- participants = message.messageStubParameters.map(a => JSON.parse(a)) || []
822
- emitParticipantsUpdate('demote')
823
- break
824
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
825
- participants = message.messageStubParameters.map(a => JSON.parse(a)) || []
826
- emitParticipantsUpdate('promote')
827
- break
828
- case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
829
- const announceValue = message.messageStubParameters?.[0]
830
- emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' })
831
- break
832
- case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:
833
- const restrictValue = message.messageStubParameters?.[0]
834
- emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' })
835
- break
836
- case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:
837
- const name = message.messageStubParameters?.[0]
838
- chat.name = name
839
- emitGroupUpdate({ subject: name })
840
- break
841
- case Types_1.WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
842
- const description = message.messageStubParameters?.[0]
843
- chat.description = description
844
- emitGroupUpdate({ desc: description })
845
- break
846
- case Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
847
- const code = message.messageStubParameters?.[0]
848
- emitGroupUpdate({ inviteCode: code })
849
- break
850
- case Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
851
- const memberAddValue = message.messageStubParameters?.[0]
852
- emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' })
853
- break
854
- case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
855
- const approvalMode = message.messageStubParameters?.[0]
856
- emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' })
857
- break
858
- case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
859
- const participant = JSON.parse(message.messageStubParameters?.[0])
860
- const action = message.messageStubParameters?.[1]
861
- const method = message.messageStubParameters?.[2]
862
- emitGroupRequestJoin(participant, action, method)
863
- break
864
- case Types_1.WAMessageStubType.GROUP_CREATE:
865
- emitGroupUpdate({ subject: message.messageStubParameters?.[0] })
866
- break
867
- case Types_1.WAMessageStubType.GROUP_CHANGE_ICON:
868
- emitGroupUpdate({ pictureId: message.messageStubParameters?.[0] })
869
- break
870
- case Types_1.WAMessageStubType.GROUP_DELETE:
871
- emitGroupUpdate({ readOnly: true, deleted: true })
872
- break
873
- case Types_1.WAMessageStubType.GROUP_CREATING:
874
- emitGroupUpdate({ subject: message.messageStubParameters?.[0], creating: true })
875
- break
876
- case Types_1.WAMessageStubType.GROUP_CREATE_FAILED:
877
- emitGroupUpdate({ createFailed: true })
878
- break
879
- case Types_1.WAMessageStubType.GROUP_BOUNCED:
880
- emitGroupUpdate({ bounced: true })
881
- break
882
- case Types_1.WAMessageStubType.CHANGE_EPHEMERAL_SETTING:
883
- const ephValue = message.messageStubParameters?.[0]
884
- emitGroupUpdate({ ephemeralDuration: ephValue ? +ephValue : 0 })
885
- break
886
- case Types_1.WAMessageStubType.DISAPPEARING_MODE:
887
- const disappearingValue = message.messageStubParameters?.[0]
888
- emitGroupUpdate({ ephemeralDuration: disappearingValue ? +disappearingValue : 0 })
889
- break
890
- case Types_1.WAMessageStubType.ADMIN_REVOKE:
891
- emitGroupUpdate({ adminRevoked: true, revokedBy: message.messageStubParameters?.[0] })
892
- break
893
- case Types_1.WAMessageStubType.BLOCK_CONTACT:
894
- ev.emit('contacts.update', [{ id: jid, isBlocked: true }])
895
- break
896
- case Types_1.WAMessageStubType.CHANGE_USERNAME:
897
- ev.emit('contacts.update', [
898
- { id: message.key.participant || jid, username: message.messageStubParameters?.[0] }
899
- ])
900
- break
901
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ACCEPT:
902
- participants =
903
- message.messageStubParameters?.map(a => {
904
- try {
905
- return JSON.parse(a)
906
- } catch {
907
- return a
908
- }
909
- }) || []
910
- emitParticipantsUpdate('accept')
911
- break
912
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LINKED_GROUP_JOIN:
913
- participants =
914
- message.messageStubParameters?.map(a => {
915
- try {
916
- return JSON.parse(a)
917
- } catch {
918
- return a
919
- }
920
- }) || []
921
- emitParticipantsUpdate('linked-group-join')
922
- break
923
- case Types_1.WAMessageStubType.EPHEMERAL_KEEP_IN_CHAT:
924
- ev.emit('messages.update', [{ key: message.key, update: { keepInChat: true } }])
925
- break
926
- case Types_1.WAMessageStubType.PINNED_MESSAGE_IN_CHAT:
927
- ev.emit('messages.update', [
928
- {
929
- key: message.key,
930
- update: {
931
- pinInChat: {
932
- pinned: true,
933
- pinnedMessageKey: message.messageStubParameters?.[0]
934
- ? { id: message.messageStubParameters[0] }
935
- : undefined,
936
- senderTimestampMs: message.messageTimestamp
937
- ? (0, generics_1.toNumber)(message.messageTimestamp) * 1000
938
- : undefined
939
- }
940
- }
941
- }
942
- ])
943
- break
944
- case Types_1.WAMessageStubType.CHAT_PSA:
945
- ev.emit('chats.update', [{ id: jid, psa: { urlParams: message.messageStubParameters } }])
946
- break
947
- case Types_1.WAMessageStubType.CHAT_POLL_CREATION_MESSAGE:
948
-
949
- break
950
- case Types_1.WAMessageStubType.BIZ_CHAT_ASSIGNMENT:
951
- ev.emit('chats.update', [
952
- {
953
- id: jid,
954
- bizAssignment: {
955
- assigned: true,
956
- assignee: message.messageStubParameters?.[0],
957
- agent: message.messageStubParameters?.[1]
958
- }
959
- }
960
- ])
961
- break
962
- case Types_1.WAMessageStubType.BIZ_CHAT_ASSIGNMENT_UNASSIGN:
963
- ev.emit('chats.update', [{ id: jid, bizAssignment: { assigned: false } }])
964
- break
965
- case Types_1.WAMessageStubType.SILENCED_UNKNOWN_CALLER_AUDIO:
966
- case Types_1.WAMessageStubType.SILENCED_UNKNOWN_CALLER_VIDEO:
967
- ev.emit('call', [
968
- {
969
- id: message.messageStubParameters?.[0] || message.key.id,
970
- from: jid,
971
- isVideo: message.messageStubType === Types_1.WAMessageStubType.SILENCED_UNKNOWN_CALLER_VIDEO,
972
- status: 'silenced',
973
- isUnknown: true,
974
- timestamp: (0, generics_1.toNumber)(message.messageTimestamp)
975
- }
976
- ])
977
- break
978
- case Types_1.WAMessageStubType.SCHEDULED_CALL_START_MESSAGE:
979
- ev.emit('call', [
980
- {
981
- id: message.messageStubParameters?.[0] || message.key.id,
982
- from: message.key.participant || jid,
983
- status: 'scheduled-start',
984
- timestamp: (0, generics_1.toNumber)(message.messageTimestamp)
985
- }
986
- ])
987
- break
988
- case Types_1.WAMessageStubType.SCHEDULED_CALL_CANCEL:
989
- ev.emit('call', [
990
- {
991
- id: message.messageStubParameters?.[0] || message.key.id,
992
- from: message.key.participant || jid,
993
- status: 'schedule-cancelled',
994
- timestamp: (0, generics_1.toNumber)(message.messageTimestamp)
995
- }
996
- ])
997
- break
998
- case Types_1.WAMessageStubType.COMMUNITY_CREATE:
999
- emitGroupUpdate({ isCommunity: true, subject: message.messageStubParameters?.[0] })
1000
- break
1001
- case Types_1.WAMessageStubType.COMMUNITY_LINK_PARENT_GROUP:
1002
- case Types_1.WAMessageStubType.COMMUNITY_LINK_PARENT_GROUP_RICH:
1003
- emitGroupUpdate({ linkedParent: message.messageStubParameters?.[0] })
1004
- break
1005
- case Types_1.WAMessageStubType.COMMUNITY_LINK_SUB_GROUP:
1006
- case Types_1.WAMessageStubType.COMMUNITY_LINK_SIBLING_GROUP:
1007
- emitGroupUpdate({ linkedSubGroup: message.messageStubParameters?.[0] })
1008
- break
1009
- case Types_1.WAMessageStubType.COMMUNITY_UNLINK_PARENT_GROUP:
1010
- emitGroupUpdate({ linkedParent: undefined })
1011
- break
1012
- case Types_1.WAMessageStubType.COMMUNITY_UNLINK_SUB_GROUP:
1013
- case Types_1.WAMessageStubType.COMMUNITY_UNLINK_SIBLING_GROUP:
1014
- emitGroupUpdate({ unlinkedSubGroup: message.messageStubParameters?.[0] })
1015
- break
1016
- case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_PROMOTE:
1017
- participants =
1018
- message.messageStubParameters?.map(a => {
1019
- try {
1020
- return JSON.parse(a)
1021
- } catch {
1022
- return a
1023
- }
1024
- }) || []
1025
- emitParticipantsUpdate('promote')
1026
- break
1027
- case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_DEMOTE:
1028
- participants =
1029
- message.messageStubParameters?.map(a => {
1030
- try {
1031
- return JSON.parse(a)
1032
- } catch {
1033
- return a
1034
- }
1035
- }) || []
1036
- emitParticipantsUpdate('demote')
1037
- break
1038
- case Types_1.WAMessageStubType.COMMUNITY_PARENT_GROUP_DELETED:
1039
- emitGroupUpdate({ parentDeleted: true, readOnly: true })
1040
- break
1041
- case Types_1.WAMessageStubType.COMMUNITY_PARENT_GROUP_SUBJECT_CHANGED:
1042
- emitGroupUpdate({ parentSubject: message.messageStubParameters?.[0] })
1043
- break
1044
- case Types_1.WAMessageStubType.COMMUNITY_LINK_PARENT_GROUP_MEMBERSHIP_APPROVAL:
1045
- emitGroupUpdate({ linkedParentMembershipApproval: message.messageStubParameters?.[0] === 'true' })
1046
- break
1047
- case Types_1.WAMessageStubType.COMMUNITY_INVITE_RICH:
1048
- case Types_1.WAMessageStubType.COMMUNITY_INVITE_AUTO_ADD_RICH:
1049
- participants =
1050
- message.messageStubParameters?.map(a => {
1051
- try {
1052
- return JSON.parse(a)
1053
- } catch {
1054
- return a
1055
- }
1056
- }) || []
1057
- emitParticipantsUpdate('add')
1058
- break
1059
- case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_ADD_RICH:
1060
- participants =
1061
- message.messageStubParameters?.map(a => {
1062
- try {
1063
- return JSON.parse(a)
1064
- } catch {
1065
- return a
1066
- }
1067
- }) || []
1068
- emitParticipantsUpdate('add')
1069
- break
1070
- case Types_1.WAMessageStubType.COMMUNITY_CHANGE_DESCRIPTION:
1071
- const communityDesc = message.messageStubParameters?.[0]
1072
- emitGroupUpdate({ desc: communityDesc })
1073
- break
1074
- case Types_1.WAMessageStubType.COMMUNITY_ALLOW_MEMBER_ADDED_GROUPS:
1075
- emitGroupUpdate({ communityMemberAddGroupMode: message.messageStubParameters?.[0] })
1076
- break
1077
- case Types_1.WAMessageStubType.EMPTY_SUBGROUP_CREATE:
1078
- emitGroupUpdate({ subject: message.messageStubParameters?.[0], isEmpty: true })
1079
- break
1080
- case Types_1.WAMessageStubType.COMMUNITY_DEACTIVATE_SIBLING_GROUP:
1081
- emitGroupUpdate({ deactivated: true, linkedSibling: message.messageStubParameters?.[0] })
1082
- break
1083
- case Types_1.WAMessageStubType.COMMUNITY_OWNER_UPDATED:
1084
- emitGroupUpdate({ owner: message.messageStubParameters?.[0] })
1085
- break
1086
- case Types_1.WAMessageStubType.COMMUNITY_SUB_GROUP_VISIBILITY_HIDDEN:
1087
- emitGroupUpdate({ hidden: true })
1088
- break
1089
- }
1090
- } else if (content?.pollUpdateMessage) {
1091
- const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey
1092
- const pollMsg = await getMessage(creationMsgKey)
1093
- if (pollMsg) {
1094
-
1095
- const inner = pollMsg.viewOnceMessage?.message || pollMsg.botInvokeMessage?.message || pollMsg
1096
- const pollEncKey = inner.messageContextInfo?.messageSecret || pollMsg.messageContextInfo?.messageSecret || null
1097
- if (!pollEncKey) {
1098
- logger?.warn({ creationMsgKey }, 'poll: messageSecret missing, cannot decrypt vote')
1099
- } else {
1100
-
1101
- const rawLid = creds.me?.lid
1102
- const meLidNorm = rawLid ? `${rawLid.split(':')[0]}@lid` : ''
1103
- const pollCreatorJid = meLidNorm || (0, WABinary_1.jidNormalizedUser)(meId)
1104
-
1105
- const voterJid = message.key.fromMe ? pollCreatorJid : message.key.participant || message.key.remoteJid
1106
- try {
1107
- const voteMsg = decryptPollVote(content.pollUpdateMessage.vote, {
1108
- pollEncKey,
1109
- pollCreatorJid,
1110
- pollMsgId: creationMsgKey.id,
1111
- voterJid
1112
- })
1113
- ev.emit('messages.update', [
1114
- {
1115
- key: creationMsgKey,
1116
- update: {
1117
- pollUpdates: [
1118
- {
1119
- pollUpdateMessageKey: message.key,
1120
- vote: voteMsg,
1121
- senderTimestampMs: (0, generics_1.toNumber)(content.pollUpdateMessage.senderTimestampMs)
1122
- }
1123
- ]
1124
- }
1125
- }
1126
- ])
1127
- } catch (err) {
1128
- logger?.warn({ err, creationMsgKey, pollCreatorJid, voterJid }, 'failed to decrypt poll vote')
1129
- }
1130
- }
1131
- } else {
1132
- logger?.warn({ creationMsgKey }, 'poll: creation message not found, cannot decrypt vote')
1133
- }
1134
- }
1135
- if (Object.keys(chat).length > 1) {
1136
- ev.emit('chats.update', [chat])
1137
- }
1138
- }
1139
- exports.default = processMessage