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 { 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' import MAIN_LOGGER from '../src/Utils/logger'
const logger = MAIN_LOGGER.child({ }) const logger = MAIN_LOGGER.child({ })
@@ -39,6 +39,9 @@ const startSock = async() => {
}, },
msgRetryCounterMap, msgRetryCounterMap,
generateHighQualityLinkPreview: true, generateHighQualityLinkPreview: true,
// ignore all broadcast messages -- to receive the same
// comment the line below out
shouldIgnoreJid: jid => isJidBroadcast(jid),
// implement to handle retries // implement to handle retries
getMessage: async key => { getMessage: async key => {
if(store) { if(store) {

View File

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

View File

@@ -13,7 +13,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const { const {
logger, logger,
retryRequestDelayMs, retryRequestDelayMs,
getMessage getMessage,
shouldIgnoreJid
} = config } = config
const sock = makeMessagesSocket(config) const sock = makeMessagesSocket(config)
const { const {
@@ -400,12 +401,6 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const remoteJid = !isNodeFromMe || isJidGroup(attrs.from) ? attrs.from : attrs.recipient const remoteJid = !isNodeFromMe || isJidGroup(attrs.from) ? attrs.from : attrs.recipient
const fromMe = !attrs.recipient || (attrs.type === 'retry' && isNodeFromMe) 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 = { const key: proto.IMessageKey = {
remoteJid, remoteJid,
id: '', id: '',
@@ -413,6 +408,18 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
participant: attrs.participant 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([ await Promise.all([
processingMutex.mutex( processingMutex.mutex(
async() => { async() => {
@@ -478,6 +485,12 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const handleNotification = async(node: BinaryNode) => { const handleNotification = async(node: BinaryNode) => {
const remoteJid = node.attrs.from const remoteJid = node.attrs.from
if(shouldIgnoreJid(remoteJid)) {
logger.debug({ remoteJid, id: node.attrs.id }, 'ignored notification')
await sendMessageAck(node)
return
}
await Promise.all([ await Promise.all([
processingMutex.mutex( processingMutex.mutex(
async() => { async() => {
@@ -504,7 +517,14 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
} }
const handleMessage = async(node: BinaryNode) => { 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([ await Promise.all([
processingMutex.mutex( processingMutex.mutex(
async() => { async() => {

View File

@@ -70,6 +70,13 @@ export type SocketConfig = {
* */ * */
generateHighQualityLinkPreview: boolean 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 */ /** verify app state MACs */
appStateMacVerification: { appStateMacVerification: {
patch: boolean patch: boolean

View File

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