From 19484e5cfced4c2d229687e74f714534375a57a3 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Fri, 12 Aug 2022 09:05:58 +0530 Subject: [PATCH] fix: fire init queries toggle --- src/Defaults/index.ts | 1 + src/Socket/chats.ts | 22 +++++++++++++++------- src/Types/index.ts | 2 ++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index ad79449..b1ce4ca 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -43,6 +43,7 @@ const BASE_CONNECTION_CONFIG: CommonSocketConfig = { export const DEFAULT_CONNECTION_CONFIG: SocketConfig = { ...BASE_CONNECTION_CONFIG, + fireInitQueries: true, auth: undefined as any, downloadHistory: true, markOnlineOnConnect: true, diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index d3370dd..3820be8 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -12,7 +12,7 @@ const MAX_SYNC_ATTEMPTS = 5 const APP_STATE_SYNC_TIMEOUT_MS = 10_000 export const makeChatsSocket = (config: SocketConfig) => { - const { logger, markOnlineOnConnect, downloadHistory } = config + const { logger, markOnlineOnConnect, downloadHistory, fireInitQueries } = config const sock = makeSocket(config) const { ev, @@ -207,8 +207,10 @@ export const makeChatsSocket = (config: SocketConfig) => { type: 'get' } }) - const child = result.content?.[0] as BinaryNode - return (child.content as BinaryNode[])?.map(i => i.attrs.jid) + + const listNode = getBinaryNodeChild(result, 'list') + return getBinaryNodeChildren(listNode, 'item') + .map(n => n.attrs.jid) } const updateBlockStatus = async(jid: string, action: 'block' | 'unblock') => { @@ -689,13 +691,12 @@ export const makeChatsSocket = (config: SocketConfig) => { * queries need to be fired on connection open * help ensure parity with WA Web * */ - const fireInitQueries = async() => { + const executeInitQueries = async() => { await Promise.all([ fetchAbt(), fetchProps(), fetchBlocklist(), fetchPrivacySettings(), - sendPresenceUpdate(markOnlineOnConnect ? 'available' : 'unavailable') ]) } @@ -771,9 +772,16 @@ export const makeChatsSocket = (config: SocketConfig) => { ev.on('connection.update', ({ connection }) => { if(connection === 'open') { - fireInitQueries() + if(fireInitQueries) { + executeInitQueries() + .catch( + error => onUnexpectedError(error, 'init queries') + ) + } + + sendPresenceUpdate(markOnlineOnConnect ? 'available' : 'unavailable') .catch( - error => onUnexpectedError(error, 'connection open requests') + error => onUnexpectedError(error, 'presence update requests') ) } }) diff --git a/src/Types/index.ts b/src/Types/index.ts index 6660ec2..232ae4a 100644 --- a/src/Types/index.ts +++ b/src/Types/index.ts @@ -36,6 +36,8 @@ export type SocketConfig = CommonSocketConfig & { linkPreviewImageThumbnailWidth: number /** Should Baileys ask the phone for full history, will be received async */ syncFullHistory: boolean + /** Should baileys fire init queries automatically, default true */ + fireInitQueries: boolean /** * fetch a message from your store * implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried