mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Removed deprecations + merge message-status-update into chat-update
1. Update package version to 4.0.0 2. Retry query if the connection unexpectedly closes + make query iterative instead of recursive 3. Remove message-new & message-update deprecations 4. Deprecate loadProfilePicturesForChatsAutomatically
This commit is contained in:
@@ -61,7 +61,8 @@ export class WAConnection extends EventEmitter {
|
||||
messageLog: { tag: string, json: string, fromMe: boolean, binaryTags?: any[] }[] = []
|
||||
|
||||
maxCachedMessages = 50
|
||||
loadProfilePicturesForChatsAutomatically = true
|
||||
/** @deprecated won't be supported soon */
|
||||
loadProfilePicturesForChatsAutomatically = false
|
||||
|
||||
lastChatsReceived: Date
|
||||
chats = new KeyedDB (Utils.waChatKey(false), value => value.jid)
|
||||
@@ -207,39 +208,52 @@ export class WAConnection extends EventEmitter {
|
||||
* @param tag the tag to attach to the message
|
||||
*/
|
||||
async query(q: WAQuery): Promise<any> {
|
||||
let {json, binaryTags, tag, timeoutMs, expect200, waitForOpen, longTag, requiresPhoneConnection, startDebouncedTimeout} = q
|
||||
let {json, binaryTags, tag, timeoutMs, expect200, waitForOpen, longTag, requiresPhoneConnection, startDebouncedTimeout, maxRetries} = q
|
||||
requiresPhoneConnection = requiresPhoneConnection !== false
|
||||
waitForOpen = waitForOpen !== false
|
||||
if (waitForOpen) await this.waitForConnection()
|
||||
|
||||
let triesLeft = maxRetries || 2
|
||||
tag = tag || this.generateMessageTag (longTag)
|
||||
const promise = this.waitForMessage(tag, requiresPhoneConnection, timeoutMs)
|
||||
|
||||
if (this.logger.level === 'trace') {
|
||||
this.logger.trace ({ fromMe: true },`${tag},${JSON.stringify(json)}`)
|
||||
}
|
||||
while (triesLeft >= 0) {
|
||||
if (waitForOpen) await this.waitForConnection()
|
||||
|
||||
const promise = this.waitForMessage(tag, requiresPhoneConnection, timeoutMs)
|
||||
|
||||
if (binaryTags) tag = await this.sendBinary(json as WANode, binaryTags, tag)
|
||||
else tag = await this.sendJSON(json, tag)
|
||||
|
||||
const response = await promise
|
||||
|
||||
if (expect200 && response.status && Math.floor(+response.status / 100) !== 2) {
|
||||
// read here: http://getstatuscode.com/599
|
||||
if (response.status === 599) {
|
||||
this.unexpectedDisconnect (DisconnectReason.badSession)
|
||||
const response = await this.query (q)
|
||||
return response
|
||||
if (this.logger.level === 'trace') {
|
||||
this.logger.trace ({ fromMe: true },`${tag},${JSON.stringify(json)}`)
|
||||
}
|
||||
|
||||
const message = STATUS_CODES[response.status] || 'unknown'
|
||||
throw new BaileysError (
|
||||
`Unexpected status in '${json[0] || 'generic query'}': ${STATUS_CODES[response.status]}(${response.status})`,
|
||||
{query: json, message, status: response.status}
|
||||
)
|
||||
if (binaryTags) tag = await this.sendBinary(json as WANode, binaryTags, tag)
|
||||
else tag = await this.sendJSON(json, tag)
|
||||
|
||||
try {
|
||||
const response = await promise
|
||||
if (expect200 && response.status && Math.floor(+response.status / 100) !== 2) {
|
||||
const message = STATUS_CODES[response.status] || 'unknown'
|
||||
throw new BaileysError (
|
||||
`Unexpected status in '${json[0] || 'query'}': ${STATUS_CODES[response.status]}(${response.status})`,
|
||||
{query: json, message, status: response.status}
|
||||
)
|
||||
}
|
||||
if (startDebouncedTimeout) {
|
||||
this.startDebouncedTimeout()
|
||||
}
|
||||
return response
|
||||
} catch (error) {
|
||||
if (triesLeft === 0) {
|
||||
throw error
|
||||
}
|
||||
// read here: http://getstatuscode.com/599
|
||||
if (error.status === 599) {
|
||||
this.unexpectedDisconnect (DisconnectReason.badSession)
|
||||
} else if ((error.message === 'close' || error.message === 'lost') && waitForOpen && this.state !== 'close') {
|
||||
// nothing here
|
||||
} else throw error
|
||||
|
||||
triesLeft -= 1
|
||||
this.logger.debug(`query failed due to ${error}, retrying...`)
|
||||
}
|
||||
}
|
||||
if (startDebouncedTimeout) this.startDebouncedTimeout ()
|
||||
return response
|
||||
}
|
||||
/** interval is started when a query takes too long to respond */
|
||||
protected startPhoneCheckInterval () {
|
||||
@@ -330,7 +344,7 @@ export class WAConnection extends EventEmitter {
|
||||
let onOpen: () => void
|
||||
let onClose: ({ reason }) => void
|
||||
|
||||
if (this.pendingRequestTimeoutMs <= 0) {
|
||||
if (this.pendingRequestTimeoutMs !== null && this.pendingRequestTimeoutMs <= 0) {
|
||||
throw new BaileysError(DisconnectReason.close, { status: 428 })
|
||||
}
|
||||
await (
|
||||
@@ -396,7 +410,6 @@ export class WAConnection extends EventEmitter {
|
||||
this.phoneCheckListeners = 0
|
||||
this.clearPhoneCheckInterval ()
|
||||
|
||||
|
||||
this.emit ('ws-close', { reason: DisconnectReason.close })
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user