fix: delivery receipts

This commit is contained in:
Adhiraj Singh
2021-11-06 16:58:46 +05:30
parent 5a33fd85a4
commit 609c925225
2 changed files with 25 additions and 17 deletions

View File

@@ -18,6 +18,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
assertingPreKeys,
sendNode,
relayMessage,
sendDeliveryReceipt,
} = sock
const sendMessageAck = async({ attrs }: BinaryNode) => {
@@ -28,7 +29,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
attrs: {
class: 'receipt',
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) {
@@ -353,16 +354,19 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
recpAttrs.participant = stanza.attrs.participant
recpAttrs.to = dec.chatId
} else {
recpAttrs.to = jidEncode(jidDecode(dec.chatId).user, 'c.us')
recpAttrs.to = jidNormalizedUser(dec.chatId)
}
}
await sendNode({ tag: 'receipt', attrs: recpAttrs })
logger.debug({ msgId: dec.msgId }, 'sent message receipt')
await sendMessageAck(stanza)
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
fullMessages.push({
key: {
@@ -413,14 +417,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
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)
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 ids = [attrs.id]
if(Array.isArray(content)) {
@@ -442,7 +440,6 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
}
ws.on('CB:receipt', handleReceipt)
ws.on('CB:ack,class:message', handleReceipt)
ws.on('CB:notification', async(node: BinaryNode) => {
const sendAck = async() => {

View File

@@ -1,7 +1,7 @@
import got from "got"
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 { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidDecode, jidEncode, jidNormalizedUser, S_WHATSAPP_NET, BinaryNodeAttributes } from '../WABinary'
import { proto } from "../../WAProto"
@@ -77,19 +77,18 @@ export const makeMessagesSocket = (config: SocketConfig) => {
return mediaConn
}
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'
const sendReceipt = async(jid: string, participant: string | undefined, messageIds: string[], type: 'read' | 'read-self' | undefined) => {
const node: BinaryNode = {
tag: 'receipt',
attrs: {
id: messageIds[0],
t: Date.now().toString(),
to: jid,
type: readType
},
}
if(type) {
node.attrs.type = type
}
if(participant) {
node.attrs.participant = participant
}
@@ -111,6 +110,17 @@ export const makeMessagesSocket = (config: SocketConfig) => {
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) => {
jids = Array.from(new Set(jids))
const users = jids.map<BinaryNode>(jid => ({
@@ -387,6 +397,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
...sock,
assertSession,
relayMessage,
sendDeliveryReceipt,
sendReadReceipt,
refreshMediaConn,
fetchPrivacySettings,