@nerxa/socketon 0.0.0-stage → 1.21.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/EXPERIMENTAL.md +350 -0
- package/LICENSE +24 -0
- package/README.md +6317 -2
- package/WAProto/index.d.ts +17301 -0
- package/WAProto/index.js +125088 -0
- package/engine-requirements.js +14 -0
- package/package.json +125 -4
- package/src/Defaults/index.d.ts +152 -0
- package/src/Defaults/index.js +164 -0
- package/src/MessageBuilder/bot-signature.d.ts +22 -0
- package/src/MessageBuilder/bot-signature.js +122 -0
- package/src/MessageBuilder/extras.d.ts +449 -0
- package/src/MessageBuilder/extras.js +1356 -0
- package/src/MessageBuilder/index.d.ts +210 -0
- package/src/MessageBuilder/index.js +3594 -0
- package/src/MessageBuilder/metaai.d.ts +438 -0
- package/src/MessageBuilder/metaai.js +890 -0
- package/src/Signal/Group/ciphertext-message.d.ts +9 -0
- package/src/Signal/Group/ciphertext-message.js +11 -0
- package/src/Signal/Group/group-session-builder.d.ts +7 -0
- package/src/Signal/Group/group-session-builder.js +27 -0
- package/src/Signal/Group/group_cipher.d.ts +11 -0
- package/src/Signal/Group/group_cipher.js +78 -0
- package/src/Signal/Group/index.d.ts +11 -0
- package/src/Signal/Group/index.js +11 -0
- package/src/Signal/Group/keyhelper.d.ts +6 -0
- package/src/Signal/Group/keyhelper.js +17 -0
- package/src/Signal/Group/sender-chain-key.d.ts +13 -0
- package/src/Signal/Group/sender-chain-key.js +25 -0
- package/src/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/src/Signal/Group/sender-key-distribution-message.js +62 -0
- package/src/Signal/Group/sender-key-message.d.ts +18 -0
- package/src/Signal/Group/sender-key-message.js +65 -0
- package/src/Signal/Group/sender-key-name.d.ts +11 -0
- package/src/Signal/Group/sender-key-name.js +47 -0
- package/src/Signal/Group/sender-key-record.d.ts +12 -0
- package/src/Signal/Group/sender-key-record.js +40 -0
- package/src/Signal/Group/sender-key-state.d.ts +16 -0
- package/src/Signal/Group/sender-key-state.js +83 -0
- package/src/Signal/Group/sender-message-key.d.ts +11 -0
- package/src/Signal/Group/sender-message-key.js +25 -0
- package/src/Signal/libsignal.d.ts +54 -0
- package/src/Signal/libsignal.js +427 -0
- package/src/Signal/lid-mapping.d.ts +20 -0
- package/src/Signal/lid-mapping.js +274 -0
- package/src/Socket/Client/index.d.ts +2 -0
- package/src/Socket/Client/index.js +3 -0
- package/src/Socket/Client/tcp.js +177 -0
- package/src/Socket/Client/types.d.ts +5 -0
- package/src/Socket/Client/types.js +9 -0
- package/src/Socket/Client/websocket.d.ts +12 -0
- package/src/Socket/Client/websocket.js +53 -0
- package/src/Socket/business.d.ts +431 -0
- package/src/Socket/business.js +379 -0
- package/src/Socket/chats.d.ts +247 -0
- package/src/Socket/chats.js +1484 -0
- package/src/Socket/communities.d.ts +598 -0
- package/src/Socket/communities.js +421 -0
- package/src/Socket/email.js +145 -0
- package/src/Socket/groups.d.ts +298 -0
- package/src/Socket/groups.js +339 -0
- package/src/Socket/index.d.ts +581 -0
- package/src/Socket/index.js +77 -0
- package/src/Socket/messages-recv.d.ts +370 -0
- package/src/Socket/messages-recv.js +1864 -0
- package/src/Socket/messages-send.d.ts +390 -0
- package/src/Socket/messages-send.js +1574 -0
- package/src/Socket/mex.d.ts +1 -0
- package/src/Socket/mex.js +44 -0
- package/src/Socket/mobile-builders.js +72 -0
- package/src/Socket/mobile.js +424 -0
- package/src/Socket/newsletter.d.ts +468 -0
- package/src/Socket/newsletter.js +671 -0
- package/src/Socket/socket.d.ts +77 -0
- package/src/Socket/socket.js +1106 -0
- package/src/Store/index.d.ts +3 -0
- package/src/Store/index.js +3 -0
- package/src/Store/make-in-memory-store.d.ts +62 -0
- package/src/Store/make-in-memory-store.js +396 -0
- package/src/Store/make-ordered-dictionary.d.ts +12 -0
- package/src/Store/make-ordered-dictionary.js +78 -0
- package/src/Store/object-repository.d.ts +10 -0
- package/src/Store/object-repository.js +23 -0
- package/src/Types/Auth.d.ts +1 -0
- package/src/Types/Auth.js +1 -0
- package/src/Types/Bussines.d.ts +1 -0
- package/src/Types/Bussines.js +1 -0
- package/src/Types/Call.d.ts +1 -0
- package/src/Types/Call.js +1 -0
- package/src/Types/Chat.d.ts +2 -0
- package/src/Types/Chat.js +24 -0
- package/src/Types/Contact.d.ts +1 -0
- package/src/Types/Contact.js +1 -0
- package/src/Types/Events.d.ts +1 -0
- package/src/Types/Events.js +0 -0
- package/src/Types/GroupMetadata.d.ts +5 -0
- package/src/Types/GroupMetadata.js +6 -0
- package/src/Types/Label.d.ts +2 -0
- package/src/Types/Label.js +24 -0
- package/src/Types/LabelAssociation.d.ts +2 -0
- package/src/Types/LabelAssociation.js +6 -0
- package/src/Types/Message.d.ts +13 -0
- package/src/Types/Message.js +23 -0
- package/src/Types/Mex.d.ts +2 -0
- package/src/Types/Mex.js +95 -0
- package/src/Types/Product.d.ts +1 -0
- package/src/Types/Product.js +1 -0
- package/src/Types/Signal.d.ts +1 -0
- package/src/Types/Signal.js +0 -0
- package/src/Types/Socket.d.ts +1 -0
- package/src/Types/Socket.js +0 -0
- package/src/Types/State.d.ts +5 -0
- package/src/Types/State.js +54 -0
- package/src/Types/USync.d.ts +1 -0
- package/src/Types/USync.js +0 -0
- package/src/Types/index.d.ts +13 -0
- package/src/Types/index.js +25 -0
- package/src/Utils/auth-store.d.ts +29 -0
- package/src/Utils/auth-store.js +76 -0
- package/src/Utils/auth-utils.d.ts +58 -0
- package/src/Utils/auth-utils.js +273 -0
- package/src/Utils/browser-utils.d.ts +9 -0
- package/src/Utils/browser-utils.js +28 -0
- package/src/Utils/business.d.ts +49 -0
- package/src/Utils/business.js +226 -0
- package/src/Utils/chat-utils.d.ts +408 -0
- package/src/Utils/chat-utils.js +846 -0
- package/src/Utils/companion-reg-client-utils.d.ts +4 -0
- package/src/Utils/companion-reg-client-utils.js +39 -0
- package/src/Utils/crypto.d.ts +39 -0
- package/src/Utils/crypto.js +110 -0
- package/src/Utils/decode-wa-message.d.ts +99 -0
- package/src/Utils/decode-wa-message.js +373 -0
- package/src/Utils/event-buffer.d.ts +22 -0
- package/src/Utils/event-buffer.js +624 -0
- package/src/Utils/generics.d.ts +63 -0
- package/src/Utils/generics.js +375 -0
- package/src/Utils/group-metadata-cache.d.ts +4 -0
- package/src/Utils/group-metadata-cache.js +25 -0
- package/src/Utils/history.d.ts +75 -0
- package/src/Utils/history.js +131 -0
- package/src/Utils/html-app.d.ts +19 -0
- package/src/Utils/html-app.js +96 -0
- package/src/Utils/html-multiplayer.d.ts +10 -0
- package/src/Utils/html-multiplayer.js +94 -0
- package/src/Utils/identity-change-handler.d.ts +13 -0
- package/src/Utils/identity-change-handler.js +48 -0
- package/src/Utils/index.d.ts +44 -0
- package/src/Utils/index.js +44 -0
- package/src/Utils/link-preview-metadata.d.ts +47 -0
- package/src/Utils/link-preview-metadata.js +88 -0
- package/src/Utils/link-preview.d.ts +12 -0
- package/src/Utils/link-preview.js +79 -0
- package/src/Utils/logger.d.ts +2 -0
- package/src/Utils/logger.js +2 -0
- package/src/Utils/lt-hash.d.ts +6 -0
- package/src/Utils/lt-hash.js +3 -0
- package/src/Utils/make-mutex.d.ts +6 -0
- package/src/Utils/make-mutex.js +32 -0
- package/src/Utils/message-retry-manager.d.ts +79 -0
- package/src/Utils/message-retry-manager.js +255 -0
- package/src/Utils/messages-media.d.ts +136 -0
- package/src/Utils/messages-media.js +814 -0
- package/src/Utils/messages.d.ts +62 -0
- package/src/Utils/messages.js +2223 -0
- package/src/Utils/mobile-companion-crypto.js +191 -0
- package/src/Utils/modern-messages.d.ts +150 -0
- package/src/Utils/modern-messages.js +496 -0
- package/src/Utils/native-flow.d.ts +11 -0
- package/src/Utils/native-flow.js +115 -0
- package/src/Utils/newsletter-status.d.ts +158 -0
- package/src/Utils/newsletter-status.js +651 -0
- package/src/Utils/noise-handler.d.ts +20 -0
- package/src/Utils/noise-handler.js +200 -0
- package/src/Utils/offline-node-processor.d.ts +9 -0
- package/src/Utils/offline-node-processor.js +33 -0
- package/src/Utils/optional-media.d.ts +3 -0
- package/src/Utils/optional-media.js +45 -0
- package/src/Utils/payment-messages.d.ts +66 -0
- package/src/Utils/payment-messages.js +160 -0
- package/src/Utils/pre-key-manager.d.ts +25 -0
- package/src/Utils/pre-key-manager.js +95 -0
- package/src/Utils/process-message.d.ts +60 -0
- package/src/Utils/process-message.js +688 -0
- package/src/Utils/reporting-utils.d.ts +12 -0
- package/src/Utils/reporting-utils.js +256 -0
- package/src/Utils/scheduled-message.d.ts +51 -0
- package/src/Utils/scheduled-message.js +112 -0
- package/src/Utils/sender-key-memory.d.ts +19 -0
- package/src/Utils/sender-key-memory.js +51 -0
- package/src/Utils/signal.d.ts +82 -0
- package/src/Utils/signal.js +193 -0
- package/src/Utils/stanza-ack.d.ts +16 -0
- package/src/Utils/stanza-ack.js +30 -0
- package/src/Utils/status-stickers.d.ts +127 -0
- package/src/Utils/status-stickers.js +260 -0
- package/src/Utils/sync-action-utils.d.ts +2 -0
- package/src/Utils/sync-action-utils.js +44 -0
- package/src/Utils/tc-token-utils.d.ts +29 -0
- package/src/Utils/tc-token-utils.js +155 -0
- package/src/Utils/ttl-cache.d.ts +28 -0
- package/src/Utils/ttl-cache.js +120 -0
- package/src/Utils/use-mongo-auth-state.d.ts +21 -0
- package/src/Utils/use-mongo-auth-state.js +67 -0
- package/src/Utils/use-multi-file-auth-state.d.ts +11 -0
- package/src/Utils/use-multi-file-auth-state.js +106 -0
- package/src/Utils/use-mysql-auth-state.d.ts +19 -0
- package/src/Utils/use-mysql-auth-state.js +79 -0
- package/src/Utils/use-postgres-auth-state.d.ts +19 -0
- package/src/Utils/use-postgres-auth-state.js +79 -0
- package/src/Utils/use-redis-auth-state.d.ts +19 -0
- package/src/Utils/use-redis-auth-state.js +89 -0
- package/src/Utils/use-single-file-auth-state.d.ts +10 -0
- package/src/Utils/use-single-file-auth-state.js +108 -0
- package/src/Utils/use-sqlite-auth-state.d.ts +12 -0
- package/src/Utils/use-sqlite-auth-state.js +120 -0
- package/src/Utils/useNekoDBAuth.d.ts +19 -0
- package/src/Utils/useNekoDBAuth.js +111 -0
- package/src/Utils/username.d.ts +12 -0
- package/src/Utils/username.js +58 -0
- package/src/Utils/validate-connection.d.ts +43 -0
- package/src/Utils/validate-connection.js +277 -0
- package/src/Utils/voice-recognition.d.ts +53 -0
- package/src/Utils/voice-recognition.js +219 -0
- package/src/Voip/audio-feeder.d.ts +24 -0
- package/src/Voip/audio-feeder.js +268 -0
- package/src/Voip/index.d.ts +88 -0
- package/src/Voip/index.js +936 -0
- package/src/Voip/relay-transport.d.ts +5 -0
- package/src/Voip/relay-transport.js +824 -0
- package/src/Voip/signaling.d.ts +11 -0
- package/src/Voip/signaling.js +1099 -0
- package/src/Voip/types.d.ts +2 -0
- package/src/Voip/types.js +12 -0
- package/src/Voip/video-feeder.d.ts +24 -0
- package/src/Voip/video-feeder.js +268 -0
- package/src/Voip/wasm-engine.d.ts +26 -0
- package/src/Voip/wasm-engine.js +1307 -0
- package/src/Voip/worker-bootstrap.js +1023 -0
- package/src/WABinary/constants.d.ts +22 -0
- package/src/WABinary/constants.js +1300 -0
- package/src/WABinary/decode.d.ts +9 -0
- package/src/WABinary/decode.js +261 -0
- package/src/WABinary/encode.d.ts +2 -0
- package/src/WABinary/encode.js +219 -0
- package/src/WABinary/generic-utils.d.ts +76 -0
- package/src/WABinary/generic-utils.js +214 -0
- package/src/WABinary/index.d.ts +5 -0
- package/src/WABinary/index.js +5 -0
- package/src/WABinary/jid-utils.d.ts +30 -0
- package/src/WABinary/jid-utils.js +97 -0
- package/src/WABinary/types.d.ts +1 -0
- package/src/WABinary/types.js +0 -0
- package/src/WAM/BinaryInfo.d.ts +7 -0
- package/src/WAM/BinaryInfo.js +9 -0
- package/src/WAM/constants.d.ts +34926 -0
- package/src/WAM/constants.js +22852 -0
- package/src/WAM/encode.d.ts +1 -0
- package/src/WAM/encode.js +148 -0
- package/src/WAM/index.d.ts +3 -0
- package/src/WAM/index.js +3 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.js +50 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.js +51 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/src/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
- package/src/WAUSync/Protocols/USyncUsernameProtocol.js +23 -0
- package/src/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
- package/src/WAUSync/Protocols/UsyncBotProfileProtocol.js +49 -0
- package/src/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
- package/src/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
- package/src/WAUSync/Protocols/index.d.ts +5 -0
- package/src/WAUSync/Protocols/index.js +5 -0
- package/src/WAUSync/USyncQuery.d.ts +20 -0
- package/src/WAUSync/USyncQuery.js +93 -0
- package/src/WAUSync/USyncUser.d.ts +16 -0
- package/src/WAUSync/USyncUser.js +30 -0
- package/src/WAUSync/index.d.ts +3 -0
- package/src/WAUSync/index.js +3 -0
- package/src/assets/wasm/README.md +76 -0
- package/src/assets/wasm/loader.js +5 -0
- package/src/assets/wasm/whatsapp.wasm +0 -0
- package/src/assets/wasm/worker-modules.js +273 -0
- package/src/index.d.ts +43 -0
- package/src/index.js +48 -0
|
@@ -0,0 +1,688 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
import { WAMessageStubType } from '../Types/index.js';
|
|
4
|
+
import { getContentType, normalizeMessageContent } from '../Utils/messages.js';
|
|
5
|
+
import { areJidsSameUser, isHostedLidUser, isHostedPnUser, isJidBroadcast, isJidStatusBroadcast, isLidUser, jidDecode, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
|
|
6
|
+
import { aesDecryptGCM, hmacSign } from './crypto.js';
|
|
7
|
+
import { getKeyAuthor, toNumber } from './generics.js';
|
|
8
|
+
import { downloadAndProcessHistorySyncNotification } from './history.js';
|
|
9
|
+
import { buildMergedTcTokenIndexWrite, resolveTcTokenJid } from './tc-token-utils.js';
|
|
10
|
+
const REAL_MSG_STUB_TYPES = new Set([
|
|
11
|
+
WAMessageStubType.CALL_MISSED_GROUP_VIDEO,
|
|
12
|
+
WAMessageStubType.CALL_MISSED_GROUP_VOICE,
|
|
13
|
+
WAMessageStubType.CALL_MISSED_VIDEO,
|
|
14
|
+
WAMessageStubType.CALL_MISSED_VOICE
|
|
15
|
+
]);
|
|
16
|
+
const REAL_MSG_REQ_ME_STUB_TYPES = new Set([WAMessageStubType.GROUP_PARTICIPANT_ADD]);
|
|
17
|
+
async function storeTcTokensFromHistorySync(chats, signalRepository, keyStore, logger) {
|
|
18
|
+
const getLIDForPN = signalRepository.lidMapping.getLIDForPN.bind(signalRepository.lidMapping);
|
|
19
|
+
const candidates = [];
|
|
20
|
+
for (const chat of chats) {
|
|
21
|
+
const ts = chat.tcTokenTimestamp ? toNumber(chat.tcTokenTimestamp) : 0;
|
|
22
|
+
if (chat.tcToken?.length && ts > 0) {
|
|
23
|
+
const jid = jidNormalizedUser(chat.id);
|
|
24
|
+
const storageJid = await resolveTcTokenJid(jid, getLIDForPN);
|
|
25
|
+
candidates.push({
|
|
26
|
+
storageJid,
|
|
27
|
+
token: Buffer.from(chat.tcToken),
|
|
28
|
+
ts,
|
|
29
|
+
senderTs: chat.tcTokenSenderTimestamp ? toNumber(chat.tcTokenSenderTimestamp) : undefined
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (!candidates.length) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const jids = candidates.map(c => c.storageJid);
|
|
37
|
+
const existing = await keyStore.get('tctoken', jids);
|
|
38
|
+
const entries = {};
|
|
39
|
+
for (const c of candidates) {
|
|
40
|
+
const existingEntry = existing[c.storageJid];
|
|
41
|
+
const existingTs = existingEntry?.timestamp ? Number(existingEntry.timestamp) : 0;
|
|
42
|
+
if (existingTs > 0 && existingTs >= c.ts) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
entries[c.storageJid] = {
|
|
46
|
+
...existingEntry,
|
|
47
|
+
token: c.token,
|
|
48
|
+
timestamp: String(c.ts),
|
|
49
|
+
...(c.senderTs !== undefined ? { senderTimestamp: c.senderTs } : {})
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (Object.keys(entries).length) {
|
|
53
|
+
logger?.debug({ count: Object.keys(entries).length }, 'storing tctokens from history sync');
|
|
54
|
+
try {
|
|
55
|
+
|
|
56
|
+
const indexWrite = await buildMergedTcTokenIndexWrite(keyStore, Object.keys(entries));
|
|
57
|
+
await keyStore.set({ tctoken: { ...entries, ...indexWrite } });
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
logger?.warn({ err }, 'failed to store tctokens from history sync');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const cleanMessage = (message, meId, meLid) => {
|
|
66
|
+
|
|
67
|
+
if (isHostedPnUser(message.key.remoteJid) || isHostedLidUser(message.key.remoteJid)) {
|
|
68
|
+
message.key.remoteJid = jidEncode(jidDecode(message.key?.remoteJid)?.user, isHostedPnUser(message.key.remoteJid) ? 's.whatsapp.net' : 'lid');
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
message.key.remoteJid = jidNormalizedUser(message.key.remoteJid);
|
|
72
|
+
}
|
|
73
|
+
if (isHostedPnUser(message.key.participant) || isHostedLidUser(message.key.participant)) {
|
|
74
|
+
message.key.participant = jidEncode(jidDecode(message.key.participant)?.user, isHostedPnUser(message.key.participant) ? 's.whatsapp.net' : 'lid');
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
message.key.participant = jidNormalizedUser(message.key.participant);
|
|
78
|
+
}
|
|
79
|
+
const content = normalizeMessageContent(message.message);
|
|
80
|
+
|
|
81
|
+
if (content?.reactionMessage) {
|
|
82
|
+
normaliseKey(content.reactionMessage.key);
|
|
83
|
+
}
|
|
84
|
+
if (content?.pollUpdateMessage) {
|
|
85
|
+
normaliseKey(content.pollUpdateMessage.pollCreationMessageKey);
|
|
86
|
+
}
|
|
87
|
+
function normaliseKey(msgKey) {
|
|
88
|
+
|
|
89
|
+
if (!message.key.fromMe) {
|
|
90
|
+
|
|
91
|
+
msgKey.fromMe = !msgKey.fromMe
|
|
92
|
+
? areJidsSameUser(msgKey.participant || msgKey.remoteJid, meId) ||
|
|
93
|
+
areJidsSameUser(msgKey.participant || msgKey.remoteJid, meLid)
|
|
94
|
+
:
|
|
95
|
+
|
|
96
|
+
false;
|
|
97
|
+
|
|
98
|
+
msgKey.remoteJid = message.key.remoteJid;
|
|
99
|
+
|
|
100
|
+
msgKey.participant = msgKey.participant || message.key.participant;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const isRealMessage = (message) => {
|
|
106
|
+
const normalizedContent = normalizeMessageContent(message.message);
|
|
107
|
+
const hasSomeContent = !!getContentType(normalizedContent);
|
|
108
|
+
return ((!!normalizedContent ||
|
|
109
|
+
REAL_MSG_STUB_TYPES.has(message.messageStubType) ||
|
|
110
|
+
REAL_MSG_REQ_ME_STUB_TYPES.has(message.messageStubType)) &&
|
|
111
|
+
hasSomeContent &&
|
|
112
|
+
!normalizedContent?.protocolMessage &&
|
|
113
|
+
!normalizedContent?.reactionMessage &&
|
|
114
|
+
!normalizedContent?.pollUpdateMessage);
|
|
115
|
+
};
|
|
116
|
+
export const shouldIncrementChatUnread = (message) => !message.key.fromMe && !message.messageStubType;
|
|
117
|
+
|
|
118
|
+
export const getChatId = ({ remoteJid, participant, fromMe }) => {
|
|
119
|
+
if (!remoteJid) {
|
|
120
|
+
throw new Boom('Cannot derive chat id: message key is missing remoteJid', {
|
|
121
|
+
data: { remoteJid, participant, fromMe }
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
if (isJidBroadcast(remoteJid) && !isJidStatusBroadcast(remoteJid) && !fromMe) {
|
|
125
|
+
if (!participant) {
|
|
126
|
+
throw new Boom('Cannot derive chat id: broadcast message key is missing participant', {
|
|
127
|
+
data: { remoteJid, fromMe }
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return participant;
|
|
131
|
+
}
|
|
132
|
+
return remoteJid;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export function decryptPollVote({ encPayload, encIv }, { pollCreatorJid, pollMsgId, pollEncKey, voterJid }) {
|
|
136
|
+
const sign = Buffer.concat([
|
|
137
|
+
toBinary(pollMsgId),
|
|
138
|
+
toBinary(pollCreatorJid),
|
|
139
|
+
toBinary(voterJid),
|
|
140
|
+
toBinary('Poll Vote'),
|
|
141
|
+
new Uint8Array([1])
|
|
142
|
+
]);
|
|
143
|
+
const key0 = hmacSign(pollEncKey, new Uint8Array(32), 'sha256');
|
|
144
|
+
const decKey = hmacSign(sign, key0, 'sha256');
|
|
145
|
+
const aad = toBinary(`${pollMsgId}\u0000${voterJid}`);
|
|
146
|
+
const decrypted = aesDecryptGCM(encPayload, decKey, encIv, aad);
|
|
147
|
+
return proto.Message.PollVoteMessage.decode(decrypted);
|
|
148
|
+
function toBinary(txt) {
|
|
149
|
+
return Buffer.from(txt);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function decryptEventResponse({ encPayload, encIv }, { eventCreatorJid, eventMsgId, eventEncKey, responderJid }) {
|
|
154
|
+
const sign = Buffer.concat([
|
|
155
|
+
toBinary(eventMsgId),
|
|
156
|
+
toBinary(eventCreatorJid),
|
|
157
|
+
toBinary(responderJid),
|
|
158
|
+
toBinary('Event Response'),
|
|
159
|
+
new Uint8Array([1])
|
|
160
|
+
]);
|
|
161
|
+
const key0 = hmacSign(eventEncKey, new Uint8Array(32), 'sha256');
|
|
162
|
+
const decKey = hmacSign(sign, key0, 'sha256');
|
|
163
|
+
const aad = toBinary(`${eventMsgId}\u0000${responderJid}`);
|
|
164
|
+
const decrypted = aesDecryptGCM(encPayload, decKey, encIv, aad);
|
|
165
|
+
return proto.Message.EventResponseMessage.decode(decrypted);
|
|
166
|
+
function toBinary(txt) {
|
|
167
|
+
return Buffer.from(txt);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
export function decryptMessageEdit({ encPayload, encIv }, { originalSenderJid, originalMsgId, editEncKey, editorJid }) {
|
|
171
|
+
const sign = Buffer.concat([
|
|
172
|
+
toBinary(originalMsgId),
|
|
173
|
+
toBinary(originalSenderJid),
|
|
174
|
+
toBinary(editorJid),
|
|
175
|
+
toBinary('Message Edit'),
|
|
176
|
+
new Uint8Array([1])
|
|
177
|
+
]);
|
|
178
|
+
const key0 = hmacSign(editEncKey, new Uint8Array(32), 'sha256');
|
|
179
|
+
const decKey = hmacSign(sign, key0, 'sha256');
|
|
180
|
+
const aad = Buffer.alloc(0);
|
|
181
|
+
const decrypted = aesDecryptGCM(encPayload, decKey, encIv, aad);
|
|
182
|
+
return proto.Message.decode(decrypted);
|
|
183
|
+
function toBinary(txt) {
|
|
184
|
+
return Buffer.from(txt);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const resolveMessageEditAuthor = async (key, meId, lidMapping) => {
|
|
188
|
+
if (!key) {
|
|
189
|
+
return '';
|
|
190
|
+
}
|
|
191
|
+
const rawJid = key.fromMe
|
|
192
|
+
? key.participantAlt || key.participant || meId
|
|
193
|
+
: key.participantAlt || key.participant || key.remoteJidAlt || key.remoteJid;
|
|
194
|
+
const normalized = jidNormalizedUser(rawJid);
|
|
195
|
+
if (!normalized) {
|
|
196
|
+
return '';
|
|
197
|
+
}
|
|
198
|
+
if (!isLidUser(normalized)) {
|
|
199
|
+
return normalized;
|
|
200
|
+
}
|
|
201
|
+
const pn = await lidMapping.getPNForLID(normalized);
|
|
202
|
+
return jidNormalizedUser(pn);
|
|
203
|
+
};
|
|
204
|
+
const buildEditUpdate = (args) => {
|
|
205
|
+
const editedInner = decryptMessageEdit({ encPayload: args.encPayload, encIv: args.encIv }, {
|
|
206
|
+
editEncKey: args.editEncKey,
|
|
207
|
+
originalSenderJid: args.originalSenderJid,
|
|
208
|
+
originalMsgId: args.originalMsgId,
|
|
209
|
+
editorJid: args.editorJid
|
|
210
|
+
});
|
|
211
|
+
const editProtocol = editedInner.protocolMessage;
|
|
212
|
+
const innerEdited = editProtocol?.editedMessage;
|
|
213
|
+
if (!innerEdited) {
|
|
214
|
+
args.logger?.warn({ targetKey: args.targetKey }, 'decrypted MESSAGE_EDIT plaintext had no protocolMessage.editedMessage — skipping update');
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
key: { ...args.targetKey },
|
|
219
|
+
update: {
|
|
220
|
+
message: {
|
|
221
|
+
editedMessage: {
|
|
222
|
+
message: innerEdited
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
messageTimestamp: editProtocol?.timestampMs
|
|
226
|
+
? Math.floor(toNumber(editProtocol.timestampMs) / 1000)
|
|
227
|
+
: args.fallbackTimestamp
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
const processMessage = async (message, { shouldProcessHistoryMsg, placeholderResendCache, ev, creds, signalRepository, keyStore, logger, options, getMessage }) => {
|
|
232
|
+
const meId = creds.me.id;
|
|
233
|
+
const { accountSettings } = creds;
|
|
234
|
+
const chat = { id: jidNormalizedUser(getChatId(message.key)) };
|
|
235
|
+
const isRealMsg = isRealMessage(message);
|
|
236
|
+
if (isRealMsg) {
|
|
237
|
+
chat.messages = [{ message }];
|
|
238
|
+
chat.conversationTimestamp = toNumber(message.messageTimestamp);
|
|
239
|
+
|
|
240
|
+
if (shouldIncrementChatUnread(message)) {
|
|
241
|
+
chat.unreadCount = (chat.unreadCount || 0) + 1;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const content = normalizeMessageContent(message.message);
|
|
245
|
+
|
|
246
|
+
if ((isRealMsg || content?.reactionMessage?.key?.fromMe) && accountSettings?.unarchiveChats) {
|
|
247
|
+
chat.archived = false;
|
|
248
|
+
chat.readOnly = false;
|
|
249
|
+
}
|
|
250
|
+
const protocolMsg = content?.protocolMessage;
|
|
251
|
+
if (protocolMsg) {
|
|
252
|
+
|
|
253
|
+
const SELF_ONLY_TYPES = new Set([
|
|
254
|
+
proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION,
|
|
255
|
+
proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE,
|
|
256
|
+
proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC,
|
|
257
|
+
proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE
|
|
258
|
+
]);
|
|
259
|
+
if (protocolMsg.type !== null &&
|
|
260
|
+
protocolMsg.type !== undefined &&
|
|
261
|
+
SELF_ONLY_TYPES.has(protocolMsg.type) &&
|
|
262
|
+
!message.key.fromMe) {
|
|
263
|
+
logger?.warn({ msgId: message.key.id, type: protocolMsg.type, from: message.key.participant || message.key.remoteJid }, 'dropping spoofed self-only protocolMessage from non-self origin');
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
switch (protocolMsg.type) {
|
|
267
|
+
case proto.Message.ProtocolMessage.Type.HISTORY_SYNC_NOTIFICATION:
|
|
268
|
+
const histNotification = protocolMsg.historySyncNotification;
|
|
269
|
+
const process = shouldProcessHistoryMsg;
|
|
270
|
+
const isLatest = !creds.processedHistoryMessages?.length;
|
|
271
|
+
logger?.info({
|
|
272
|
+
histNotification,
|
|
273
|
+
process,
|
|
274
|
+
id: message.key.id,
|
|
275
|
+
isLatest
|
|
276
|
+
}, 'got history notification');
|
|
277
|
+
if (process) {
|
|
278
|
+
|
|
279
|
+
if (histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND) {
|
|
280
|
+
ev.emit('creds.update', {
|
|
281
|
+
processedHistoryMessages: [
|
|
282
|
+
...(creds.processedHistoryMessages || []),
|
|
283
|
+
{ key: message.key, messageTimestamp: message.messageTimestamp }
|
|
284
|
+
]
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
const data = await downloadAndProcessHistorySyncNotification(histNotification, options, logger);
|
|
288
|
+
if (data.lidPnMappings?.length) {
|
|
289
|
+
logger?.debug({ count: data.lidPnMappings.length }, 'processing LID-PN mappings from history sync');
|
|
290
|
+
await signalRepository.lidMapping
|
|
291
|
+
.storeLIDPNMappings(data.lidPnMappings)
|
|
292
|
+
.catch(err => logger?.warn({ err }, 'failed to store LID-PN mappings from history sync'));
|
|
293
|
+
}
|
|
294
|
+
await storeTcTokensFromHistorySync(data.chats, signalRepository, keyStore, logger);
|
|
295
|
+
ev.emit('messaging-history.set', {
|
|
296
|
+
...data,
|
|
297
|
+
isLatest: histNotification.syncType !== proto.HistorySync.HistorySyncType.ON_DEMAND ? isLatest : undefined,
|
|
298
|
+
chunkOrder: histNotification.chunkOrder,
|
|
299
|
+
peerDataRequestSessionId: histNotification.peerDataRequestSessionId
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
break;
|
|
303
|
+
case proto.Message.ProtocolMessage.Type.APP_STATE_SYNC_KEY_SHARE:
|
|
304
|
+
const keys = protocolMsg.appStateSyncKeyShare.keys;
|
|
305
|
+
if (keys?.length) {
|
|
306
|
+
let newAppStateSyncKeyId = '';
|
|
307
|
+
await keyStore.transaction(async () => {
|
|
308
|
+
const newKeys = [];
|
|
309
|
+
for (const { keyData, keyId } of keys) {
|
|
310
|
+
const strKeyId = Buffer.from(keyId.keyId).toString('base64');
|
|
311
|
+
newKeys.push(strKeyId);
|
|
312
|
+
await keyStore.set({ 'app-state-sync-key': { [strKeyId]: keyData } });
|
|
313
|
+
newAppStateSyncKeyId = strKeyId;
|
|
314
|
+
}
|
|
315
|
+
logger?.info({ newAppStateSyncKeyId, newKeys }, 'injecting new app state sync keys');
|
|
316
|
+
}, meId);
|
|
317
|
+
ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId });
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
logger?.info({ protocolMsg }, 'recv app state sync with 0 keys');
|
|
321
|
+
}
|
|
322
|
+
break;
|
|
323
|
+
case proto.Message.ProtocolMessage.Type.REVOKE:
|
|
324
|
+
ev.emit('messages.update', [
|
|
325
|
+
{
|
|
326
|
+
key: {
|
|
327
|
+
...message.key,
|
|
328
|
+
id: protocolMsg.key.id
|
|
329
|
+
},
|
|
330
|
+
update: { message: null, messageStubType: WAMessageStubType.REVOKE, key: message.key }
|
|
331
|
+
}
|
|
332
|
+
]);
|
|
333
|
+
break;
|
|
334
|
+
case proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING:
|
|
335
|
+
Object.assign(chat, {
|
|
336
|
+
ephemeralSettingTimestamp: toNumber(message.messageTimestamp),
|
|
337
|
+
ephemeralExpiration: protocolMsg.ephemeralExpiration || null
|
|
338
|
+
});
|
|
339
|
+
break;
|
|
340
|
+
case proto.Message.ProtocolMessage.Type.PEER_DATA_OPERATION_REQUEST_RESPONSE_MESSAGE:
|
|
341
|
+
const response = protocolMsg.peerDataOperationRequestResponseMessage;
|
|
342
|
+
if (response) {
|
|
343
|
+
|
|
344
|
+
const peerDataOperationResult = response.peerDataOperationResult || [];
|
|
345
|
+
for (const result of peerDataOperationResult) {
|
|
346
|
+
const retryResponse = result?.placeholderMessageResendResponse;
|
|
347
|
+
|
|
348
|
+
if (!retryResponse?.webMessageInfoBytes) {
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
try {
|
|
353
|
+
const webMessageInfo = proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes);
|
|
354
|
+
const msgId = webMessageInfo.key?.id;
|
|
355
|
+
|
|
356
|
+
const cachedData = msgId ? await placeholderResendCache?.get(msgId) : undefined;
|
|
357
|
+
|
|
358
|
+
if (msgId) {
|
|
359
|
+
await placeholderResendCache?.del(msgId);
|
|
360
|
+
}
|
|
361
|
+
let finalMsg;
|
|
362
|
+
|
|
363
|
+
if (cachedData && typeof cachedData === 'object') {
|
|
364
|
+
|
|
365
|
+
cachedData.message = webMessageInfo.message;
|
|
366
|
+
|
|
367
|
+
if (webMessageInfo.messageTimestamp) {
|
|
368
|
+
cachedData.messageTimestamp = webMessageInfo.messageTimestamp;
|
|
369
|
+
}
|
|
370
|
+
finalMsg = cachedData;
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
finalMsg = webMessageInfo;
|
|
374
|
+
}
|
|
375
|
+
logger?.debug({ msgId, requestId: response.stanzaId }, 'received placeholder resend');
|
|
376
|
+
ev.emit('messages.upsert', {
|
|
377
|
+
messages: [finalMsg],
|
|
378
|
+
type: 'notify',
|
|
379
|
+
requestId: response.stanzaId
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
catch (err) {
|
|
383
|
+
logger?.warn({ err, stanzaId: response.stanzaId }, 'failed to decode placeholder resend response');
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
break;
|
|
388
|
+
case proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
|
|
389
|
+
ev.emit('messages.update', [
|
|
390
|
+
{
|
|
391
|
+
|
|
392
|
+
key: { ...message.key, id: protocolMsg.key?.id },
|
|
393
|
+
update: {
|
|
394
|
+
message: {
|
|
395
|
+
editedMessage: {
|
|
396
|
+
message: protocolMsg.editedMessage
|
|
397
|
+
}
|
|
398
|
+
},
|
|
399
|
+
messageTimestamp: protocolMsg.timestampMs
|
|
400
|
+
? Math.floor(toNumber(protocolMsg.timestampMs) / 1000)
|
|
401
|
+
: message.messageTimestamp
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
]);
|
|
405
|
+
break;
|
|
406
|
+
case proto.Message.ProtocolMessage.Type.GROUP_MEMBER_LABEL_CHANGE:
|
|
407
|
+
const labelAssociationMsg = protocolMsg.memberLabel;
|
|
408
|
+
if (labelAssociationMsg?.label) {
|
|
409
|
+
ev.emit('group.member-tag.update', {
|
|
410
|
+
groupId: chat.id,
|
|
411
|
+
label: labelAssociationMsg.label,
|
|
412
|
+
participant: message.key.participant,
|
|
413
|
+
participantAlt: message.key.participantAlt,
|
|
414
|
+
messageTimestamp: Number(message.messageTimestamp)
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
break;
|
|
418
|
+
case proto.Message.ProtocolMessage.Type.LID_MIGRATION_MAPPING_SYNC:
|
|
419
|
+
const encodedPayload = protocolMsg.lidMigrationMappingSyncMessage?.encodedMappingPayload;
|
|
420
|
+
const { pnToLidMappings, chatDbMigrationTimestamp } = proto.LIDMigrationMappingSyncPayload.decode(encodedPayload);
|
|
421
|
+
logger?.debug({ pnToLidMappings, chatDbMigrationTimestamp }, 'got lid mappings and chat db migration timestamp');
|
|
422
|
+
const pairs = [];
|
|
423
|
+
for (const { pn, latestLid, assignedLid } of pnToLidMappings) {
|
|
424
|
+
const lid = latestLid || assignedLid;
|
|
425
|
+
pairs.push({ lid: `${lid}@lid`, pn: `${pn}@s.whatsapp.net` });
|
|
426
|
+
}
|
|
427
|
+
await signalRepository.lidMapping.storeLIDPNMappings(pairs);
|
|
428
|
+
if (pairs.length) {
|
|
429
|
+
for (const { pn, lid } of pairs) {
|
|
430
|
+
await signalRepository.migrateSession(pn, lid);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
else if (content?.reactionMessage) {
|
|
436
|
+
const reaction = {
|
|
437
|
+
...content.reactionMessage,
|
|
438
|
+
key: message.key
|
|
439
|
+
};
|
|
440
|
+
ev.emit('messages.reaction', [
|
|
441
|
+
{
|
|
442
|
+
reaction,
|
|
443
|
+
key: content.reactionMessage?.key
|
|
444
|
+
}
|
|
445
|
+
]);
|
|
446
|
+
}
|
|
447
|
+
else if (content?.encEventResponseMessage) {
|
|
448
|
+
const encEventResponse = content.encEventResponseMessage;
|
|
449
|
+
const creationMsgKey = encEventResponse.eventCreationMessageKey;
|
|
450
|
+
|
|
451
|
+
const eventMsg = await getMessage(creationMsgKey);
|
|
452
|
+
if (eventMsg) {
|
|
453
|
+
try {
|
|
454
|
+
const meIdNormalised = jidNormalizedUser(meId);
|
|
455
|
+
|
|
456
|
+
const eventCreatorKey = creationMsgKey.participant || creationMsgKey.remoteJid;
|
|
457
|
+
const eventCreatorPn = isLidUser(eventCreatorKey)
|
|
458
|
+
? await signalRepository.lidMapping.getPNForLID(eventCreatorKey)
|
|
459
|
+
: eventCreatorKey;
|
|
460
|
+
const eventCreatorJid = getKeyAuthor({ remoteJid: jidNormalizedUser(eventCreatorPn), fromMe: meIdNormalised === eventCreatorPn }, meIdNormalised);
|
|
461
|
+
const responderJid = getKeyAuthor(message.key, meIdNormalised);
|
|
462
|
+
const eventEncKey = eventMsg?.messageContextInfo?.messageSecret;
|
|
463
|
+
if (!eventEncKey) {
|
|
464
|
+
logger?.warn({ creationMsgKey }, 'event response: missing messageSecret for decryption');
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
const responseMsg = decryptEventResponse(encEventResponse, {
|
|
468
|
+
eventEncKey,
|
|
469
|
+
eventCreatorJid,
|
|
470
|
+
eventMsgId: creationMsgKey.id,
|
|
471
|
+
responderJid
|
|
472
|
+
});
|
|
473
|
+
const eventResponse = {
|
|
474
|
+
eventResponseMessageKey: message.key,
|
|
475
|
+
senderTimestampMs: responseMsg.timestampMs,
|
|
476
|
+
response: responseMsg
|
|
477
|
+
};
|
|
478
|
+
ev.emit('messages.update', [
|
|
479
|
+
{
|
|
480
|
+
key: creationMsgKey,
|
|
481
|
+
update: {
|
|
482
|
+
eventResponses: [eventResponse]
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
]);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
catch (err) {
|
|
489
|
+
logger?.warn({ err, creationMsgKey }, 'failed to decrypt event response');
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
logger?.warn({ creationMsgKey }, 'event creation message not found, cannot decrypt response');
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
else if (content?.secretEncryptedMessage &&
|
|
497
|
+
content.secretEncryptedMessage.secretEncType === proto.Message.SecretEncryptedMessage.SecretEncType.MESSAGE_EDIT) {
|
|
498
|
+
const secEnc = content.secretEncryptedMessage;
|
|
499
|
+
const targetKey = secEnc.targetMessageKey;
|
|
500
|
+
const targetId = typeof targetKey?.id === 'string' ? targetKey.id.trim() : '';
|
|
501
|
+
const targetRemoteJid = targetKey?.remoteJid || targetKey?.remoteJidAlt;
|
|
502
|
+
if (!targetKey || !targetId || !targetRemoteJid) {
|
|
503
|
+
logger?.warn({ targetKey }, 'message edit: incomplete targetMessageKey — cannot decrypt');
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
const lookupTargetKey = {
|
|
507
|
+
...targetKey,
|
|
508
|
+
remoteJid: targetKey.remoteJid || targetKey.remoteJidAlt,
|
|
509
|
+
participant: targetKey.participant || targetKey.participantAlt
|
|
510
|
+
};
|
|
511
|
+
const targetMsg = await getMessage(lookupTargetKey);
|
|
512
|
+
if (!targetMsg) {
|
|
513
|
+
logger?.warn({ targetKey }, 'original message not found in store, cannot decrypt secretEncryptedMessage edit');
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
try {
|
|
517
|
+
const meIdNormalised = jidNormalizedUser(meId);
|
|
518
|
+
const editEncKey = targetMsg?.messageContextInfo?.messageSecret;
|
|
519
|
+
if (!editEncKey?.length) {
|
|
520
|
+
logger?.warn({ targetKey }, 'message edit: missing messageSecret on original message — cannot decrypt');
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
const originalSenderJid = await resolveMessageEditAuthor(targetKey, meIdNormalised, signalRepository.lidMapping);
|
|
524
|
+
if (!originalSenderJid) {
|
|
525
|
+
logger?.warn({ targetKey }, 'message edit: original sender JID unavailable — cannot decrypt');
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
const editorJid = await resolveMessageEditAuthor(message.key, meIdNormalised, signalRepository.lidMapping);
|
|
529
|
+
if (!editorJid) {
|
|
530
|
+
logger?.warn({ key: message.key, targetKey }, 'message edit: editor JID unavailable — cannot decrypt');
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
if (!secEnc.encPayload?.length || !secEnc.encIv?.length) {
|
|
534
|
+
logger?.warn({ targetKey }, 'message edit: encrypted payload or IV missing — cannot decrypt');
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const update = buildEditUpdate({
|
|
538
|
+
editEncKey,
|
|
539
|
+
encPayload: secEnc.encPayload,
|
|
540
|
+
encIv: secEnc.encIv,
|
|
541
|
+
originalSenderJid,
|
|
542
|
+
originalMsgId: targetId,
|
|
543
|
+
editorJid,
|
|
544
|
+
targetKey,
|
|
545
|
+
fallbackTimestamp: toNumber(message.messageTimestamp),
|
|
546
|
+
logger
|
|
547
|
+
});
|
|
548
|
+
if (update) {
|
|
549
|
+
ev.emit('messages.update', [update]);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
catch (err) {
|
|
553
|
+
logger?.warn({ err, targetKey }, 'failed to decrypt secretEncryptedMessage MESSAGE_EDIT');
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
else if (message.messageStubType) {
|
|
557
|
+
const jid = message.key?.remoteJid;
|
|
558
|
+
|
|
559
|
+
let participants;
|
|
560
|
+
const emitParticipantsUpdate = (action) => ev.emit('group-participants.update', {
|
|
561
|
+
id: jid,
|
|
562
|
+
author: message.key.participant,
|
|
563
|
+
authorPn: message.key.participantAlt,
|
|
564
|
+
authorUsername: message.key.participantUsername,
|
|
565
|
+
participants,
|
|
566
|
+
action
|
|
567
|
+
});
|
|
568
|
+
const emitGroupUpdate = (update) => {
|
|
569
|
+
ev.emit('groups.update', [
|
|
570
|
+
{
|
|
571
|
+
id: jid,
|
|
572
|
+
...update,
|
|
573
|
+
author: message.key.participant ?? undefined,
|
|
574
|
+
authorPn: message.key.participantAlt,
|
|
575
|
+
authorUsername: message.key.participantUsername
|
|
576
|
+
}
|
|
577
|
+
]);
|
|
578
|
+
};
|
|
579
|
+
const emitGroupRequestJoin = (participant, action, method) => {
|
|
580
|
+
ev.emit('group.join-request', {
|
|
581
|
+
id: jid,
|
|
582
|
+
author: message.key.participant,
|
|
583
|
+
authorPn: message.key.participantAlt,
|
|
584
|
+
authorUsername: message.key.participantUsername,
|
|
585
|
+
participant: participant.lid,
|
|
586
|
+
participantPn: participant.pn,
|
|
587
|
+
action,
|
|
588
|
+
method: method
|
|
589
|
+
});
|
|
590
|
+
};
|
|
591
|
+
const participantsIncludesMe = () => participants.find(jid => areJidsSameUser(meId, jid.phoneNumber));
|
|
592
|
+
const readStubJson = (index) => {
|
|
593
|
+
const raw = message.messageStubParameters?.[index];
|
|
594
|
+
if (typeof raw !== 'string') {
|
|
595
|
+
return undefined;
|
|
596
|
+
}
|
|
597
|
+
try {
|
|
598
|
+
return JSON.parse(raw);
|
|
599
|
+
}
|
|
600
|
+
catch (error) {
|
|
601
|
+
logger?.warn?.({ jid, stubType: message.messageStubType, raw }, 'group stub parameter is not JSON, skipping it');
|
|
602
|
+
return undefined;
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
const readStubParticipants = () => {
|
|
606
|
+
const raw = message.messageStubParameters;
|
|
607
|
+
if (!Array.isArray(raw)) {
|
|
608
|
+
logger?.warn?.({ jid, stubType: message.messageStubType }, 'group stub carries no parameters, treating it as an empty participant list');
|
|
609
|
+
return [];
|
|
610
|
+
}
|
|
611
|
+
return raw.map((_, index) => readStubJson(index)).filter(entry => entry !== undefined);
|
|
612
|
+
};
|
|
613
|
+
switch (message.messageStubType) {
|
|
614
|
+
case WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
|
|
615
|
+
participants = readStubParticipants();
|
|
616
|
+
emitParticipantsUpdate('modify');
|
|
617
|
+
break;
|
|
618
|
+
case WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
|
|
619
|
+
case WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
|
|
620
|
+
participants = readStubParticipants();
|
|
621
|
+
emitParticipantsUpdate('remove');
|
|
622
|
+
|
|
623
|
+
if (participantsIncludesMe()) {
|
|
624
|
+
chat.readOnly = true;
|
|
625
|
+
}
|
|
626
|
+
break;
|
|
627
|
+
case WAMessageStubType.GROUP_PARTICIPANT_ADD:
|
|
628
|
+
case WAMessageStubType.GROUP_PARTICIPANT_INVITE:
|
|
629
|
+
case WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
|
|
630
|
+
participants = readStubParticipants();
|
|
631
|
+
if (participantsIncludesMe()) {
|
|
632
|
+
chat.readOnly = false;
|
|
633
|
+
}
|
|
634
|
+
emitParticipantsUpdate('add');
|
|
635
|
+
break;
|
|
636
|
+
case WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
|
|
637
|
+
participants = readStubParticipants();
|
|
638
|
+
emitParticipantsUpdate('demote');
|
|
639
|
+
break;
|
|
640
|
+
case WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
|
|
641
|
+
participants = readStubParticipants();
|
|
642
|
+
emitParticipantsUpdate('promote');
|
|
643
|
+
break;
|
|
644
|
+
case WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
|
|
645
|
+
const announceValue = message.messageStubParameters?.[0];
|
|
646
|
+
emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' });
|
|
647
|
+
break;
|
|
648
|
+
case WAMessageStubType.GROUP_CHANGE_RESTRICT:
|
|
649
|
+
const restrictValue = message.messageStubParameters?.[0];
|
|
650
|
+
emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' });
|
|
651
|
+
break;
|
|
652
|
+
case WAMessageStubType.GROUP_CHANGE_SUBJECT:
|
|
653
|
+
const name = message.messageStubParameters?.[0];
|
|
654
|
+
chat.name = name;
|
|
655
|
+
emitGroupUpdate({ subject: name });
|
|
656
|
+
break;
|
|
657
|
+
case WAMessageStubType.GROUP_CHANGE_DESCRIPTION:
|
|
658
|
+
const description = message.messageStubParameters?.[0];
|
|
659
|
+
chat.description = description;
|
|
660
|
+
emitGroupUpdate({ desc: description });
|
|
661
|
+
break;
|
|
662
|
+
case WAMessageStubType.GROUP_CHANGE_INVITE_LINK:
|
|
663
|
+
const code = message.messageStubParameters?.[0];
|
|
664
|
+
emitGroupUpdate({ inviteCode: code });
|
|
665
|
+
break;
|
|
666
|
+
case WAMessageStubType.GROUP_MEMBER_ADD_MODE:
|
|
667
|
+
const memberAddValue = message.messageStubParameters?.[0];
|
|
668
|
+
emitGroupUpdate({ memberAddMode: memberAddValue === 'all_member_add' });
|
|
669
|
+
break;
|
|
670
|
+
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE:
|
|
671
|
+
const approvalMode = message.messageStubParameters?.[0];
|
|
672
|
+
emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' });
|
|
673
|
+
break;
|
|
674
|
+
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
|
|
675
|
+
const participant = readStubJson(0);
|
|
676
|
+
const action = message.messageStubParameters?.[1];
|
|
677
|
+
const method = message.messageStubParameters?.[2];
|
|
678
|
+
if (participant) {
|
|
679
|
+
emitGroupRequestJoin(participant, action, method);
|
|
680
|
+
}
|
|
681
|
+
break;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
if (Object.keys(chat).length > 1) {
|
|
685
|
+
ev.emit('chats.update', [chat]);
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
export default processMessage;
|