diff --git a/Example/example.ts b/Example/example.ts index afba810..30b3c27 100644 --- a/Example/example.ts +++ b/Example/example.ts @@ -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) { diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index 8c3a360..a348b93 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -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, diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 862e1b1..4ec5dd0 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -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() => { diff --git a/src/Types/Socket.ts b/src/Types/Socket.ts index 4e43cf8..a6d9685 100644 --- a/src/Types/Socket.ts +++ b/src/Types/Socket.ts @@ -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 diff --git a/src/Utils/decode-wa-message.ts b/src/Utils/decode-wa-message.ts index daba362..27e0aa6 100644 --- a/src/Utils/decode-wa-message.ts +++ b/src/Utils/decode-wa-message.ts @@ -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] } - })() + } } } \ No newline at end of file