mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: process pollupdate automatically
This commit is contained in:
@@ -764,6 +764,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
keyStore: authState.keys,
|
||||
logger,
|
||||
options: config.options,
|
||||
getMessage: config.getMessage,
|
||||
}
|
||||
)
|
||||
])
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user