added connection mode

This commit is contained in:
Adhiraj
2020-08-05 15:55:27 +05:30
parent 243461c4c2
commit df89d210e2
3 changed files with 16 additions and 5 deletions

View File

@@ -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

View File

@@ -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<WAChat>, 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

View File

@@ -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