mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
chore: use cache instead of permanent maps
This commit is contained in:
@@ -339,7 +339,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
name,
|
||||
version: state.version.toString(),
|
||||
// return snapshot if being synced from scratch
|
||||
return_snapshot: (!state.version).toString()
|
||||
'return_snapshot': (!state.version).toString()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
|
||||
import NodeCache from 'node-cache'
|
||||
import { proto } from '../../WAProto'
|
||||
import { KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults'
|
||||
import { MessageReceiptType, MessageRelayOptions, MessageUserReceipt, SocketConfig, WACallEvent, WAMessageKey, WAMessageStubType, WAPatchName } from '../Types'
|
||||
@@ -36,8 +37,14 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
/** this mutex ensures that each retryRequest will wait for the previous one to finish */
|
||||
const retryMutex = makeMutex()
|
||||
|
||||
const msgRetryMap = config.msgRetryCounterMap || { }
|
||||
const callOfferData: { [id: string]: WACallEvent } = { }
|
||||
const msgRetryCache = config.msgRetryCounterCache || new NodeCache({
|
||||
stdTTL: 60 * 60, // 1 hour
|
||||
useClones: false
|
||||
})
|
||||
const callOfferCache = config.callOfferCache || new NodeCache({
|
||||
stdTTL: 5 * 60, // 5 mins
|
||||
useClones: false
|
||||
})
|
||||
|
||||
let sendActiveReceipts = false
|
||||
|
||||
@@ -90,15 +97,15 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
const sendRetryRequest = async(node: BinaryNode, forceIncludeKeys = false) => {
|
||||
const msgId = node.attrs.id
|
||||
|
||||
let retryCount = msgRetryMap[msgId] || 0
|
||||
let retryCount = msgRetryCache.get<number>(msgId) || 0
|
||||
if(retryCount >= 5) {
|
||||
logger.debug({ retryCount, msgId }, 'reached retry limit, clearing')
|
||||
delete msgRetryMap[msgId]
|
||||
msgRetryCache.del(msgId)
|
||||
return
|
||||
}
|
||||
|
||||
retryCount += 1
|
||||
msgRetryMap[msgId] = retryCount
|
||||
msgRetryCache.set(msgId, retryCount)
|
||||
|
||||
const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds
|
||||
|
||||
@@ -362,13 +369,14 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
const willSendMessageAgain = (id: string, participant: string) => {
|
||||
const key = `${id}:${participant}`
|
||||
const retryCount = msgRetryMap[key] || 0
|
||||
const retryCount = msgRetryCache.get<number>(key) || 0
|
||||
return retryCount < 5
|
||||
}
|
||||
|
||||
const updateSendMessageAgainCount = (id: string, participant: string) => {
|
||||
const key = `${id}:${participant}`
|
||||
msgRetryMap[key] = (msgRetryMap[key] || 0) + 1
|
||||
const newValue = (msgRetryCache.get<number>(key) || 0) + 1
|
||||
msgRetryCache.set(key, newValue)
|
||||
}
|
||||
|
||||
const sendMessagesAgain = async(
|
||||
@@ -618,18 +626,20 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
if(status === 'offer') {
|
||||
call.isVideo = !!getBinaryNodeChild(infoChild, 'video')
|
||||
call.isGroup = infoChild.attrs.type === 'group'
|
||||
callOfferData[call.id] = call
|
||||
callOfferCache.set(call.id, call)
|
||||
}
|
||||
|
||||
const existingCall = callOfferCache.get<WACallEvent>(call.id)
|
||||
|
||||
// use existing call info to populate this event
|
||||
if(callOfferData[call.id]) {
|
||||
call.isVideo = callOfferData[call.id].isVideo
|
||||
call.isGroup = callOfferData[call.id].isGroup
|
||||
if(existingCall) {
|
||||
call.isVideo = existingCall.isVideo
|
||||
call.isGroup = existingCall.isGroup
|
||||
}
|
||||
|
||||
// delete data once call has ended
|
||||
if(status === 'reject' || status === 'accept' || status === 'timeout') {
|
||||
delete callOfferData[call.id]
|
||||
callOfferCache.del(call.id)
|
||||
}
|
||||
|
||||
ev.emit('call', [call])
|
||||
|
||||
Reference in New Issue
Block a user