Skip to content

Commit

Permalink
Refactor instance deletion logic and enhance WhatsApp connection updates
Browse files Browse the repository at this point in the history
- Updated the `deleteInstance` method to allow logout for instances in 'connecting' or 'open' states, improving instance management.
- Enhanced the `BaileysStartupService` to include additional profile information (wuid, profileName, profilePictureUrl) in connection update webhooks.
- Removed redundant webhook data sending logic, streamlining connection state updates for better performance.
- Adjusted settings schema to ensure required fields are properly validated.
  • Loading branch information
DavidsonGomes committed Jan 17, 2025
1 parent ac58f58 commit cfe6bd9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
6 changes: 1 addition & 5 deletions src/api/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,11 @@ export class InstanceController {

public async deleteInstance({ instanceName }: InstanceDto) {
const { instance } = await this.connectionState({ instanceName });

if (instance.state === 'open') {
throw new BadRequestException('The "' + instanceName + '" instance needs to be disconnected');
}
try {
const waInstances = this.waMonitor.waInstances[instanceName];
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) waInstances?.clearCacheChatwoot();

if (instance.state === 'connecting') {
if (instance.state === 'connecting' || instance.state === 'open') {
await this.logout({ instanceName });
}

Expand Down
28 changes: 23 additions & 5 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ export class BaileysStartupService extends ChannelStartupService {
instance: this.instance.name,
state: 'refused',
statusReason: DisconnectReason.connectionClosed,
wuid: this.instance.wuid,
profileName: await this.getProfileName(),
profilePictureUrl: this.instance.profilePictureUrl,
});

this.endSession = true;
Expand Down Expand Up @@ -391,11 +394,6 @@ export class BaileysStartupService extends ChannelStartupService {
state: connection,
statusReason: (lastDisconnect?.error as Boom)?.output?.statusCode ?? 200,
};

this.sendDataWebhook(Events.CONNECTION_UPDATE, {
instance: this.instance.name,
...this.stateConnection,
});
}

if (connection === 'close') {
Expand Down Expand Up @@ -437,6 +435,11 @@ export class BaileysStartupService extends ChannelStartupService {
this.eventEmitter.emit('logout.instance', this.instance.name, 'inner');
this.client?.ws?.close();
this.client.end(new Error('Close connection'));

this.sendDataWebhook(Events.CONNECTION_UPDATE, {
instance: this.instance.name,
...this.stateConnection,
});
}
}

Expand Down Expand Up @@ -484,6 +487,21 @@ export class BaileysStartupService extends ChannelStartupService {
);
this.syncChatwootLostMessages();
}

this.sendDataWebhook(Events.CONNECTION_UPDATE, {
instance: this.instance.name,
wuid: this.instance.wuid,
profileName: await this.getProfileName(),
profilePictureUrl: this.instance.profilePictureUrl,
...this.stateConnection,
});
}

if (connection === 'connecting') {
this.sendDataWebhook(Events.CONNECTION_UPDATE, {
instance: this.instance.name,
...this.stateConnection,
});
}
}

Expand Down
20 changes: 2 additions & 18 deletions src/validate/settings.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@ export const settingsSchema: JSONSchema7 = {
syncFullHistory: { type: 'boolean' },
wavoipToken: { type: 'string' },
},
required: [
'rejectCall',
'groupsIgnore',
'alwaysOnline',
'readMessages',
'readStatus',
'syncFullHistory',
'wavoipToken',
],
...isNotEmpty(
'rejectCall',
'groupsIgnore',
'alwaysOnline',
'readMessages',
'readStatus',
'syncFullHistory',
'wavoipToken',
),
required: ['rejectCall', 'groupsIgnore', 'alwaysOnline', 'readMessages', 'readStatus', 'syncFullHistory'],
...isNotEmpty('rejectCall', 'groupsIgnore', 'alwaysOnline', 'readMessages', 'readStatus', 'syncFullHistory'),
};

0 comments on commit cfe6bd9

Please sign in to comment.