[core] Fix logout (unpair) from device

fix #148 fix #327 fix #194 fix #211
This commit is contained in:
devlikepro committed 2024-10-25 16:56:06 +07:00
1 parent c4aa9804d2
commit 77db6c5e98
7 files changed
+55 -2

No files matched your search

+4
View File
@@ -135,6 +135,7 @@ class SessionsController {
async delete(@Param('session') name: string): Promise<void> {
await this.withLock(name, async () => {
await this.manager.unassign(name);
await this.manager.unpair(name);
await this.manager.stop(name, true);
await this.manager.logout(name);
await this.manager.delete(name);
@@ -190,6 +191,7 @@ class SessionsController {
throw new NotFoundException('Session not found');
}
const isRunning = this.manager.isRunning(name);
await this.manager.unpair(name);
await this.manager.stop(name, true);
await this.manager.logout(name);
if (isRunning) {
@@ -258,6 +260,7 @@ class SessionsController {
// Old API did remove the session complete
await this.withLock(name, async () => {
await this.manager.unassign(name);
await this.manager.unpair(name);
await this.manager.stop(name, true);
await this.manager.logout(name);
await this.manager.delete(name);
@@ -283,6 +286,7 @@ class SessionsController {
const name = request.name;
await this.withLock(name, async () => {
await this.manager.unassign(name);
await this.manager.unpair(name);
await this.manager.stop(name, true);
await this.manager.logout(name);
await this.manager.delete(name);
+2
View File
@@ -94,6 +94,8 @@ export abstract class SessionManager implements BeforeApplicationShutdown {
abstract logout(name: string): Promise<void>;
abstract unpair(name: string): Promise<void>;
abstract getSession(name: string): WhatsappSession;
abstract getSessionInfo(name: string): Promise<SessionDetailedInfo | null>;
+5
View File
@@ -214,6 +214,11 @@ export abstract class WhatsappSession {
/** Stop the session */
abstract stop(): Promise<void>;
/* Unpair the account */
async unpair(): Promise<void> {
return;
}
/** Subscribe the handler to specific hook */
subscribeSessionEvent(
hook: WAHAEvents | string,
+9 -1
View File
@@ -59,7 +59,7 @@ import {
WAMessageAckBody,
} from '@waha/structures/webhooks.dto';
import { LoggerBuilder } from '@waha/utils/logging';
import { waitUntil } from '@waha/utils/promiseTimeout';
import { sleep, waitUntil } from '@waha/utils/promiseTimeout';
import { SingleDelayedJobRunner } from '@waha/utils/SingleDelayedJobRunner';
import { SinglePeriodicJobRunner } from '@waha/utils/SinglePeriodicJobRunner';
import * as Buffer from 'buffer';
@@ -225,6 +225,11 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
});
}
async unpair() {
this.shouldRestart = false;
await this.sock?.logout();
}
getSocketConfig(agent, state): any {
const fullSyncEnabled = this.sessionConfig?.noweb?.store?.fullSync || false;
const browser = fullSyncEnabled
@@ -443,6 +448,9 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
this.status = WAHASessionStatus.FAILED;
// Wait in case of intentional logout
await sleep(1_000);
await this.end();
await this.store?.close();
}
+16
View File
@@ -29,6 +29,22 @@ export class WebjsClient extends Client {
await this.pupPage.evaluate(LoadWAHA);
}
async unpair() {
await this.pupPage.evaluate(async () => {
if (
// @ts-ignore
window.Store &&
// @ts-ignore
window.Store.AppState &&
// @ts-ignore
typeof window.Store.AppState.logout === 'function'
) {
// @ts-ignore
await window.Store.AppState.logout();
}
});
}
async getChats(pagination?: PaginationParams) {
if (lodash.isEmpty(pagination)) {
return await super.getChats();
+7 -1
View File
@@ -64,7 +64,7 @@ import { WAMessage, WAMessageReaction } from '@waha/structures/responses.dto';
import { MeInfo } from '@waha/structures/sessions.dto';
import { WAMessageRevokedBody } from '@waha/structures/webhooks.dto';
import { PaginatorInMemory } from '@waha/utils/Paginator';
import { waitUntil } from '@waha/utils/promiseTimeout';
import { sleep, waitUntil } from '@waha/utils/promiseTimeout';
import { SingleDelayedJobRunner } from '@waha/utils/SingleDelayedJobRunner';
import {
Call,
@@ -254,6 +254,12 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
await this.end();
}
async unpair() {
this.shouldRestart = false;
await this.whatsapp.unpair();
await sleep(2_000);
}
private async end() {
this.engineStateCheckDelayedJob.cancel();
try {
+12
View File
@@ -211,6 +211,18 @@ export class SessionManagerCore extends SessionManager {
await sleep(this.SESSION_STOP_TIMEOUT);
}
async unpair(name: string) {
if (!this.session) {
return;
}
const session = this.session as WhatsappSession;
this.log.info('Unpairing device from account...', { session: name });
await session.unpair().catch((err) => {
this.log.warn(`Error while logging out from device: ${err}`);
});
await sleep(1000);
}
async logout(name: string): Promise<void> {
this.onlyDefault(name);
await this.sessionAuthRepository.clean(name);