mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: handle delayed myAppStateKeyId
This commit is contained in:
@@ -21,10 +21,11 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
sendNode,
|
sendNode,
|
||||||
query,
|
query,
|
||||||
onUnexpectedError,
|
onUnexpectedError,
|
||||||
logout
|
|
||||||
} = sock
|
} = sock
|
||||||
|
|
||||||
let privacySettings: { [_: string]: string } | undefined
|
let privacySettings: { [_: string]: string } | undefined
|
||||||
|
let needToFlushWithAppStateSync = false
|
||||||
|
let pendingAppStateSync = false
|
||||||
/** this mutex ensures that the notifications (receipts, messages etc.) are processed in order */
|
/** this mutex ensures that the notifications (receipts, messages etc.) are processed in order */
|
||||||
const processingMutex = makeMutex()
|
const processingMutex = makeMutex()
|
||||||
|
|
||||||
@@ -691,23 +692,20 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
&& PROCESSABLE_HISTORY_TYPES.includes(historyMsg.syncType!)
|
&& PROCESSABLE_HISTORY_TYPES.includes(historyMsg.syncType!)
|
||||||
)
|
)
|
||||||
: false
|
: false
|
||||||
// we should have app state keys before we process any history
|
|
||||||
if(shouldProcessHistoryMsg) {
|
if(shouldProcessHistoryMsg && !authState.creds.myAppStateKeyId) {
|
||||||
if(!authState.creds.myAppStateKeyId) {
|
logger.warn('skipping app state sync, as myAppStateKeyId is not set')
|
||||||
logger.warn('myAppStateKeyId not synced, bad link')
|
pendingAppStateSync = true
|
||||||
await logout('Incomplete app state key sync')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
(async() => {
|
(async() => {
|
||||||
if(shouldProcessHistoryMsg && !authState.creds.accountSyncCounter) {
|
if(
|
||||||
logger.info('doing initial app state sync')
|
shouldProcessHistoryMsg
|
||||||
await resyncAppState(ALL_WA_PATCH_NAMES, true)
|
&& authState.creds.myAppStateKeyId
|
||||||
|
) {
|
||||||
const accountSyncCounter = (authState.creds.accountSyncCounter || 0) + 1
|
pendingAppStateSync = false
|
||||||
ev.emit('creds.update', { accountSyncCounter })
|
await doAppStateSync()
|
||||||
}
|
}
|
||||||
})(),
|
})(),
|
||||||
processMessage(
|
processMessage(
|
||||||
@@ -722,6 +720,29 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
])
|
])
|
||||||
|
|
||||||
|
if(
|
||||||
|
msg.message?.protocolMessage?.appStateSyncKeyShare
|
||||||
|
&& pendingAppStateSync
|
||||||
|
) {
|
||||||
|
await doAppStateSync()
|
||||||
|
pendingAppStateSync = false
|
||||||
|
}
|
||||||
|
|
||||||
|
async function doAppStateSync() {
|
||||||
|
if(!authState.creds.accountSyncCounter) {
|
||||||
|
logger.info('doing initial app state sync')
|
||||||
|
await resyncAppState(ALL_WA_PATCH_NAMES, true)
|
||||||
|
|
||||||
|
const accountSyncCounter = (authState.creds.accountSyncCounter || 0) + 1
|
||||||
|
ev.emit('creds.update', { accountSyncCounter })
|
||||||
|
|
||||||
|
if(needToFlushWithAppStateSync) {
|
||||||
|
logger.debug('flushing with app state sync')
|
||||||
|
ev.flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ws.on('CB:presence', handlePresenceUpdate)
|
ws.on('CB:presence', handlePresenceUpdate)
|
||||||
@@ -749,7 +770,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ev.on('connection.update', ({ connection }) => {
|
ev.on('connection.update', ({ connection, receivedPendingNotifications }) => {
|
||||||
if(connection === 'open') {
|
if(connection === 'open') {
|
||||||
if(fireInitQueries) {
|
if(fireInitQueries) {
|
||||||
executeInitQueries()
|
executeInitQueries()
|
||||||
@@ -763,6 +784,15 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
error => onUnexpectedError(error, 'presence update requests')
|
error => onUnexpectedError(error, 'presence update requests')
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(receivedPendingNotifications) {
|
||||||
|
// if we don't have the app state key
|
||||||
|
// we keep buffering events until we finally have
|
||||||
|
// the key and can sync the messages
|
||||||
|
if(!authState.creds?.myAppStateKeyId) {
|
||||||
|
needToFlushWithAppStateSync = ev.buffer()
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user