From 4f27a2b3f42b5f310edf3f3b00f6b15be6b9cdf0 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Wed, 18 Jan 2023 13:43:13 +0530 Subject: [PATCH] feat: add "decodeMessageNode" util --- src/Socket/messages-recv.ts | 4 ++-- src/Utils/decode-wa-message.ts | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 7310b4d..7b948ec 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -2,7 +2,7 @@ import { proto } from '../../WAProto' import { KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults' import { MessageReceiptType, MessageRelayOptions, MessageUserReceipt, SocketConfig, WACallEvent, WAMessageKey, WAMessageStubType, WAPatchName } from '../Types' -import { decodeMediaRetryNode, decodeMessageStanza, delay, encodeBigEndian, encodeSignedDeviceIdentity, getCallStatusFromNode, getHistoryMsg, getNextPreKeys, getStatusFromReceiptType, unixTimestampSeconds, xmppPreKey, xmppSignedPreKey } from '../Utils' +import { decodeMediaRetryNode, decryptMessageNode, delay, encodeBigEndian, encodeSignedDeviceIdentity, getCallStatusFromNode, getHistoryMsg, getNextPreKeys, getStatusFromReceiptType, unixTimestampSeconds, xmppPreKey, xmppSignedPreKey } from '../Utils' import { makeMutex } from '../Utils/make-mutex' import { cleanMessage } from '../Utils/process-message' import { areJidsSameUser, BinaryNode, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isJidUser, jidDecode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary' @@ -535,7 +535,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { } const handleMessage = async(node: BinaryNode) => { - const { fullMessage: msg, category, author, decrypt } = decodeMessageStanza(node, authState) + const { fullMessage: msg, category, author, decrypt } = decryptMessageNode(node, authState) if(shouldIgnoreJid(msg.key.remoteJid!)) { logger.debug({ key: msg.key }, 'ignored message') await sendMessageAck(node) diff --git a/src/Utils/decode-wa-message.ts b/src/Utils/decode-wa-message.ts index 27e0aa6..c526186 100644 --- a/src/Utils/decode-wa-message.ts +++ b/src/Utils/decode-wa-message.ts @@ -9,7 +9,11 @@ const NO_MESSAGE_FOUND_ERROR_TEXT = 'Message absent from node' type MessageType = 'chat' | 'peer_broadcast' | 'other_broadcast' | 'group' | 'direct_peer_status' | 'other_status' -export const decodeMessageStanza = (stanza: BinaryNode, auth: AuthenticationState) => { +/** + * Decode the received node as a message. + * @note this will only parse the message, not decrypt it + */ +export function decodeMessageNode(stanza: BinaryNode, meId: string) { let msgType: MessageType let chatId: string let author: string @@ -19,7 +23,7 @@ export const decodeMessageStanza = (stanza: BinaryNode, auth: AuthenticationStat const participant: string | undefined = stanza.attrs.participant const recipient: string | undefined = stanza.attrs.recipient - const isMe = (jid: string) => areJidsSameUser(jid, auth.creds.me!.id) + const isMe = (jid: string) => areJidsSameUser(jid, meId) if(isJidUser(from)) { if(recipient) { @@ -60,8 +64,6 @@ export const decodeMessageStanza = (stanza: BinaryNode, auth: AuthenticationStat throw new Boom('Unknown message type', { data: stanza }) } - const sender = msgType === 'chat' ? author : chatId - const fromMe = isMe(stanza.attrs.participant || stanza.attrs.from) const pushname = stanza.attrs.notify @@ -75,13 +77,23 @@ export const decodeMessageStanza = (stanza: BinaryNode, auth: AuthenticationStat const fullMessage: proto.IWebMessageInfo = { key, messageTimestamp: +stanza.attrs.t, - pushName: pushname + pushName: pushname, + broadcast: isJidBroadcast(from) } if(key.fromMe) { fullMessage.status = proto.WebMessageInfo.Status.SERVER_ACK } + return { + fullMessage, + author, + sender: msgType === 'chat' ? author : chatId + } +} + +export const decryptMessageNode = (stanza: BinaryNode, auth: AuthenticationState) => { + const { fullMessage, author, sender } = decodeMessageNode(stanza, auth.creds.me!.id) return { fullMessage, category: stanza.attrs.category,