Async + Multi Cache (#1741)
* fix: handle invalid signatureKeyPublic types in sender-key-state
- added extra check to ensure signatureKeyPublic is either a base64 string or a Buffer
- fallback to empty buffer when unexpected object type is received
- prevents ERR_INVALID_ARG_TYPE error in Buffer.from
* adjusted based on @jlucaso1 recommmendation
* fix: handle chain key objects for skmsg group message decryption
previously, some sender chain keys were stored or retrieved as plain objects
(e.g. { '0': 85, '1': 100, ... }) instead of Buffers. this caused
"ERR_INVALID_ARG_TYPE" during initial decryption of skmsg group messages.
this fixes any chain key object is converted to a proper Buffer
before being passed to SenderChainKey.
* fix: add more robust checks and fallbacks
* feat: async cache
feat: async caching
* fix: linting issues
* fix: mget logic
---------
Co-authored-by: αѕтяσχ11 <devastro0010@gmail.com>
This commit is contained in:
+18
-18
@@ -400,7 +400,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
} else {
|
||||
// Fallback to old system
|
||||
const key = `${msgId}:${msgKey?.participant}`
|
||||
let retryCount = msgRetryCache.get<number>(key) || 0
|
||||
let retryCount = (await msgRetryCache.get<number>(key)) || 0
|
||||
if (retryCount >= maxMsgRetryCount) {
|
||||
logger.debug({ retryCount, msgId }, 'reached retry limit, clearing')
|
||||
msgRetryCache.del(key)
|
||||
@@ -408,11 +408,11 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
}
|
||||
|
||||
retryCount += 1
|
||||
msgRetryCache.set(key, retryCount)
|
||||
await msgRetryCache.set(key, retryCount)
|
||||
}
|
||||
|
||||
const key = `${msgId}:${msgKey?.participant}`
|
||||
const retryCount = msgRetryCache.get<number>(key) || 1
|
||||
const retryCount = (await msgRetryCache.get<number>(key)) || 1
|
||||
|
||||
const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds
|
||||
const fromJid = node.attrs.from!
|
||||
@@ -862,16 +862,16 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
return data instanceof Buffer ? data : Buffer.from(data)
|
||||
}
|
||||
|
||||
const willSendMessageAgain = (id: string, participant: string) => {
|
||||
const willSendMessageAgain = async (id: string, participant: string) => {
|
||||
const key = `${id}:${participant}`
|
||||
const retryCount = msgRetryCache.get<number>(key) || 0
|
||||
return retryCount <= maxMsgRetryCount
|
||||
const retryCount = (await msgRetryCache.get<number>(key)) || 0
|
||||
return retryCount < maxMsgRetryCount
|
||||
}
|
||||
|
||||
const updateSendMessageAgainCount = (id: string, participant: string) => {
|
||||
const updateSendMessageAgainCount = async (id: string, participant: string) => {
|
||||
const key = `${id}:${participant}`
|
||||
const newValue = (msgRetryCache.get<number>(key) || 0) + 1
|
||||
msgRetryCache.set(key, newValue)
|
||||
const newValue = ((await msgRetryCache.get<number>(key)) || 0) + 1
|
||||
await msgRetryCache.set(key, newValue)
|
||||
}
|
||||
|
||||
const sendMessagesAgain = async (key: proto.IMessageKey, ids: string[], retryNode: BinaryNode) => {
|
||||
@@ -950,7 +950,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
for (const [i, msg] of msgs.entries()) {
|
||||
if (!ids[i]) continue
|
||||
|
||||
if (msg && willSendMessageAgain(ids[i], participant)) {
|
||||
if (msg && (await willSendMessageAgain(ids[i], participant))) {
|
||||
updateSendMessageAgainCount(ids[i], participant)
|
||||
const msgRelayOpts: MessageRelayOptions = { messageId: ids[i] }
|
||||
|
||||
@@ -1039,7 +1039,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
// correctly set who is asking for the retry
|
||||
key.participant = key.participant || attrs.from
|
||||
const retryNode = getBinaryNodeChild(node, 'retry')
|
||||
if (ids[0] && key.participant && willSendMessageAgain(ids[0], key.participant)) {
|
||||
if (ids[0] && key.participant && (await willSendMessageAgain(ids[0], key.participant))) {
|
||||
if (key.fromMe) {
|
||||
try {
|
||||
updateSendMessageAgainCount(ids[0], key.participant)
|
||||
@@ -1127,8 +1127,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
logger.debug('received unavailable message, acked and requested resend from phone')
|
||||
} else {
|
||||
if (placeholderResendCache.get(node.attrs.id!)) {
|
||||
placeholderResendCache.del(node.attrs.id!)
|
||||
if (await placeholderResendCache.get(node.attrs.id!)) {
|
||||
await placeholderResendCache.del(node.attrs.id!)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1292,7 +1292,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
status = getCallStatusFromNode(infoChild)
|
||||
|
||||
if (isLidUser(from) && infoChild.tag === 'relaylatency') {
|
||||
const verify = callOfferCache.get(callId)
|
||||
const verify = await callOfferCache.get(callId)
|
||||
if (!verify) {
|
||||
status = 'offer'
|
||||
const callLid: WACallEvent = {
|
||||
@@ -1303,7 +1303,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
offline: !!attrs.offline,
|
||||
status
|
||||
}
|
||||
callOfferCache.set(callId, callLid)
|
||||
await callOfferCache.set(callId, callLid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1320,10 +1320,10 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
call.isVideo = !!getBinaryNodeChild(infoChild, 'video')
|
||||
call.isGroup = infoChild.attrs.type === 'group' || !!infoChild.attrs['group-jid']
|
||||
call.groupJid = infoChild.attrs['group-jid']
|
||||
callOfferCache.set(call.id, call)
|
||||
await callOfferCache.set(call.id, call)
|
||||
}
|
||||
|
||||
const existingCall = callOfferCache.get<WACallEvent>(call.id)
|
||||
const existingCall = await callOfferCache.get<WACallEvent>(call.id)
|
||||
|
||||
// use existing call info to populate this event
|
||||
if (existingCall) {
|
||||
@@ -1333,7 +1333,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
// delete data once call has ended
|
||||
if (status === 'reject' || status === 'accept' || status === 'timeout' || status === 'terminate') {
|
||||
callOfferCache.del(call.id)
|
||||
await callOfferCache.del(call.id)
|
||||
}
|
||||
|
||||
ev.emit('call', [call])
|
||||
|
||||
Reference in New Issue
Block a user