mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Detect new login
This commit is contained in:
@@ -356,7 +356,9 @@ export class WAConnection extends EventEmitter {
|
|||||||
//throw new Error("You're not even connected, you can't log out")
|
//throw new Error("You're not even connected, you can't log out")
|
||||||
await new Promise(resolve => this.conn.send('goodbye,["admin","Conn","disconnect"]', null, resolve))
|
await new Promise(resolve => this.conn.send('goodbye,["admin","Conn","disconnect"]', null, resolve))
|
||||||
}
|
}
|
||||||
this.user = null
|
this.user = undefined
|
||||||
|
this.chats.clear()
|
||||||
|
this.contacts = {}
|
||||||
this.close()
|
this.close()
|
||||||
}
|
}
|
||||||
/** Close the connection to WhatsApp Web */
|
/** Close the connection to WhatsApp Web */
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ export class WAConnection extends Base {
|
|||||||
if (!this.authInfo?.clientID) {
|
if (!this.authInfo?.clientID) {
|
||||||
this.authInfo = { clientID: Utils.generateClientID() } as any
|
this.authInfo = { clientID: Utils.generateClientID() } as any
|
||||||
}
|
}
|
||||||
|
|
||||||
const canLogin = this.authInfo?.encKey && this.authInfo?.macKey
|
const canLogin = this.authInfo?.encKey && this.authInfo?.macKey
|
||||||
this.referenceDate = new Date () // refresh reference date
|
this.referenceDate = new Date () // refresh reference date
|
||||||
|
let isNewUser = false
|
||||||
|
|
||||||
this.startDebouncedTimeout ()
|
this.startDebouncedTimeout ()
|
||||||
|
|
||||||
@@ -70,7 +70,15 @@ export class WAConnection extends Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const validationJSON = (await Promise.all (initQueries)).slice(-1)[0] // get the last result
|
const validationJSON = (await Promise.all (initQueries)).slice(-1)[0] // get the last result
|
||||||
this.user = await this.validateNewConnection(validationJSON[1]) // validate the connection
|
const newUser = await this.validateNewConnection(validationJSON[1]) // validate the connection
|
||||||
|
if (newUser.jid !== this.user?.jid) {
|
||||||
|
isNewUser = true
|
||||||
|
// clear out old data
|
||||||
|
this.chats.clear()
|
||||||
|
this.contacts = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.user = newUser
|
||||||
|
|
||||||
this.logger.info('validated connection successfully')
|
this.logger.info('validated connection successfully')
|
||||||
this.emit ('connection-validated', this.user)
|
this.emit ('connection-validated', this.user)
|
||||||
@@ -89,6 +97,8 @@ export class WAConnection extends Base {
|
|||||||
this.sendPostConnectQueries ()
|
this.sendPostConnectQueries ()
|
||||||
|
|
||||||
this.logger.debug('sent init queries')
|
this.logger.debug('sent init queries')
|
||||||
|
|
||||||
|
return { isNewUser }
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Send the same queries WA Web sends after connect
|
* Send the same queries WA Web sends after connect
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ export class WAConnection extends Base {
|
|||||||
|
|
||||||
this.conn.on ('open', async () => {
|
this.conn.on ('open', async () => {
|
||||||
this.logger.info(`connected to WhatsApp Web server, authenticating via ${reconnectID ? 'reconnect' : 'takeover'}`)
|
this.logger.info(`connected to WhatsApp Web server, authenticating via ${reconnectID ? 'reconnect' : 'takeover'}`)
|
||||||
let waitForChats: Promise<any>
|
let waitForChats: Promise<{ hasNewChats: boolean }>
|
||||||
// add wait for chats promise if required
|
// add wait for chats promise if required
|
||||||
if (typeof options?.waitForChats === 'undefined' ? true : options?.waitForChats) {
|
if (typeof options?.waitForChats === 'undefined' ? true : options?.waitForChats) {
|
||||||
const {wait, cancellations} = this.receiveChatsAndContacts(this.connectOptions.waitOnlyForLastMessage)
|
const {wait, cancellations} = this.receiveChatsAndContacts(this.connectOptions.waitOnlyForLastMessage)
|
||||||
@@ -95,25 +95,25 @@ export class WAConnection extends Base {
|
|||||||
rejections.push (...cancellations)
|
rejections.push (...cancellations)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const [, result] = await Promise.all (
|
const [authResult, chatsResult] = await Promise.all (
|
||||||
[
|
[
|
||||||
this.authenticate(reconnectID)
|
this.authenticate(reconnectID),
|
||||||
.then(() => this.startKeepAliveRequest()),
|
|
||||||
waitForChats || undefined
|
waitForChats || undefined
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
this.startKeepAliveRequest()
|
||||||
this.conn
|
this.conn
|
||||||
.removeAllListeners ('error')
|
.removeAllListeners ('error')
|
||||||
.removeAllListeners ('close')
|
.removeAllListeners ('close')
|
||||||
this.stopDebouncedTimeout ()
|
this.stopDebouncedTimeout ()
|
||||||
resolve (result)
|
resolve ({ ...authResult, ...chatsResult })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject (error)
|
reject (error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.conn.on('error', rejectAll)
|
this.conn.on('error', rejectAll)
|
||||||
this.conn.on('close', () => rejectAll(new Error(DisconnectReason.close)))
|
this.conn.on('close', () => rejectAll(new Error(DisconnectReason.close)))
|
||||||
}) as Promise<void | any>
|
}) as Promise<{ hasNewChats?: boolean, isNewUser: boolean }>
|
||||||
)
|
)
|
||||||
|
|
||||||
this.on ('ws-close', rejectAll)
|
this.on ('ws-close', rejectAll)
|
||||||
|
|||||||
@@ -382,6 +382,7 @@ export interface WAOpenResult {
|
|||||||
/** Was this connection opened via a QR scan */
|
/** Was this connection opened via a QR scan */
|
||||||
newConnection: boolean
|
newConnection: boolean
|
||||||
user: WAUser
|
user: WAUser
|
||||||
|
isNewUser: boolean
|
||||||
hasNewChats?: boolean
|
hasNewChats?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user