feat: add "decodeMessageNode" util

This commit is contained in:
Adhiraj Singh
2023-01-18 13:43:13 +05:30
parent c24ffc1bed
commit 4f27a2b3f4
2 changed files with 19 additions and 7 deletions

View File

@@ -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)

View File

@@ -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,