feat: use (participant, ID) tuple for retry counter

This commit is contained in:
Adhiraj Singh
2022-06-16 15:25:31 +05:30
parent 542617d79c
commit 349002857e

View File

@@ -304,11 +304,17 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
} }
} }
const willSendMessageAgain = (id: string) => { const willSendMessageAgain = (id: string, participant: string) => {
const retryCount = msgRetryMap[id] || 0 const key = `${id}:${participant}`
const retryCount = msgRetryMap[key] || 0
return retryCount < 5 return retryCount < 5
} }
const updateSendMessageAgainCount = (id: string, participant: string) => {
const key = `${id}:${participant}`
msgRetryMap[key] = (msgRetryMap[key] || 0) + 1
}
const sendMessagesAgain = async(key: proto.IMessageKey, ids: string[]) => { const sendMessagesAgain = async(key: proto.IMessageKey, ids: string[]) => {
const msgs = await Promise.all( const msgs = await Promise.all(
ids.map(id => ( ids.map(id => (
@@ -327,7 +333,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
for(let i = 0; i < msgs.length;i++) { for(let i = 0; i < msgs.length;i++) {
if(msgs[i]) { if(msgs[i]) {
msgRetryMap[ids[i]] = (msgRetryMap[ids[i]] || 0) + 1 updateSendMessageAgainCount(ids[i], participant)
await relayMessage(key.remoteJid, msgs[i], { await relayMessage(key.remoteJid, msgs[i], {
messageId: ids[i], messageId: ids[i],
participant participant
@@ -397,9 +403,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
} }
if(attrs.type === 'retry') { if(attrs.type === 'retry') {
if(willSendMessageAgain(ids[0])) { // correctly set who is asking for the retry
// correctly set who is asking for the retry key.participant = key.participant || attrs.from
key.participant = key.participant || attrs.from if(willSendMessageAgain(ids[0], key.participant)) {
if(key.fromMe) { if(key.fromMe) {
try { try {
logger.debug({ attrs, key }, 'recv retry request') logger.debug({ attrs, key }, 'recv retry request')