General cleanup

This commit is contained in:
Adhiraj Singh
2021-01-04 21:21:43 +05:30
parent efe6bf1dd3
commit 96a6dc7e6d
5 changed files with 13 additions and 24 deletions

View File

@@ -181,14 +181,10 @@ Also, these events are fired regardless of whether they are initiated by the Bai
on (event: 'open', listener: (result: WAOpenResult) => void): this
/** when the connection is opening */
on (event: 'connecting', listener: () => void): this
/** when the connection has been validated */
on (event: 'connection-validated', listener: (user: WAUser) => void): this
/** when the connection has closed */
on (event: 'close', listener: (err: {reason?: DisconnectReason | string, isReconnecting: boolean}) => void): this
/** when the socket is closed */
on (event: 'ws-close', listener: (err: {reason?: DisconnectReason | string}) => void): this
/** when WA updates the credentials */
on (event: 'credentials-updated', listener: (auth: AuthenticationCredentials) => void): this
/** when a new QR is generated, ready for scanning */
on (event: 'qr', listener: (qr: string) => void): this
/** when the connection to the phone changes */

View File

@@ -1,7 +1,7 @@
import * as Curve from 'curve25519-js'
import * as Utils from './Utils'
import {WAConnection as Base} from './0.Base'
import { WAMetric, WAFlag, BaileysError, Presence, WAUser, WAInitResponse } from './Constants'
import { WAMetric, WAFlag, BaileysError, Presence, WAUser, WAInitResponse, WAOpenResult } from './Constants'
export class WAConnection extends Base {
@@ -87,21 +87,21 @@ export class WAConnection extends Base {
response = await this.waitForMessage('s2', true)
}
const {user, auth} = this.validateNewConnection(response[1]) // validate the connection
if (user.jid !== this.user?.jid) {
isNewUser = true
const result = this.validateNewConnection(response[1])// validate the connection
if (result.user.jid !== this.user?.jid) {
result.isNewUser = true
// clear out old data
this.chats.clear()
this.contacts = {}
}
this.user = user
this.user = result.user
this.logger.info('validated connection successfully')
this.sendPostConnectQueries ()
this.logger.debug('sent init queries')
return { user, auth, isNewUser }
return result
}
/**
* Send the same queries WA Web sends after connect
@@ -142,9 +142,9 @@ export class WAConnection extends Base {
name: json.pushname,
phone: json.phone,
imgUrl: null
} as WAUser,
},
auth: this.authInfo
})
}) as WAOpenResult
if (!json.secret) {
// if we didn't get a secret, we don't need it, we're validated

View File

@@ -49,7 +49,7 @@ export class WAConnection extends Base {
if (!willReconnect) throw error
}
}
result.newConnection = newConnection
if (newConnection) result.newConnection = newConnection
this.emit ('open', result)
this.logger.info ('opened connection to WhatsApp Web')

View File

@@ -608,8 +608,6 @@ export class WAConnection extends Base {
on (event: 'open', listener: (result: WAOpenResult) => void): this
/** when the connection is opening */
on (event: 'connecting', listener: () => void): this
/** when the connection has been validated */
on (event: 'connection-validated', listener: (item: { user: WAUser, auth: AuthenticationCredentials, isNewUser: boolean }) => void): this
/** when the connection has closed */
on (event: 'close', listener: (err: {reason?: DisconnectReason | string, isReconnecting: boolean}) => void): this
/** when the socket is closed */

View File

@@ -402,9 +402,9 @@ export interface WAMessageStatusUpdate {
export interface WAOpenResult {
/** Was this connection opened via a QR scan */
newConnection: boolean
newConnection?: true
user: WAUser
isNewUser: boolean
isNewUser?: true
auth: AuthenticationCredentials
}
@@ -420,8 +420,8 @@ export interface PresenceUpdate {
deny?: boolean
}
export interface BlocklistUpdate {
added: string[]
removed: string[]
added?: string[]
removed?: string[]
}
// path to upload the media
export const MediaPathMap = {
@@ -439,11 +439,6 @@ export const MimetypeMap = {
audioMessage: Mimetype.ogg,
stickerMessage: Mimetype.webp,
}
export interface WASendMessageResponse {
status: number
messageID: string
message: WAMessage
}
export type WAParticipantAction = 'add' | 'remove' | 'promote' | 'demote'
export type BaileysEvent =
'open' |