mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: more accurately handle app state sync
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Boom } from '@hapi/boom'
|
||||
import { proto } from '../../WAProto'
|
||||
import { AppStateChunk, ChatModification, ChatMutation, LTHashState, PresenceData, SocketConfig, WABusinessHoursConfig, WABusinessProfile, WAMediaUpload, WAPatchCreate, WAPatchName, WAPresence } from '../Types'
|
||||
import { ALL_WA_PATCH_NAMES, AppStateChunk, ChatModification, ChatMutation, InitialReceivedChatsState, LTHashState, PresenceData, SocketConfig, WABusinessHoursConfig, WABusinessProfile, WAMediaUpload, WAPatchCreate, WAPatchName, WAPresence } from '../Types'
|
||||
import { chatModificationToAppPatch, decodePatches, decodeSyncdSnapshot, encodeSyncdPatch, extractSyncdPatches, generateProfilePicture, newLTHashState, processSyncActions } from '../Utils'
|
||||
import { makeMutex } from '../Utils/make-mutex'
|
||||
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, jidNormalizedUser, reduceBinaryNodeToDictionary, S_WHATSAPP_NET } from '../WABinary'
|
||||
@@ -228,7 +228,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
})
|
||||
}
|
||||
|
||||
const resyncAppState = async(collections: WAPatchName[]) => {
|
||||
const resyncAppState = async(collections: readonly WAPatchName[], ctx: InitialReceivedChatsState | undefined) => {
|
||||
const appStateChunk: AppStateChunk = { totalMutations: [], collectionsToHandle: [] }
|
||||
// we use this to determine which events to fire
|
||||
// otherwise when we resync from scratch -- all notifications will fire
|
||||
@@ -344,7 +344,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
}
|
||||
)
|
||||
|
||||
processSyncActionsLocal(appStateChunk.totalMutations)
|
||||
processSyncActionsLocal(appStateChunk.totalMutations, ctx)
|
||||
|
||||
return appStateChunk
|
||||
}
|
||||
@@ -446,18 +446,12 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
}
|
||||
}
|
||||
|
||||
const resyncMainAppState = async() => {
|
||||
const resyncMainAppState = async(ctx?: InitialReceivedChatsState) => {
|
||||
logger.debug('resyncing main app state')
|
||||
|
||||
await (
|
||||
mutationMutex.mutex(
|
||||
() => resyncAppState([
|
||||
'critical_block',
|
||||
'critical_unblock_low',
|
||||
'regular_high',
|
||||
'regular_low',
|
||||
'regular'
|
||||
])
|
||||
() => resyncAppState(ALL_WA_PATCH_NAMES, ctx)
|
||||
)
|
||||
.catch(err => (
|
||||
onUnexpectedError(err, 'main app sync')
|
||||
@@ -465,8 +459,13 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
)
|
||||
}
|
||||
|
||||
const processSyncActionsLocal = (actions: ChatMutation[]) => {
|
||||
const events = processSyncActions(actions, authState.creds.me!, logger)
|
||||
const processSyncActionsLocal = (actions: ChatMutation[], recvChats: InitialReceivedChatsState | undefined) => {
|
||||
const events = processSyncActions(
|
||||
actions,
|
||||
authState.creds.me!,
|
||||
recvChats ? { recvChats, accountSettings: authState.creds.accountSettings } : undefined,
|
||||
logger
|
||||
)
|
||||
emitEventsFromMap(events)
|
||||
// resend available presence to update name on servers
|
||||
if(events['creds.update']?.me?.name && markOnlineOnConnect) {
|
||||
@@ -490,7 +489,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
async() => {
|
||||
logger.debug({ patch: patchCreate }, 'applying app patch')
|
||||
|
||||
await resyncAppState([name])
|
||||
await resyncAppState([name], undefined)
|
||||
|
||||
const { [name]: currentSyncVersion } = await authState.keys.get('app-state-sync-version', [name])
|
||||
initial = currentSyncVersion || newLTHashState()
|
||||
@@ -544,7 +543,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
|
||||
if(config.emitOwnEvents) {
|
||||
const result = await decodePatches(name, [{ ...encodeResult.patch, version: { version: encodeResult.state.version }, }], initial, getAppStateSyncKey)
|
||||
processSyncActionsLocal(result.newMutations)
|
||||
processSyncActionsLocal(result.newMutations, undefined)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -654,7 +653,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
if(update) {
|
||||
const name = update.attrs.name as WAPatchName
|
||||
mutationMutex.mutex(() => (
|
||||
resyncAppState([name])
|
||||
resyncAppState([name], undefined)
|
||||
.catch(err => logger.error({ trace: err.stack, node }, 'failed to sync state'))
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
import { proto } from '../../WAProto'
|
||||
import { KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaults'
|
||||
import { BaileysEventMap, MessageReceiptType, MessageUserReceipt, SocketConfig, WACallEvent, WAMessageStubType } from '../Types'
|
||||
import { BaileysEventMap, InitialReceivedChatsState, MessageReceiptType, MessageUserReceipt, SocketConfig, WACallEvent, WAMessageStubType } from '../Types'
|
||||
import { debouncedTimeout, decodeMediaRetryNode, decodeMessageStanza, delay, encodeBigEndian, generateSignalPubKey, getCallStatusFromNode, getNextPreKeys, getStatusFromReceiptType, normalizeMessageContent, unixTimestampSeconds, xmppPreKey, xmppSignedPreKey } from '../Utils'
|
||||
import { makeKeyedMutex, makeMutex } from '../Utils/make-mutex'
|
||||
import processMessage, { cleanMessage } from '../Utils/process-message'
|
||||
@@ -37,16 +37,28 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
/** this mutex ensures that each retryRequest will wait for the previous one to finish */
|
||||
const retryMutex = makeMutex()
|
||||
|
||||
const historyCache = new Set<string>()
|
||||
let recvChats: InitialReceivedChatsState = { }
|
||||
|
||||
const appStateSyncTimeout = debouncedTimeout(
|
||||
6_000,
|
||||
() => ws.readyState === ws.OPEN && resyncMainAppState()
|
||||
async() => {
|
||||
logger.info(
|
||||
{ recvChats: Object.keys(recvChats).length },
|
||||
'doing initial app state sync'
|
||||
)
|
||||
if(ws.readyState === ws.OPEN) {
|
||||
await resyncMainAppState(recvChats)
|
||||
}
|
||||
|
||||
historyCache.clear()
|
||||
recvChats = { }
|
||||
}
|
||||
)
|
||||
|
||||
const msgRetryMap = config.msgRetryCounterMap || { }
|
||||
const callOfferData: { [id: string]: WACallEvent } = { }
|
||||
|
||||
const historyCache = new Set<string>()
|
||||
|
||||
let sendActiveReceipts = false
|
||||
|
||||
const sendMessageAck = async({ tag, attrs }: BinaryNode, extraAttrs: BinaryNodeAttributes = { }) => {
|
||||
@@ -160,6 +172,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
{
|
||||
downloadHistory,
|
||||
historyCache,
|
||||
recvChats,
|
||||
creds: authState.creds,
|
||||
keyStore: authState.keys,
|
||||
logger,
|
||||
|
||||
Reference in New Issue
Block a user