@queenanya/baileys 6.7.0 → 6.8.0

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 (129) hide show
  1. package/WASignalGroup/readme.md +6 -0
  2. package/lib/Defaults/baileys-version.json +3 -3
  3. package/lib/Defaults/index.d.ts +284 -284
  4. package/lib/Defaults/index.js +120 -120
  5. package/lib/Defaults/phonenumber-mcc.json +223 -223
  6. package/lib/Signal/libsignal.d.ts +3 -3
  7. package/lib/Signal/libsignal.js +152 -152
  8. package/lib/Socket/Client/abstract-socket-client.d.ts +17 -17
  9. package/lib/Socket/Client/abstract-socket-client.js +13 -13
  10. package/lib/Socket/Client/index.d.ts +3 -3
  11. package/lib/Socket/Client/index.js +19 -19
  12. package/lib/Socket/Client/mobile-socket-client.d.ts +13 -13
  13. package/lib/Socket/Client/mobile-socket-client.js +65 -65
  14. package/lib/Socket/Client/web-socket-client.d.ts +12 -12
  15. package/lib/Socket/Client/web-socket-client.js +62 -62
  16. package/lib/Socket/business.d.ts +135 -135
  17. package/lib/Socket/business.js +259 -259
  18. package/lib/Socket/chats.d.ts +79 -79
  19. package/lib/Socket/chats.js +854 -854
  20. package/lib/Socket/groups.d.ts +113 -113
  21. package/lib/Socket/groups.js +302 -302
  22. package/lib/Socket/index.d.ts +137 -137
  23. package/lib/Socket/index.js +10 -10
  24. package/lib/Socket/messages-recv.d.ts +124 -124
  25. package/lib/Socket/messages-recv.js +747 -747
  26. package/lib/Socket/messages-send.d.ts +119 -119
  27. package/lib/Socket/messages-send.js +663 -663
  28. package/lib/Socket/registration.d.ts +232 -232
  29. package/lib/Socket/registration.js +166 -166
  30. package/lib/Socket/socket.d.ts +42 -42
  31. package/lib/Socket/socket.js +588 -588
  32. package/lib/Store/index.d.ts +3 -3
  33. package/lib/Store/index.js +10 -10
  34. package/lib/Store/make-cache-manager-store.d.ts +13 -13
  35. package/lib/Store/make-cache-manager-store.js +83 -83
  36. package/lib/Store/make-in-memory-store.d.ts +117 -117
  37. package/lib/Store/make-in-memory-store.js +437 -437
  38. package/lib/Store/make-ordered-dictionary.d.ts +13 -13
  39. package/lib/Store/make-ordered-dictionary.js +81 -81
  40. package/lib/Store/object-repository.d.ts +10 -10
  41. package/lib/Store/object-repository.js +27 -27
  42. package/lib/Types/Auth.d.ts +108 -108
  43. package/lib/Types/Auth.js +2 -2
  44. package/lib/Types/Call.d.ts +13 -13
  45. package/lib/Types/Call.js +2 -2
  46. package/lib/Types/Chat.d.ts +102 -102
  47. package/lib/Types/Chat.js +4 -4
  48. package/lib/Types/Contact.d.ts +19 -19
  49. package/lib/Types/Contact.js +2 -2
  50. package/lib/Types/Events.d.ts +157 -157
  51. package/lib/Types/Events.js +2 -2
  52. package/lib/Types/GroupMetadata.d.ts +52 -52
  53. package/lib/Types/GroupMetadata.js +2 -2
  54. package/lib/Types/Label.d.ts +35 -35
  55. package/lib/Types/Label.js +27 -27
  56. package/lib/Types/LabelAssociation.d.ts +29 -29
  57. package/lib/Types/LabelAssociation.js +9 -9
  58. package/lib/Types/Message.d.ts +261 -261
  59. package/lib/Types/Message.js +9 -9
  60. package/lib/Types/Product.d.ts +78 -78
  61. package/lib/Types/Product.js +2 -2
  62. package/lib/Types/Signal.d.ts +57 -57
  63. package/lib/Types/Signal.js +2 -2
  64. package/lib/Types/Socket.d.ts +111 -111
  65. package/lib/Types/Socket.js +2 -2
  66. package/lib/Types/State.d.ts +27 -27
  67. package/lib/Types/State.js +2 -2
  68. package/lib/Types/index.d.ts +56 -56
  69. package/lib/Types/index.js +41 -41
  70. package/lib/Utils/auth-utils.d.ts +18 -18
  71. package/lib/Utils/auth-utils.js +204 -204
  72. package/lib/Utils/baileys-event-stream.d.ts +16 -16
  73. package/lib/Utils/baileys-event-stream.js +63 -63
  74. package/lib/Utils/business.d.ts +22 -22
  75. package/lib/Utils/business.js +234 -234
  76. package/lib/Utils/chat-utils.d.ts +71 -71
  77. package/lib/Utils/chat-utils.js +724 -724
  78. package/lib/Utils/crypto.d.ts +41 -41
  79. package/lib/Utils/crypto.js +151 -151
  80. package/lib/Utils/decode-wa-message.d.ts +19 -19
  81. package/lib/Utils/decode-wa-message.js +174 -174
  82. package/lib/Utils/event-buffer.d.ts +35 -35
  83. package/lib/Utils/event-buffer.js +514 -514
  84. package/lib/Utils/generics.d.ts +94 -94
  85. package/lib/Utils/generics.js +367 -367
  86. package/lib/Utils/history.d.ts +15 -15
  87. package/lib/Utils/history.js +91 -91
  88. package/lib/Utils/index.d.ts +17 -17
  89. package/lib/Utils/index.js +33 -33
  90. package/lib/Utils/link-preview.d.ts +21 -21
  91. package/lib/Utils/link-preview.js +93 -93
  92. package/lib/Utils/logger.d.ts +4 -4
  93. package/lib/Utils/logger.js +7 -7
  94. package/lib/Utils/lt-hash.d.ts +12 -12
  95. package/lib/Utils/lt-hash.js +51 -51
  96. package/lib/Utils/make-mutex.d.ts +7 -7
  97. package/lib/Utils/make-mutex.js +43 -43
  98. package/lib/Utils/messages-media.d.ts +107 -107
  99. package/lib/Utils/messages-media.js +680 -680
  100. package/lib/Utils/messages.d.ts +76 -76
  101. package/lib/Utils/messages.js +768 -768
  102. package/lib/Utils/noise-handler.d.ts +20 -20
  103. package/lib/Utils/noise-handler.js +142 -142
  104. package/lib/Utils/process-message.d.ts +41 -41
  105. package/lib/Utils/process-message.js +320 -320
  106. package/lib/Utils/signal.d.ts +32 -32
  107. package/lib/Utils/signal.js +151 -151
  108. package/lib/Utils/use-multi-file-auth-state.d.ts +12 -12
  109. package/lib/Utils/use-multi-file-auth-state.js +80 -80
  110. package/lib/Utils/validate-connection.d.ts +11 -11
  111. package/lib/Utils/validate-connection.js +191 -222
  112. package/lib/WABinary/constants.d.ts +27 -27
  113. package/lib/WABinary/constants.js +40 -40
  114. package/lib/WABinary/decode.d.ts +7 -7
  115. package/lib/WABinary/decode.js +252 -252
  116. package/lib/WABinary/encode.d.ts +3 -3
  117. package/lib/WABinary/encode.js +228 -228
  118. package/lib/WABinary/generic-utils.d.ts +15 -15
  119. package/lib/WABinary/generic-utils.js +110 -110
  120. package/lib/WABinary/index.d.ts +5 -5
  121. package/lib/WABinary/index.js +21 -21
  122. package/lib/WABinary/jid-utils.d.ts +29 -29
  123. package/lib/WABinary/jid-utils.js +59 -59
  124. package/lib/WABinary/types.d.ts +18 -18
  125. package/lib/WABinary/types.js +2 -2
  126. package/lib/index.d.ts +10 -10
  127. package/lib/index.js +29 -29
  128. package/package.json +1 -1
  129. package/CHANGELOG.md +0 -4
@@ -1,320 +1,320 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decryptPollVote = exports.getChatId = exports.shouldIncrementChatUnread = exports.isRealMessage = exports.cleanMessage = void 0;
4
- const WAProto_1 = require("../../WAProto");
5
- const Types_1 = require("../Types");
6
- const messages_1 = require("../Utils/messages");
7
- const WABinary_1 = require("../WABinary");
8
- const crypto_1 = require("./crypto");
9
- const generics_1 = require("./generics");
10
- const history_1 = require("./history");
11
- const REAL_MSG_STUB_TYPES = new Set([
12
- Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
13
- Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE,
14
- Types_1.WAMessageStubType.CALL_MISSED_VIDEO,
15
- Types_1.WAMessageStubType.CALL_MISSED_VOICE
16
- ]);
17
- const REAL_MSG_REQ_ME_STUB_TYPES = new Set([
18
- Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD
19
- ]);
20
- /** Cleans a received message to further processing */
21
- const cleanMessage = (message, meId) => {
22
- // ensure remoteJid and participant doesn't have device or agent in it
23
- message.key.remoteJid = (0, WABinary_1.jidNormalizedUser)(message.key.remoteJid);
24
- message.key.participant = message.key.participant ? (0, WABinary_1.jidNormalizedUser)(message.key.participant) : undefined;
25
- const content = (0, messages_1.normalizeMessageContent)(message.message);
26
- // if the message has a reaction, ensure fromMe & remoteJid are from our perspective
27
- if (content === null || content === void 0 ? void 0 : content.reactionMessage) {
28
- normaliseKey(content.reactionMessage.key);
29
- }
30
- if (content === null || content === void 0 ? void 0 : content.pollUpdateMessage) {
31
- normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
32
- }
33
- function normaliseKey(msgKey) {
34
- // if the reaction is from another user
35
- // we've to correctly map the key to this user's perspective
36
- if (!message.key.fromMe) {
37
- // if the sender believed the message being reacted to is not from them
38
- // we've to correct the key to be from them, or some other participant
39
- msgKey.fromMe = !msgKey.fromMe
40
- ? (0, WABinary_1.areJidsSameUser)(msgKey.participant || msgKey.remoteJid, meId)
41
- // if the message being reacted to, was from them
42
- // fromMe automatically becomes false
43
- : false;
44
- // set the remoteJid to being the same as the chat the message came from
45
- msgKey.remoteJid = message.key.remoteJid;
46
- // set participant of the message
47
- msgKey.participant = msgKey.participant || message.key.participant;
48
- }
49
- }
50
- };
51
- exports.cleanMessage = cleanMessage;
52
- const isRealMessage = (message, meId) => {
53
- var _a;
54
- const normalizedContent = (0, messages_1.normalizeMessageContent)(message.message);
55
- const hasSomeContent = !!(0, messages_1.getContentType)(normalizedContent);
56
- return (!!normalizedContent
57
- || REAL_MSG_STUB_TYPES.has(message.messageStubType)
58
- || (REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)
59
- && ((_a = message.messageStubParameters) === null || _a === void 0 ? void 0 : _a.some(p => (0, WABinary_1.areJidsSameUser)(meId, p)))))
60
- && hasSomeContent
61
- && !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.protocolMessage)
62
- && !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.reactionMessage)
63
- && !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.pollUpdateMessage);
64
- };
65
- exports.isRealMessage = isRealMessage;
66
- const shouldIncrementChatUnread = (message) => (!message.key.fromMe && !message.messageStubType);
67
- exports.shouldIncrementChatUnread = shouldIncrementChatUnread;
68
- /**
69
- * Get the ID of the chat from the given key.
70
- * Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant
71
- */
72
- const getChatId = ({ remoteJid, participant, fromMe }) => {
73
- if ((0, WABinary_1.isJidBroadcast)(remoteJid)
74
- && !(0, WABinary_1.isJidStatusBroadcast)(remoteJid)
75
- && !fromMe) {
76
- return participant;
77
- }
78
- return remoteJid;
79
- };
80
- exports.getChatId = getChatId;
81
- /**
82
- * Decrypt a poll vote
83
- * @param vote encrypted vote
84
- * @param ctx additional info about the poll required for decryption
85
- * @returns list of SHA256 options
86
- */
87
- function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid, }) {
88
- const sign = Buffer.concat([
89
- toBinary(pollMsgId),
90
- toBinary(pollCreatorJid),
91
- toBinary(voterJid),
92
- toBinary('Poll Vote'),
93
- new Uint8Array([1])
94
- ]);
95
- const key0 = (0, crypto_1.hmacSign)(pollEncKey, new Uint8Array(32), 'sha256');
96
- const decKey = (0, crypto_1.hmacSign)(sign, key0, 'sha256');
97
- const aad = toBinary(`${pollMsgId}\u0000${voterJid}`);
98
- const decrypted = (0, crypto_1.aesDecryptGCM)(encPayload, decKey, encIv, aad);
99
- return WAProto_1.proto.Message.PollVoteMessage.decode(decrypted);
100
- function toBinary(txt) {
101
- return Buffer.from(txt);
102
- }
103
- }
104
- exports.decryptPollVote = decryptPollVote;
105
- const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, keyStore, logger, options, getMessage }) => {
106
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
107
- const meId = creds.me.id;
108
- const { accountSettings } = creds;
109
- const chat = { id: (0, WABinary_1.jidNormalizedUser)((0, exports.getChatId)(message.key)) };
110
- const isRealMsg = (0, exports.isRealMessage)(message, meId);
111
- if (isRealMsg) {
112
- chat.conversationTimestamp = (0, generics_1.toNumber)(message.messageTimestamp);
113
- // only increment unread count if not CIPHERTEXT and from another person
114
- if ((0, exports.shouldIncrementChatUnread)(message)) {
115
- chat.unreadCount = (chat.unreadCount || 0) + 1;
116
- }
117
- }
118
- const content = (0, messages_1.normalizeMessageContent)(message.message);
119
- // unarchive chat if it's a real message, or someone reacted to our message
120
- // and we've the unarchive chats setting on
121
- if ((isRealMsg || ((_b = (_a = content === null || content === void 0 ? void 0 : content.reactionMessage) === null || _a === void 0 ? void 0 : _a.key) === null || _b === void 0 ? void 0 : _b.fromMe))
122
- && (accountSettings === null || accountSettings === void 0 ? void 0 : accountSettings.unarchiveChats)) {
123
- chat.archived = false;
124
- chat.readOnly = false;
125
- }
126
- const protocolMsg = content === null || content === void 0 ? void 0 : content.protocolMessage;
127
- if (protocolMsg) {
128
- switch (protocolMsg.type) {
129
- case WAProto_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
130
- const histNotification = protocolMsg.historySyncNotification;
131
- const process = shouldProcessHistoryMsg;
132
- const isLatest = !((_c = creds.processedHistoryMessages) === null || _c === void 0 ? void 0 : _c.length);
133
- logger === null || logger === void 0 ? void 0 : logger.info({
134
- histNotification,
135
- process,
136
- id: message.key.id,
137
- isLatest,
138
- }, 'got history notification');
139
- if (process) {
140
- ev.emit('creds.update', {
141
- processedHistoryMessages: [
142
- ...(creds.processedHistoryMessages || []),
143
- { key: message.key, messageTimestamp: message.messageTimestamp }
144
- ]
145
- });
146
- const data = await (0, history_1.downloadAndProcessHistorySyncNotification)(histNotification, options);
147
- ev.emit('messaging-history.set', { ...data, isLatest });
148
- }
149
- break;
150
- case WAProto_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
151
- const keys = protocolMsg.appStateSyncKeyShare.keys;
152
- if (keys === null || keys === void 0 ? void 0 : keys.length) {
153
- let newAppStateSyncKeyId = '';
154
- await keyStore.transaction(async () => {
155
- const newKeys = [];
156
- for (const { keyData, keyId } of keys) {
157
- const strKeyId = Buffer.from(keyId.keyId).toString('base64');
158
- newKeys.push(strKeyId);
159
- await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } });
160
- newAppStateSyncKeyId = strKeyId;
161
- }
162
- logger === null || logger === void 0 ? void 0 : logger.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys');
163
- });
164
- ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId });
165
- }
166
- else {
167
- logger === null || logger === void 0 ? void 0 : logger.info({ protocolMsg }, 'recv app state sync with 0 keys');
168
- }
169
- break;
170
- case WAProto_1.proto.Message.ProtocolMessage.Type.REVOKE:
171
- ev.emit('messages.update', [
172
- {
173
- key: {
174
- ...message.key,
175
- id: protocolMsg.key.id
176
- },
177
- update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }
178
- }
179
- ]);
180
- break;
181
- case WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
182
- Object.assign(chat, {
183
- ephemeralSettingTimestamp: (0, generics_1.toNumber)(message.messageTimestamp),
184
- ephemeralExpiration: protocolMsg.ephemeralExpiration || null
185
- });
186
- break;
187
- case WAProto_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
188
- const response = protocolMsg.peerDataOperationRequestResponseMessage;
189
- if (response) {
190
- const { peerDataOperationResult } = response;
191
- for (const result of peerDataOperationResult) {
192
- const { placeholderMessageResendResponse: retryResponse } = result;
193
- if (retryResponse) {
194
- const webMessageInfo = WAProto_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes);
195
- ev.emit('messages.update', [
196
- { key: webMessageInfo.key, update: { message: webMessageInfo.message } }
197
- ]);
198
- }
199
- }
200
- }
201
- break;
202
- }
203
- }
204
- else if (content === null || content === void 0 ? void 0 : content.reactionMessage) {
205
- const reaction = {
206
- ...content.reactionMessage,
207
- key: message.key,
208
- };
209
- ev.emit('messages.reaction', [{
210
- reaction,
211
- key: content.reactionMessage.key,
212
- }]);
213
- }
214
- else if (message.messageStubType) {
215
- const jid = message.key.remoteJid;
216
- //let actor = whatsappID (message.participant)
217
- let participants;
218
- const emitParticipantsUpdate = (action) => (ev.emit('group-participants.update', { id: jid, author: message.participant, participants, action }));
219
- const emitGroupUpdate = (update) => {
220
- ev.emit('groups.update', [{ id: jid, ...update }]);
221
- };
222
- const participantsIncludesMe = () => participants.find(jid => (0, WABinary_1.areJidsSameUser)(meId, jid));
223
- switch (message.messageStubType) {
224
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
225
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
226
- participants = message.messageStubParameters || [];
227
- emitParticipantsUpdate('remove');
228
- // mark the chat read only if you left the group
229
- if (participantsIncludesMe()) {
230
- chat.readOnly = true;
231
- }
232
- break;
233
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:
234
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
235
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
236
- participants = message.messageStubParameters || [];
237
- if (participantsIncludesMe()) {
238
- chat.readOnly = false;
239
- }
240
- emitParticipantsUpdate('add');
241
- break;
242
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
243
- participants = message.messageStubParameters || [];
244
- emitParticipantsUpdate('demote');
245
- break;
246
- case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
247
- participants = message.messageStubParameters || [];
248
- emitParticipantsUpdate('promote');
249
- break;
250
- case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
251
- const announceValue = (_d = message.messageStubParameters) === null || _d === void 0 ? void 0 : _d[0];
252
- emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' });
253
- break;
254
- case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:
255
- const restrictValue = (_e = message.messageStubParameters) === null || _e === void 0 ? void 0 : _e[0];
256
- emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' });
257
- break;
258
- case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:
259
- const name = (_f = message.messageStubParameters) === null || _f === void 0 ? void 0 : _f[0];
260
- chat.name = name;
261
- emitGroupUpdate({ subject: name });
262
- break;
263
- case Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
264
- const code = (_g = message.messageStubParameters) === null || _g === void 0 ? void 0 : _g[0];
265
- emitGroupUpdate({ inviteCode: code });
266
- break;
267
- case Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
268
- const memberAddValue = (_h = message.messageStubParameters) === null || _h === void 0 ? void 0 : _h[0];
269
- emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' });
270
- break;
271
- case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
272
- const approvalMode = (_j = message.messageStubParameters) === null || _j === void 0 ? void 0 : _j[0];
273
- emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' });
274
- break;
275
- }
276
- }
277
- else if (content === null || content === void 0 ? void 0 : content.pollUpdateMessage) {
278
- const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey;
279
- // we need to fetch the poll creation message to get the poll enc key
280
- const pollMsg = await getMessage(creationMsgKey);
281
- if (pollMsg) {
282
- const meIdNormalised = (0, WABinary_1.jidNormalizedUser)(meId);
283
- const pollCreatorJid = (0, generics_1.getKeyAuthor)(creationMsgKey, meIdNormalised);
284
- const voterJid = (0, generics_1.getKeyAuthor)(message.key, meIdNormalised);
285
- const pollEncKey = (_k = pollMsg.messageContextInfo) === null || _k === void 0 ? void 0 : _k.messageSecret;
286
- try {
287
- const voteMsg = decryptPollVote(content.pollUpdateMessage.vote, {
288
- pollEncKey,
289
- pollCreatorJid,
290
- pollMsgId: creationMsgKey.id,
291
- voterJid,
292
- });
293
- ev.emit('messages.update', [
294
- {
295
- key: creationMsgKey,
296
- update: {
297
- pollUpdates: [
298
- {
299
- pollUpdateMessageKey: message.key,
300
- vote: voteMsg,
301
- senderTimestampMs: content.pollUpdateMessage.senderTimestampMs.toNumber(),
302
- }
303
- ]
304
- }
305
- }
306
- ]);
307
- }
308
- catch (err) {
309
- logger === null || logger === void 0 ? void 0 : logger.warn({ err, creationMsgKey }, 'failed to decrypt poll vote');
310
- }
311
- }
312
- else {
313
- logger === null || logger === void 0 ? void 0 : logger.warn({ creationMsgKey }, 'poll creation message not found, cannot decrypt update');
314
- }
315
- }
316
- if (Object.keys(chat).length > 1) {
317
- ev.emit('chats.update', [chat]);
318
- }
319
- };
320
- exports.default = processMessage;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decryptPollVote = exports.getChatId = exports.shouldIncrementChatUnread = exports.isRealMessage = exports.cleanMessage = void 0;
4
+ const WAProto_1 = require("../../WAProto");
5
+ const Types_1 = require("../Types");
6
+ const messages_1 = require("../Utils/messages");
7
+ const WABinary_1 = require("../WABinary");
8
+ const crypto_1 = require("./crypto");
9
+ const generics_1 = require("./generics");
10
+ const history_1 = require("./history");
11
+ const REAL_MSG_STUB_TYPES = new Set([
12
+ Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
13
+ Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE,
14
+ Types_1.WAMessageStubType.CALL_MISSED_VIDEO,
15
+ Types_1.WAMessageStubType.CALL_MISSED_VOICE
16
+ ]);
17
+ const REAL_MSG_REQ_ME_STUB_TYPES = new Set([
18
+ Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD
19
+ ]);
20
+ /** Cleans a received message to further processing */
21
+ const cleanMessage = (message, meId) => {
22
+ // ensure remoteJid and participant doesn't have device or agent in it
23
+ message.key.remoteJid = (0, WABinary_1.jidNormalizedUser)(message.key.remoteJid);
24
+ message.key.participant = message.key.participant ? (0, WABinary_1.jidNormalizedUser)(message.key.participant) : undefined;
25
+ const content = (0, messages_1.normalizeMessageContent)(message.message);
26
+ // if the message has a reaction, ensure fromMe & remoteJid are from our perspective
27
+ if (content === null || content === void 0 ? void 0 : content.reactionMessage) {
28
+ normaliseKey(content.reactionMessage.key);
29
+ }
30
+ if (content === null || content === void 0 ? void 0 : content.pollUpdateMessage) {
31
+ normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
32
+ }
33
+ function normaliseKey(msgKey) {
34
+ // if the reaction is from another user
35
+ // we've to correctly map the key to this user's perspective
36
+ if (!message.key.fromMe) {
37
+ // if the sender believed the message being reacted to is not from them
38
+ // we've to correct the key to be from them, or some other participant
39
+ msgKey.fromMe = !msgKey.fromMe
40
+ ? (0, WABinary_1.areJidsSameUser)(msgKey.participant || msgKey.remoteJid, meId)
41
+ // if the message being reacted to, was from them
42
+ // fromMe automatically becomes false
43
+ : false;
44
+ // set the remoteJid to being the same as the chat the message came from
45
+ msgKey.remoteJid = message.key.remoteJid;
46
+ // set participant of the message
47
+ msgKey.participant = msgKey.participant || message.key.participant;
48
+ }
49
+ }
50
+ };
51
+ exports.cleanMessage = cleanMessage;
52
+ const isRealMessage = (message, meId) => {
53
+ var _a;
54
+ const normalizedContent = (0, messages_1.normalizeMessageContent)(message.message);
55
+ const hasSomeContent = !!(0, messages_1.getContentType)(normalizedContent);
56
+ return (!!normalizedContent
57
+ || REAL_MSG_STUB_TYPES.has(message.messageStubType)
58
+ || (REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)
59
+ && ((_a = message.messageStubParameters) === null || _a === void 0 ? void 0 : _a.some(p => (0, WABinary_1.areJidsSameUser)(meId, p)))))
60
+ && hasSomeContent
61
+ && !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.protocolMessage)
62
+ && !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.reactionMessage)
63
+ && !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.pollUpdateMessage);
64
+ };
65
+ exports.isRealMessage = isRealMessage;
66
+ const shouldIncrementChatUnread = (message) => (!message.key.fromMe && !message.messageStubType);
67
+ exports.shouldIncrementChatUnread = shouldIncrementChatUnread;
68
+ /**
69
+ * Get the ID of the chat from the given key.
70
+ * Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant
71
+ */
72
+ const getChatId = ({ remoteJid, participant, fromMe }) => {
73
+ if ((0, WABinary_1.isJidBroadcast)(remoteJid)
74
+ && !(0, WABinary_1.isJidStatusBroadcast)(remoteJid)
75
+ && !fromMe) {
76
+ return participant;
77
+ }
78
+ return remoteJid;
79
+ };
80
+ exports.getChatId = getChatId;
81
+ /**
82
+ * Decrypt a poll vote
83
+ * @param vote encrypted vote
84
+ * @param ctx additional info about the poll required for decryption
85
+ * @returns list of SHA256 options
86
+ */
87
+ function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid, }) {
88
+ const sign = Buffer.concat([
89
+ toBinary(pollMsgId),
90
+ toBinary(pollCreatorJid),
91
+ toBinary(voterJid),
92
+ toBinary('Poll Vote'),
93
+ new Uint8Array([1])
94
+ ]);
95
+ const key0 = (0, crypto_1.hmacSign)(pollEncKey, new Uint8Array(32), 'sha256');
96
+ const decKey = (0, crypto_1.hmacSign)(sign, key0, 'sha256');
97
+ const aad = toBinary(`${pollMsgId}\u0000${voterJid}`);
98
+ const decrypted = (0, crypto_1.aesDecryptGCM)(encPayload, decKey, encIv, aad);
99
+ return WAProto_1.proto.Message.PollVoteMessage.decode(decrypted);
100
+ function toBinary(txt) {
101
+ return Buffer.from(txt);
102
+ }
103
+ }
104
+ exports.decryptPollVote = decryptPollVote;
105
+ const processMessage = async (message, { shouldProcessHistoryMsg, ev, creds, keyStore, logger, options, getMessage }) => {
106
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
107
+ const meId = creds.me.id;
108
+ const { accountSettings } = creds;
109
+ const chat = { id: (0, WABinary_1.jidNormalizedUser)((0, exports.getChatId)(message.key)) };
110
+ const isRealMsg = (0, exports.isRealMessage)(message, meId);
111
+ if (isRealMsg) {
112
+ chat.conversationTimestamp = (0, generics_1.toNumber)(message.messageTimestamp);
113
+ // only increment unread count if not CIPHERTEXT and from another person
114
+ if ((0, exports.shouldIncrementChatUnread)(message)) {
115
+ chat.unreadCount = (chat.unreadCount || 0) + 1;
116
+ }
117
+ }
118
+ const content = (0, messages_1.normalizeMessageContent)(message.message);
119
+ // unarchive chat if it's a real message, or someone reacted to our message
120
+ // and we've the unarchive chats setting on
121
+ if ((isRealMsg || ((_b = (_a = content === null || content === void 0 ? void 0 : content.reactionMessage) === null || _a === void 0 ? void 0 : _a.key) === null || _b === void 0 ? void 0 : _b.fromMe))
122
+ && (accountSettings === null || accountSettings === void 0 ? void 0 : accountSettings.unarchiveChats)) {
123
+ chat.archived = false;
124
+ chat.readOnly = false;
125
+ }
126
+ const protocolMsg = content === null || content === void 0 ? void 0 : content.protocolMessage;
127
+ if (protocolMsg) {
128
+ switch (protocolMsg.type) {
129
+ case WAProto_1.proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
130
+ const histNotification = protocolMsg.historySyncNotification;
131
+ const process = shouldProcessHistoryMsg;
132
+ const isLatest = !((_c = creds.processedHistoryMessages) === null || _c === void 0 ? void 0 : _c.length);
133
+ logger === null || logger === void 0 ? void 0 : logger.info({
134
+ histNotification,
135
+ process,
136
+ id: message.key.id,
137
+ isLatest,
138
+ }, 'got history notification');
139
+ if (process) {
140
+ ev.emit('creds.update', {
141
+ processedHistoryMessages: [
142
+ ...(creds.processedHistoryMessages || []),
143
+ { key: message.key, messageTimestamp: message.messageTimestamp }
144
+ ]
145
+ });
146
+ const data = await (0, history_1.downloadAndProcessHistorySyncNotification)(histNotification, options);
147
+ ev.emit('messaging-history.set', { ...data, isLatest });
148
+ }
149
+ break;
150
+ case WAProto_1.proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
151
+ const keys = protocolMsg.appStateSyncKeyShare.keys;
152
+ if (keys === null || keys === void 0 ? void 0 : keys.length) {
153
+ let newAppStateSyncKeyId = '';
154
+ await keyStore.transaction(async () => {
155
+ const newKeys = [];
156
+ for (const { keyData, keyId } of keys) {
157
+ const strKeyId = Buffer.from(keyId.keyId).toString('base64');
158
+ newKeys.push(strKeyId);
159
+ await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } });
160
+ newAppStateSyncKeyId = strKeyId;
161
+ }
162
+ logger === null || logger === void 0 ? void 0 : logger.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys');
163
+ });
164
+ ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId });
165
+ }
166
+ else {
167
+ logger === null || logger === void 0 ? void 0 : logger.info({ protocolMsg }, 'recv app state sync with 0 keys');
168
+ }
169
+ break;
170
+ case WAProto_1.proto.Message.ProtocolMessage.Type.REVOKE:
171
+ ev.emit('messages.update', [
172
+ {
173
+ key: {
174
+ ...message.key,
175
+ id: protocolMsg.key.id
176
+ },
177
+ update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }
178
+ }
179
+ ]);
180
+ break;
181
+ case WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
182
+ Object.assign(chat, {
183
+ ephemeralSettingTimestamp: (0, generics_1.toNumber)(message.messageTimestamp),
184
+ ephemeralExpiration: protocolMsg.ephemeralExpiration || null
185
+ });
186
+ break;
187
+ case WAProto_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
188
+ const response = protocolMsg.peerDataOperationRequestResponseMessage;
189
+ if (response) {
190
+ const { peerDataOperationResult } = response;
191
+ for (const result of peerDataOperationResult) {
192
+ const { placeholderMessageResendResponse: retryResponse } = result;
193
+ if (retryResponse) {
194
+ const webMessageInfo = WAProto_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes);
195
+ ev.emit('messages.update', [
196
+ { key: webMessageInfo.key, update: { message: webMessageInfo.message } }
197
+ ]);
198
+ }
199
+ }
200
+ }
201
+ break;
202
+ }
203
+ }
204
+ else if (content === null || content === void 0 ? void 0 : content.reactionMessage) {
205
+ const reaction = {
206
+ ...content.reactionMessage,
207
+ key: message.key,
208
+ };
209
+ ev.emit('messages.reaction', [{
210
+ reaction,
211
+ key: content.reactionMessage.key,
212
+ }]);
213
+ }
214
+ else if (message.messageStubType) {
215
+ const jid = message.key.remoteJid;
216
+ //let actor = whatsappID (message.participant)
217
+ let participants;
218
+ const emitParticipantsUpdate = (action) => (ev.emit('group-participants.update', { id: jid, author: message.participant, participants, action }));
219
+ const emitGroupUpdate = (update) => {
220
+ ev.emit('groups.update', [{ id: jid, ...update }]);
221
+ };
222
+ const participantsIncludesMe = () => participants.find(jid => (0, WABinary_1.areJidsSameUser)(meId, jid));
223
+ switch (message.messageStubType) {
224
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
225
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
226
+ participants = message.messageStubParameters || [];
227
+ emitParticipantsUpdate('remove');
228
+ // mark the chat read only if you left the group
229
+ if (participantsIncludesMe()) {
230
+ chat.readOnly = true;
231
+ }
232
+ break;
233
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:
234
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
235
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
236
+ participants = message.messageStubParameters || [];
237
+ if (participantsIncludesMe()) {
238
+ chat.readOnly = false;
239
+ }
240
+ emitParticipantsUpdate('add');
241
+ break;
242
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
243
+ participants = message.messageStubParameters || [];
244
+ emitParticipantsUpdate('demote');
245
+ break;
246
+ case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
247
+ participants = message.messageStubParameters || [];
248
+ emitParticipantsUpdate('promote');
249
+ break;
250
+ case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
251
+ const announceValue = (_d = message.messageStubParameters) === null || _d === void 0 ? void 0 : _d[0];
252
+ emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' });
253
+ break;
254
+ case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:
255
+ const restrictValue = (_e = message.messageStubParameters) === null || _e === void 0 ? void 0 : _e[0];
256
+ emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' });
257
+ break;
258
+ case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:
259
+ const name = (_f = message.messageStubParameters) === null || _f === void 0 ? void 0 : _f[0];
260
+ chat.name = name;
261
+ emitGroupUpdate({ subject: name });
262
+ break;
263
+ case Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
264
+ const code = (_g = message.messageStubParameters) === null || _g === void 0 ? void 0 : _g[0];
265
+ emitGroupUpdate({ inviteCode: code });
266
+ break;
267
+ case Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
268
+ const memberAddValue = (_h = message.messageStubParameters) === null || _h === void 0 ? void 0 : _h[0];
269
+ emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' });
270
+ break;
271
+ case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
272
+ const approvalMode = (_j = message.messageStubParameters) === null || _j === void 0 ? void 0 : _j[0];
273
+ emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' });
274
+ break;
275
+ }
276
+ }
277
+ else if (content === null || content === void 0 ? void 0 : content.pollUpdateMessage) {
278
+ const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey;
279
+ // we need to fetch the poll creation message to get the poll enc key
280
+ const pollMsg = await getMessage(creationMsgKey);
281
+ if (pollMsg) {
282
+ const meIdNormalised = (0, WABinary_1.jidNormalizedUser)(meId);
283
+ const pollCreatorJid = (0, generics_1.getKeyAuthor)(creationMsgKey, meIdNormalised);
284
+ const voterJid = (0, generics_1.getKeyAuthor)(message.key, meIdNormalised);
285
+ const pollEncKey = (_k = pollMsg.messageContextInfo) === null || _k === void 0 ? void 0 : _k.messageSecret;
286
+ try {
287
+ const voteMsg = decryptPollVote(content.pollUpdateMessage.vote, {
288
+ pollEncKey,
289
+ pollCreatorJid,
290
+ pollMsgId: creationMsgKey.id,
291
+ voterJid,
292
+ });
293
+ ev.emit('messages.update', [
294
+ {
295
+ key: creationMsgKey,
296
+ update: {
297
+ pollUpdates: [
298
+ {
299
+ pollUpdateMessageKey: message.key,
300
+ vote: voteMsg,
301
+ senderTimestampMs: content.pollUpdateMessage.senderTimestampMs.toNumber(),
302
+ }
303
+ ]
304
+ }
305
+ }
306
+ ]);
307
+ }
308
+ catch (err) {
309
+ logger === null || logger === void 0 ? void 0 : logger.warn({ err, creationMsgKey }, 'failed to decrypt poll vote');
310
+ }
311
+ }
312
+ else {
313
+ logger === null || logger === void 0 ? void 0 : logger.warn({ creationMsgKey }, 'poll creation message not found, cannot decrypt update');
314
+ }
315
+ }
316
+ if (Object.keys(chat).length > 1) {
317
+ ev.emit('chats.update', [chat]);
318
+ }
319
+ };
320
+ exports.default = processMessage;