From b9d37a06a3ac5723765d01fedc43e89d280de0d6 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Fri, 26 Nov 2021 10:31:46 +0530 Subject: [PATCH] fix: incorrect updates being forwarded to messages.update --- src/Socket/messages-recv.ts | 48 ++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 4be4838..46921d7 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -7,7 +7,15 @@ import { KEY_BUNDLE_TYPE } from "../Defaults" import { makeChatsSocket } from "./chats" import { extractGroupMetadata } from "./groups" -const isReadReceipt = (type: string) => type === 'read' || type === 'read-self' +const getStatusFromReceiptType = (type: string | undefined) => { + if(type === 'read' || type === 'read-self') { + return proto.WebMessageInfo.WebMessageInfoStatus.READ + } + if(typeof type === 'undefined') { + return proto.WebMessageInfo.WebMessageInfoStatus.DELIVERY_ACK + } + return undefined +} export const makeMessagesRecvSocket = (config: SocketConfig) => { const { logger } = config @@ -458,25 +466,27 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { const handleReceipt = async(node: BinaryNode) => { const { attrs, content } = node - const isRead = isReadReceipt(attrs.type) - const status = isRead ? proto.WebMessageInfo.WebMessageInfoStatus.READ : proto.WebMessageInfo.WebMessageInfoStatus.DELIVERY_ACK - const ids = [attrs.id] - if(Array.isArray(content)) { - const items = getBinaryNodeChildren(content[0], 'item') - ids.push(...items.map(i => i.attrs.id)) + const status = getStatusFromReceiptType(attrs.type) + + if(typeof status !== 'undefined' && !areJidsSameUser(attrs.from, authState.creds.me?.id)) { + const ids = [attrs.id] + if(Array.isArray(content)) { + const items = getBinaryNodeChildren(content[0], 'item') + ids.push(...items.map(i => i.attrs.id)) + } + + const remoteJid = attrs.recipient || attrs.from + const fromMe = attrs.recipient ? false : true + ev.emit('messages.update', ids.map(id => ({ + key: { + remoteJid, + id: id, + fromMe, + participant: attrs.participant + }, + update: { status } + }))) } - - const remoteJid = attrs.recipient || attrs.from - const fromMe = attrs.recipient ? false : true - ev.emit('messages.update', ids.map(id => ({ - key: { - remoteJid, - id: id, - fromMe, - participant: attrs.participant - }, - update: { status } - }))) await sendMessageAck(node, { class: 'receipt', type: attrs.type }) }