From 96a6dc7e6d3ad633108032c1a5bfa1bc0620f879 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Mon, 4 Jan 2021 21:21:43 +0530 Subject: [PATCH] General cleanup --- README.md | 4 ---- src/WAConnection/1.Validation.ts | 16 ++++++++-------- src/WAConnection/3.Connect.ts | 2 +- src/WAConnection/4.Events.ts | 2 -- src/WAConnection/Constants.ts | 13 ++++--------- 5 files changed, 13 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 5722976..c8edc0d 100644 --- a/README.md +++ b/README.md @@ -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 */ diff --git a/src/WAConnection/1.Validation.ts b/src/WAConnection/1.Validation.ts index 81171c8..7c4ac16 100644 --- a/src/WAConnection/1.Validation.ts +++ b/src/WAConnection/1.Validation.ts @@ -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 diff --git a/src/WAConnection/3.Connect.ts b/src/WAConnection/3.Connect.ts index 40a240f..9c3a0bc 100644 --- a/src/WAConnection/3.Connect.ts +++ b/src/WAConnection/3.Connect.ts @@ -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') diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index d764369..909a537 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -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 */ diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index 1721d24..52b4b95 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -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' |