@baileys-md/baileys 11.2.4 → 12.0.1
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.
- package/LICENSE +1 -1
- package/WAProto/WAProto.proto +5311 -0
- package/WAProto/index.js +70084 -131686
- package/lib/Defaults/index.js +118 -117
- package/lib/KeyDB/BinarySearch.js +20 -0
- package/lib/KeyDB/KeyedDB.js +167 -0
- package/lib/KeyDB/index.js +4 -0
- package/lib/Signal/Group/ciphertext-message.js +13 -0
- package/lib/Signal/Group/group-session-builder.js +32 -0
- package/lib/Signal/Group/group_cipher.js +84 -0
- package/lib/Signal/Group/index.js +13 -0
- package/lib/Signal/Group/keyhelper.js +20 -0
- package/lib/Signal/Group/sender-chain-key.js +28 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +65 -0
- package/lib/Signal/Group/sender-key-message.js +68 -0
- package/{WASignalGroup/sender_key_name.js → lib/Signal/Group/sender-key-name.js} +10 -28
- package/lib/Signal/Group/sender-key-record.js +43 -0
- package/lib/Signal/Group/sender-key-state.js +86 -0
- package/lib/Signal/Group/sender-message-key.js +28 -0
- package/lib/Signal/libsignal.js +314 -151
- package/lib/Signal/lid-mapping.js +155 -0
- package/lib/Socket/Client/index.js +4 -19
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +52 -0
- package/lib/Socket/business.js +359 -242
- package/lib/Socket/chats.js +847 -844
- package/lib/Socket/communities.js +413 -0
- package/lib/Socket/groups.js +304 -319
- package/lib/Socket/index.js +15 -9
- package/lib/Socket/messages-recv.js +1109 -989
- package/lib/Socket/messages-send.js +611 -347
- package/lib/Socket/mex.js +45 -0
- package/lib/Socket/newsletter.js +230 -231
- package/lib/Socket/socket.js +795 -616
- package/lib/Store/index.js +6 -8
- package/lib/Store/make-cache-manager-store.js +73 -81
- package/lib/Store/make-in-memory-store.js +286 -427
- package/lib/Store/make-ordered-dictionary.js +77 -79
- package/lib/Store/object-repository.js +24 -26
- package/lib/Types/Auth.js +3 -2
- package/lib/Types/Bussines.js +3 -0
- package/lib/Types/Call.js +3 -2
- package/lib/Types/Chat.js +9 -4
- package/lib/Types/Contact.js +3 -2
- package/lib/Types/Events.js +3 -2
- package/lib/Types/GroupMetadata.js +3 -2
- package/lib/Types/Label.js +24 -26
- package/lib/Types/LabelAssociation.js +6 -8
- package/lib/Types/Message.js +12 -7
- package/lib/Types/Newsletter.js +32 -17
- package/lib/Types/Product.js +3 -2
- package/lib/Types/Signal.js +3 -2
- package/lib/Types/Socket.js +4 -2
- package/lib/Types/State.js +11 -2
- package/lib/Types/USync.js +3 -2
- package/lib/Types/index.js +27 -41
- package/lib/Utils/auth-utils.js +211 -198
- package/lib/Utils/baileys-event-stream.js +42 -61
- package/lib/Utils/browser-utils.js +25 -0
- package/lib/Utils/business.js +213 -214
- package/lib/Utils/chat-utils.js +711 -689
- package/lib/Utils/crypto.js +112 -133
- package/lib/Utils/decode-wa-message.js +254 -186
- package/lib/Utils/event-buffer.js +510 -502
- package/lib/Utils/generics.js +318 -408
- package/lib/Utils/history.js +83 -90
- package/lib/Utils/index.js +21 -33
- package/lib/Utils/link-preview.js +71 -116
- package/lib/Utils/logger.js +5 -7
- package/lib/Utils/lt-hash.js +40 -46
- package/lib/Utils/make-mutex.js +34 -41
- package/lib/Utils/message-retry-manager.js +113 -0
- package/lib/Utils/messages-media.js +575 -671
- package/lib/Utils/messages.js +354 -462
- package/lib/Utils/noise-handler.js +138 -149
- package/lib/Utils/pre-key-manager.js +85 -0
- package/lib/Utils/process-message.js +323 -354
- package/lib/Utils/signal.js +148 -130
- package/lib/Utils/use-multi-file-auth-state.js +109 -91
- package/lib/Utils/validate-connection.js +183 -190
- package/lib/WABinary/constants.js +1298 -35
- package/lib/WABinary/decode.js +231 -256
- package/lib/WABinary/encode.js +207 -239
- package/lib/WABinary/generic-utils.js +119 -40
- package/lib/WABinary/index.js +7 -21
- package/lib/WABinary/jid-utils.js +88 -64
- package/lib/WABinary/types.js +3 -2
- package/lib/WAM/BinaryInfo.js +10 -12
- package/lib/WAM/constants.js +22851 -15348
- package/lib/WAM/encode.js +135 -136
- package/lib/WAM/index.js +5 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -30
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +49 -53
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -28
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +36 -39
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +26 -20
- package/lib/WAUSync/Protocols/index.js +6 -20
- package/lib/WAUSync/USyncQuery.js +86 -85
- package/lib/WAUSync/USyncUser.js +23 -25
- package/lib/WAUSync/index.js +5 -19
- package/lib/index.js +18 -33
- package/package.json +52 -57
- package/README.md +0 -2
- package/WASignalGroup/GroupProtocol.js +0 -1697
- package/WASignalGroup/ciphertext_message.js +0 -16
- package/WASignalGroup/group_cipher.js +0 -120
- package/WASignalGroup/group_session_builder.js +0 -46
- package/WASignalGroup/index.js +0 -5
- package/WASignalGroup/keyhelper.js +0 -21
- package/WASignalGroup/protobufs.js +0 -3
- package/WASignalGroup/queue_job.js +0 -69
- package/WASignalGroup/sender_chain_key.js +0 -50
- package/WASignalGroup/sender_key_distribution_message.js +0 -78
- package/WASignalGroup/sender_key_message.js +0 -92
- package/WASignalGroup/sender_key_record.js +0 -56
- package/WASignalGroup/sender_key_state.js +0 -129
- package/WASignalGroup/sender_message_key.js +0 -39
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Socket/Client/abstract-socket-client.js +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.js +0 -62
- package/lib/Socket/registration.js +0 -166
- package/lib/Socket/usync.js +0 -70
- package/lib/Store/make-mongo-store.js +0 -567
|
@@ -1,372 +1,341 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const generics_1 = require("./generics");
|
|
11
|
-
const history_1 = require("./history");
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidStatusBroadcast, jidDecode, jidEncode, jidNormalizedUser } from "../WABinary/index.js";
|
|
3
|
+
import { getContentType, normalizeMessageContent } from "../Utils/messages.js";
|
|
4
|
+
import { downloadAndProcessHistorySyncNotification } from "./history.js";
|
|
5
|
+
import { WAMessageStubType } from "../Types/index.js";
|
|
6
|
+
import { aesDecryptGCM, hmacSign } from "./crypto.js";
|
|
7
|
+
import { proto } from "../../WAProto/index.js";
|
|
8
|
+
import { toNumber } from "./generics.js";
|
|
9
|
+
//=======================================================//
|
|
12
10
|
const REAL_MSG_STUB_TYPES = new Set([
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
|
|
12
|
+
WAMessageStubType.CALL_MISSED_GROUP_VOICE,
|
|
13
|
+
WAMessageStubType.CALL_MISSED_VIDEO,
|
|
14
|
+
WAMessageStubType.CALL_MISSED_VOICE
|
|
17
15
|
]);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
16
|
+
//=======================================================//
|
|
17
|
+
const REAL_MSG_REQ_ME_STUB_TYPES = new Set([WAMessageStubType.GROUP_PARTICIPANT_ADD]);
|
|
18
|
+
//=======================================================//
|
|
19
|
+
export const cleanMessage = (message, meId, meLid) => {
|
|
20
|
+
if (isHostedPnUser(message.key.remoteJid) || isHostedLidUser(message.key.remoteJid)) {
|
|
21
|
+
message.key.remoteJid = jidEncode(jidDecode(message.key?.remoteJid)?.user, isHostedPnUser(message.key.remoteJid) ? "s.whatsapp.net" : "lid");
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
message.key.remoteJid = jidNormalizedUser(message.key.remoteJid);
|
|
25
|
+
}
|
|
26
|
+
if (isHostedPnUser(message.key.participant) || isHostedLidUser(message.key.participant)) {
|
|
27
|
+
message.key.participant = jidEncode(jidDecode(message.key.participant)?.user, isHostedPnUser(message.key.participant) ? "s.whatsapp.net" : "lid");
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
message.key.participant = jidNormalizedUser(message.key.participant);
|
|
31
|
+
}
|
|
32
|
+
const content = normalizeMessageContent(message.message);
|
|
33
|
+
if (content?.reactionMessage) {
|
|
34
|
+
normaliseKey(content.reactionMessage.key);
|
|
35
|
+
}
|
|
36
|
+
if (content?.pollUpdateMessage) {
|
|
37
|
+
normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
|
|
38
|
+
}
|
|
39
|
+
function normaliseKey(msgKey) {
|
|
40
|
+
if (!message.key.fromMe) {
|
|
41
|
+
msgKey.fromMe = !msgKey.fromMe
|
|
42
|
+
? areJidsSameUser(msgKey.participant || msgKey.remoteJid, meId) ||
|
|
43
|
+
areJidsSameUser(msgKey.participant || msgKey.remoteJid, meLid)
|
|
44
|
+
:
|
|
45
|
+
false;
|
|
46
|
+
msgKey.remoteJid = message.key.remoteJid;
|
|
47
|
+
msgKey.participant = msgKey.participant || message.key.participant;
|
|
50
48
|
}
|
|
49
|
+
}
|
|
51
50
|
};
|
|
52
|
-
|
|
53
|
-
const isRealMessage = (message
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
&& !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.reactionMessage)
|
|
64
|
-
&& !(normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.pollUpdateMessage);
|
|
51
|
+
//=======================================================//
|
|
52
|
+
export const isRealMessage = (message) => {
|
|
53
|
+
const normalizedContent = normalizeMessageContent(message.message);
|
|
54
|
+
const hasSomeContent = !!getContentType(normalizedContent);
|
|
55
|
+
return ((!!normalizedContent ||
|
|
56
|
+
REAL_MSG_STUB_TYPES.has(message.messageStubType) ||
|
|
57
|
+
REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)) &&
|
|
58
|
+
hasSomeContent &&
|
|
59
|
+
!normalizedContent?.protocolMessage &&
|
|
60
|
+
!normalizedContent?.reactionMessage &&
|
|
61
|
+
!normalizedContent?.pollUpdateMessage);
|
|
65
62
|
};
|
|
66
|
-
|
|
67
|
-
const shouldIncrementChatUnread = (message) =>
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if ((0, WABinary_1.isJidBroadcast)(remoteJid)
|
|
75
|
-
&& !(0, WABinary_1.isJidStatusBroadcast)(remoteJid)
|
|
76
|
-
&& !fromMe) {
|
|
77
|
-
return participant;
|
|
78
|
-
}
|
|
79
|
-
return remoteJid;
|
|
63
|
+
//=======================================================//
|
|
64
|
+
export const shouldIncrementChatUnread = (message) => !message.key.fromMe && !message.messageStubType;
|
|
65
|
+
//=======================================================//
|
|
66
|
+
export const getChatId = ({ remoteJid, participant, fromMe }) => {
|
|
67
|
+
if (isJidBroadcast(remoteJid) && !isJidStatusBroadcast(remoteJid) && !fromMe) {
|
|
68
|
+
return participant;
|
|
69
|
+
}
|
|
70
|
+
return remoteJid;
|
|
80
71
|
};
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const aad = toBinary(`${pollMsgId}\u0000${voterJid}`);
|
|
99
|
-
const decrypted = (0, crypto_1.aesDecryptGCM)(encPayload, decKey, encIv, aad);
|
|
100
|
-
return WAProto_1.proto.Message.PollVoteMessage.decode(decrypted);
|
|
101
|
-
function toBinary(txt) {
|
|
102
|
-
return Buffer.from(txt);
|
|
103
|
-
}
|
|
72
|
+
//=======================================================//
|
|
73
|
+
export function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid }) {
|
|
74
|
+
const sign = Buffer.concat([
|
|
75
|
+
toBinary(pollMsgId),
|
|
76
|
+
toBinary(pollCreatorJid),
|
|
77
|
+
toBinary(voterJid),
|
|
78
|
+
toBinary("Poll Vote"),
|
|
79
|
+
new Uint8Array([1])
|
|
80
|
+
]);
|
|
81
|
+
const key0 = hmacSign(pollEncKey, new Uint8Array(32), "sha256");
|
|
82
|
+
const decKey = hmacSign(sign, key0, "sha256");
|
|
83
|
+
const aad = toBinary(`${pollMsgId}\u0000${voterJid}`);
|
|
84
|
+
const decrypted = aesDecryptGCM(encPayload, decKey, encIv, aad);
|
|
85
|
+
return proto.Message.PollVoteMessage.decode(decrypted);
|
|
86
|
+
function toBinary(txt) {
|
|
87
|
+
return Buffer.from(txt);
|
|
88
|
+
}
|
|
104
89
|
}
|
|
105
|
-
const processMessage = async (message, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, keyStore, logger, options
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if ((0, exports.shouldIncrementChatUnread)(message)) {
|
|
116
|
-
chat.unreadCount = (chat.unreadCount || 0) + 1;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
const content = (0, messages_1.normalizeMessageContent)(message.message);
|
|
120
|
-
// unarchive chat if it's a real message, or someone reacted to our message
|
|
121
|
-
// and we've the unarchive chats setting on
|
|
122
|
-
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))
|
|
123
|
-
&& (accountSettings === null || accountSettings === void 0 ? void 0 : accountSettings.unarchiveChats)) {
|
|
124
|
-
chat.archived = false;
|
|
125
|
-
chat.readOnly = false;
|
|
90
|
+
const processMessage = async (message, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, signalRepository, keyStore, logger, options }) => {
|
|
91
|
+
const meId = creds.me.id;
|
|
92
|
+
const { accountSettings } = creds;
|
|
93
|
+
const chat = { id: jidNormalizedUser(getChatId(message.key)) };
|
|
94
|
+
const isRealMsg = isRealMessage(message);
|
|
95
|
+
if (isRealMsg) {
|
|
96
|
+
chat.messages = [{ message }];
|
|
97
|
+
chat.conversationTimestamp = toNumber(message.messageTimestamp);
|
|
98
|
+
if (shouldIncrementChatUnread(message)) {
|
|
99
|
+
chat.unreadCount = (chat.unreadCount || 0) + 1;
|
|
126
100
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (keys === null || keys === void 0 ? void 0 : keys.length) {
|
|
162
|
-
let newAppStateSyncKeyId = '';
|
|
163
|
-
await keyStore.transaction(async () => {
|
|
164
|
-
const newKeys = [];
|
|
165
|
-
for (const { keyData, keyId } of keys) {
|
|
166
|
-
const strKeyId = Buffer.from(keyId.keyId).toString('base64');
|
|
167
|
-
newKeys.push(strKeyId);
|
|
168
|
-
await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } });
|
|
169
|
-
newAppStateSyncKeyId = strKeyId;
|
|
170
|
-
}
|
|
171
|
-
logger === null || logger === void 0 ? void 0 : logger.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys');
|
|
172
|
-
});
|
|
173
|
-
ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId });
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
logger === null || logger === void 0 ? void 0 : logger.info({ protocolMsg }, 'recv app state sync with 0 keys');
|
|
177
|
-
}
|
|
178
|
-
break;
|
|
179
|
-
case WAProto_1.proto.Message.ProtocolMessage.Type.REVOKE:
|
|
180
|
-
ev.emit('messages.update', [
|
|
181
|
-
{
|
|
182
|
-
key: {
|
|
183
|
-
...message.key,
|
|
184
|
-
id: protocolMsg.key.id
|
|
185
|
-
},
|
|
186
|
-
update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }
|
|
187
|
-
}
|
|
188
|
-
]);
|
|
189
|
-
break;
|
|
190
|
-
case WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
|
|
191
|
-
Object.assign(chat, {
|
|
192
|
-
ephemeralSettingTimestamp: (0, generics_1.toNumber)(message.messageTimestamp),
|
|
193
|
-
ephemeralExpiration: protocolMsg.ephemeralExpiration || null
|
|
194
|
-
});
|
|
195
|
-
break;
|
|
196
|
-
case WAProto_1.proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
|
|
197
|
-
const response = protocolMsg.peerDataOperationRequestResponseMessage;
|
|
198
|
-
if (response) {
|
|
199
|
-
placeholderResendCache === null || placeholderResendCache === void 0 ? void 0 : placeholderResendCache.del(response.stanzaId);
|
|
200
|
-
// TODO: IMPLEMENT HISTORY SYNC ETC (sticker uploads etc.).
|
|
201
|
-
const { peerDataOperationResult } = response;
|
|
202
|
-
for (const result of peerDataOperationResult) {
|
|
203
|
-
const { placeholderMessageResendResponse: retryResponse } = result;
|
|
204
|
-
if (retryResponse) {
|
|
205
|
-
const webMessageInfo = WAProto_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes);
|
|
206
|
-
// wait till another upsert event is available, don't want it to be part of the PDO response message
|
|
207
|
-
setTimeout(() => {
|
|
208
|
-
ev.emit('messages.upsert', {
|
|
209
|
-
messages: [webMessageInfo],
|
|
210
|
-
type: 'notify',
|
|
211
|
-
requestId: response.stanzaId
|
|
212
|
-
});
|
|
213
|
-
}, 500);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
case WAProto_1.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
|
|
218
|
-
ev.emit('messages.update', [
|
|
219
|
-
{
|
|
220
|
-
// flip the sender / fromMe properties because they're in the perspective of the sender
|
|
221
|
-
key: { ...message.key, id: (_d = protocolMsg.key) === null || _d === void 0 ? void 0 : _d.id },
|
|
222
|
-
update: {
|
|
223
|
-
message: {
|
|
224
|
-
editedMessage: {
|
|
225
|
-
message: protocolMsg.editedMessage
|
|
226
|
-
}
|
|
227
|
-
},
|
|
228
|
-
messageTimestamp: protocolMsg.timestampMs
|
|
229
|
-
? Math.floor((0, generics_1.toNumber)(protocolMsg.timestampMs) / 1000)
|
|
230
|
-
: message.messageTimestamp
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
]);
|
|
234
|
-
break;
|
|
101
|
+
}
|
|
102
|
+
const content = normalizeMessageContent(message.message);
|
|
103
|
+
if ((isRealMsg || content?.reactionMessage?.key?.fromMe) && accountSettings?.unarchiveChats) {
|
|
104
|
+
chat.archived = false;
|
|
105
|
+
chat.readOnly = false;
|
|
106
|
+
}
|
|
107
|
+
const protocolMsg = content?.protocolMessage;
|
|
108
|
+
if (protocolMsg) {
|
|
109
|
+
switch (protocolMsg.type) {
|
|
110
|
+
case proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
|
|
111
|
+
const histNotification = protocolMsg.historySyncNotification;
|
|
112
|
+
const process = shouldProcessHistoryMsg;
|
|
113
|
+
const isLatest = !creds.processedHistoryMessages?.length;
|
|
114
|
+
logger?.info({
|
|
115
|
+
histNotification,
|
|
116
|
+
process,
|
|
117
|
+
id: message.key.id,
|
|
118
|
+
isLatest
|
|
119
|
+
}, "got history notification");
|
|
120
|
+
if (process) {
|
|
121
|
+
if (histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND) {
|
|
122
|
+
ev.emit("creds.update", {
|
|
123
|
+
processedHistoryMessages: [
|
|
124
|
+
...(creds.processedHistoryMessages || []),
|
|
125
|
+
{ key: message.key, messageTimestamp: message.messageTimestamp }
|
|
126
|
+
]
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
const data = await downloadAndProcessHistorySyncNotification(histNotification, options);
|
|
130
|
+
ev.emit("messaging-history.set", {
|
|
131
|
+
...data,
|
|
132
|
+
isLatest: histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND ? isLatest : undefined,
|
|
133
|
+
peerDataRequestSessionId: histNotification.peerDataRequestSessionId
|
|
134
|
+
});
|
|
235
135
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const emitGroupUpdate = (update) => {
|
|
253
|
-
var _a;
|
|
254
|
-
ev.emit('groups.update', [{ id: jid, ...update, author: (_a = message.participant) !== null && _a !== void 0 ? _a : undefined }]);
|
|
255
|
-
};
|
|
256
|
-
const emitGroupRequestJoin = (participant, action, method) => {
|
|
257
|
-
ev.emit('group.join-request', { id: jid, author: message.participant, participant, action, method: method });
|
|
258
|
-
};
|
|
259
|
-
const participantsIncludesMe = () => participants.find(jid => (0, WABinary_1.areJidsSameUser)(meId, jid));
|
|
260
|
-
switch (message.messageStubType) {
|
|
261
|
-
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
|
|
262
|
-
participants = message.messageStubParameters || [];
|
|
263
|
-
emitParticipantsUpdate('modify');
|
|
264
|
-
break;
|
|
265
|
-
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
|
|
266
|
-
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
|
|
267
|
-
participants = message.messageStubParameters || [];
|
|
268
|
-
emitParticipantsUpdate('remove');
|
|
269
|
-
// mark the chat read only if you left the group
|
|
270
|
-
if (participantsIncludesMe()) {
|
|
271
|
-
chat.readOnly = true;
|
|
272
|
-
}
|
|
273
|
-
break;
|
|
274
|
-
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:
|
|
275
|
-
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
|
|
276
|
-
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
|
|
277
|
-
participants = message.messageStubParameters || [];
|
|
278
|
-
if (participantsIncludesMe()) {
|
|
279
|
-
chat.readOnly = false;
|
|
280
|
-
}
|
|
281
|
-
emitParticipantsUpdate('add');
|
|
282
|
-
break;
|
|
283
|
-
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
|
|
284
|
-
participants = message.messageStubParameters || [];
|
|
285
|
-
emitParticipantsUpdate('demote');
|
|
286
|
-
break;
|
|
287
|
-
case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
|
|
288
|
-
participants = message.messageStubParameters || [];
|
|
289
|
-
emitParticipantsUpdate('promote');
|
|
290
|
-
break;
|
|
291
|
-
case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
|
|
292
|
-
const announceValue = (_g = message.messageStubParameters) === null || _g === void 0 ? void 0 : _g[0];
|
|
293
|
-
emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' });
|
|
294
|
-
break;
|
|
295
|
-
case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:
|
|
296
|
-
const restrictValue = (_h = message.messageStubParameters) === null || _h === void 0 ? void 0 : _h[0];
|
|
297
|
-
emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' });
|
|
298
|
-
break;
|
|
299
|
-
case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:
|
|
300
|
-
const name = (_j = message.messageStubParameters) === null || _j === void 0 ? void 0 : _j[0];
|
|
301
|
-
chat.name = name;
|
|
302
|
-
emitGroupUpdate({ subject: name });
|
|
303
|
-
break;
|
|
304
|
-
case Types_1.WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
|
|
305
|
-
const description = (_k = message.messageStubParameters) === null || _k === void 0 ? void 0 : _k[0];
|
|
306
|
-
chat.description = description;
|
|
307
|
-
emitGroupUpdate({ desc: description });
|
|
308
|
-
break;
|
|
309
|
-
case Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
|
|
310
|
-
const code = (_l = message.messageStubParameters) === null || _l === void 0 ? void 0 : _l[0];
|
|
311
|
-
emitGroupUpdate({ inviteCode: code });
|
|
312
|
-
break;
|
|
313
|
-
case Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE:
|
|
314
|
-
const memberAddValue = (_m = message.messageStubParameters) === null || _m === void 0 ? void 0 : _m[0];
|
|
315
|
-
emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' });
|
|
316
|
-
break;
|
|
317
|
-
case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
|
|
318
|
-
const approvalMode = (_o = message.messageStubParameters) === null || _o === void 0 ? void 0 : _o[0];
|
|
319
|
-
emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' });
|
|
320
|
-
break;
|
|
321
|
-
case Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
|
|
322
|
-
const participant = (_p = message.messageStubParameters) === null || _p === void 0 ? void 0 : _p[0];
|
|
323
|
-
const action = (_q = message.messageStubParameters) === null || _q === void 0 ? void 0 : _q[1];
|
|
324
|
-
const method = (_r = message.messageStubParameters) === null || _r === void 0 ? void 0 : _r[2];
|
|
325
|
-
emitGroupRequestJoin(participant, action, method);
|
|
326
|
-
break;
|
|
136
|
+
break;
|
|
137
|
+
case proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
|
|
138
|
+
const keys = protocolMsg.appStateSyncKeyShare.keys;
|
|
139
|
+
if (keys?.length) {
|
|
140
|
+
let newAppStateSyncKeyId = "";
|
|
141
|
+
await keyStore.transaction(async () => {
|
|
142
|
+
const newKeys = [];
|
|
143
|
+
for (const { keyData, keyId } of keys) {
|
|
144
|
+
const strKeyId = Buffer.from(keyId.keyId).toString("base64");
|
|
145
|
+
newKeys.push(strKeyId);
|
|
146
|
+
await keyStore.set({ "app-state-sync-key": { [strKeyId]: keyData } });
|
|
147
|
+
newAppStateSyncKeyId = strKeyId;
|
|
148
|
+
}
|
|
149
|
+
logger?.info({ newAppStateSyncKeyId, newKeys }, "injecting new app state sync keys");
|
|
150
|
+
}, meId);
|
|
151
|
+
ev.emit("creds.update", { myAppStateKeyId: newAppStateSyncKeyId });
|
|
327
152
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
153
|
+
else {
|
|
154
|
+
logger?.info({ protocolMsg }, "recv app state sync with 0 keys");
|
|
155
|
+
}
|
|
156
|
+
break;
|
|
157
|
+
case proto.Message.ProtocolMessage.Type.REVOKE:
|
|
158
|
+
ev.emit("messages.update", [
|
|
159
|
+
{
|
|
160
|
+
key: {
|
|
161
|
+
...message.key,
|
|
162
|
+
id: protocolMsg.key.id
|
|
163
|
+
},
|
|
164
|
+
update: { message: null, messageStubType: WAMessageStubType.REVOKE, key: message.key }
|
|
165
|
+
}
|
|
166
|
+
]);
|
|
167
|
+
break;
|
|
168
|
+
case proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
|
|
169
|
+
Object.assign(chat, {
|
|
170
|
+
ephemeralSettingTimestamp: toNumber(message.messageTimestamp),
|
|
171
|
+
ephemeralExpiration: protocolMsg.ephemeralExpiration || null
|
|
172
|
+
});
|
|
173
|
+
break;
|
|
174
|
+
case proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
|
|
175
|
+
const response = protocolMsg.peerDataOperationRequestResponseMessage;
|
|
176
|
+
if (response) {
|
|
177
|
+
await placeholderResendCache?.del(response.stanzaId);
|
|
178
|
+
const { peerDataOperationResult } = response;
|
|
179
|
+
for (const result of peerDataOperationResult) {
|
|
180
|
+
const { placeholderMessageResendResponse: retryResponse } = result;
|
|
181
|
+
if (retryResponse) {
|
|
182
|
+
const webMessageInfo = proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes);
|
|
183
|
+
setTimeout(() => {
|
|
184
|
+
ev.emit("messages.upsert", {
|
|
185
|
+
messages: [webMessageInfo],
|
|
186
|
+
type: "notify",
|
|
187
|
+
requestId: response.stanzaId
|
|
344
188
|
});
|
|
345
|
-
|
|
346
|
-
{
|
|
347
|
-
key: creationMsgKey,
|
|
348
|
-
update: {
|
|
349
|
-
pollUpdates: [
|
|
350
|
-
{
|
|
351
|
-
pollUpdateMessageKey: message.key,
|
|
352
|
-
vote: voteMsg,
|
|
353
|
-
senderTimestampMs: content.pollUpdateMessage.senderTimestampMs.toNumber(),
|
|
354
|
-
}
|
|
355
|
-
]
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
]);
|
|
189
|
+
}, 500);
|
|
359
190
|
}
|
|
360
|
-
|
|
361
|
-
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
break;
|
|
194
|
+
case proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
|
|
195
|
+
ev.emit("messages.update", [
|
|
196
|
+
{
|
|
197
|
+
key: { ...message.key, id: protocolMsg.key?.id },
|
|
198
|
+
update: {
|
|
199
|
+
message: {
|
|
200
|
+
editedMessage: {
|
|
201
|
+
message: protocolMsg.editedMessage
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
messageTimestamp: protocolMsg.timestampMs
|
|
205
|
+
? Math.floor(toNumber(protocolMsg.timestampMs) / 1000)
|
|
206
|
+
: message.messageTimestamp
|
|
362
207
|
}
|
|
208
|
+
}
|
|
209
|
+
]);
|
|
210
|
+
break;
|
|
211
|
+
case proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
|
|
212
|
+
const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload;
|
|
213
|
+
const { pnToLidMappings, chatDbMigrationTimestamp } = proto.LIDMigrationMappingSyncPayload.decode(encodedPayload);
|
|
214
|
+
logger?.debug({ pnToLidMappings, chatDbMigrationTimestamp }, "got lid mappings and chat db migration timestamp");
|
|
215
|
+
const pairs = [];
|
|
216
|
+
for (const { pn, latestLid, assignedLid } of pnToLidMappings) {
|
|
217
|
+
const lid = latestLid || assignedLid;
|
|
218
|
+
pairs.push({ lid: `${lid}@lid`, pn: `${pn}@s.whatsapp.net` });
|
|
363
219
|
}
|
|
364
|
-
|
|
365
|
-
|
|
220
|
+
await signalRepository.lidMapping.storeLIDPNMappings(pairs);
|
|
221
|
+
if (pairs.length) {
|
|
222
|
+
for (const { pn, lid } of pairs) {
|
|
223
|
+
await signalRepository.migrateSession(pn, lid);
|
|
224
|
+
}
|
|
366
225
|
}
|
|
367
226
|
}
|
|
368
|
-
|
|
369
|
-
|
|
227
|
+
}
|
|
228
|
+
else if (content?.reactionMessage) {
|
|
229
|
+
const reaction = {
|
|
230
|
+
...content.reactionMessage,
|
|
231
|
+
key: message.key
|
|
232
|
+
};
|
|
233
|
+
ev.emit("messages.reaction", [
|
|
234
|
+
{
|
|
235
|
+
reaction,
|
|
236
|
+
key: content.reactionMessage?.key
|
|
237
|
+
}
|
|
238
|
+
]);
|
|
239
|
+
}
|
|
240
|
+
else if (message.messageStubType) {
|
|
241
|
+
const jid = message.key?.remoteJid;
|
|
242
|
+
let participants;
|
|
243
|
+
const emitParticipantsUpdate = (action) => ev.emit("group-participants.update", {
|
|
244
|
+
id: jid,
|
|
245
|
+
author: message.key.participant,
|
|
246
|
+
authorPn: message.key.participantAlt,
|
|
247
|
+
participants,
|
|
248
|
+
action
|
|
249
|
+
});
|
|
250
|
+
const emitGroupUpdate = (update) => {
|
|
251
|
+
ev.emit("groups.update", [
|
|
252
|
+
{ id: jid, ...update, author: message.key.participant ?? undefined, authorPn: message.key.participantAlt }
|
|
253
|
+
]);
|
|
254
|
+
};
|
|
255
|
+
const emitGroupRequestJoin = (participant, action, method) => {
|
|
256
|
+
ev.emit("group.join-request", {
|
|
257
|
+
id: jid,
|
|
258
|
+
author: message.key.participant,
|
|
259
|
+
authorPn: message.key.participantAlt,
|
|
260
|
+
participant: participant.lid,
|
|
261
|
+
participantPn: participant.pn,
|
|
262
|
+
action,
|
|
263
|
+
method: method
|
|
264
|
+
});
|
|
265
|
+
};
|
|
266
|
+
const participantsIncludesMe = () => participants.find(jid => areJidsSameUser(meId, jid.phoneNumber));
|
|
267
|
+
switch (message.messageStubType) {
|
|
268
|
+
case WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
|
|
269
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
270
|
+
emitParticipantsUpdate("modify");
|
|
271
|
+
break;
|
|
272
|
+
case WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
|
|
273
|
+
case WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
|
|
274
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
275
|
+
emitParticipantsUpdate("remove");
|
|
276
|
+
if (participantsIncludesMe()) {
|
|
277
|
+
chat.readOnly = true;
|
|
278
|
+
}
|
|
279
|
+
break;
|
|
280
|
+
case WAMessageStubType.GROUP_PARTICIPANT_ADD:
|
|
281
|
+
case WAMessageStubType.GROUP_PARTICIPANT_INVITE:
|
|
282
|
+
case WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
|
|
283
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
284
|
+
if (participantsIncludesMe()) {
|
|
285
|
+
chat.readOnly = false;
|
|
286
|
+
}
|
|
287
|
+
emitParticipantsUpdate("add");
|
|
288
|
+
break;
|
|
289
|
+
case WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
|
|
290
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
291
|
+
emitParticipantsUpdate("demote");
|
|
292
|
+
break;
|
|
293
|
+
case WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
|
|
294
|
+
participants = message.messageStubParameters.map((a) => JSON.parse(a)) || [];
|
|
295
|
+
emitParticipantsUpdate("promote");
|
|
296
|
+
break;
|
|
297
|
+
case WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
|
|
298
|
+
const announceValue = message.messageStubParameters?.[0];
|
|
299
|
+
emitGroupUpdate({ announce: announceValue === "true" || announceValue === "on" });
|
|
300
|
+
break;
|
|
301
|
+
case WAMessageStubType.GROUP_CHANGE_RESTRICT:
|
|
302
|
+
const restrictValue = message.messageStubParameters?.[0];
|
|
303
|
+
emitGroupUpdate({ restrict: restrictValue === "true" || restrictValue === "on" });
|
|
304
|
+
break;
|
|
305
|
+
case WAMessageStubType.GROUP_CHANGE_SUBJECT:
|
|
306
|
+
const name = message.messageStubParameters?.[0];
|
|
307
|
+
chat.name = name;
|
|
308
|
+
emitGroupUpdate({ subject: name });
|
|
309
|
+
break;
|
|
310
|
+
case WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
|
|
311
|
+
const description = message.messageStubParameters?.[0];
|
|
312
|
+
chat.description = description;
|
|
313
|
+
emitGroupUpdate({ desc: description });
|
|
314
|
+
break;
|
|
315
|
+
case WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
|
|
316
|
+
const code = message.messageStubParameters?.[0];
|
|
317
|
+
emitGroupUpdate({ inviteCode: code });
|
|
318
|
+
break;
|
|
319
|
+
case WAMessageStubType.GROUP_MEMBER_ADD_MODE:
|
|
320
|
+
const memberAddValue = message.messageStubParameters?.[0];
|
|
321
|
+
emitGroupUpdate({ memberAddMode: memberAddValue === "all_member_add" });
|
|
322
|
+
break;
|
|
323
|
+
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
|
|
324
|
+
const approvalMode = message.messageStubParameters?.[0];
|
|
325
|
+
emitGroupUpdate({ joinApprovalMode: approvalMode === "on" });
|
|
326
|
+
break;
|
|
327
|
+
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
|
|
328
|
+
const participant = JSON.parse(message.messageStubParameters?.[0]);
|
|
329
|
+
const action = message.messageStubParameters?.[1];
|
|
330
|
+
const method = message.messageStubParameters?.[2];
|
|
331
|
+
emitGroupRequestJoin(participant, action, method);
|
|
332
|
+
break;
|
|
370
333
|
}
|
|
334
|
+
}
|
|
335
|
+
if (Object.keys(chat).length > 1) {
|
|
336
|
+
ev.emit("chats.update", [chat]);
|
|
337
|
+
}
|
|
371
338
|
};
|
|
372
|
-
|
|
339
|
+
//=======================================================//
|
|
340
|
+
export default processMessage;
|
|
341
|
+
//=======================================================//
|