sanka-baileyss 2.3.2 → 2.3.4

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