mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
added connection mode
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user