feat: add clean message util before calling "messages.upsert"

This commit is contained in:
Adhiraj Singh
2022-05-23 12:32:43 +05:30
parent e131b7c4d3
commit ebec02908c
3 changed files with 22 additions and 9 deletions

View File

@@ -4,7 +4,7 @@ import { KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults'
import { BaileysEventMap, MessageReceiptType, MessageUserReceipt, SocketConfig, WACallEvent, WAMessageStubType } from '../Types'
import { debouncedTimeout, decodeMessageStanza, delay, encodeBigEndian, generateSignalPubKey, getCallStatusFromNode, getNextPreKeys, getStatusFromReceiptType, normalizeMessageContent, unixTimestampSeconds, xmppPreKey, xmppSignedPreKey } from '../Utils'
import { makeKeyedMutex, makeMutex } from '../Utils/make-mutex'
import processMessage from '../Utils/process-message'
import processMessage, { cleanMessage } from '../Utils/process-message'
import { areJidsSameUser, BinaryNode, BinaryNodeAttributes, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isJidUser, jidDecode, jidEncode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary'
import { makeChatsSocket } from './chats'
import { extractGroupMetadata } from './groups'
@@ -505,7 +505,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
await sendReceipt(msg.key.remoteJid!, participant, [msg.key.id!], type)
}
msg.key.remoteJid = jidNormalizedUser(msg.key.remoteJid!)
cleanMessage(msg)
ev.emit('messages.upsert', { messages: [msg], type: stanza.attrs.offline ? 'append' : 'notify' })
}
)

View File

@@ -490,7 +490,7 @@ export const getContentType = (content: WAProto.IMessage | undefined) => {
* @param content
* @returns
*/
export const normalizeMessageContent = (content: WAMessageContent): WAMessageContent => {
export const normalizeMessageContent = (content: WAMessageContent | undefined): WAMessageContent => {
content = content?.ephemeralMessage?.message?.viewOnceMessage?.message ||
content?.ephemeralMessage?.message ||
content?.viewOnceMessage?.message ||

View File

@@ -21,6 +21,21 @@ const MSG_MISSED_CALL_TYPES = new Set([
WAMessageStubType.CALL_MISSED_VOICE
])
/** Cleans a received message to further processing */
export const cleanMessage = (message: proto.IWebMessageInfo) => {
// ensure remoteJid doesn't have device or agent in it
message.key.remoteJid = jidNormalizedUser(message.key.remoteJid!)
const content = normalizeMessageContent(message.message)
if(content) {
// if the message has a reaction, ensure fromMe & remoteJid are from our perspective
const msgKey = content.reactionMessage!.key!
if(!message.key.fromMe) {
msgKey.remoteJid = message.key.remoteJid
msgKey.fromMe = !msgKey.fromMe
}
}
}
const processMessage = async(
message: proto.IWebMessageInfo,
{ downloadHistory, historyCache, meId, keyStore, accountSettings, logger, treatCiphertextMessagesAsReal }: ProcessMessageContext
@@ -124,13 +139,11 @@ const processMessage = async(
key: message.key,
}
const operation = content.reactionMessage?.text ? 'add' : 'remove'
const msgKey = content.reactionMessage!.key!
if(!message.key.fromMe) {
msgKey.remoteJid = message.key.remoteJid
msgKey.fromMe = !msgKey.fromMe
map['messages.reaction'] = {
reaction,
key: content.reactionMessage!.key!,
operation
}
map['messages.reaction'] = { reaction, key: msgKey, operation }
} else if(message.messageStubType) {
const jid = message.key!.remoteJid!
//let actor = whatsappID (message.participant)