From 9591a02382cfc4bb9117ccaecccc66b1baa3e696 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Wed, 18 Jan 2023 14:33:16 +0530 Subject: [PATCH] feat: getChatId for broadcasts --- src/Utils/process-message.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Utils/process-message.ts b/src/Utils/process-message.ts index 9953f09..3a2f48c 100644 --- a/src/Utils/process-message.ts +++ b/src/Utils/process-message.ts @@ -3,7 +3,7 @@ import type { Logger } from 'pino' import { proto } from '../../WAProto' import { AuthenticationCreds, BaileysEventEmitter, Chat, GroupMetadata, ParticipantAction, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types' import { downloadAndProcessHistorySyncNotification, getContentType, normalizeMessageContent, toNumber } from '../Utils' -import { areJidsSameUser, jidNormalizedUser } from '../WABinary' +import { areJidsSameUser, isJidBroadcast, isJidStatusBroadcast, jidNormalizedUser } from '../WABinary' type ProcessMessageContext = { shouldProcessHistoryMsg: boolean @@ -72,6 +72,18 @@ export const shouldIncrementChatUnread = (message: proto.IWebMessageInfo) => ( !message.key.fromMe && !message.messageStubType ) +/** + * Get the ID of the chat from the given key. + * Typically -- that'll be the remoteJid, but for broadcasts, it'll be the participant + */ +export const getChatId = ({ remoteJid, participant }: proto.IMessageKey) => { + if(isJidBroadcast(remoteJid!) && !isJidStatusBroadcast(remoteJid!)) { + return participant! + } + + return remoteJid! +} + const processMessage = async( message: proto.IWebMessageInfo, { @@ -86,7 +98,7 @@ const processMessage = async( const meId = creds.me!.id const { accountSettings } = creds - const chat: Partial = { id: jidNormalizedUser(message.key.remoteJid!) } + const chat: Partial = { id: jidNormalizedUser(getChatId(message.key)) } const isRealMsg = isRealMessage(message, meId) if(isRealMsg) {