[core] Add UNPAIRING status
This commit is contained in:
1 parent
0f2641909f
commit
9d27616003
3 files changed
+27
-15
No files matched your search
@@ -22,7 +22,6 @@ import {
|
||||
ChatRequest,
|
||||
CheckNumberStatusQuery,
|
||||
EditMessageRequest,
|
||||
GetMessageQuery,
|
||||
MessageContactVcardRequest,
|
||||
MessageFileRequest,
|
||||
MessageForwardRequest,
|
||||
@@ -116,6 +115,7 @@ export abstract class WhatsappSession {
|
||||
protected proxyConfig?: ProxyConfig;
|
||||
public sessionConfig?: SessionConfig;
|
||||
protected engineConfig?: any;
|
||||
protected unpairing: boolean = false;
|
||||
|
||||
private _status: WAHASessionStatus;
|
||||
private shouldPrintQR: boolean;
|
||||
@@ -143,6 +143,11 @@ export abstract class WhatsappSession {
|
||||
}
|
||||
|
||||
protected set status(value: WAHASessionStatus) {
|
||||
if (this.unpairing && value !== WAHASessionStatus.STOPPED) {
|
||||
// In case of unpairing
|
||||
// wait for STOPPED event, ignore the rest
|
||||
return;
|
||||
}
|
||||
this._status = value;
|
||||
const body: WASessionStatusBody = { name: this.name, status: value };
|
||||
this.events.emit(WAHAEvents.SESSION_STATUS, body);
|
||||
|
||||
@@ -29,7 +29,7 @@ import { UnprocessableEntityException } from '@nestjs/common';
|
||||
import { sendButtonMessage } from '@waha/core/engines/noweb/noweb.buttons';
|
||||
import { NowebInMemoryStore } from '@waha/core/engines/noweb/store/NowebInMemoryStore';
|
||||
import { IMediaEngineProcessor } from '@waha/core/media/IMediaEngineProcessor';
|
||||
import { flipObject, parseBool, splitAt } from '@waha/helpers';
|
||||
import { flipObject, splitAt } from '@waha/helpers';
|
||||
import { PairingCodeResponse } from '@waha/structures/auth.dto';
|
||||
import { CallData } from '@waha/structures/calls.dto';
|
||||
import {
|
||||
@@ -65,7 +65,6 @@ import { SinglePeriodicJobRunner } from '@waha/utils/SinglePeriodicJobRunner';
|
||||
import * as Buffer from 'buffer';
|
||||
import { Agent } from 'https';
|
||||
import * as lodash from 'lodash';
|
||||
import { toNumber } from 'lodash';
|
||||
import * as NodeCache from 'node-cache';
|
||||
|
||||
import {
|
||||
@@ -226,6 +225,7 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
|
||||
}
|
||||
|
||||
async unpair() {
|
||||
this.unpairing = true;
|
||||
this.shouldRestart = false;
|
||||
await this.sock?.logout();
|
||||
}
|
||||
@@ -446,10 +446,13 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
|
||||
this.startDelayedJob.cancel();
|
||||
this.autoRestartJob.stop();
|
||||
|
||||
// We'll restart the client if it's in the process of unpairing
|
||||
this.status = WAHASessionStatus.FAILED;
|
||||
|
||||
// Wait in case of intentional logout
|
||||
await sleep(1_000);
|
||||
if (this.unpairing) {
|
||||
// Wait for unpairing to complete before ending the socket
|
||||
await sleep(1_000);
|
||||
}
|
||||
|
||||
await this.end();
|
||||
await this.store?.close();
|
||||
|
||||
@@ -200,15 +200,13 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
|
||||
.then(() => {
|
||||
// Listen for browser disconnected event
|
||||
this.whatsapp.pupBrowser.on('disconnected', () => {
|
||||
this.status = WAHASessionStatus.FAILED;
|
||||
this.logger.error('The browser has been disconnected');
|
||||
this.restartClient();
|
||||
this.failed();
|
||||
});
|
||||
// Listen for page close event
|
||||
this.whatsapp.pupPage.on('close', () => {
|
||||
this.status = WAHASessionStatus.FAILED;
|
||||
this.logger.error('The WhatsApp Web page has been closed');
|
||||
this.restartClient();
|
||||
this.failed();
|
||||
});
|
||||
|
||||
// Listen for page error event
|
||||
@@ -223,9 +221,8 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
this.status = WAHASessionStatus.FAILED;
|
||||
this.logger.error(error);
|
||||
this.restartClient();
|
||||
this.failed();
|
||||
return;
|
||||
});
|
||||
if (this.isDebugEnabled()) {
|
||||
@@ -240,8 +237,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
|
||||
await this.init().catch((err) => {
|
||||
this.logger.error('Failed to start the client');
|
||||
this.logger.error(err, err.stack);
|
||||
this.status = WAHASessionStatus.FAILED;
|
||||
this.restartClient();
|
||||
this.failed();
|
||||
});
|
||||
return this;
|
||||
}
|
||||
@@ -254,9 +250,17 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
|
||||
await this.end();
|
||||
}
|
||||
|
||||
protected failed() {
|
||||
// We'll restart the client if it's in the process of unpairing
|
||||
this.status = WAHASessionStatus.FAILED;
|
||||
this.restartClient();
|
||||
}
|
||||
|
||||
async unpair() {
|
||||
this.unpairing = true;
|
||||
this.shouldRestart = false;
|
||||
await this.whatsapp.unpair();
|
||||
// Wait for unpairing to complete
|
||||
await sleep(2_000);
|
||||
}
|
||||
|
||||
@@ -331,13 +335,13 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
|
||||
});
|
||||
|
||||
this.whatsapp.on(Events.AUTHENTICATION_FAILURE, (args) => {
|
||||
this.status = WAHASessionStatus.FAILED;
|
||||
this.failed();
|
||||
this.qr.save('');
|
||||
this.logger.info({ args: args }, `Session has failed to authenticate!`);
|
||||
});
|
||||
|
||||
this.whatsapp.on(Events.DISCONNECTED, (args) => {
|
||||
this.status = WAHASessionStatus.FAILED;
|
||||
this.failed();
|
||||
this.qr.save('');
|
||||
this.logger.info({ args: args }, `Session has been disconnected!`);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user