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,
|
MessageLogLevel,
|
||||||
AuthenticationCredentialsBrowser,
|
AuthenticationCredentialsBrowser,
|
||||||
BaileysError,
|
BaileysError,
|
||||||
|
WAConnectionMode,
|
||||||
} from './Constants'
|
} from './Constants'
|
||||||
|
|
||||||
/** Generate a QR code from the ref & the curve public key. This is scanned by the phone */
|
/** 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
|
logLevel: MessageLogLevel = MessageLogLevel.info
|
||||||
/** Should requests be queued when the connection breaks in between; if false, then an error will be thrown */
|
/** Should requests be queued when the connection breaks in between; if false, then an error will be thrown */
|
||||||
pendingRequestTimeoutMs: number = null
|
pendingRequestTimeoutMs: number = null
|
||||||
|
connectionMode: WAConnectionMode = WAConnectionMode.onlyRequireValidation
|
||||||
/** What to do when you need the phone to authenticate the connection (generate QR code by default) */
|
/** What to do when you need the phone to authenticate the connection (generate QR code by default) */
|
||||||
onReadyForPhoneAuthentication = generateQRCode
|
onReadyForPhoneAuthentication = generateQRCode
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import WS from 'ws'
|
import WS from 'ws'
|
||||||
import KeyedDB from '@adiwajshing/keyed-db'
|
import KeyedDB from '@adiwajshing/keyed-db'
|
||||||
import * as Utils from './Utils'
|
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 WAConnectionValidator from './Validation'
|
||||||
import Decoder from '../Binary/Decoder'
|
import Decoder from '../Binary/Decoder'
|
||||||
|
|
||||||
@@ -58,10 +58,7 @@ export default class WAConnectionConnector extends WAConnectionValidator {
|
|||||||
this.conn.on('error', error => { this.close(); reject(error) })
|
this.conn.on('error', error => { this.close(); reject(error) })
|
||||||
})
|
})
|
||||||
const user = await Utils.promiseTimeout(timeoutMs, promise).catch(err => {this.close(); throw err})
|
const user = await Utils.promiseTimeout(timeoutMs, promise).catch(err => {this.close(); throw err})
|
||||||
|
if (this.connectionMode === WAConnectionMode.onlyRequireValidation) this.releasePendingRequests ()
|
||||||
this.pendingRequests.forEach (send => send()) // send off all pending request
|
|
||||||
this.pendingRequests = []
|
|
||||||
|
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@@ -134,8 +131,14 @@ export default class WAConnectionConnector extends WAConnectionValidator {
|
|||||||
const promise = Promise.all([waitForChats(), waitForContacts()])
|
const promise = Promise.all([waitForChats(), waitForContacts()])
|
||||||
await Utils.promiseTimeout (timeoutMs, promise)
|
await Utils.promiseTimeout (timeoutMs, promise)
|
||||||
|
|
||||||
|
if (this.connectionMode === WAConnectionMode.requireChatsAndContacts) this.releasePendingRequests ()
|
||||||
|
|
||||||
return [chats, contacts] as [KeyedDB<WAChat>, WAContact[]]
|
return [chats, contacts] as [KeyedDB<WAChat>, WAContact[]]
|
||||||
}
|
}
|
||||||
|
private releasePendingRequests () {
|
||||||
|
this.pendingRequests.forEach (send => send()) // send off all pending request
|
||||||
|
this.pendingRequests = []
|
||||||
|
}
|
||||||
private onMessageRecieved(message) {
|
private onMessageRecieved(message) {
|
||||||
if (message[0] === '!') {
|
if (message[0] === '!') {
|
||||||
// when the first character in the message is an '!', the server is updating the last seen
|
// 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 WAMessage = proto.WebMessageInfo
|
||||||
export type WAMessageContent = proto.IMessage
|
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 {
|
export interface WAGroupCreateResponse {
|
||||||
status: number
|
status: number
|
||||||
gid?: string
|
gid?: string
|
||||||
|
|||||||
Reference in New Issue
Block a user