From bdfd9d9a8b4f8202f5405d62ea34b2d78ccb164e Mon Sep 17 00:00:00 2001 From: Edgard Date: Tue, 18 Oct 2022 17:45:54 -0300 Subject: [PATCH] fix: Fixed paired session detection (fix #1339) --- src/api/whatsapp.ts | 107 +++++++++++++++------------------------- src/controllers/auth.ts | 30 +++-------- 2 files changed, 48 insertions(+), 89 deletions(-) diff --git a/src/api/whatsapp.ts b/src/api/whatsapp.ts index cba38a4e8..057d1de61 100644 --- a/src/api/whatsapp.ts +++ b/src/api/whatsapp.ts @@ -23,7 +23,6 @@ import { useragentOverride } from '../config/WAuserAgente'; import { CreateConfig } from '../config/create-config'; import axios from 'axios'; import treekill = require('tree-kill'); -import { SocketState } from './model/enum'; import { evaluateAndReturn } from './helpers'; export class Whatsapp extends BusinessLayer { @@ -49,77 +48,53 @@ export class Whatsapp extends BusinessLayer { await removeToken(); } }); + + page + .evaluate(() => WPP.conn.isAuthenticated()) + .then((isAuthenticated) => { + connected = isAuthenticated; + }) + .catch(() => null); } this.onStateChange(async (state) => { - switch (state) { - case SocketState.CONNECTED: - connected = true; - // wait for localStore populate - setTimeout(async () => { - this.log('verbose', 'Updating session token', { type: 'token' }); - const tokenData = await this.getSessionTokenBrowser(); - const updated = await Promise.resolve( - this.tokenStore.setToken(this.session, tokenData) - ); - - if (updated) { - this.log('verbose', 'Session token updated', { - type: 'token', - }); - } else { - this.log('warn', 'Failed to update session token', { - type: 'token', - }); - } - }, 1000); - break; - - case SocketState.UNPAIRED: - case SocketState.UNPAIRED_IDLE: - setTimeout(async () => { - await removeToken(); + connected = await page + .evaluate(() => WPP.conn.isAuthenticated()) + .catch(() => null); - // Fire only after a success connection and disconnection - if (connected && this.statusFind) { - try { - this.statusFind('desconnectedMobile', session); - } catch (error) {} - } - - if (connected) { - await page.evaluate(() => localStorage.clear()); - await page.evaluate(() => { - const promises = []; - window.indexedDB - .databases() - .then((dbs) => { - dbs.forEach((db) => { - promises.push( - new Promise((resolve) => { - const r = window.indexedDB.deleteDatabase(db.name); - r.onerror = r.onblocked = function () { - this.result.close(); - window.indexedDB.deleteDatabase(db.name); - resolve(); - }; - r.onsuccess = function () { - resolve(); - }; - }) - ); - }); - }) - .catch(() => null); + if (connected === null) { + return; + } - return Promise.all(promises); - }); - await page.reload(); - } + if (connected) { + setTimeout(async () => { + this.log('verbose', 'Updating session token', { type: 'token' }); + const tokenData = await this.getSessionTokenBrowser(); + const updated = await Promise.resolve( + this.tokenStore.setToken(this.session, tokenData) + ); + + if (updated) { + this.log('verbose', 'Session token updated', { + type: 'token', + }); + } else { + this.log('warn', 'Failed to update session token', { + type: 'token', + }); + } + }, 1000); + } else { + setTimeout(async () => { + await removeToken(); - connected = false; - }, 1000); - break; + // Fire only after a success connection and disconnection + if (connected && this.statusFind) { + try { + this.statusFind('desconnectedMobile', session); + } catch (error) {} + } + }, 1000); } }); } diff --git a/src/controllers/auth.ts b/src/controllers/auth.ts index a648a7349..d3aea0048 100644 --- a/src/controllers/auth.ts +++ b/src/controllers/auth.ts @@ -67,39 +67,23 @@ export const getInterfaceStatus = async ( * @param waPage */ export const isAuthenticated = async (waPage: puppeteer.Page) => { - const status = await getInterfaceStatus(waPage); - if (typeof status !== 'string') { - return null; - } - - return ['CONNECTED', 'PAIRING'].includes(status); + return await waPage.evaluate(() => WPP.conn.isAuthenticated()); }; export const needsToScan = async (waPage: puppeteer.Page) => { - const status = await getInterfaceStatus(waPage); - if (typeof status !== 'string') { - return null; - } + const connected = await isAuthenticated(waPage); - return status === 'UNPAIRED'; + return !connected; }; export const isInsideChat = async (waPage: puppeteer.Page) => { - const status = await getInterfaceStatus(waPage); - if (typeof status !== 'string') { - return null; - } - - return status === 'CONNECTED'; + return await waPage.evaluate(() => WPP.conn.isMainReady()); }; export const isConnectingToPhone = async (waPage: puppeteer.Page) => { - const status = await getInterfaceStatus(waPage); - if (typeof status !== 'string') { - return null; - } - - return status === 'PAIRING'; + return await waPage.evaluate( + () => WPP.conn.isMainLoaded() && !WPP.conn.isMainReady() + ); }; export async function asciiQr(code: string): Promise {