@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
@@ -0,0 +1,950 @@
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)
31
+ return;
32
+ const jids = candidates.map(c => c.storageJid);
33
+ const existing = await keyStore.get('tctoken', jids);
34
+ const entries = {};
35
+ for (const c of candidates) {
36
+ const existingEntry = existing[c.storageJid];
37
+ const existingTs = existingEntry?.timestamp ? Number(existingEntry.timestamp) : 0;
38
+ if (existingTs > 0 && existingTs >= c.ts)
39
+ continue;
40
+ entries[c.storageJid] = {
41
+ ...existingEntry,
42
+ token: c.token,
43
+ timestamp: String(c.ts),
44
+ ...(c.senderTs !== undefined ? { senderTimestamp: c.senderTs } : {})
45
+ };
46
+ }
47
+ if (Object.keys(entries).length) {
48
+ logger?.debug({ count: Object.keys(entries).length }, 'storing tctokens from history sync');
49
+ try {
50
+ const indexWrite = await (0, tc_token_utils_1.buildMergedTcTokenIndexWrite)(keyStore, Object.keys(entries));
51
+ await keyStore.set({ tctoken: { ...entries, ...indexWrite } });
52
+ }
53
+ catch (err) {
54
+ logger?.warn({ err }, 'failed to store tctokens from history sync');
55
+ }
56
+ }
57
+ }
58
+ const REAL_MSG_STUB_TYPES = new Set([
59
+ Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
60
+ Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE,
61
+ Types_1.WAMessageStubType.CALL_MISSED_VIDEO,
62
+ Types_1.WAMessageStubType.CALL_MISSED_VOICE
63
+ ]);
64
+ const REAL_MSG_REQ_ME_STUB_TYPES = new Set([Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD]);
65
+ const cleanMessage = (message, meId, meLid) => {
66
+ if ((0, WABinary_1.isHostedPnUser)(message.key.remoteJid) || (0, WABinary_1.isHostedLidUser)(message.key.remoteJid)) {
67
+ message.key.remoteJid = (0, WABinary_1.jidEncode)((0, WABinary_1.jidDecode)(message.key?.remoteJid)?.user, (0, WABinary_1.isHostedPnUser)(message.key.remoteJid) ? 's.whatsapp.net' : 'lid');
68
+ }
69
+ else {
70
+ message.key.remoteJid = (0, WABinary_1.jidNormalizedUser)(message.key.remoteJid);
71
+ }
72
+ if ((0, WABinary_1.isHostedPnUser)(message.key.participant) ||
73
+ (0, WABinary_1.isHostedLidUser)(message.key.participant)) {
74
+ message.key.participant = (0, WABinary_1.jidEncode)((0, WABinary_1.jidDecode)(message.key.participant)?.user, (0, WABinary_1.isHostedPnUser)(message.key.participant) ? 's.whatsapp.net' : 'lid');
75
+ }
76
+ else {
77
+ message.key.participant = (0, WABinary_1.jidNormalizedUser)(message.key.participant);
78
+ }
79
+ const content = (0, messages_1.normalizeMessageContent)(message.message);
80
+ if (content?.reactionMessage) {
81
+ normaliseKey(content.reactionMessage.key);
82
+ }
83
+ if (content?.pollUpdateMessage) {
84
+ normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
85
+ }
86
+ function normaliseKey(msgKey) {
87
+ if (!message.key.fromMe) {
88
+ msgKey.fromMe = !msgKey.fromMe
89
+ ? (0, WABinary_1.areJidsSameUser)(msgKey.participant || msgKey.remoteJid, meId) ||
90
+ (0, WABinary_1.areJidsSameUser)(msgKey.participant || msgKey.remoteJid, meLid)
91
+ :
92
+ false;
93
+ msgKey.remoteJid = message.key.remoteJid;
94
+ msgKey.participant = msgKey.participant || message.key.participant;
95
+ }
96
+ }
97
+ };
98
+ exports.cleanMessage = cleanMessage;
99
+ const isRealMessage = message => {
100
+ const normalizedContent = (0, messages_1.normalizeMessageContent)(message.message);
101
+ const hasSomeContent = !!(0, messages_1.getContentType)(normalizedContent);
102
+ return ((!!normalizedContent ||
103
+ REAL_MSG_STUB_TYPES.has(message.messageStubType) ||
104
+ REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)) &&
105
+ hasSomeContent &&
106
+ !normalizedContent?.protocolMessage &&
107
+ !normalizedContent?.reactionMessage &&
108
+ !normalizedContent?.pollUpdateMessage);
109
+ };
110
+ exports.isRealMessage = isRealMessage;
111
+ const shouldIncrementChatUnread = message => !message.key.fromMe && !message.messageStubType;
112
+ exports.shouldIncrementChatUnread = shouldIncrementChatUnread;
113
+ const getChatId = ({ remoteJid, participant, fromMe }) => {
114
+ if ((0, WABinary_1.isJidBroadcast)(remoteJid) && !(0, WABinary_1.isJidStatusBroadcast)(remoteJid) && !fromMe) {
115
+ return participant;
116
+ }
117
+ return remoteJid;
118
+ };
119
+ exports.getChatId = getChatId;
120
+ function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid }) {
121
+ const sign = Buffer.concat([
122
+ toBinary(pollMsgId),
123
+ toBinary(pollCreatorJid),
124
+ toBinary(voterJid),
125
+ toBinary('Poll Vote'),
126
+ new Uint8Array([1])
127
+ ]);
128
+ const key0 = (0, crypto_1.hmacSign)(pollEncKey, new Uint8Array(32), 'sha256');
129
+ const decKey = (0, crypto_1.hmacSign)(sign, key0, 'sha256');
130
+ const aad = toBinary(`${pollMsgId}\u0000${voterJid}`);
131
+ const decrypted = (0, crypto_1.aesDecryptGCM)(encPayload, decKey, encIv, aad);
132
+ return index_js_1.proto.Message.PollVoteMessage.decode(decrypted);
133
+ function toBinary(txt) {
134
+ return Buffer.from(txt);
135
+ }
136
+ }
137
+ function decryptEventResponse({ encPayload, encIv }, { eventCreatorJid, eventMsgId, eventEncKey, responderJid }) {
138
+ const sign = Buffer.concat([
139
+ toBinary(eventMsgId),
140
+ toBinary(eventCreatorJid),
141
+ toBinary(responderJid),
142
+ toBinary('Event Response'),
143
+ new Uint8Array([1])
144
+ ]);
145
+ const key0 = (0, crypto_1.hmacSign)(eventEncKey, new Uint8Array(32), 'sha256');
146
+ const decKey = (0, crypto_1.hmacSign)(sign, key0, 'sha256');
147
+ const aad = toBinary(`${eventMsgId}\u0000${responderJid}`);
148
+ const decrypted = (0, crypto_1.aesDecryptGCM)(encPayload, decKey, encIv, aad);
149
+ return index_js_1.proto.Message.EventResponseMessage.decode(decrypted);
150
+ function toBinary(txt) {
151
+ return Buffer.from(txt);
152
+ }
153
+ }
154
+ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, signalRepository, keyStore, logger, options, getMessage }) => {
155
+ const meId = creds.me.id;
156
+ const { accountSettings } = creds;
157
+ const chat = { id: (0, WABinary_1.jidNormalizedUser)((0, exports.getChatId)(message.key)) };
158
+ const isRealMsg = (0, exports.isRealMessage)(message);
159
+ if (isRealMsg) {
160
+ chat.messages = [{ message }];
161
+ chat.conversationTimestamp = (0, generics_1.toNumber)(message.messageTimestamp);
162
+ if ((0, exports.shouldIncrementChatUnread)(message)) {
163
+ chat.unreadCount = (chat.unreadCount || 0) + 1;
164
+ }
165
+ }
166
+ if (message.statusPsa) {
167
+ ev.emit('status.psa', {
168
+ key: message.key,
169
+ psa: message.statusPsa,
170
+ chatId: chat.id
171
+ });
172
+ }
173
+ if (message.quarantinedMessage) {
174
+ ev.emit('message.quarantined', {
175
+ key: message.key,
176
+ quarantineInfo: message.quarantinedMessage,
177
+ chatId: chat.id
178
+ });
179
+ }
180
+ if (message.interactiveMessageAdditionalMetadata?.isGalaxyFlowCompleted) {
181
+ ev.emit('galaxy.flow.completed', {
182
+ key: message.key,
183
+ chatId: chat.id
184
+ });
185
+ }
186
+ if (message.ephemeralExpirationTimestamp) {
187
+ Object.assign(chat, {
188
+ ephemeralExpirationTimestamp: (0, generics_1.toNumber)(message.ephemeralExpirationTimestamp)
189
+ });
190
+ }
191
+ const content = (0, messages_1.normalizeMessageContent)(message.message);
192
+ if ((isRealMsg || content?.reactionMessage?.key?.fromMe) && accountSettings?.unarchiveChats) {
193
+ chat.archived = false;
194
+ chat.readOnly = false;
195
+ }
196
+ const protocolMsg = content?.protocolMessage;
197
+ if (protocolMsg) {
198
+ switch (protocolMsg.type) {
199
+ case index_js_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
200
+ const histNotification = protocolMsg.historySyncNotification;
201
+ const process = shouldProcessHistoryMsg;
202
+ const isLatest = !creds.processedHistoryMessages?.length;
203
+ logger?.info({
204
+ histNotification,
205
+ process,
206
+ id: message.key.id,
207
+ isLatest
208
+ }, 'got history notification');
209
+ if (process) {
210
+ if (histNotification.syncType !== index_js_1.proto.HistorySync.HistorySyncType.ON_DEMAND) {
211
+ ev.emit('creds.update', {
212
+ processedHistoryMessages: [
213
+ ...(creds.processedHistoryMessages || []),
214
+ { key: message.key, messageTimestamp: message.messageTimestamp }
215
+ ]
216
+ });
217
+ }
218
+ const data = await (0, history_1.downloadAndProcessHistorySyncNotification)(histNotification, options, logger);
219
+ if (data.lidPnMappings?.length) {
220
+ logger?.debug({ count: data.lidPnMappings.length }, 'processing LID-PN mappings from history sync');
221
+ await signalRepository.lidMapping
222
+ .storeLIDPNMappings(data.lidPnMappings)
223
+ .catch(err => logger?.warn({ err }, 'failed to store LID-PN mappings from history sync'));
224
+ }
225
+ await storeTcTokensFromHistorySync(data.chats, signalRepository, keyStore, logger);
226
+ ev.emit('messaging-history.set', {
227
+ ...data,
228
+ isLatest: histNotification.syncType !== index_js_1.proto.HistorySync.HistorySyncType.ON_DEMAND
229
+ ? isLatest
230
+ : undefined,
231
+ peerDataRequestSessionId: histNotification.peerDataRequestSessionId
232
+ });
233
+ }
234
+ break;
235
+ case index_js_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
236
+ const keys = protocolMsg.appStateSyncKeyShare.keys;
237
+ if (keys?.length) {
238
+ let newAppStateSyncKeyId = '';
239
+ await keyStore.transaction(async () => {
240
+ const newKeys = [];
241
+ for (const { keyData, keyId } of keys) {
242
+ const strKeyId = Buffer.from(keyId.keyId).toString('base64');
243
+ newKeys.push(strKeyId);
244
+ await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } });
245
+ newAppStateSyncKeyId = strKeyId;
246
+ }
247
+ logger?.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys');
248
+ }, meId);
249
+ ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId });
250
+ }
251
+ else {
252
+ logger?.info({ protocolMsg }, 'recv app state sync with 0 keys');
253
+ }
254
+ break;
255
+ case index_js_1.proto.Message.ProtocolMessage.Type.REVOKE:
256
+ ev.emit('messages.update', [
257
+ {
258
+ key: {
259
+ ...message.key,
260
+ id: protocolMsg.key.id
261
+ },
262
+ update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }
263
+ }
264
+ ]);
265
+ break;
266
+ case index_js_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
267
+ Object.assign(chat, {
268
+ ephemeralSettingTimestamp: (0, generics_1.toNumber)(message.messageTimestamp),
269
+ ephemeralExpiration: protocolMsg.ephemeralExpiration || null
270
+ });
271
+ break;
272
+ case index_js_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
273
+ const response = protocolMsg.peerDataOperationRequestResponseMessage;
274
+ if (response) {
275
+ const peerDataOperationResult = response.peerDataOperationResult || [];
276
+ for (const result of peerDataOperationResult) {
277
+ const retryResponse = result?.placeholderMessageResendResponse;
278
+ if (!retryResponse?.webMessageInfoBytes) {
279
+ continue;
280
+ }
281
+ try {
282
+ const webMessageInfo = index_js_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes);
283
+ const msgId = webMessageInfo.key?.id;
284
+ const cachedData = msgId ? await placeholderResendCache?.get(msgId) : undefined;
285
+ if (msgId) {
286
+ await placeholderResendCache?.del(msgId);
287
+ }
288
+ let finalMsg;
289
+ if (cachedData && typeof cachedData === 'object') {
290
+ cachedData.message = webMessageInfo.message;
291
+ if (webMessageInfo.messageTimestamp) {
292
+ cachedData.messageTimestamp = webMessageInfo.messageTimestamp;
293
+ }
294
+ finalMsg = cachedData;
295
+ }
296
+ else {
297
+ finalMsg = webMessageInfo;
298
+ }
299
+ logger?.debug({ msgId, requestId: response.stanzaId }, 'received placeholder resend');
300
+ ev.emit('messages.upsert', {
301
+ messages: [finalMsg],
302
+ type: 'notify',
303
+ requestId: response.stanzaId
304
+ });
305
+ }
306
+ catch (err) {
307
+ logger?.warn({ err, stanzaId: response.stanzaId }, 'failed to decode placeholder resend response');
308
+ }
309
+ }
310
+ }
311
+ break;
312
+ case index_js_1.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
313
+ ev.emit('messages.update', [
314
+ {
315
+ key: { ...message.key, id: protocolMsg.key?.id },
316
+ update: {
317
+ message: {
318
+ editedMessage: {
319
+ message: protocolMsg.editedMessage
320
+ }
321
+ },
322
+ messageTimestamp: protocolMsg.timestampMs
323
+ ? Math.floor((0, generics_1.toNumber)(protocolMsg.timestampMs) / 1000)
324
+ : message.messageTimestamp
325
+ }
326
+ }
327
+ ]);
328
+ break;
329
+ case index_js_1.proto.Message.ProtocolMessage.Type.GROUP_MEMBER_LABEL_CHANGE:
330
+ const labelAssociationMsg = protocolMsg.memberLabel;
331
+ if (labelAssociationMsg?.label) {
332
+ ev.emit('group.member-tag.update', {
333
+ groupId: chat.id,
334
+ label: labelAssociationMsg.label,
335
+ participant: message.key.participant,
336
+ participantAlt: message.key.participantAlt,
337
+ messageTimestamp: Number(message.messageTimestamp)
338
+ });
339
+ }
340
+ break;
341
+ case index_js_1.proto.Message.ProtocolMessage.Type.BOT_FEEDBACK_MESSAGE:
342
+ if (protocolMsg.botFeedbackMessage) {
343
+ ev.emit('bot.feedback', {
344
+ key: message.key,
345
+ botFeedback: protocolMsg.botFeedbackMessage,
346
+ targetKey: protocolMsg.key
347
+ });
348
+ }
349
+ break;
350
+ case index_js_1.proto.Message.ProtocolMessage.Type.CLOUD_API_THREAD_CONTROL_NOTIFICATION:
351
+ if (protocolMsg.cloudAPIThreadControlNotification) {
352
+ ev.emit('cloud.thread.control', {
353
+ key: message.key,
354
+ notification: protocolMsg.cloudAPIThreadControlNotification,
355
+ chatId: chat.id
356
+ });
357
+ }
358
+ break;
359
+ case index_js_1.proto.Message.ProtocolMessage.Type.CHAT_THEME_SETTING:
360
+ if (protocolMsg.chatThemeSetting) {
361
+ ev.emit('chats.update', [
362
+ {
363
+ id: chat.id,
364
+ chatThemeSetting: protocolMsg.chatThemeSetting
365
+ }
366
+ ]);
367
+ }
368
+ break;
369
+ case index_js_1.proto.Message.ProtocolMessage.Type.STOP_GENERATION_MESSAGE:
370
+ ev.emit('bot.stop-generation', {
371
+ key: message.key,
372
+ targetKey: protocolMsg.key,
373
+ chatId: chat.id
374
+ });
375
+ break;
376
+ case index_js_1.proto.Message.ProtocolMessage.Type.MEDIA_NOTIFY_MESSAGE:
377
+ if (protocolMsg.mediaNotifyMessage) {
378
+ ev.emit('media.notify', {
379
+ key: message.key,
380
+ mediaNotify: protocolMsg.mediaNotifyMessage
381
+ });
382
+ }
383
+ break;
384
+ case index_js_1.proto.Message.ProtocolMessage.Type.REQUEST_WELCOME_MESSAGE:
385
+ ev.emit('bot.welcome-request', {
386
+ key: message.key,
387
+ chatId: chat.id,
388
+ timestamp: message.messageTimestamp
389
+ });
390
+ break;
391
+ case index_js_1.proto.Message.ProtocolMessage.Type.BOT_MEMU_ONBOARDING_MESSAGE:
392
+ ev.emit('bot.memu-onboarding', {
393
+ key: message.key,
394
+ chatId: chat.id
395
+ });
396
+ break;
397
+ case index_js_1.proto.Message.ProtocolMessage.Type.STATUS_MENTION_MESSAGE:
398
+ if (protocolMsg.statusMentionMessage) {
399
+ ev.emit('status.mention', {
400
+ key: message.key,
401
+ statusMention: protocolMsg.statusMentionMessage,
402
+ chatId: chat.id
403
+ });
404
+ }
405
+ break;
406
+ case index_js_1.proto.Message.ProtocolMessage.Type.AI_PSI_METADATA:
407
+ if (protocolMsg.aiPsiMetadata) {
408
+ ev.emit('bot.psi-metadata', {
409
+ key: message.key,
410
+ psiMetadata: protocolMsg.aiPsiMetadata,
411
+ chatId: chat.id
412
+ });
413
+ }
414
+ break;
415
+ case index_js_1.proto.Message.ProtocolMessage.Type.AI_QUERY_FANOUT:
416
+ if (protocolMsg.aiQueryFanout) {
417
+ ev.emit('bot.query-fanout', {
418
+ key: message.key,
419
+ queryFanout: protocolMsg.aiQueryFanout,
420
+ chatId: chat.id
421
+ });
422
+ }
423
+ break;
424
+ case index_js_1.proto.Message.ProtocolMessage.Type.AI_MEDIA_COLLECTION_MESSAGE:
425
+ if (protocolMsg.aiMediaCollectionMessage) {
426
+ ev.emit('bot.media-collection', {
427
+ key: message.key,
428
+ collection: protocolMsg.aiMediaCollectionMessage,
429
+ chatId: chat.id
430
+ });
431
+ }
432
+ break;
433
+ case index_js_1.proto.Message.ProtocolMessage.Type.REMINDER_MESSAGE:
434
+ ev.emit('reminder.update', {
435
+ key: message.key,
436
+ chatId: chat.id,
437
+ timestamp: message.messageTimestamp
438
+ });
439
+ break;
440
+ case index_js_1.proto.Message.ProtocolMessage.Type.MESSAGE_UNSCHEDULE:
441
+ if (protocolMsg.key) {
442
+ ev.emit('messages.update', [
443
+ {
444
+ key: { ...message.key, id: protocolMsg.key.id },
445
+ update: { scheduledMessageMetadata: null }
446
+ }
447
+ ]);
448
+ }
449
+ break;
450
+ case index_js_1.proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
451
+ const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload;
452
+ const { pnToLidMappings, chatDbMigrationTimestamp } = index_js_1.proto.LIDMigrationMappingSyncPayload.decode(encodedPayload);
453
+ logger?.debug({ pnToLidMappings, chatDbMigrationTimestamp }, 'got lid mappings and chat db migration timestamp');
454
+ const pairs = [];
455
+ for (const { pn, latestLid, assignedLid } of pnToLidMappings) {
456
+ const lid = latestLid || assignedLid;
457
+ pairs.push({ lid: `${lid}@lid`, pn: `${pn}@s.whatsapp.net` });
458
+ }
459
+ await signalRepository.lidMapping.storeLIDPNMappings(pairs);
460
+ if (pairs.length) {
461
+ for (const { pn, lid } of pairs) {
462
+ await signalRepository.migrateSession(pn, lid);
463
+ }
464
+ }
465
+ }
466
+ }
467
+ else if (content?.reactionMessage) {
468
+ const reaction = {
469
+ ...content.reactionMessage,
470
+ key: message.key
471
+ };
472
+ ev.emit('messages.reaction', [
473
+ {
474
+ reaction,
475
+ key: content.reactionMessage?.key
476
+ }
477
+ ]);
478
+ }
479
+ else if (content?.keepInChatMessage) {
480
+ const kic = content.keepInChatMessage;
481
+ if (kic.key) {
482
+ ev.emit('messages.update', [
483
+ {
484
+ key: kic.key,
485
+ update: { keepInChat: { keepType: kic.keepType, serverTimestamp: message.messageTimestamp, key: kic.key } }
486
+ }
487
+ ]);
488
+ }
489
+ }
490
+ else if (content?.encReactionMessage) {
491
+ ev.emit('messages.update', [
492
+ {
493
+ key: content.encReactionMessage.targetMessageKey,
494
+ update: { encReactionMessage: content.encReactionMessage }
495
+ }
496
+ ]);
497
+ }
498
+ else if (content?.commentMessage) {
499
+ if (content.commentMessage.targetMessageKey) {
500
+ ev.emit('message.comment', {
501
+ comment: content.commentMessage,
502
+ commentKey: message.key,
503
+ targetKey: content.commentMessage.targetMessageKey
504
+ });
505
+ }
506
+ }
507
+ else if (content?.encCommentMessage) {
508
+ ev.emit('message.comment', {
509
+ comment: content.encCommentMessage,
510
+ commentKey: message.key,
511
+ targetKey: content.encCommentMessage.targetMessageKey,
512
+ encrypted: true
513
+ });
514
+ }
515
+ else if (content?.bcallMessage) {
516
+ ev.emit('call', [
517
+ {
518
+ id: content.bcallMessage.sessionId || message.key.id,
519
+ from: message.key.participant || message.key.remoteJid,
520
+ chatId: chat.id,
521
+ isVideo: content.bcallMessage.mediaType === index_js_1.proto.Message.BCallMessage.MediaType.VIDEO,
522
+ isBroadcast: true,
523
+ caption: content.bcallMessage.caption
524
+ }
525
+ ]);
526
+ }
527
+ else if (content?.pollAddOptionMessage) {
528
+ const pao = content.pollAddOptionMessage;
529
+ if (pao.pollCreationMessageKey) {
530
+ ev.emit('poll.add-option', {
531
+ pollKey: pao.pollCreationMessageKey,
532
+ addedOption: pao.addOption,
533
+ addedBy: message.key.participant || message.key.remoteJid,
534
+ messageKey: message.key
535
+ });
536
+ }
537
+ }
538
+ else if (content?.scheduledCallCreationMessage) {
539
+ ev.emit('call.scheduled', {
540
+ messageKey: message.key,
541
+ scheduledCall: content.scheduledCallCreationMessage,
542
+ chatId: chat.id
543
+ });
544
+ }
545
+ else if (content?.scheduledCallEditMessage) {
546
+ ev.emit('call.schedule-cancelled', {
547
+ messageKey: message.key,
548
+ editedCallKey: content.scheduledCallEditMessage.key,
549
+ chatId: chat.id
550
+ });
551
+ }
552
+ else if (content?.splitPaymentMessage) {
553
+ ev.emit('payment.split', {
554
+ messageKey: message.key,
555
+ splitPayment: content.splitPaymentMessage,
556
+ chatId: chat.id
557
+ });
558
+ }
559
+ else if (content?.p2PPaymentReminderNotification) {
560
+ ev.emit('payment.reminder', {
561
+ messageKey: message.key,
562
+ reminder: content.p2PPaymentReminderNotification,
563
+ chatId: chat.id
564
+ });
565
+ }
566
+ else if (content?.encEventResponseMessage) {
567
+ const encEventResponse = content.encEventResponseMessage;
568
+ const creationMsgKey = encEventResponse.eventCreationMessageKey;
569
+ const eventMsg = await getMessage(creationMsgKey);
570
+ if (eventMsg) {
571
+ try {
572
+ const meIdNormalised = (0, WABinary_1.jidNormalizedUser)(meId);
573
+ const eventCreatorKey = creationMsgKey.participant || creationMsgKey.remoteJid;
574
+ const eventCreatorPn = (0, WABinary_1.isLidUser)(eventCreatorKey)
575
+ ? await signalRepository.lidMapping.getPNForLID(eventCreatorKey)
576
+ : eventCreatorKey;
577
+ const eventCreatorJid = (0, generics_1.getKeyAuthor)({ remoteJid: (0, WABinary_1.jidNormalizedUser)(eventCreatorPn), fromMe: meIdNormalised === eventCreatorPn }, meIdNormalised);
578
+ const responderJid = (0, generics_1.getKeyAuthor)(message.key, meIdNormalised);
579
+ const eventEncKey = eventMsg?.messageContextInfo?.messageSecret;
580
+ if (!eventEncKey) {
581
+ logger?.warn({ creationMsgKey }, 'event response: missing messageSecret for decryption');
582
+ }
583
+ else {
584
+ const responseMsg = decryptEventResponse(encEventResponse, {
585
+ eventEncKey,
586
+ eventCreatorJid,
587
+ eventMsgId: creationMsgKey.id,
588
+ responderJid
589
+ });
590
+ const eventResponse = {
591
+ eventResponseMessageKey: message.key,
592
+ senderTimestampMs: responseMsg.timestampMs,
593
+ response: responseMsg
594
+ };
595
+ ev.emit('messages.update', [
596
+ {
597
+ key: creationMsgKey,
598
+ update: {
599
+ eventResponses: [eventResponse]
600
+ }
601
+ }
602
+ ]);
603
+ }
604
+ }
605
+ catch (err) {
606
+ logger?.warn({ err, creationMsgKey }, 'failed to decrypt event response');
607
+ }
608
+ }
609
+ else {
610
+ logger?.warn({ creationMsgKey }, 'event creation message not found, cannot decrypt response');
611
+ }
612
+ }
613
+ else if (message.messageStubType) {
614
+ const jid = message.key?.remoteJid;
615
+ let participants;
616
+ const emitParticipantsUpdate = action => ev.emit('group-participants.update', {
617
+ id: jid,
618
+ author: message.key.participant,
619
+ authorPn: message.key.participantAlt,
620
+ authorUsername: message.key.participantUsername,
621
+ participants,
622
+ action
623
+ });
624
+ const emitGroupUpdate = update => {
625
+ ev.emit('groups.update', [
626
+ {
627
+ id: jid,
628
+ ...update,
629
+ author: message.key.participant ?? undefined,
630
+ authorPn: message.key.participantAlt,
631
+ authorUsername: message.key.participantUsername
632
+ }
633
+ ]);
634
+ };
635
+ const emitGroupRequestJoin = (participant, action, method) => {
636
+ ev.emit('group.join-request', {
637
+ id: jid,
638
+ author: message.key.participant,
639
+ authorPn: message.key.participantAlt,
640
+ authorUsername: message.key.participantUsername,
641
+ participant: participant.lid,
642
+ participantPn: participant.pn,
643
+ action,
644
+ method: method
645
+ });
646
+ };
647
+ const participantsIncludesMe = () => participants.find(jid => (0, WABinary_1.areJidsSameUser)(meId, jid.phoneNumber));
648
+ switch (message.messageStubType) {
649
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
650
+ participants = message.messageStubParameters.map(a => JSON.parse(a)) || [];
651
+ emitParticipantsUpdate('modify');
652
+ break;
653
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
654
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
655
+ participants = message.messageStubParameters.map(a => JSON.parse(a)) || [];
656
+ emitParticipantsUpdate('remove');
657
+ if (participantsIncludesMe()) {
658
+ chat.readOnly = true;
659
+ }
660
+ break;
661
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:
662
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
663
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
664
+ participants = message.messageStubParameters.map(a => JSON.parse(a)) || [];
665
+ if (participantsIncludesMe()) {
666
+ chat.readOnly = false;
667
+ }
668
+ emitParticipantsUpdate('add');
669
+ break;
670
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
671
+ participants = message.messageStubParameters.map(a => JSON.parse(a)) || [];
672
+ emitParticipantsUpdate('demote');
673
+ break;
674
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
675
+ participants = message.messageStubParameters.map(a => JSON.parse(a)) || [];
676
+ emitParticipantsUpdate('promote');
677
+ break;
678
+ case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
679
+ const announceValue = message.messageStubParameters?.[0];
680
+ emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' });
681
+ break;
682
+ case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:
683
+ const restrictValue = message.messageStubParameters?.[0];
684
+ emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' });
685
+ break;
686
+ case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:
687
+ const name = message.messageStubParameters?.[0];
688
+ chat.name = name;
689
+ emitGroupUpdate({ subject: name });
690
+ break;
691
+ case Types_1.WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
692
+ const description = message.messageStubParameters?.[0];
693
+ chat.description = description;
694
+ emitGroupUpdate({ desc: description });
695
+ break;
696
+ case Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
697
+ const code = message.messageStubParameters?.[0];
698
+ emitGroupUpdate({ inviteCode: code });
699
+ break;
700
+ case Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
701
+ const memberAddValue = message.messageStubParameters?.[0];
702
+ emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' });
703
+ break;
704
+ case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
705
+ const approvalMode = message.messageStubParameters?.[0];
706
+ emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' });
707
+ break;
708
+ case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
709
+ const participant = JSON.parse(message.messageStubParameters?.[0]);
710
+ const action = message.messageStubParameters?.[1];
711
+ const method = message.messageStubParameters?.[2];
712
+ emitGroupRequestJoin(participant, action, method);
713
+ break;
714
+ case Types_1.WAMessageStubType.GROUP_CREATE:
715
+ emitGroupUpdate({ subject: message.messageStubParameters?.[0] });
716
+ break;
717
+ case Types_1.WAMessageStubType.GROUP_CHANGE_ICON:
718
+ emitGroupUpdate({ pictureId: message.messageStubParameters?.[0] });
719
+ break;
720
+ case Types_1.WAMessageStubType.GROUP_DELETE:
721
+ emitGroupUpdate({ readOnly: true, deleted: true });
722
+ break;
723
+ case Types_1.WAMessageStubType.GROUP_CREATING:
724
+ emitGroupUpdate({ subject: message.messageStubParameters?.[0], creating: true });
725
+ break;
726
+ case Types_1.WAMessageStubType.GROUP_CREATE_FAILED:
727
+ emitGroupUpdate({ createFailed: true });
728
+ break;
729
+ case Types_1.WAMessageStubType.GROUP_BOUNCED:
730
+ emitGroupUpdate({ bounced: true });
731
+ break;
732
+ case Types_1.WAMessageStubType.CHANGE_EPHEMERAL_SETTING:
733
+ const ephValue = message.messageStubParameters?.[0];
734
+ emitGroupUpdate({ ephemeralDuration: ephValue ? +ephValue : 0 });
735
+ break;
736
+ case Types_1.WAMessageStubType.DISAPPEARING_MODE:
737
+ const disappearingValue = message.messageStubParameters?.[0];
738
+ emitGroupUpdate({ ephemeralDuration: disappearingValue ? +disappearingValue : 0 });
739
+ break;
740
+ case Types_1.WAMessageStubType.ADMIN_REVOKE:
741
+ emitGroupUpdate({ adminRevoked: true, revokedBy: message.messageStubParameters?.[0] });
742
+ break;
743
+ case Types_1.WAMessageStubType.BLOCK_CONTACT:
744
+ ev.emit('contacts.update', [{ id: jid, isBlocked: true }]);
745
+ break;
746
+ case Types_1.WAMessageStubType.CHANGE_USERNAME:
747
+ ev.emit('contacts.update', [
748
+ { id: message.key.participant || jid, username: message.messageStubParameters?.[0] }
749
+ ]);
750
+ break;
751
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ACCEPT:
752
+ participants =
753
+ message.messageStubParameters?.map(a => {
754
+ try {
755
+ return JSON.parse(a);
756
+ }
757
+ catch {
758
+ return a;
759
+ }
760
+ }) || [];
761
+ emitParticipantsUpdate('accept');
762
+ break;
763
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LINKED_GROUP_JOIN:
764
+ participants =
765
+ message.messageStubParameters?.map(a => {
766
+ try {
767
+ return JSON.parse(a);
768
+ }
769
+ catch {
770
+ return a;
771
+ }
772
+ }) || [];
773
+ emitParticipantsUpdate('linked-group-join');
774
+ break;
775
+ case Types_1.WAMessageStubType.EPHEMERAL_KEEP_IN_CHAT:
776
+ ev.emit('messages.update', [{ key: message.key, update: { keepInChat: true } }]);
777
+ break;
778
+ case Types_1.WAMessageStubType.PINNED_MESSAGE_IN_CHAT:
779
+ ev.emit('messages.update', [
780
+ {
781
+ key: message.key,
782
+ update: {
783
+ pinInChat: {
784
+ pinned: true,
785
+ pinnedMessageKey: message.messageStubParameters?.[0]
786
+ ? { id: message.messageStubParameters[0] }
787
+ : undefined,
788
+ senderTimestampMs: message.messageTimestamp
789
+ ? (0, generics_1.toNumber)(message.messageTimestamp) * 1000
790
+ : undefined
791
+ }
792
+ }
793
+ }
794
+ ]);
795
+ break;
796
+ case Types_1.WAMessageStubType.CHAT_PSA:
797
+ ev.emit('chats.update', [{ id: jid, psa: { urlParams: message.messageStubParameters } }]);
798
+ break;
799
+ case Types_1.WAMessageStubType.CHAT_POLL_CREATION_MESSAGE:
800
+ break;
801
+ case Types_1.WAMessageStubType.BIZ_CHAT_ASSIGNMENT:
802
+ ev.emit('chats.update', [
803
+ {
804
+ id: jid,
805
+ bizAssignment: {
806
+ assigned: true,
807
+ assignee: message.messageStubParameters?.[0],
808
+ agent: message.messageStubParameters?.[1]
809
+ }
810
+ }
811
+ ]);
812
+ break;
813
+ case Types_1.WAMessageStubType.BIZ_CHAT_ASSIGNMENT_UNASSIGN:
814
+ ev.emit('chats.update', [{ id: jid, bizAssignment: { assigned: false } }]);
815
+ break;
816
+ case Types_1.WAMessageStubType.SILENCED_UNKNOWN_CALLER_AUDIO:
817
+ case Types_1.WAMessageStubType.SILENCED_UNKNOWN_CALLER_VIDEO:
818
+ ev.emit('call', [
819
+ {
820
+ id: message.messageStubParameters?.[0] || message.key.id,
821
+ from: jid,
822
+ isVideo: message.messageStubType === Types_1.WAMessageStubType.SILENCED_UNKNOWN_CALLER_VIDEO,
823
+ status: 'silenced',
824
+ isUnknown: true,
825
+ timestamp: (0, generics_1.toNumber)(message.messageTimestamp)
826
+ }
827
+ ]);
828
+ break;
829
+ case Types_1.WAMessageStubType.SCHEDULED_CALL_START_MESSAGE:
830
+ ev.emit('call', [
831
+ {
832
+ id: message.messageStubParameters?.[0] || message.key.id,
833
+ from: message.key.participant || jid,
834
+ status: 'scheduled-start',
835
+ timestamp: (0, generics_1.toNumber)(message.messageTimestamp)
836
+ }
837
+ ]);
838
+ break;
839
+ case Types_1.WAMessageStubType.SCHEDULED_CALL_CANCEL:
840
+ ev.emit('call', [
841
+ {
842
+ id: message.messageStubParameters?.[0] || message.key.id,
843
+ from: message.key.participant || jid,
844
+ status: 'schedule-cancelled',
845
+ timestamp: (0, generics_1.toNumber)(message.messageTimestamp)
846
+ }
847
+ ]);
848
+ break;
849
+ case Types_1.WAMessageStubType.COMMUNITY_CREATE:
850
+ emitGroupUpdate({ isCommunity: true, subject: message.messageStubParameters?.[0] });
851
+ break;
852
+ case Types_1.WAMessageStubType.COMMUNITY_LINK_PARENT_GROUP:
853
+ case Types_1.WAMessageStubType.COMMUNITY_LINK_PARENT_GROUP_RICH:
854
+ emitGroupUpdate({ linkedParent: message.messageStubParameters?.[0] });
855
+ break;
856
+ case Types_1.WAMessageStubType.COMMUNITY_LINK_SUB_GROUP:
857
+ case Types_1.WAMessageStubType.COMMUNITY_LINK_SIBLING_GROUP:
858
+ emitGroupUpdate({ linkedSubGroup: message.messageStubParameters?.[0] });
859
+ break;
860
+ case Types_1.WAMessageStubType.COMMUNITY_UNLINK_PARENT_GROUP:
861
+ emitGroupUpdate({ linkedParent: undefined });
862
+ break;
863
+ case Types_1.WAMessageStubType.COMMUNITY_UNLINK_SUB_GROUP:
864
+ case Types_1.WAMessageStubType.COMMUNITY_UNLINK_SIBLING_GROUP:
865
+ emitGroupUpdate({ unlinkedSubGroup: message.messageStubParameters?.[0] });
866
+ break;
867
+ case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_PROMOTE:
868
+ participants =
869
+ message.messageStubParameters?.map(a => {
870
+ try {
871
+ return JSON.parse(a);
872
+ }
873
+ catch {
874
+ return a;
875
+ }
876
+ }) || [];
877
+ emitParticipantsUpdate('promote');
878
+ break;
879
+ case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_DEMOTE:
880
+ participants =
881
+ message.messageStubParameters?.map(a => {
882
+ try {
883
+ return JSON.parse(a);
884
+ }
885
+ catch {
886
+ return a;
887
+ }
888
+ }) || [];
889
+ emitParticipantsUpdate('demote');
890
+ break;
891
+ case Types_1.WAMessageStubType.COMMUNITY_PARENT_GROUP_DELETED:
892
+ emitGroupUpdate({ parentDeleted: true, readOnly: true });
893
+ break;
894
+ case Types_1.WAMessageStubType.COMMUNITY_PARENT_GROUP_SUBJECT_CHANGED:
895
+ emitGroupUpdate({ parentSubject: message.messageStubParameters?.[0] });
896
+ break;
897
+ case Types_1.WAMessageStubType.COMMUNITY_LINK_PARENT_GROUP_MEMBERSHIP_APPROVAL:
898
+ emitGroupUpdate({ linkedParentMembershipApproval: message.messageStubParameters?.[0] === 'true' });
899
+ break;
900
+ case Types_1.WAMessageStubType.COMMUNITY_INVITE_RICH:
901
+ case Types_1.WAMessageStubType.COMMUNITY_INVITE_AUTO_ADD_RICH:
902
+ participants =
903
+ message.messageStubParameters?.map(a => {
904
+ try {
905
+ return JSON.parse(a);
906
+ }
907
+ catch {
908
+ return a;
909
+ }
910
+ }) || [];
911
+ emitParticipantsUpdate('add');
912
+ break;
913
+ case Types_1.WAMessageStubType.COMMUNITY_PARTICIPANT_ADD_RICH:
914
+ participants =
915
+ message.messageStubParameters?.map(a => {
916
+ try {
917
+ return JSON.parse(a);
918
+ }
919
+ catch {
920
+ return a;
921
+ }
922
+ }) || [];
923
+ emitParticipantsUpdate('add');
924
+ break;
925
+ case Types_1.WAMessageStubType.COMMUNITY_CHANGE_DESCRIPTION:
926
+ const communityDesc = message.messageStubParameters?.[0];
927
+ emitGroupUpdate({ desc: communityDesc });
928
+ break;
929
+ case Types_1.WAMessageStubType.COMMUNITY_ALLOW_MEMBER_ADDED_GROUPS:
930
+ emitGroupUpdate({ communityMemberAddGroupMode: message.messageStubParameters?.[0] });
931
+ break;
932
+ case Types_1.WAMessageStubType.EMPTY_SUBGROUP_CREATE:
933
+ emitGroupUpdate({ subject: message.messageStubParameters?.[0], isEmpty: true });
934
+ break;
935
+ case Types_1.WAMessageStubType.COMMUNITY_DEACTIVATE_SIBLING_GROUP:
936
+ emitGroupUpdate({ deactivated: true, linkedSibling: message.messageStubParameters?.[0] });
937
+ break;
938
+ case Types_1.WAMessageStubType.COMMUNITY_OWNER_UPDATED:
939
+ emitGroupUpdate({ owner: message.messageStubParameters?.[0] });
940
+ break;
941
+ case Types_1.WAMessageStubType.COMMUNITY_SUB_GROUP_VISIBILITY_HIDDEN:
942
+ emitGroupUpdate({ hidden: true });
943
+ break;
944
+ }
945
+ }
946
+ if (Object.keys(chat).length > 1) {
947
+ ev.emit('chats.update', [chat]);
948
+ }
949
+ };
950
+ exports.default = processMessage;