@neelegirl/baileys 1.5.2 → 1.5.3

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 (193) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +195 -187
  3. package/WAProto/WAProto.proto +537 -236
  4. package/WAProto/index.d.ts +5971 -2388
  5. package/WAProto/index.js +17298 -6513
  6. package/lib/Defaults/baileys-version.json +3 -3
  7. package/lib/Defaults/index.d.ts +77 -67
  8. package/lib/Defaults/index.js +148 -136
  9. package/lib/Defaults/phonenumber-mcc.json +223 -223
  10. package/lib/Signal/WASignalGroup/GroupProtocol.js +1908 -1908
  11. package/lib/Signal/WASignalGroup/ciphertext-message.d.ts +9 -0
  12. package/lib/Signal/WASignalGroup/ciphertext-message.js +19 -0
  13. package/lib/Signal/WASignalGroup/ciphertext_message.js +15 -15
  14. package/lib/Signal/WASignalGroup/group-session-builder.d.ts +17 -0
  15. package/lib/Signal/WASignalGroup/group-session-builder.js +72 -0
  16. package/lib/Signal/WASignalGroup/group.proto +41 -41
  17. package/lib/Signal/WASignalGroup/group_cipher.d.ts +19 -0
  18. package/lib/Signal/WASignalGroup/group_cipher.js +101 -110
  19. package/lib/Signal/WASignalGroup/group_session_builder.js +45 -45
  20. package/lib/Signal/WASignalGroup/index.d.ts +11 -0
  21. package/lib/Signal/WASignalGroup/index.js +61 -6
  22. package/lib/Signal/WASignalGroup/keyhelper.d.ts +16 -0
  23. package/lib/Signal/WASignalGroup/keyhelper.js +58 -13
  24. package/lib/Signal/WASignalGroup/protobufs.js +2 -2
  25. package/lib/Signal/WASignalGroup/queue_job.js +68 -68
  26. package/lib/Signal/WASignalGroup/readme.md +5 -5
  27. package/lib/Signal/WASignalGroup/sender-chain-key.d.ts +14 -0
  28. package/lib/Signal/WASignalGroup/sender-chain-key.js +47 -0
  29. package/lib/Signal/WASignalGroup/sender-key-distribution-message.d.ts +17 -0
  30. package/lib/Signal/WASignalGroup/sender-key-distribution-message.js +71 -0
  31. package/lib/Signal/WASignalGroup/sender-key-message.d.ts +19 -0
  32. package/lib/Signal/WASignalGroup/sender-key-message.js +73 -0
  33. package/lib/Signal/WASignalGroup/sender-key-name.d.ts +19 -0
  34. package/lib/Signal/WASignalGroup/sender-key-name.js +59 -0
  35. package/lib/Signal/WASignalGroup/sender-key-record.d.ts +32 -0
  36. package/lib/Signal/WASignalGroup/sender-key-record.js +58 -0
  37. package/lib/Signal/WASignalGroup/sender-key-state.d.ts +44 -0
  38. package/lib/Signal/WASignalGroup/sender-key-state.js +147 -0
  39. package/lib/Signal/WASignalGroup/sender-message-key.d.ts +11 -0
  40. package/lib/Signal/WASignalGroup/sender-message-key.js +33 -0
  41. package/lib/Signal/WASignalGroup/sender_chain_key.js +49 -49
  42. package/lib/Signal/WASignalGroup/sender_key_distribution_message.js +77 -77
  43. package/lib/Signal/WASignalGroup/sender_key_message.js +91 -91
  44. package/lib/Signal/WASignalGroup/sender_key_name.js +69 -69
  45. package/lib/Signal/WASignalGroup/sender_key_record.js +55 -55
  46. package/lib/Signal/WASignalGroup/sender_key_state.js +128 -128
  47. package/lib/Signal/WASignalGroup/sender_message_key.js +38 -38
  48. package/lib/Signal/libsignal.d.ts +5 -1
  49. package/lib/Signal/libsignal.js +390 -161
  50. package/lib/Signal/lid-mapping.d.ts +28 -0
  51. package/lib/Signal/lid-mapping.js +184 -0
  52. package/lib/Socket/Client/abstract-socket-client.d.ts +15 -15
  53. package/lib/Socket/Client/abstract-socket-client.js +13 -13
  54. package/lib/Socket/Client/index.d.ts +2 -2
  55. package/lib/Socket/Client/mobile-socket-client.d.ts +12 -12
  56. package/lib/Socket/Client/mobile-socket-client.js +65 -65
  57. package/lib/Socket/Client/types.d.ts +1 -1
  58. package/lib/Socket/Client/websocket.d.ts +1 -1
  59. package/lib/Socket/business.d.ts +6 -6
  60. package/lib/Socket/business.js +152 -5
  61. package/lib/Socket/chats.d.ts +3 -4
  62. package/lib/Socket/chats.js +31 -26
  63. package/lib/Socket/communities.d.ts +223 -223
  64. package/lib/Socket/communities.js +432 -432
  65. package/lib/Socket/groups.d.ts +2 -4
  66. package/lib/Socket/groups.js +22 -14
  67. package/lib/Socket/index.d.ts +69 -69
  68. package/lib/Socket/index.js +3 -2
  69. package/lib/Socket/messages-recv.d.ts +3 -6
  70. package/lib/Socket/messages-recv.js +1449 -1707
  71. package/lib/Socket/messages-send.d.ts +2 -4
  72. package/lib/Socket/messages-send.js +617 -126
  73. package/lib/Socket/mex.d.ts +2 -2
  74. package/lib/Socket/mex.js +46 -46
  75. package/lib/Socket/newsletter.d.ts +2 -4
  76. package/lib/Socket/newsletter.js +294 -285
  77. package/lib/Socket/socket.js +318 -132
  78. package/lib/Socket/usync.js +3 -3
  79. package/lib/Store/index.d.ts +4 -4
  80. package/lib/Store/index.js +23 -23
  81. package/lib/Store/make-cache-manager-store.d.ts +13 -13
  82. package/lib/Store/make-cache-manager-store.js +89 -89
  83. package/lib/Store/make-in-memory-store.d.ts +122 -122
  84. package/lib/Store/make-in-memory-store.js +428 -428
  85. package/lib/Store/make-ordered-dictionary.d.ts +11 -11
  86. package/lib/Store/make-ordered-dictionary.js +85 -85
  87. package/lib/Store/object-repository.d.ts +9 -9
  88. package/lib/Store/object-repository.js +30 -30
  89. package/lib/Types/Auth.d.ts +5 -4
  90. package/lib/Types/Bussines.js +3 -0
  91. package/lib/Types/Bussiness.d.ts +28 -0
  92. package/lib/Types/Chat.d.ts +13 -8
  93. package/lib/Types/Contact.d.ts +4 -1
  94. package/lib/Types/Events.d.ts +13 -16
  95. package/lib/Types/GroupMetadata.d.ts +1 -1
  96. package/lib/Types/Message.d.ts +18 -7
  97. package/lib/Types/Message.js +7 -1
  98. package/lib/Types/MexUpdates.d.ts +8 -8
  99. package/lib/Types/MexUpdates.js +17 -17
  100. package/lib/Types/Newsletter.d.ts +1 -1
  101. package/lib/Types/Product.d.ts +1 -1
  102. package/lib/Types/Signal.d.ts +31 -1
  103. package/lib/Types/Socket.d.ts +34 -13
  104. package/lib/Types/State.d.ts +1 -1
  105. package/lib/Types/USync.d.ts +2 -2
  106. package/lib/Types/index.d.ts +16 -15
  107. package/lib/Types/index.js +4 -2
  108. package/lib/Utils/auth-utils.d.ts +20 -20
  109. package/lib/Utils/auth-utils.js +527 -204
  110. package/lib/Utils/baileys-event-stream.d.ts +17 -17
  111. package/lib/Utils/baileys-event-stream.js +69 -69
  112. package/lib/Utils/business.d.ts +28 -28
  113. package/lib/Utils/business.js +254 -254
  114. package/lib/Utils/chat-utils.d.ts +81 -81
  115. package/lib/Utils/chat-utils.js +808 -780
  116. package/lib/Utils/crypto.d.ts +55 -55
  117. package/lib/Utils/crypto.js +188 -178
  118. package/lib/Utils/decode-wa-message.d.ts +52 -40
  119. package/lib/Utils/decode-wa-message.js +322 -252
  120. package/lib/Utils/event-buffer.d.ts +38 -38
  121. package/lib/Utils/event-buffer.js +594 -564
  122. package/lib/Utils/generics.d.ts +131 -129
  123. package/lib/Utils/generics.js +629 -623
  124. package/lib/Utils/history.d.ts +22 -22
  125. package/lib/Utils/history.js +103 -109
  126. package/lib/Utils/index.d.ts +20 -19
  127. package/lib/Utils/index.js +39 -38
  128. package/lib/Utils/link-preview.d.ts +22 -22
  129. package/lib/Utils/link-preview.js +119 -119
  130. package/lib/Utils/logger.d.ts +13 -13
  131. package/lib/Utils/logger.js +7 -7
  132. package/lib/Utils/lt-hash.d.ts +13 -13
  133. package/lib/Utils/lt-hash.js +57 -57
  134. package/lib/Utils/make-mutex.d.ts +8 -8
  135. package/lib/Utils/make-mutex.js +48 -48
  136. package/lib/Utils/message-retry-manager.d.ts +88 -0
  137. package/lib/Utils/message-retry-manager.js +160 -0
  138. package/lib/Utils/messages-media.d.ts +134 -128
  139. package/lib/Utils/messages-media.js +868 -805
  140. package/lib/Utils/messages.d.ts +104 -102
  141. package/lib/Utils/messages.js +1744 -1578
  142. package/lib/Utils/noise-handler.d.ts +20 -19
  143. package/lib/Utils/noise-handler.js +164 -154
  144. package/lib/Utils/process-message.d.ts +48 -48
  145. package/lib/Utils/process-message.js +427 -428
  146. package/lib/Utils/signal.d.ts +41 -41
  147. package/lib/Utils/signal.js +165 -165
  148. package/lib/Utils/use-mongo-file-auth-state.d.ts +5 -5
  149. package/lib/Utils/use-mongo-file-auth-state.js +83 -83
  150. package/lib/Utils/use-multi-file-auth-state.d.ts +17 -17
  151. package/lib/Utils/use-multi-file-auth-state.js +237 -237
  152. package/lib/Utils/use-single-file-auth-state.d.ts +12 -12
  153. package/lib/Utils/use-single-file-auth-state.js +79 -79
  154. package/lib/Utils/validate-connection.d.ts +12 -12
  155. package/lib/Utils/validate-connection.js +219 -186
  156. package/lib/WABinary/constants.d.ts +29 -29
  157. package/lib/WABinary/constants.js +1315 -1315
  158. package/lib/WABinary/decode.d.ts +8 -8
  159. package/lib/WABinary/decode.js +287 -287
  160. package/lib/WABinary/encode.d.ts +2 -2
  161. package/lib/WABinary/encode.js +264 -264
  162. package/lib/WABinary/generic-utils.d.ts +27 -27
  163. package/lib/WABinary/generic-utils.js +141 -141
  164. package/lib/WABinary/index.d.ts +5 -5
  165. package/lib/WABinary/index.js +24 -24
  166. package/lib/WABinary/jid-utils.d.ts +58 -53
  167. package/lib/WABinary/jid-utils.js +103 -91
  168. package/lib/WABinary/types.d.ts +21 -21
  169. package/lib/WABinary/types.js +2 -2
  170. package/lib/WAM/BinaryInfo.d.ts +15 -15
  171. package/lib/WAM/BinaryInfo.js +16 -16
  172. package/lib/WAM/constants.d.ts +46 -46
  173. package/lib/WAM/constants.js +15370 -15370
  174. package/lib/WAM/encode.d.ts +2 -2
  175. package/lib/WAM/encode.js +163 -164
  176. package/lib/WAM/index.d.ts +3 -3
  177. package/lib/WAM/index.js +22 -22
  178. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +27 -27
  179. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +68 -68
  180. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +3 -3
  181. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +2 -2
  182. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +2 -2
  183. package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +9 -8
  184. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +37 -29
  185. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +2 -2
  186. package/lib/WAUSync/Protocols/index.d.ts +6 -6
  187. package/lib/WAUSync/USyncQuery.d.ts +3 -3
  188. package/lib/WAUSync/index.d.ts +3 -3
  189. package/lib/index.d.ts +13 -13
  190. package/lib/index.js +33 -33
  191. package/package.json +96 -94
  192. package/lib/Socket/registration.d.ts +0 -266
  193. package/lib/Socket/registration.js +0 -166
@@ -1,428 +1,427 @@
1
- "use strict"
2
-
3
- Object.defineProperty(exports, "__esModule", { value: true })
4
-
5
- const WAProto_1 = require("../../WAProto")
6
- const Types_1 = require("../Types")
7
- const messages_1 = require("../Utils/messages")
8
- const WABinary_1 = require("../WABinary")
9
- const crypto_1 = require("./crypto")
10
- const generics_1 = require("./generics")
11
- const history_1 = require("./history")
12
- const chats_1 = require("../Socket/chats")
13
- const REAL_MSG_STUB_TYPES = new Set([
14
- Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
15
- Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE,
16
- Types_1.WAMessageStubType.CALL_MISSED_VIDEO,
17
- Types_1.WAMessageStubType.CALL_MISSED_VOICE
18
- ])
19
-
20
- const REAL_MSG_REQ_ME_STUB_TYPES = new Set([
21
- Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD
22
- ])
23
-
24
- /** Cleans a received message to further processing */
25
- const cleanMessage = (message, meId) => {
26
- // ensure remoteJid and participant doesn't have device or agent in it
27
- message.key.remoteJid = WABinary_1.jidNormalizedUser(message.key.remoteJid)
28
- message.key.participant = message.key.participant ? WABinary_1.jidNormalizedUser(message.key.participant) : undefined
29
- const content = messages_1.normalizeMessageContent(message.message)
30
- // if the message has a reaction, ensure fromMe & remoteJid are from our perspective
31
- if (content?.reactionMessage) {
32
- normaliseKey(content.reactionMessage.key)
33
- }
34
- if (content?.pollUpdateMessage) {
35
- normaliseKey(content.pollUpdateMessage.pollCreationMessageKey)
36
- }
37
- function normaliseKey(msgKey) {
38
- // if the reaction is from another user
39
- // we've to correctly map the key to this user's perspective
40
- if (!message.key.fromMe) {
41
- // if the sender believed the message being reacted to is not from them
42
- // we've to correct the key to be from them, or some other participant
43
- msgKey.fromMe = !msgKey.fromMe
44
- ? WABinary_1.areJidsSameUser(msgKey.participant || msgKey.remoteJid, meId)
45
- // if the message being reacted to, was from them
46
- // fromMe automatically becomes false
47
- : false
48
- // set the remoteJid to being the same as the chat the message came from
49
- msgKey.remoteJid = message.key.remoteJid
50
- // set participant of the message
51
- msgKey.participant = msgKey.participant || message.key.participant
52
- }
53
- }
54
- }
55
-
56
- const isRealMessage = (message, meId) => {
57
- const normalizedContent = messages_1.normalizeMessageContent(message.message)
58
- const hasSomeContent = !!messages_1.getContentType(normalizedContent)
59
- return (
60
- !!normalizedContent ||
61
- REAL_MSG_STUB_TYPES.has(message.messageStubType) ||
62
- (
63
- REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType) &&
64
- message.messageStubParameters?.some(p => WABinary_1.areJidsSameUser(meId, p))
65
- )
66
- ) &&
67
- hasSomeContent &&
68
- !normalizedContent?.protocolMessage &&
69
- !normalizedContent?.reactionMessage &&
70
- !normalizedContent?.pollUpdateMessage
71
- }
72
-
73
- const shouldIncrementChatUnread = (message) => (!message.key.fromMe && !message.messageStubType)
74
-
75
- /**
76
- * Get the ID of the chat from the given key.
77
- * Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant
78
- */
79
- const getChatId = ({ remoteJid, participant, fromMe }) => {
80
- if (WABinary_1.isJidBroadcast(remoteJid)
81
- && !WABinary_1.isJidStatusBroadcast(remoteJid)
82
- && !fromMe) {
83
- return participant
84
- }
85
- return remoteJid
86
- }
87
-
88
- /**
89
- * Decrypt a poll vote
90
- * @param vote encrypted vote
91
- * @param ctx additional info about the poll required for decryption
92
- * @returns list of SHA256 options
93
- */
94
- function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid, }) {
95
- const sign = Buffer.concat([
96
- toBinary(pollMsgId),
97
- toBinary(pollCreatorJid),
98
- toBinary(voterJid),
99
- toBinary('Poll Vote'),
100
- new Uint8Array([1])
101
- ])
102
- const key0 = crypto_1.hmacSign(pollEncKey, new Uint8Array(32), 'sha256')
103
- const decKey = crypto_1.hmacSign(sign, key0, 'sha256')
104
- const aad = toBinary(`${pollMsgId}\u0000${voterJid}`)
105
- const decrypted = crypto_1.aesDecryptGCM(encPayload, decKey, encIv, aad)
106
- return WAProto_1.proto.Message.PollVoteMessage.decode(decrypted)
107
- function toBinary(txt) {
108
- return Buffer.from(txt)
109
- }
110
- }
111
-
112
- const processMessage = async (message, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, keyStore, logger, options, getMessage }) => {
113
- const meId = creds.me.id
114
- const { accountSettings } = creds
115
- const chat = { id: WABinary_1.jidNormalizedUser(getChatId(message.key)) }
116
- const isRealMsg = isRealMessage(message, meId)
117
- if (isRealMsg) {
118
- chat.messages = [{ message }]
119
- chat.conversationTimestamp = generics_1.toNumber(message.messageTimestamp)
120
- // only increment unread count if not CIPHERTEXT and from another person
121
- if (shouldIncrementChatUnread(message)) {
122
- chat.unreadCount = (chat.unreadCount || 0) + 1
123
- }
124
- }
125
- const content = messages_1.normalizeMessageContent(message.message)
126
- // unarchive chat if it's a real message, or someone reacted to our message
127
- // and we've the unarchive chats setting on
128
- if (isRealMsg || content?.reactionMessage?.key?.fromMe
129
- && accountSettings?.unarchiveChats) {
130
- chat.archived = false
131
- chat.readOnly = false
132
- }
133
- const protocolMsg = content?.protocolMessage
134
- if (protocolMsg) {
135
- switch (protocolMsg.type) {
136
- case WAProto_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
137
- const histNotification = protocolMsg.historySyncNotification
138
- const process = shouldProcessHistoryMsg
139
- const isLatest = !creds.processedHistoryMessages?.length
140
- logger?.info({
141
- histNotification,
142
- process,
143
- id: message.key.id,
144
- isLatest,
145
- }, 'got history notification')
146
- if (process) {
147
- if (histNotification.syncType !== WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND) {
148
- ev.emit('creds.update', {
149
- processedHistoryMessages: [
150
- ...(creds.processedHistoryMessages || []),
151
- { key: message.key, messageTimestamp: message.messageTimestamp }
152
- ]
153
- })
154
- }
155
- const data = await history_1.downloadAndProcessHistorySyncNotification(histNotification, options)
156
- ev.emit('messaging-history.set', {
157
- ...data,
158
- isLatest: histNotification.syncType !== WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND
159
- ? isLatest
160
- : undefined,
161
- peerDataRequestSessionId: histNotification.peerDataRequestSessionId
162
- })
163
- }
164
- break
165
- case WAProto_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
166
- const keys = protocolMsg.appStateSyncKeyShare.keys
167
- if (keys?.length) {
168
- let newAppStateSyncKeyId = ''
169
- await keyStore.transaction(async () => {
170
- const newKeys = []
171
- for (const { keyData, keyId } of keys) {
172
- const strKeyId = Buffer.from(keyId.keyId).toString('base64')
173
- newKeys.push(strKeyId)
174
- await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } })
175
- newAppStateSyncKeyId = strKeyId
176
- }
177
- logger?.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys')
178
- })
179
- ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId })
180
- }
181
- else {
182
- logger?.info({ protocolMsg }, 'recv app state sync with 0 keys')
183
- }
184
- break
185
- case WAProto_1.proto.Message.ProtocolMessage.Type.REVOKE:
186
- ev.emit('messages.update', [
187
- {
188
- key: {
189
- ...message.key,
190
- id: protocolMsg.key.id
191
- },
192
- update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }
193
- }
194
- ])
195
- break
196
- case WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
197
- Object.assign(chat, {
198
- ephemeralSettingTimestamp: generics_1.toNumber(message.messageTimestamp),
199
- ephemeralExpiration: protocolMsg.ephemeralExpiration || null
200
- })
201
- break
202
- case WAProto_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
203
- const response = protocolMsg.peerDataOperationRequestResponseMessage
204
- if (response) {
205
- placeholderResendCache?.del(response.stanzaId)
206
- // TODO: IMPLEMENT HISTORY SYNC ETC (sticker uploads etc.).
207
- const { peerDataOperationResult } = response
208
- for (const result of peerDataOperationResult) {
209
- const { placeholderMessageResendResponse: retryResponse } = result
210
- //eslint-disable-next-line max-depth
211
- if (retryResponse) {
212
- const webMessageInfo = WAProto_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes)
213
- // wait till another upsert event is available, don't want it to be part of the PDO response message
214
- setTimeout(() => {
215
- ev.emit('messages.upsert', {
216
- messages: [webMessageInfo],
217
- type: 'notify',
218
- requestId: response.stanzaId
219
- })
220
- }, 500)
221
- }
222
- }
223
- }
224
- break
225
- case WAProto_1.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
226
- ev.emit('messages.update', [
227
- {
228
- // flip the sender / fromMe properties because they're in the perspective of the sender
229
- key: { ...message.key, id: protocolMsg.key?.id },
230
- update: {
231
- message: {
232
- editedMessage: {
233
- message: protocolMsg.editedMessage
234
- }
235
- },
236
- messageTimestamp: protocolMsg.timestampMs
237
- ? Math.floor(generics_1.toNumber(protocolMsg.timestampMs) / 1000)
238
- : message.messageTimestamp
239
- }
240
- }
241
- ])
242
- break
243
- case WAProto_1.proto.Message.ProtocolMessage.Type.LIMIT_SHARING:
244
- ev.emit('limit-sharing.update', {
245
- id: protocolMsg.key.remoteJid,
246
- author: WABinary_1.areJidsSameUser(message.key.remoteJid, protocolMsg.key.remoteJid) ? WABinary_1.jidNormalizedUser(meId) : message.key.remoteJid,
247
- action: `${protocolMsg.limitSharing.sharingLimited ? 'on' : 'off'}`,
248
- trigger: protocolMsg.limitSharing.trigger,
249
- update_time: protocolMsg.limitSharing.limitSharingSettingTimestamp
250
- })
251
- break
252
- }
253
- }
254
- else if (content?.reactionMessage) {
255
- const reaction = {
256
- ...content.reactionMessage,
257
- key: message.key,
258
- }
259
- ev.emit('messages.reaction', [{
260
- reaction,
261
- key: content.reactionMessage?.key
262
- }])
263
- }
264
- else if (message.messageStubType) {
265
- const jid = message.key?.remoteJid
266
-
267
- //let actor = whatsappID (message.participant)
268
- let participants
269
-
270
- const jid2 = message.key.participant;
271
- const emitParticipantsUpdate = (action) => (ev.emit('group-participants.update', { id: jid, author: jid2, participants, action}))
272
- const emitGroupUpdate = (update) => {
273
- ev.emit('groups.update', [{ id: jid, ...update, author: message.participant ? message.participant : undefined }])
274
- }
275
- const emitCommunityUpdate = (update) => {
276
- ev.emit('communities.update', [{ id: jid, ...update, author: message.participant ? message.participant : undefined }])
277
- }
278
- const emitGroupRequestJoin = (participant, action, method) => {
279
- ev.emit('group.join-request', { id: jid, author: message.participant, participant, action, method: method })
280
- }
281
- const participantsIncludesMe = () => participants.find(jid => WABinary_1.areJidsSameUser(meId, jid))
282
- switch (message.messageStubType) {
283
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
284
- participants = message.messageStubParameters || []
285
- emitParticipantsUpdate('modify')
286
- break
287
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
288
- participants = message.messageStubParameters || [];
289
- emitParticipantsUpdate('leave');
290
- // mark the chat read only if you left the group
291
- if (participantsIncludesMe()) {
292
- chat.readOnly = true;
293
- }
294
- break;
295
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
296
- participants = message.messageStubParameters || []
297
- emitParticipantsUpdate('remove')
298
- // mark the chat read only if you left the group
299
- if (participantsIncludesMe()) {
300
- chat.readOnly = true
301
- }
302
- break
303
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:
304
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
305
- let actionGp = 'invite'
306
- participants = message.messageStubParameters || []
307
- if (participantsIncludesMe()) chat.readOnly = false;
308
- if (message?.key?.participant && !participants.includes(message?.key?.participant)) {
309
- actionGp = 'approval-invite'
310
- }
311
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
312
- participants = message.messageStubParameters || []
313
- if (participantsIncludesMe()) {
314
- chat.readOnly = false
315
- }
316
- emitParticipantsUpdate('add')
317
- break
318
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
319
- case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_DEMOTE:
320
- participants = message.messageStubParameters || []
321
- emitParticipantsUpdate('demote')
322
- break
323
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
324
- case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_PROMOTE:
325
- participants = message.messageStubParameters || []
326
- emitParticipantsUpdate('promote')
327
- break
328
- case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
329
- const announceValue = message.messageStubParameters?.[0]
330
- emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' })
331
- break
332
- case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:
333
- const restrictValue = message.messageStubParameters?.[0]
334
- emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' })
335
- break
336
- case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:
337
- const name = message.messageStubParameters?.[0]
338
- chat.name = name
339
- emitGroupUpdate({ subject: name })
340
- break
341
- case Types_1.WAMessageStubType.COMMUNITY_PARENT_GROUP_SUBJECT_CHANGED:
342
- const name2 = message.messageStubParameters?.[0]
343
- chat.name2 = name2
344
- emitCommunityUpdate({ subject: name2 })
345
- break
346
- case Types_1.WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
347
- const description = message.messageStubParameters?.[0]
348
- chat.description = description
349
- emitGroupUpdate({ desc: description })
350
- break
351
- case Types_1.WAMessageStubType.COMMUNITY_CHANGE_DESCRIPTION:
352
- const description2 = message.messageStubParameters?.[0]
353
- chat.description2 = description2
354
- emitCommunityUpdate({ desc: description2 })
355
- break
356
- case Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
357
- const code = message.messageStubParameters?.[0]
358
- emitGroupUpdate({ inviteCode: code })
359
- break
360
- case Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
361
- const memberAddValue = message.messageStubParameters?.[0]
362
- emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' })
363
- break
364
- case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
365
- const approvalMode = message.messageStubParameters?.[0]
366
- emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' })
367
- break
368
- case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
369
- const participant = message.messageStubParameters?.[0]
370
- const action = message.messageStubParameters?.[1]
371
- const method = message.messageStubParameters?.[2]
372
- emitGroupRequestJoin(participant, action, method)
373
- break
374
- }
375
- }
376
- else if (content?.pollUpdateMessage) {
377
- const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey
378
- // we need to fetch the poll creation message to get the poll enc key
379
- const pollMsg2 = await getMessage(creationMsgKey);
380
- const pollMsg = pollMsg.botInvokeMessage?.message || pollMsg2;
381
- if (pollMsg) {
382
- const meIdNormalised = WABinary_1.jidNormalizedUser(meId)
383
- const pollCreatorJid = generics_1.getKeyAuthor(creationMsgKey, meIdNormalised)
384
- const voterJid = generics_1.getKeyAuthor(message.key, meIdNormalised)
385
- const pollEncKey = pollMsg.messageContextInfo?.messageSecret
386
- try {
387
- const voteMsg = decryptPollVote(content.pollUpdateMessage.vote, {
388
- pollEncKey,
389
- pollCreatorJid,
390
- pollMsgId: creationMsgKey.id,
391
- voterJid,
392
- })
393
- ev.emit('messages.update', [
394
- {
395
- key: creationMsgKey,
396
- update: {
397
- pollUpdates: [
398
- {
399
- pollUpdateMessageKey: message.key,
400
- vote: voteMsg,
401
- senderTimestampMs: content.pollUpdateMessage.senderTimestampMs.toNumber(),
402
- }
403
- ]
404
- }
405
- }
406
- ])
407
- }
408
- catch (err) {
409
- logger?.warn({ err, creationMsgKey }, 'failed to decrypt poll vote')
410
- }
411
- }
412
- else {
413
- logger?.warn({ creationMsgKey }, 'poll creation message not found, cannot decrypt update')
414
- }
415
- }
416
- if (Object.keys(chat).length > 1) {
417
- ev.emit('chats.update', [chat])
418
- }
419
- }
420
-
421
- module.exports = {
422
- cleanMessage,
423
- isRealMessage,
424
- shouldIncrementChatUnread,
425
- getChatId,
426
- decryptPollVote,
427
- processMessage
428
- }
1
+ "use strict"
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true })
4
+
5
+ const WAProto_1 = require("../../WAProto")
6
+ const Types_1 = require("../Types")
7
+ const messages_1 = require("../Utils/messages")
8
+ const WABinary_1 = require("../WABinary")
9
+ const crypto_1 = require("./crypto")
10
+ const generics_1 = require("./generics")
11
+ const history_1 = require("./history")
12
+
13
+ const REAL_MSG_STUB_TYPES = new Set([
14
+ Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
15
+ Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE,
16
+ Types_1.WAMessageStubType.CALL_MISSED_VIDEO,
17
+ Types_1.WAMessageStubType.CALL_MISSED_VOICE
18
+ ])
19
+
20
+ const REAL_MSG_REQ_ME_STUB_TYPES = new Set([
21
+ Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD
22
+ ])
23
+
24
+ /** Cleans a received message to further processing */
25
+ const cleanMessage = (message, meId) => {
26
+ // ensure remoteJid and participant doesn't have device or agent in it
27
+ message.key.remoteJid = WABinary_1.jidNormalizedUser(message.key.remoteJid)
28
+ message.key.participant = message.key.participant ? WABinary_1.jidNormalizedUser(message.key.participant) : undefined
29
+ const content = messages_1.normalizeMessageContent(message.message)
30
+ // if the message has a reaction, ensure fromMe & remoteJid are from our perspective
31
+ if (content?.reactionMessage) {
32
+ normaliseKey(content.reactionMessage.key)
33
+ }
34
+ if (content?.pollUpdateMessage) {
35
+ normaliseKey(content.pollUpdateMessage.pollCreationMessageKey)
36
+ }
37
+ function normaliseKey(msgKey) {
38
+ // if the reaction is from another user
39
+ // we've to correctly map the key to this user's perspective
40
+ if (!message.key.fromMe) {
41
+ // if the sender believed the message being reacted to is not from them
42
+ // we've to correct the key to be from them, or some other participant
43
+ msgKey.fromMe = !msgKey.fromMe
44
+ ? WABinary_1.areJidsSameUser(msgKey.participant || msgKey.remoteJid, meId)
45
+ // if the message being reacted to, was from them
46
+ // fromMe automatically becomes false
47
+ : false
48
+ // set the remoteJid to being the same as the chat the message came from
49
+ msgKey.remoteJid = message.key.remoteJid
50
+ // set participant of the message
51
+ msgKey.participant = msgKey.participant || message.key.participant
52
+ }
53
+ }
54
+ }
55
+
56
+ const isRealMessage = (message, meId) => {
57
+ const normalizedContent = messages_1.normalizeMessageContent(message.message)
58
+ const hasSomeContent = !!messages_1.getContentType(normalizedContent)
59
+ return (
60
+ !!normalizedContent ||
61
+ REAL_MSG_STUB_TYPES.has(message.messageStubType) ||
62
+ (
63
+ REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType) &&
64
+ message.messageStubParameters?.some(p => WABinary_1.areJidsSameUser(meId, p))
65
+ )
66
+ ) &&
67
+ hasSomeContent &&
68
+ !normalizedContent?.protocolMessage &&
69
+ !normalizedContent?.reactionMessage &&
70
+ !normalizedContent?.pollUpdateMessage
71
+ }
72
+
73
+ const shouldIncrementChatUnread = (message) => (!message.key.fromMe && !message.messageStubType)
74
+
75
+ /**
76
+ * Get the ID of the chat from the given key.
77
+ * Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant
78
+ */
79
+ const getChatId = ({ remoteJid, participant, fromMe }) => {
80
+ if (WABinary_1.isJidBroadcast(remoteJid)
81
+ && !WABinary_1.isJidStatusBroadcast(remoteJid)
82
+ && !fromMe) {
83
+ return participant
84
+ }
85
+ return remoteJid
86
+ }
87
+
88
+ /**
89
+ * Decrypt a poll vote
90
+ * @param vote encrypted vote
91
+ * @param ctx additional info about the poll required for decryption
92
+ * @returns list of SHA256 options
93
+ */
94
+ function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid, }) {
95
+ const sign = Buffer.concat([
96
+ toBinary(pollMsgId),
97
+ toBinary(pollCreatorJid),
98
+ toBinary(voterJid),
99
+ toBinary('Poll Vote'),
100
+ new Uint8Array([1])
101
+ ])
102
+ const key0 = crypto_1.hmacSign(pollEncKey, new Uint8Array(32), 'sha256')
103
+ const decKey = crypto_1.hmacSign(sign, key0, 'sha256')
104
+ const aad = toBinary(`${pollMsgId}\u0000${voterJid}`)
105
+ const decrypted = crypto_1.aesDecryptGCM(encPayload, decKey, encIv, aad)
106
+ return WAProto_1.proto.Message.PollVoteMessage.decode(decrypted)
107
+ function toBinary(txt) {
108
+ return Buffer.from(txt)
109
+ }
110
+ }
111
+
112
+ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, signalRepository, keyStore, logger, options, getMessage }) => {
113
+ const meId = creds.me.id
114
+ const { accountSettings } = creds
115
+ const chat = { id: WABinary_1.jidNormalizedUser(getChatId(message.key)) }
116
+ const isRealMsg = isRealMessage(message, meId)
117
+ if (isRealMsg) {
118
+ chat.messages = [{ message }]
119
+ chat.conversationTimestamp = generics_1.toNumber(message.messageTimestamp)
120
+ // only increment unread count if not CIPHERTEXT and from another person
121
+ if (shouldIncrementChatUnread(message)) {
122
+ chat.unreadCount = (chat.unreadCount || 0) + 1
123
+ }
124
+ }
125
+ const content = messages_1.normalizeMessageContent(message.message)
126
+ // unarchive chat if it's a real message, or someone reacted to our message
127
+ // and we've the unarchive chats setting on
128
+ if (isRealMsg || content?.reactionMessage?.key?.fromMe
129
+ && accountSettings?.unarchiveChats) {
130
+ chat.archived = false
131
+ chat.readOnly = false
132
+ }
133
+ const protocolMsg = content?.protocolMessage
134
+ if (protocolMsg) {
135
+ switch (protocolMsg.type) {
136
+ case WAProto_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
137
+ const histNotification = protocolMsg.historySyncNotification
138
+ const process = shouldProcessHistoryMsg
139
+ const isLatest = !creds.processedHistoryMessages?.length
140
+ logger?.info({
141
+ histNotification,
142
+ process,
143
+ id: message.key.id,
144
+ isLatest,
145
+ }, 'got history notification')
146
+ if (process) {
147
+ if (histNotification.syncType !== WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND) {
148
+ ev.emit('creds.update', {
149
+ processedHistoryMessages: [
150
+ ...(creds.processedHistoryMessages || []),
151
+ { key: message.key, messageTimestamp: message.messageTimestamp }
152
+ ]
153
+ })
154
+ }
155
+ const data = await history_1.downloadAndProcessHistorySyncNotification(histNotification, options)
156
+ ev.emit('messaging-history.set', {
157
+ ...data,
158
+ isLatest: histNotification.syncType !== WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND
159
+ ? isLatest
160
+ : undefined,
161
+ peerDataRequestSessionId: histNotification.peerDataRequestSessionId
162
+ })
163
+ }
164
+ break
165
+ case WAProto_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
166
+ const keys = protocolMsg.appStateSyncKeyShare.keys
167
+ if (keys?.length) {
168
+ let newAppStateSyncKeyId = ''
169
+ await keyStore.transaction(async () => {
170
+ const newKeys = []
171
+ for (const { keyData, keyId } of keys) {
172
+ const strKeyId = Buffer.from(keyId.keyId).toString('base64')
173
+ newKeys.push(strKeyId)
174
+ await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } })
175
+ newAppStateSyncKeyId = strKeyId
176
+ }
177
+ logger?.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys')
178
+ }, meId)
179
+ ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId })
180
+ }
181
+ else {
182
+ logger?.info({ protocolMsg }, 'recv app state sync with 0 keys')
183
+ }
184
+ break
185
+ case WAProto_1.proto.Message.ProtocolMessage.Type.REVOKE:
186
+ ev.emit('messages.update', [
187
+ {
188
+ key: {
189
+ ...message.key,
190
+ id: protocolMsg.key.id
191
+ },
192
+ update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }
193
+ }
194
+ ])
195
+ break
196
+ case WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
197
+ Object.assign(chat, {
198
+ ephemeralSettingTimestamp: generics_1.toNumber(message.messageTimestamp),
199
+ ephemeralExpiration: protocolMsg.ephemeralExpiration || null
200
+ })
201
+ break
202
+ case WAProto_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
203
+ const response = protocolMsg.peerDataOperationRequestResponseMessage
204
+ if (response) {
205
+ await placeholderResendCache?.del(response.stanzaId)
206
+ // TODO: IMPLEMENT HISTORY SYNC ETC (sticker uploads etc.).
207
+ const { peerDataOperationResult } = response
208
+ for (const result of peerDataOperationResult) {
209
+ const { placeholderMessageResendResponse: retryResponse } = result
210
+ //eslint-disable-next-line max-depth
211
+ if (retryResponse) {
212
+ const webMessageInfo = WAProto_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes)
213
+ // wait till another upsert event is available, don't want it to be part of the PDO response message
214
+ setTimeout(() => {
215
+ ev.emit('messages.upsert', {
216
+ messages: [webMessageInfo],
217
+ type: 'notify',
218
+ requestId: response.stanzaId
219
+ })
220
+ }, 500)
221
+ }
222
+ }
223
+ }
224
+ break
225
+ case WAProto_1.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
226
+ ev.emit('messages.update', [
227
+ {
228
+ // flip the sender / fromMe properties because they're in the perspective of the sender
229
+ key: { ...message.key, id: protocolMsg.key?.id },
230
+ update: {
231
+ message: {
232
+ editedMessage: {
233
+ message: protocolMsg.editedMessage
234
+ }
235
+ },
236
+ messageTimestamp: protocolMsg.timestampMs
237
+ ? Math.floor(generics_1.toNumber(protocolMsg.timestampMs) / 1000)
238
+ : message.messageTimestamp
239
+ }
240
+ }
241
+ ])
242
+ break
243
+ case WAProto_1.proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
244
+ const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload
245
+ const { pnToLidMappings, chatDbMigrationTimestamp } = WAProto_1.proto.LIDMigrationMappingSyncPayload.decode(encodedPayload)
246
+
247
+ logger?.debug({ pnToLidMappings, chatDbMigrationTimestamp }, 'got lid mappings and chat db migration timestamp')
248
+
249
+ const pairs = []
250
+
251
+ for (const { pn, latestLid, assignedLid } of pnToLidMappings) {
252
+ const lid = latestLid || assignedLid
253
+ pairs.push({ lid: `${lid}@lid`, pn: `${pn}@s.whatsapp.net` })
254
+ }
255
+
256
+ await signalRepository.lidMapping.storeLIDPNMappings(pairs)
257
+ break
258
+ case WAProto_1.proto.Message.ProtocolMessage.Type.LIMIT_SHARING:
259
+ ev.emit('limit-sharing.update', {
260
+ id: message.key.remoteJid,
261
+ author: WABinary_1.areJidsSameUser(message.key.remoteJid, protocolMsg.key.remoteJid) ? WABinary_1.jidNormalizedUser(meId) : message.key.remoteJid,
262
+ action: `${protocolMsg.limitSharing.sharingLimited ? 'on' : 'off'}`,
263
+ trigger: protocolMsg.limitSharing.trigger,
264
+ update_time: protocolMsg.limitSharing.limitSharingSettingTimestamp
265
+ })
266
+ break
267
+ }
268
+ }
269
+ else if (content?.reactionMessage) {
270
+ const reaction = {
271
+ ...content.reactionMessage,
272
+ key: message.key,
273
+ }
274
+ ev.emit('messages.reaction', [{
275
+ reaction,
276
+ key: content.reactionMessage?.key
277
+ }])
278
+ }
279
+ else if (message.messageStubType) {
280
+ const jid = message.key?.remoteJid
281
+ //let actor = whatsappID (message.participant)
282
+ let participants
283
+ const emitParticipantsUpdate = (action) => (ev.emit('group-participants.update', { id: jid, author: message.participant, participants, action }))
284
+ const emitGroupUpdate = (update) => {
285
+ ev.emit('groups.update', [{ id: jid, ...update, author: message.participant ? message.participant : undefined }])
286
+ }
287
+ const emitGroupRequestJoin = (participant, action, method) => {
288
+ ev.emit('group.join-request', { id: jid, author: message.participant, participant, action, method: method })
289
+ }
290
+ const participantsIncludesMe = () => participants.find(jid => WABinary_1.areJidsSameUser(meId, jid))
291
+ switch (message.messageStubType) {
292
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
293
+ participants = message.messageStubParameters || []
294
+ emitParticipantsUpdate('modify')
295
+ break
296
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
297
+ participants = message.messageStubParameters || [];
298
+ emitParticipantsUpdate('leave');
299
+ // mark the chat read only if you left the group
300
+ if (participantsIncludesMe()) {
301
+ chat.readOnly = true;
302
+ }
303
+ break;
304
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
305
+ participants = message.messageStubParameters || []
306
+ emitParticipantsUpdate('remove')
307
+ // mark the chat read only if you left the group
308
+ if (participantsIncludesMe()) {
309
+ chat.readOnly = true
310
+ }
311
+ break
312
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:
313
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
314
+ let actionGp = 'invite'
315
+ participants = message.messageStubParameters || []
316
+ if (participantsIncludesMe()) chat.readOnly = false;
317
+ if (message?.key?.participant && !participants.includes(message?.key?.participant)) {
318
+ actionGp = 'approval-invite'
319
+ }
320
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
321
+ participants = message.messageStubParameters || []
322
+ if (participantsIncludesMe()) {
323
+ chat.readOnly = false
324
+ }
325
+ emitParticipantsUpdate('add')
326
+ break
327
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
328
+ // case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_DEMOTE:
329
+ participants = message.messageStubParameters || []
330
+ emitParticipantsUpdate('demote')
331
+ break
332
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
333
+ // case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_PROMOTE:
334
+ participants = message.messageStubParameters || []
335
+ emitParticipantsUpdate('promote')
336
+ break
337
+ case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
338
+ const announceValue = message.messageStubParameters?.[0]
339
+ emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' })
340
+ break
341
+ case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:
342
+ const restrictValue = message.messageStubParameters?.[0]
343
+ emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' })
344
+ break
345
+ case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:
346
+ const name = message.messageStubParameters?.[0]
347
+ chat.name = name
348
+ emitGroupUpdate({ subject: name })
349
+ break
350
+ case Types_1.WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
351
+ const description = message.messageStubParameters?.[0]
352
+ chat.description = description
353
+ emitGroupUpdate({ desc: description })
354
+ break
355
+ case Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
356
+ const code = message.messageStubParameters?.[0]
357
+ emitGroupUpdate({ inviteCode: code })
358
+ break
359
+ case Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
360
+ const memberAddValue = message.messageStubParameters?.[0]
361
+ emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' })
362
+ break
363
+ case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
364
+ const approvalMode = message.messageStubParameters?.[0]
365
+ emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' })
366
+ break
367
+ case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
368
+ const participant = message.messageStubParameters?.[0]
369
+ const action = message.messageStubParameters?.[1]
370
+ const method = message.messageStubParameters?.[2]
371
+ emitGroupRequestJoin(participant, action, method)
372
+ break
373
+ }
374
+ }
375
+ else if (content?.pollUpdateMessage) {
376
+ const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey
377
+ // we need to fetch the poll creation message to get the poll enc key
378
+ const pollMsg2 = await getMessage(creationMsgKey);
379
+ const pollMsg = pollMsg.botInvokeMessage?.message || pollMsg2;
380
+ if (pollMsg) {
381
+ const meIdNormalised = WABinary_1.jidNormalizedUser(meId)
382
+ const pollCreatorJid = generics_1.getKeyAuthor(creationMsgKey, meIdNormalised)
383
+ const voterJid = generics_1.getKeyAuthor(message.key, meIdNormalised)
384
+ const pollEncKey = pollMsg.messageContextInfo?.messageSecret
385
+ try {
386
+ const voteMsg = decryptPollVote(content.pollUpdateMessage.vote, {
387
+ pollEncKey,
388
+ pollCreatorJid,
389
+ pollMsgId: creationMsgKey.id,
390
+ voterJid,
391
+ })
392
+ ev.emit('messages.update', [
393
+ {
394
+ key: creationMsgKey,
395
+ update: {
396
+ pollUpdates: [
397
+ {
398
+ pollUpdateMessageKey: message.key,
399
+ vote: voteMsg,
400
+ senderTimestampMs: content.pollUpdateMessage.senderTimestampMs.toNumber(),
401
+ }
402
+ ]
403
+ }
404
+ }
405
+ ])
406
+ }
407
+ catch (err) {
408
+ logger?.warn({ err, creationMsgKey }, 'failed to decrypt poll vote')
409
+ }
410
+ }
411
+ else {
412
+ logger?.warn({ creationMsgKey }, 'poll creation message not found, cannot decrypt update')
413
+ }
414
+ }
415
+ if (Object.keys(chat).length > 1) {
416
+ ev.emit('chats.update', [chat])
417
+ }
418
+ }
419
+
420
+ module.exports = {
421
+ cleanMessage,
422
+ isRealMessage,
423
+ shouldIncrementChatUnread,
424
+ getChatId,
425
+ decryptPollVote,
426
+ processMessage
427
+ }