fix: minor retry logic cleanup

This commit is contained in:
Adhiraj Singh
2021-11-15 09:09:55 +05:30
parent 2dfe5adbe1
commit 9ed4c28b8e
2 changed files with 13 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
import { SocketConfig, WAMessageStubType, ParticipantAction, Chat, GroupMetadata } from "../Types"
import { SocketConfig, WAMessageStubType, ParticipantAction, Chat, GroupMetadata, WAMessageKey } from "../Types"
import { decodeMessageStanza, encodeBigEndian, toNumber, downloadHistory, generateSignalPubKey, xmppPreKey, xmppSignedPreKey } from "../Utils"
import { BinaryNode, jidDecode, jidEncode, isJidStatusBroadcast, areJidsSameUser, getBinaryNodeChildren, jidNormalizedUser, getAllBinaryNodeChildren, BinaryNodeAttributes } from '../WABinary'
import { proto } from "../../WAProto"
@@ -22,6 +22,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
sendDeliveryReceipt,
} = sock
const msgRetryMap = config.msgRetryCounterMap || { }
const sendMessageAck = async({ tag, attrs }: BinaryNode, extraAttrs: BinaryNodeAttributes) => {
const stanza: BinaryNode = {
tag: 'ack',
@@ -38,14 +40,15 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
await sendNode(stanza)
}
const retries = new Map<string, number>()
const sendRetryRequest = async(node: BinaryNode) => {
if (retries.has(node.attrs.id) && retries.get(node.attrs.id)! >= 5) {
retries.delete(node.attrs.id)
const msgId = node.attrs.id
const retryCount = msgRetryMap[msgId] || 1
if(retryCount >= 5) {
logger.debug({ retryCount, msgId }, 'reached retry limit, clearing')
delete msgRetryMap[msgId]
return
}
const retryCount = retries.get(node.attrs.id) || 1
retries.set(node.attrs.id, retryCount + 1)
msgRetryMap[msgId] = retryCount+1
const isGroup = !!node.attrs.participant
const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds
@@ -59,7 +62,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const receipt: BinaryNode = {
tag: 'receipt',
attrs: {
id: node.attrs.id,
id: msgId,
type: 'retry',
to: isGroup ? node.attrs.from : jidEncode(decFrom!.user, 's.whatsapp.net', decFrom!.device, 0)
},
@@ -102,9 +105,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
}
await sendNode(receipt)
logger.info({ msgId: node.attrs.id, retryCount }, 'sent retry receipt')
ev.emit('auth-state.update', authState)
logger.info({ msgAttrs: node.attrs, retryCount }, 'sent retry receipt')
})
}
const processMessage = async(message: proto.IWebMessageInfo, chatUpdate: Partial<Chat>) => {

View File

@@ -50,6 +50,8 @@ export type SocketConfig = {
userDevicesCache?: NodeCache
/** provide a cache to store media, so does not have to be re-uploaded */
mediaCache?: NodeCache
/** map to store the retry counts for failed messages */
msgRetryCounterMap?: { [msgId: string]: number }
}
export enum DisconnectReason {