fix: read receipts

This commit is contained in:
Adhiraj Singh
2021-11-06 15:41:49 +05:30
parent 12eb181e37
commit 5a33fd85a4
3 changed files with 38 additions and 27 deletions

View File

@@ -13,7 +13,8 @@ export const makeChatsSocket = (config: SocketConfig) => {
authState, authState,
generateMessageTag, generateMessageTag,
sendNode, sendNode,
query query,
fetchPrivacySettings,
} = sock } = sock
const interactiveQuery = async(userNodes: BinaryNode[], queryNode: BinaryNode) => { const interactiveQuery = async(userNodes: BinaryNode[], queryNode: BinaryNode) => {
@@ -145,28 +146,6 @@ export const makeChatsSocket = (config: SocketConfig) => {
}) })
} }
const fetchPrivacySettings = async() => {
const result = await query({
tag: 'iq',
attrs: {
xmlns: 'privacy',
to: S_WHATSAPP_NET,
type: 'get'
},
content: [
{ tag: 'privacy', attrs: { } }
]
})
const nodes = getBinaryNodeChildren(result, 'category')
const settings = nodes.reduce(
(dict, { attrs }) => {
dict[attrs.name] = attrs.value
return dict
}, { } as { [_: string]: string }
)
return settings
}
const updateAccountSyncTimestamp = async() => { const updateAccountSyncTimestamp = async() => {
await sendNode({ await sendNode({
tag: 'iq', tag: 'iq',
@@ -486,7 +465,6 @@ export const makeChatsSocket = (config: SocketConfig) => {
profilePictureUrl, profilePictureUrl,
onWhatsApp, onWhatsApp,
fetchBlocklist, fetchBlocklist,
fetchPrivacySettings,
fetchStatus, fetchStatus,
updateProfilePicture, updateProfilePicture,
updateBlockStatus, updateBlockStatus,

View File

@@ -6,6 +6,8 @@ import { proto } from "../../WAProto"
import { KEY_BUNDLE_TYPE } from "../Defaults" import { KEY_BUNDLE_TYPE } from "../Defaults"
import { makeMessagesSocket } from "./messages-send" import { makeMessagesSocket } from "./messages-send"
const isReadReceipt = (type: string) => type === 'read' || type === 'read-self'
export const makeMessagesRecvSocket = (config: SocketConfig) => { export const makeMessagesRecvSocket = (config: SocketConfig) => {
const { logger } = config const { logger } = config
const sock = makeMessagesSocket(config) const sock = makeMessagesSocket(config)
@@ -412,13 +414,14 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
}) })
const handleReceipt = ({ tag, attrs, content }: BinaryNode) => { const handleReceipt = ({ tag, attrs, content }: BinaryNode) => {
const isRead = isReadReceipt(attrs.type)
if(tag === 'receipt') { if(tag === 'receipt') {
// if not read or no type (no type = delivered, but message sent from other device) // if not read or no type (no type = delivered, but message sent from other device)
if(attrs.type !== 'read' && !!attrs.type) { if(!isRead && !!attrs.type) {
return return
} }
} }
const status = attrs.type === 'read' ? 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)) {
const items = getBinaryNodeChildren(content[0], 'item') const items = getBinaryNodeChildren(content[0], 'item')

View File

@@ -21,6 +21,32 @@ export const makeMessagesSocket = (config: SocketConfig) => {
groupToggleEphemeral groupToggleEphemeral
} = sock } = sock
let privacySettings: { [_: string]: string } | undefined
const fetchPrivacySettings = async(force: boolean = false) => {
if(!privacySettings || force) {
const result = await query({
tag: 'iq',
attrs: {
xmlns: 'privacy',
to: S_WHATSAPP_NET,
type: 'get'
},
content: [
{ tag: 'privacy', attrs: { } }
]
})
const nodes = getBinaryNodeChildren(result, 'category')
privacySettings = nodes.reduce(
(dict, { attrs }) => {
dict[attrs.name] = attrs.value
return dict
}, { } as { [_: string]: string }
)
}
return privacySettings
}
let mediaConn: Promise<MediaConnInfo> let mediaConn: Promise<MediaConnInfo>
const refreshMediaConn = async(forceGet = false) => { const refreshMediaConn = async(forceGet = false) => {
let media = await mediaConn let media = await mediaConn
@@ -52,13 +78,16 @@ export const makeMessagesSocket = (config: SocketConfig) => {
} }
const sendReadReceipt = async(jid: string, participant: string | undefined, messageIds: string[]) => { 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 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: 'read' type: readType
}, },
} }
if(participant) { if(participant) {
@@ -360,6 +389,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
relayMessage, relayMessage,
sendReadReceipt, sendReadReceipt,
refreshMediaConn, refreshMediaConn,
fetchPrivacySettings,
sendMessage: async( sendMessage: async(
jid: string, jid: string,
content: AnyMessageContent, content: AnyMessageContent,