feat: limit number of message retries being sent

This commit is contained in:
Adhiraj Singh
2022-04-20 13:10:53 +05:30
parent 5824a0274d
commit af7b2a5dd2

View File

@@ -273,6 +273,11 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
}
}
const willSendMessageAgain = (id: string) => {
const retryCount = msgRetryMap[id] || 0
return retryCount < 5
}
const sendMessagesAgain = async(key: proto.IMessageKey, ids: string[]) => {
const msgs = await Promise.all(
ids.map(id => (
@@ -291,6 +296,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
for(let i = 0; i < msgs.length;i++) {
if(msgs[i]) {
msgRetryMap[ids[i]] = (msgRetryMap[ids[i]] || 0) + 1
await relayMessage(key.remoteJid, msgs[i], {
messageId: ids[i],
participant
@@ -360,6 +366,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
}
if(attrs.type === 'retry') {
if(willSendMessageAgain(key.id)) {
// correctly set who is asking for the retry
key.participant = key.participant || attrs.from
if(key.fromMe) {
@@ -373,6 +380,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
} else {
logger.info({ attrs, key }, 'recv retry for not fromMe message')
}
} else {
logger.info({ attrs, key }, 'will not send message again, as sent too many times')
}
}
if(shouldAck) {