mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
fix: delivery receipts
This commit is contained in:
@@ -18,6 +18,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
assertingPreKeys,
|
assertingPreKeys,
|
||||||
sendNode,
|
sendNode,
|
||||||
relayMessage,
|
relayMessage,
|
||||||
|
sendDeliveryReceipt,
|
||||||
} = sock
|
} = sock
|
||||||
|
|
||||||
const sendMessageAck = async({ attrs }: BinaryNode) => {
|
const sendMessageAck = async({ attrs }: BinaryNode) => {
|
||||||
@@ -28,7 +29,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
attrs: {
|
attrs: {
|
||||||
class: 'receipt',
|
class: 'receipt',
|
||||||
id: attrs.id,
|
id: attrs.id,
|
||||||
to: isGroup ? attrs.from : authState.creds.me!.id,
|
to: isGroup ? attrs.from : jidEncode(jidDecode(authState.creds.me!.id).user, 'c.us'),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(isGroup) {
|
if(isGroup) {
|
||||||
@@ -353,16 +354,19 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
recpAttrs.participant = stanza.attrs.participant
|
recpAttrs.participant = stanza.attrs.participant
|
||||||
recpAttrs.to = dec.chatId
|
recpAttrs.to = dec.chatId
|
||||||
} else {
|
} else {
|
||||||
recpAttrs.to = jidEncode(jidDecode(dec.chatId).user, 'c.us')
|
recpAttrs.to = jidNormalizedUser(dec.chatId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await sendNode({ tag: 'receipt', attrs: recpAttrs })
|
await sendNode({ tag: 'receipt', attrs: recpAttrs })
|
||||||
logger.debug({ msgId: dec.msgId }, 'sent message receipt')
|
logger.debug({ msgId: dec.msgId }, 'sent message receipt')
|
||||||
|
|
||||||
await sendMessageAck(stanza)
|
await sendMessageAck(stanza)
|
||||||
logger.debug({ msgId: dec.msgId, sender }, 'sent message ack')
|
logger.debug({ msgId: dec.msgId, sender }, 'sent message ack')
|
||||||
|
|
||||||
|
await sendDeliveryReceipt(dec.chatId, dec.participant, [dec.msgId])
|
||||||
|
logger.debug({ msgId: dec.msgId }, 'sent delivery receipt')
|
||||||
|
|
||||||
const message = msg.deviceSentMessage?.message || msg
|
const message = msg.deviceSentMessage?.message || msg
|
||||||
fullMessages.push({
|
fullMessages.push({
|
||||||
key: {
|
key: {
|
||||||
@@ -413,14 +417,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
logger.debug({ attrs: node.attrs }, 'sending receipt for ack')
|
logger.debug({ attrs: node.attrs }, 'sending receipt for ack')
|
||||||
})
|
})
|
||||||
|
|
||||||
const handleReceipt = ({ tag, attrs, content }: BinaryNode) => {
|
const handleReceipt = ({ attrs, content }: BinaryNode) => {
|
||||||
const isRead = isReadReceipt(attrs.type)
|
const isRead = isReadReceipt(attrs.type)
|
||||||
if(tag === 'receipt') {
|
|
||||||
// if not read or no type (no type = delivered, but message sent from other device)
|
|
||||||
if(!isRead && !!attrs.type) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const status = isRead ? proto.WebMessageInfo.WebMessageInfoStatus.READ : proto.WebMessageInfo.WebMessageInfoStatus.DELIVERY_ACK
|
const status = isRead ? proto.WebMessageInfo.WebMessageInfoStatus.READ : proto.WebMessageInfo.WebMessageInfoStatus.DELIVERY_ACK
|
||||||
const ids = [attrs.id]
|
const ids = [attrs.id]
|
||||||
if(Array.isArray(content)) {
|
if(Array.isArray(content)) {
|
||||||
@@ -442,7 +440,6 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ws.on('CB:receipt', handleReceipt)
|
ws.on('CB:receipt', handleReceipt)
|
||||||
ws.on('CB:ack,class:message', handleReceipt)
|
|
||||||
|
|
||||||
ws.on('CB:notification', async(node: BinaryNode) => {
|
ws.on('CB:notification', async(node: BinaryNode) => {
|
||||||
const sendAck = async() => {
|
const sendAck = async() => {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
import got from "got"
|
import got from "got"
|
||||||
import { Boom } from "@hapi/boom"
|
import { Boom } from "@hapi/boom"
|
||||||
import { SocketConfig, MediaConnInfo, AnyMessageContent, MiscMessageGenerationOptions, WAMediaUploadFunction, MessageRelayOptions } from "../Types"
|
import { SocketConfig, MediaConnInfo, AnyMessageContent, MiscMessageGenerationOptions, WAMediaUploadFunction, MessageRelayOptions, WAMessageKey } from "../Types"
|
||||||
import { encodeWAMessage, generateMessageID, generateWAMessage, encryptSenderKeyMsgSignalProto, encryptSignalProto, extractDeviceJids, jidToSignalProtocolAddress, parseAndInjectE2ESession } from "../Utils"
|
import { encodeWAMessage, generateMessageID, generateWAMessage, encryptSenderKeyMsgSignalProto, encryptSignalProto, extractDeviceJids, jidToSignalProtocolAddress, parseAndInjectE2ESession } from "../Utils"
|
||||||
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidDecode, jidEncode, jidNormalizedUser, S_WHATSAPP_NET, BinaryNodeAttributes } from '../WABinary'
|
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidDecode, jidEncode, jidNormalizedUser, S_WHATSAPP_NET, BinaryNodeAttributes } from '../WABinary'
|
||||||
import { proto } from "../../WAProto"
|
import { proto } from "../../WAProto"
|
||||||
@@ -77,19 +77,18 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
return mediaConn
|
return mediaConn
|
||||||
}
|
}
|
||||||
|
|
||||||
const sendReadReceipt = async(jid: string, participant: string | undefined, messageIds: string[]) => {
|
const sendReceipt = async(jid: string, participant: string | undefined, messageIds: string[], type: 'read' | 'read-self' | undefined) => {
|
||||||
const privacySettings = await fetchPrivacySettings()
|
|
||||||
// based on privacy settings, we have to change the read type
|
|
||||||
const readType = privacySettings.readreceipts === 'all' ? 'read' : 'read-self'
|
|
||||||
const node: BinaryNode = {
|
const node: BinaryNode = {
|
||||||
tag: 'receipt',
|
tag: 'receipt',
|
||||||
attrs: {
|
attrs: {
|
||||||
id: messageIds[0],
|
id: messageIds[0],
|
||||||
t: Date.now().toString(),
|
t: Date.now().toString(),
|
||||||
to: jid,
|
to: jid,
|
||||||
type: readType
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if(type) {
|
||||||
|
node.attrs.type = type
|
||||||
|
}
|
||||||
if(participant) {
|
if(participant) {
|
||||||
node.attrs.participant = participant
|
node.attrs.participant = participant
|
||||||
}
|
}
|
||||||
@@ -111,6 +110,17 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
await sendNode(node)
|
await sendNode(node)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sendDeliveryReceipt = (jid: string, participant: string | undefined, messageIds: string[]) => {
|
||||||
|
return sendReceipt(jid, participant, messageIds, undefined)
|
||||||
|
}
|
||||||
|
|
||||||
|
const sendReadReceipt = async(jid: string, participant: string | undefined, messageIds: string[]) => {
|
||||||
|
const privacySettings = await fetchPrivacySettings()
|
||||||
|
// based on privacy settings, we have to change the read type
|
||||||
|
const readType = privacySettings.readreceipts === 'all' ? 'read' : 'read-self'
|
||||||
|
return sendReceipt(jid, participant, messageIds, readType)
|
||||||
|
}
|
||||||
|
|
||||||
const getUSyncDevices = async(jids: string[], ignoreZeroDevices: boolean) => {
|
const getUSyncDevices = async(jids: string[], ignoreZeroDevices: boolean) => {
|
||||||
jids = Array.from(new Set(jids))
|
jids = Array.from(new Set(jids))
|
||||||
const users = jids.map<BinaryNode>(jid => ({
|
const users = jids.map<BinaryNode>(jid => ({
|
||||||
@@ -387,6 +397,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
...sock,
|
...sock,
|
||||||
assertSession,
|
assertSession,
|
||||||
relayMessage,
|
relayMessage,
|
||||||
|
sendDeliveryReceipt,
|
||||||
sendReadReceipt,
|
sendReadReceipt,
|
||||||
refreshMediaConn,
|
refreshMediaConn,
|
||||||
fetchPrivacySettings,
|
fetchPrivacySettings,
|
||||||
|
|||||||
Reference in New Issue
Block a user