From df89d210e227a99b9a67d0d5655c97669f1a72df Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Wed, 5 Aug 2020 15:55:27 +0530 Subject: [PATCH] added connection mode --- src/WAConnection/Base.ts | 2 ++ src/WAConnection/Connect.ts | 13 ++++++++----- src/WAConnection/Constants.ts | 6 ++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/WAConnection/Base.ts b/src/WAConnection/Base.ts index 0770023..c4e5cfe 100644 --- a/src/WAConnection/Base.ts +++ b/src/WAConnection/Base.ts @@ -13,6 +13,7 @@ import { MessageLogLevel, AuthenticationCredentialsBrowser, BaileysError, + WAConnectionMode, } from './Constants' /** Generate a QR code from the ref & the curve public key. This is scanned by the phone */ @@ -35,6 +36,7 @@ export default class WAConnectionBase { logLevel: MessageLogLevel = MessageLogLevel.info /** Should requests be queued when the connection breaks in between; if false, then an error will be thrown */ pendingRequestTimeoutMs: number = null + connectionMode: WAConnectionMode = WAConnectionMode.onlyRequireValidation /** What to do when you need the phone to authenticate the connection (generate QR code by default) */ onReadyForPhoneAuthentication = generateQRCode diff --git a/src/WAConnection/Connect.ts b/src/WAConnection/Connect.ts index d563495..70012af 100644 --- a/src/WAConnection/Connect.ts +++ b/src/WAConnection/Connect.ts @@ -1,7 +1,7 @@ import WS from 'ws' import KeyedDB from '@adiwajshing/keyed-db' import * as Utils from './Utils' -import { AuthenticationCredentialsBase64, UserMetaData, WAMessage, WAChat, WAContact, MessageLogLevel, WANode } from './Constants' +import { AuthenticationCredentialsBase64, UserMetaData, WAMessage, WAChat, WAContact, MessageLogLevel, WANode, WAConnectionMode } from './Constants' import WAConnectionValidator from './Validation' import Decoder from '../Binary/Decoder' @@ -58,10 +58,7 @@ export default class WAConnectionConnector extends WAConnectionValidator { this.conn.on('error', error => { this.close(); reject(error) }) }) const user = await Utils.promiseTimeout(timeoutMs, promise).catch(err => {this.close(); throw err}) - - this.pendingRequests.forEach (send => send()) // send off all pending request - this.pendingRequests = [] - + if (this.connectionMode === WAConnectionMode.onlyRequireValidation) this.releasePendingRequests () return user } /** @@ -134,8 +131,14 @@ export default class WAConnectionConnector extends WAConnectionValidator { const promise = Promise.all([waitForChats(), waitForContacts()]) await Utils.promiseTimeout (timeoutMs, promise) + if (this.connectionMode === WAConnectionMode.requireChatsAndContacts) this.releasePendingRequests () + return [chats, contacts] as [KeyedDB, WAContact[]] } + private releasePendingRequests () { + this.pendingRequests.forEach (send => send()) // send off all pending request + this.pendingRequests = [] + } private onMessageRecieved(message) { if (message[0] === '!') { // when the first character in the message is an '!', the server is updating the last seen diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index 9bc3745..4db2345 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -49,6 +49,12 @@ export type WANode = WA.Node export type WAMessage = proto.WebMessageInfo export type WAMessageContent = proto.IMessage +export enum WAConnectionMode { + /** Baileys will let requests through after a simple connect */ + onlyRequireValidation = 0, + /** Baileys will let requests through only after chats & contacts are received */ + requireChatsAndContacts = 1 +} export interface WAGroupCreateResponse { status: number gid?: string