feat: allow ignoring jids

This commit is contained in:
Adhiraj Singh
2022-11-11 09:31:49 +05:30
parent 10d61d02cf
commit a58b73fba5
5 changed files with 42 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
import { Boom } from '@hapi/boom'
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, makeCacheableSignalKeyStore, makeInMemoryStore, MessageRetryMap, useMultiFileAuthState } from '../src'
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, isJidBroadcast, jidNormalizedUser, makeCacheableSignalKeyStore, makeInMemoryStore, MessageRetryMap, useMultiFileAuthState } from '../src'
import MAIN_LOGGER from '../src/Utils/logger'
const logger = MAIN_LOGGER.child({ })
@@ -39,6 +39,9 @@ const startSock = async() => {
},
msgRetryCounterMap,
generateHighQualityLinkPreview: true,
// ignore all broadcast messages -- to receive the same
// comment the line below out
shouldIgnoreJid: jid => isJidBroadcast(jid),
// implement to handle retries
getMessage: async key => {
if(store) {

View File

@@ -51,6 +51,7 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
markOnlineOnConnect: true,
syncFullHistory: false,
shouldSyncHistoryMessage: () => true,
shouldIgnoreJid: () => false,
linkPreviewImageThumbnailWidth: 192,
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
generateHighQualityLinkPreview: false,

View File

@@ -13,7 +13,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const {
logger,
retryRequestDelayMs,
getMessage
getMessage,
shouldIgnoreJid
} = config
const sock = makeMessagesSocket(config)
const {
@@ -400,12 +401,6 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const remoteJid = !isNodeFromMe || isJidGroup(attrs.from) ? attrs.from : attrs.recipient
const fromMe = !attrs.recipient || (attrs.type === 'retry' && isNodeFromMe)
const ids = [attrs.id]
if(Array.isArray(content)) {
const items = getBinaryNodeChildren(content[0], 'item')
ids.push(...items.map(i => i.attrs.id))
}
const key: proto.IMessageKey = {
remoteJid,
id: '',
@@ -413,6 +408,18 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
participant: attrs.participant
}
if(shouldIgnoreJid(remoteJid)) {
logger.debug({ remoteJid }, 'ignoring receipt from jid')
await sendMessageAck(node)
return
}
const ids = [attrs.id]
if(Array.isArray(content)) {
const items = getBinaryNodeChildren(content[0], 'item')
ids.push(...items.map(i => i.attrs.id))
}
await Promise.all([
processingMutex.mutex(
async() => {
@@ -478,6 +485,12 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const handleNotification = async(node: BinaryNode) => {
const remoteJid = node.attrs.from
if(shouldIgnoreJid(remoteJid)) {
logger.debug({ remoteJid, id: node.attrs.id }, 'ignored notification')
await sendMessageAck(node)
return
}
await Promise.all([
processingMutex.mutex(
async() => {
@@ -504,7 +517,14 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
}
const handleMessage = async(node: BinaryNode) => {
const { fullMessage: msg, category, author, decryptionTask } = decodeMessageStanza(node, authState)
const { fullMessage: msg, category, author, decrypt } = decodeMessageStanza(node, authState)
if(shouldIgnoreJid(msg.key.remoteJid!)) {
logger.debug({ key: msg.key }, 'ignored message')
await sendMessageAck(node)
return
}
const decryptionTask = decrypt()
await Promise.all([
processingMutex.mutex(
async() => {

View File

@@ -70,6 +70,13 @@ export type SocketConfig = {
* */
generateHighQualityLinkPreview: boolean
/**
* Returns if a jid should be ignored,
* no event for that jid will be triggered.
* Messages from that jid will also not be decrypted
* */
shouldIgnoreJid: (jid: string) => boolean | undefined
/** verify app state MACs */
appStateMacVerification: {
patch: boolean

View File

@@ -86,7 +86,7 @@ export const decodeMessageStanza = (stanza: BinaryNode, auth: AuthenticationStat
fullMessage,
category: stanza.attrs.category,
author,
decryptionTask: (async() => {
async decrypt() {
let decryptables = 0
if(Array.isArray(stanza.content)) {
for(const { tag, attrs, content } of stanza.content) {
@@ -146,6 +146,6 @@ export const decodeMessageStanza = (stanza: BinaryNode, auth: AuthenticationStat
fullMessage.messageStubType = proto.WebMessageInfo.StubType.CIPHERTEXT
fullMessage.messageStubParameters = [NO_MESSAGE_FOUND_ERROR_TEXT]
}
})()
}
}
}