mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-21 12:52:19 -06:00
Simplify events structure and fix minor issues
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
import { Constructor } from '@api/integrations/integration.dto';
|
||||
|
||||
export class WebsocketDto {
|
||||
enabled: boolean;
|
||||
events?: string[];
|
||||
}
|
||||
|
||||
export function WebsocketInstanceMixin<TBase extends Constructor>(Base: TBase) {
|
||||
return class extends Base {
|
||||
websocketEnabled?: boolean;
|
||||
websocketEvents?: string[];
|
||||
};
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
import { JSONSchema7 } from 'json-schema';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
|
||||
const properties = {};
|
||||
propertyNames.forEach(
|
||||
(property) =>
|
||||
(properties[property] = {
|
||||
minLength: 1,
|
||||
description: `The "${property}" cannot be empty`,
|
||||
}),
|
||||
);
|
||||
return {
|
||||
if: {
|
||||
propertyNames: {
|
||||
enum: [...propertyNames],
|
||||
},
|
||||
},
|
||||
then: { properties },
|
||||
};
|
||||
};
|
||||
|
||||
export const websocketSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
enabled: { type: 'boolean', enum: [true, false] },
|
||||
events: {
|
||||
type: 'array',
|
||||
minItems: 0,
|
||||
items: {
|
||||
type: 'string',
|
||||
enum: [
|
||||
'APPLICATION_STARTUP',
|
||||
'QRCODE_UPDATED',
|
||||
'MESSAGES_SET',
|
||||
'MESSAGES_UPSERT',
|
||||
'MESSAGES_EDITED',
|
||||
'MESSAGES_UPDATE',
|
||||
'MESSAGES_DELETE',
|
||||
'SEND_MESSAGE',
|
||||
'CONTACTS_SET',
|
||||
'CONTACTS_UPSERT',
|
||||
'CONTACTS_UPDATE',
|
||||
'PRESENCE_UPDATE',
|
||||
'CHATS_SET',
|
||||
'CHATS_UPSERT',
|
||||
'CHATS_UPDATE',
|
||||
'CHATS_DELETE',
|
||||
'GROUPS_UPSERT',
|
||||
'GROUP_UPDATE',
|
||||
'GROUP_PARTICIPANTS_UPDATE',
|
||||
'CONNECTION_UPDATE',
|
||||
'LABELS_EDIT',
|
||||
'LABELS_ASSOCIATION',
|
||||
'CALL',
|
||||
'TYPEBOT_START',
|
||||
'TYPEBOT_CHANGE_STATUS',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ['enabled'],
|
||||
...isNotEmpty('enabled'),
|
||||
};
|
||||
@@ -1,28 +1,28 @@
|
||||
import { WebsocketDto } from '@api/integrations/event/websocket/dto/websocket.dto';
|
||||
import { EventDto } from '@api/integrations/event/event.dto';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
import { wa } from '@api/types/wa.types';
|
||||
import { configService, Cors, Log, Websocket } from '@config/env.config';
|
||||
import { Logger } from '@config/logger.config';
|
||||
import { NotFoundException } from '@exceptions';
|
||||
import { Server } from 'http';
|
||||
import { Server as SocketIO } from 'socket.io';
|
||||
|
||||
import { EmitData, EventController, EventControllerInterface } from '../../event.controller';
|
||||
import { EmitData, EventController, EventControllerInterface } from '../event.controller';
|
||||
|
||||
export class WebsocketController extends EventController implements EventControllerInterface {
|
||||
private io: SocketIO;
|
||||
private corsConfig: Array<any>;
|
||||
private readonly logger = new Logger(WebsocketController.name);
|
||||
integrationEnabled = configService.get<Websocket>('WEBSOCKET')?.ENABLED;
|
||||
|
||||
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
|
||||
super(prismaRepository, waMonitor);
|
||||
super(prismaRepository, waMonitor, configService.get<Websocket>('WEBSOCKET')?.ENABLED, 'websocket');
|
||||
|
||||
this.cors = configService.get<Cors>('CORS').ORIGIN;
|
||||
}
|
||||
|
||||
public init(httpServer: Server): void {
|
||||
if (!this.integrationEnabled) return;
|
||||
if (!this.status) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.socket = new SocketIO(httpServer, {
|
||||
cors: {
|
||||
@@ -57,57 +57,6 @@ export class WebsocketController extends EventController implements EventControl
|
||||
return this.io;
|
||||
}
|
||||
|
||||
public async set(instanceName: string, data: WebsocketDto): Promise<wa.LocalWebsocket> {
|
||||
if (!this.integrationEnabled) return;
|
||||
|
||||
if (!data.enabled) {
|
||||
data.events = [];
|
||||
} else {
|
||||
if (0 === data.events.length) {
|
||||
data.events = this.events;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await this.get(instanceName);
|
||||
|
||||
return this.prisma.websocket.update({
|
||||
where: {
|
||||
instanceId: this.monitor.waInstances[instanceName].instanceId,
|
||||
},
|
||||
data,
|
||||
});
|
||||
} catch (err) {
|
||||
return this.prisma.websocket.create({
|
||||
data: {
|
||||
enabled: data.enabled,
|
||||
events: data.events,
|
||||
instanceId: this.monitor.waInstances[instanceName].instanceId,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async get(instanceName: string): Promise<wa.LocalWebsocket> {
|
||||
if (!this.integrationEnabled) return;
|
||||
|
||||
if (undefined === this.monitor.waInstances[instanceName]) {
|
||||
throw new NotFoundException('Instance not found');
|
||||
}
|
||||
|
||||
const data = await this.prisma.websocket.findUnique({
|
||||
where: {
|
||||
instanceId: this.monitor.waInstances[instanceName].instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
throw new NotFoundException('Instance websocket not found');
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public async emit({
|
||||
instanceName,
|
||||
origin,
|
||||
@@ -118,7 +67,9 @@ export class WebsocketController extends EventController implements EventControl
|
||||
sender,
|
||||
apiKey,
|
||||
}: EmitData): Promise<void> {
|
||||
if (!this.integrationEnabled) return;
|
||||
if (!this.status) {
|
||||
return;
|
||||
}
|
||||
|
||||
const configEv = event.replace(/[.-]/gm, '_').toUpperCase();
|
||||
const logEnabled = configService.get<Log>('LOG').LEVEL.includes('WEBSOCKET');
|
||||
@@ -144,13 +95,13 @@ export class WebsocketController extends EventController implements EventControl
|
||||
}
|
||||
|
||||
try {
|
||||
const instanceSocket = await this.get(instanceName);
|
||||
const instance = (await this.get(instanceName)) as EventDto;
|
||||
|
||||
if (!instanceSocket?.enabled) {
|
||||
if (!instance?.websocket.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Array.isArray(instanceSocket?.events) && instanceSocket?.events.includes(configEv)) {
|
||||
if (Array.isArray(instance?.websocket.events) && instance?.websocket.events.includes(configEv)) {
|
||||
this.socket.of(`/${instanceName}`).emit(event, message);
|
||||
|
||||
if (logEnabled) {
|
||||
@@ -1,9 +1,9 @@
|
||||
import { RouterBroker } from '@api/abstract/abstract.router';
|
||||
import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import { WebsocketDto } from '@api/integrations/event/websocket/dto/websocket.dto';
|
||||
import { websocketController } from '@api/server.module';
|
||||
import { EventDto } from '@api/integrations/event/event.dto';
|
||||
import { HttpStatus } from '@api/routes/index.router';
|
||||
import { instanceSchema, websocketSchema } from '@validate/validate.schema';
|
||||
import { eventManager } from '@api/server.module';
|
||||
import { eventSchema, instanceSchema } from '@validate/validate.schema';
|
||||
import { RequestHandler, Router } from 'express';
|
||||
|
||||
export class WebsocketRouter extends RouterBroker {
|
||||
@@ -11,11 +11,11 @@ export class WebsocketRouter extends RouterBroker {
|
||||
super();
|
||||
this.router
|
||||
.post(this.routerPath('set'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<WebsocketDto>({
|
||||
const response = await this.dataValidate<EventDto>({
|
||||
request: req,
|
||||
schema: websocketSchema,
|
||||
ClassRef: WebsocketDto,
|
||||
execute: (instance, data) => websocketController.set(instance.instanceName, data),
|
||||
schema: eventSchema,
|
||||
ClassRef: EventDto,
|
||||
execute: (instance, data) => eventManager.websocket.set(instance.instanceName, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.CREATED).json(response);
|
||||
@@ -25,7 +25,7 @@ export class WebsocketRouter extends RouterBroker {
|
||||
request: req,
|
||||
schema: instanceSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => websocketController.get(instance.instanceName),
|
||||
execute: (instance) => eventManager.websocket.get(instance.instanceName),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
Reference in New Issue
Block a user