feat: process pollupdate automatically

This commit is contained in:
Adhiraj Singh
2023-03-02 22:43:31 +05:30
parent bda2bb4717
commit 9d8aa67f22
2 changed files with 64 additions and 4 deletions

View File

@@ -764,6 +764,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
keyStore: authState.keys,
logger,
options: config.options,
getMessage: config.getMessage,
}
)
])

View File

@@ -1,7 +1,7 @@
import { AxiosRequestConfig } from 'axios'
import type { Logger } from 'pino'
import { proto } from '../../WAProto'
import { AuthenticationCreds, BaileysEventEmitter, Chat, GroupMetadata, ParticipantAction, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types'
import { AuthenticationCreds, BaileysEventEmitter, Chat, GroupMetadata, ParticipantAction, SignalKeyStoreWithTransaction, SocketConfig, WAMessageStubType } from '../Types'
import { getContentType, normalizeMessageContent } from '../Utils/messages'
import { areJidsSameUser, isJidBroadcast, isJidStatusBroadcast, jidNormalizedUser } from '../WABinary'
import { aesDecryptGCM, hmacSign } from './crypto'
@@ -13,8 +13,9 @@ type ProcessMessageContext = {
creds: AuthenticationCreds
keyStore: SignalKeyStoreWithTransaction
ev: BaileysEventEmitter
getMessage: SocketConfig['getMessage']
logger?: Logger
options: AxiosRequestConfig<any>
options: AxiosRequestConfig<{}>
}
const REAL_MSG_STUB_TYPES = new Set([
@@ -36,7 +37,14 @@ export const cleanMessage = (message: proto.IWebMessageInfo, meId: string) => {
const content = normalizeMessageContent(message.message)
// if the message has a reaction, ensure fromMe & remoteJid are from our perspective
if(content?.reactionMessage) {
const msgKey = content.reactionMessage.key!
normaliseKey(content.reactionMessage.key!)
}
if(content?.pollUpdateMessage) {
normaliseKey(content.pollUpdateMessage.pollCreationMessageKey!)
}
function normaliseKey(msgKey: proto.IMessageKey) {
// if the reaction is from another user
// we've to correctly map the key to this user's perspective
if(!message.key.fromMe) {
@@ -69,6 +77,7 @@ export const isRealMessage = (message: proto.IWebMessageInfo, meId: string) => {
&& hasSomeContent
&& !normalizedContent?.protocolMessage
&& !normalizedContent?.reactionMessage
&& !normalizedContent?.pollUpdateMessage
}
export const shouldIncrementChatUnread = (message: proto.IWebMessageInfo) => (
@@ -136,6 +145,9 @@ export function decryptPollVote(
function toBinary(txt: string) {
return Buffer.from(txt)
}
}
const processMessage = async(
message: proto.IWebMessageInfo,
{
@@ -144,7 +156,8 @@ const processMessage = async(
creds,
keyStore,
logger,
options
options,
getMessage
}: ProcessMessageContext
) => {
const meId = creds.me!.id
@@ -321,6 +334,52 @@ const processMessage = async(
emitGroupUpdate({ inviteCode: code })
break
}
} else if(content?.pollUpdateMessage) {
const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey!
// we need to fetch the poll creation message to get the poll enc key
const pollMsg = await getMessage(creationMsgKey)
if(pollMsg) {
const meIdNormalised = jidNormalizedUser(meId)
const pollCreatorJid = getKeyAuthor(creationMsgKey, meIdNormalised)
const voterJid = getKeyAuthor(message.key!, meIdNormalised)
const pollEncKey = pollMsg.messageContextInfo?.messageSecret!
try {
const voteMsg = decryptPollVote(
content.pollUpdateMessage.vote!,
{
pollEncKey,
pollCreatorJid,
pollMsgId: creationMsgKey.id!,
voterJid,
}
)
ev.emit('messages.update', [
{
key: creationMsgKey,
update: {
pollUpdates: [
{
pollUpdateMessageKey: message.key,
vote: voteMsg,
senderTimestampMs: message.messageTimestamp,
}
]
}
}
])
} catch(err) {
logger?.warn(
{ err, creationMsgKey },
'failed to decrypt poll vote'
)
}
} else {
logger?.warn(
{ creationMsgKey },
'poll creation message not found, cannot decrypt update'
)
}
}
if(Object.keys(chat).length > 1) {