Skip to content

Commit

Permalink
fix: Fixed paired session detection (fix #1339)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Oct 18, 2022
1 parent 7d10159 commit bdfd9d9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 89 deletions.
107 changes: 41 additions & 66 deletions src/api/whatsapp.ts
Expand Up @@ -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 {
Expand All @@ -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<void>((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);
}
});
}
Expand Down
30 changes: 7 additions & 23 deletions src/controllers/auth.ts
Expand Up @@ -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<string> {
Expand Down

0 comments on commit bdfd9d9

Please sign in to comment.