diff --git a/CHANGELOG.md b/CHANGELOG.md index e34529d3..3463f585 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# 1.1.2 (2023-06-28 13:43) + +### Fixed + +* Fixed problem that did not validate if the token passed in create instance already existed +* Fixed problem that does not delete instance files in server mode + # 1.1.1 (2023-06-28 10:27) ### Features @@ -8,6 +15,9 @@ ### Fixed * Adjust dockerfile variables +* Fixed baileys version in package.json +* Fixed problem that did not validate if the token passed in create instance already existed +* Fixed problem that does not delete instance files in server mode # 1.1.0 (2023-06-21 11:17) diff --git a/package.json b/package.json index 7c282d68..e99b05a9 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "homepage": "https://github.com/DavidsonGomes/evolution-api#readme", "dependencies": { "@adiwajshing/keyed-db": "^0.2.4", - "@evolution/base": "github:WhiskeySockets/Baileys", + "@whiskeysockets/baileys": "^6.3.0", "@ffmpeg-installer/ffmpeg": "^1.1.0", "@hapi/boom": "^10.0.1", "axios": "^1.3.5", diff --git a/src/db/redis.client.ts b/src/db/redis.client.ts index 892ee825..035ace54 100644 --- a/src/db/redis.client.ts +++ b/src/db/redis.client.ts @@ -1,6 +1,6 @@ import { createClient, RedisClientType } from '@redis/client'; import { Logger } from '../config/logger.config'; -import { BufferJSON } from '@evolution/base'; +import { BufferJSON } from '@whiskeysockets/baileys'; import { Redis } from '../config/env.config'; export class RedisCache { diff --git a/src/utils/use-multi-file-auth-state-db.ts b/src/utils/use-multi-file-auth-state-db.ts index f4db7e4b..d0c518da 100644 --- a/src/utils/use-multi-file-auth-state-db.ts +++ b/src/utils/use-multi-file-auth-state-db.ts @@ -5,7 +5,7 @@ import { initAuthCreds, proto, SignalDataTypeMap, -} from '@evolution/base'; +} from '@whiskeysockets/baileys'; import { configService, Database } from '../config/env.config'; import { Logger } from '../config/logger.config'; import { dbserver } from '../db/db.connect'; diff --git a/src/utils/use-multi-file-auth-state-redis-db.ts b/src/utils/use-multi-file-auth-state-redis-db.ts index 09bca0cd..76c54202 100644 --- a/src/utils/use-multi-file-auth-state-redis-db.ts +++ b/src/utils/use-multi-file-auth-state-redis-db.ts @@ -4,7 +4,7 @@ import { initAuthCreds, proto, SignalDataTypeMap, -} from '@evolution/base'; +} from '@whiskeysockets/baileys'; import { RedisCache } from '../db/redis.client'; import { Logger } from '../config/logger.config'; import { Redis } from '../config/env.config'; diff --git a/src/whatsapp/controllers/chat.controller.ts b/src/whatsapp/controllers/chat.controller.ts index f04cd9de..09d75e7e 100644 --- a/src/whatsapp/controllers/chat.controller.ts +++ b/src/whatsapp/controllers/chat.controller.ts @@ -1,4 +1,4 @@ -import { proto } from '@evolution/base'; +import { proto } from '@whiskeysockets/baileys'; import { ArchiveChatDto, DeleteMessage, diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 13ff6cb7..195f411d 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -1,4 +1,4 @@ -import { delay } from '@evolution/base'; +import { delay } from '@whiskeysockets/baileys'; import EventEmitter2 from 'eventemitter2'; import { Auth, ConfigService } from '../../config/env.config'; import { BadRequestException, InternalServerErrorException } from '../../exceptions'; @@ -41,6 +41,8 @@ export class InstanceController { ]); } + await this.authService.checkDuplicateToken(token); + const instance = new WAStartupService( this.configService, this.eventEmitter, @@ -84,6 +86,8 @@ export class InstanceController { events: getEvents, }; } else { + await this.authService.checkDuplicateToken(token); + const instance = new WAStartupService( this.configService, this.eventEmitter, @@ -159,6 +163,26 @@ export class InstanceController { } } + public async restartInstance({ instanceName }: InstanceDto) { + try { + delete this.waMonitor.waInstances[instanceName]; + console.log(this.waMonitor.waInstances[instanceName]); + const instance = new WAStartupService( + this.configService, + this.eventEmitter, + this.repository, + ); + + instance.instanceName = instanceName; + await instance.connectToWhatsapp(); + this.waMonitor.waInstances[instance.instanceName] = instance; + + return { error: false, message: 'Instance restarted' }; + } catch (error) { + this.logger.error(error); + } + } + public async connectionState({ instanceName }: InstanceDto) { return this.waMonitor.waInstances[instanceName]?.connectionStatus; } @@ -195,8 +219,15 @@ export class InstanceController { ]); } try { - delete this.waMonitor.waInstances[instanceName]; - return { error: false, message: 'Instance deleted' }; + if (stateConn.state === 'connecting') { + await this.logout({ instanceName }); + delete this.waMonitor.waInstances[instanceName]; + return { error: false, message: 'Instance deleted' }; + } else { + delete this.waMonitor.waInstances[instanceName]; + this.eventEmitter.emit('remove.instance', instanceName, 'inner'); + return { error: false, message: 'Instance deleted' }; + } } catch (error) { throw new BadRequestException(error.toString()); } diff --git a/src/whatsapp/dto/chat.dto.ts b/src/whatsapp/dto/chat.dto.ts index d95d90ca..f806ce98 100644 --- a/src/whatsapp/dto/chat.dto.ts +++ b/src/whatsapp/dto/chat.dto.ts @@ -2,7 +2,7 @@ import { WAPrivacyOnlineValue, WAPrivacyValue, WAReadReceiptsValue, -} from '@evolution/base'; +} from '@whiskeysockets/baileys'; export class OnWhatsAppDto { constructor( diff --git a/src/whatsapp/dto/sendMessage.dto.ts b/src/whatsapp/dto/sendMessage.dto.ts index 273d8aae..65b359ef 100644 --- a/src/whatsapp/dto/sendMessage.dto.ts +++ b/src/whatsapp/dto/sendMessage.dto.ts @@ -1,4 +1,4 @@ -import { proto, WAPresence } from '@evolution/base'; +import { proto, WAPresence } from '@whiskeysockets/baileys'; export class Quoted { key: proto.IMessageKey; diff --git a/src/whatsapp/repository/auth.repository.ts b/src/whatsapp/repository/auth.repository.ts index 2b53a7f0..4b6d4d00 100644 --- a/src/whatsapp/repository/auth.repository.ts +++ b/src/whatsapp/repository/auth.repository.ts @@ -4,6 +4,7 @@ import { IInsert, Repository } from '../abstract/abstract.repository'; import { IAuthModel, AuthRaw } from '../models'; import { readFileSync } from 'fs'; import { AUTH_DIR } from '../../config/path.config'; +import { InstanceDto } from '../dto/instance.dto'; export class AuthRepository extends Repository { constructor( diff --git a/src/whatsapp/routers/chat.router.ts b/src/whatsapp/routers/chat.router.ts index 9fe1141f..3c8a25ad 100644 --- a/src/whatsapp/routers/chat.router.ts +++ b/src/whatsapp/routers/chat.router.ts @@ -29,7 +29,7 @@ import { chatController } from '../whatsapp.module'; import { RouterBroker } from '../abstract/abstract.router'; import { HttpStatus } from './index.router'; import { MessageUpQuery } from '../repository/messageUp.repository'; -import { proto } from '@evolution/base'; +import { proto } from '@whiskeysockets/baileys'; import { InstanceDto } from '../dto/instance.dto'; export class ChatRouter extends RouterBroker { diff --git a/src/whatsapp/routers/instance.router.ts b/src/whatsapp/routers/instance.router.ts index 1a38da40..29a59a87 100644 --- a/src/whatsapp/routers/instance.router.ts +++ b/src/whatsapp/routers/instance.router.ts @@ -23,6 +23,16 @@ export class InstanceRouter extends RouterBroker { return res.status(HttpStatus.CREATED).json(response); }) + .put(this.routerPath('restart'), ...guards, async (req, res) => { + const response = await this.dataValidate({ + request: req, + schema: instanceNameSchema, + ClassRef: InstanceDto, + execute: (instance) => instanceController.restartInstance(instance), + }); + + return res.status(HttpStatus.OK).json(response); + }) .get(this.routerPath('connect'), ...guards, async (req, res) => { const response = await this.dataValidate({ request: req, diff --git a/src/whatsapp/services/auth.service.ts b/src/whatsapp/services/auth.service.ts index fc3cdcc1..368eb3c9 100644 --- a/src/whatsapp/services/auth.service.ts +++ b/src/whatsapp/services/auth.service.ts @@ -72,6 +72,18 @@ export class AuthService { return { apikey }; } + public async checkDuplicateToken(token: string) { + const instances = await this.waMonitor.instanceInfo(); + + const instance = instances.find((instance) => instance.instance.apikey === token); + + if (instance) { + throw new BadRequestException('Token already exists'); + } + + return true; + } + public async generateHash(instance: InstanceDto, token?: string) { const options = this.configService.get('AUTHENTICATION'); return (await this[options.TYPE](instance, token)) as diff --git a/src/whatsapp/services/monitor.service.ts b/src/whatsapp/services/monitor.service.ts index 37f06e4e..821340fe 100644 --- a/src/whatsapp/services/monitor.service.ts +++ b/src/whatsapp/services/monitor.service.ts @@ -16,7 +16,6 @@ import { NotFoundException } from '../../exceptions'; import { Db } from 'mongodb'; import { RedisCache } from '../../db/redis.client'; import { initInstance } from '../whatsapp.module'; -import { ValidationError } from 'class-validator'; export class WAMonitoringService { constructor( @@ -50,9 +49,19 @@ export class WAMonitoringService { public delInstanceTime(instance: string) { const time = this.configService.get('DEL_INSTANCE'); if (typeof time === 'number' && time > 0) { - setTimeout(() => { + setTimeout(async () => { if (this.waInstances[instance]?.connectionStatus?.state !== 'open') { - delete this.waInstances[instance]; + if (this.waInstances[instance]?.connectionStatus?.state === 'connecting') { + await this.waInstances[instance]?.client?.logout( + 'Log out instance: ' + instance, + ); + this.waInstances[instance]?.client?.ws?.close(); + this.waInstances[instance]?.client?.end(undefined); + delete this.waInstances[instance]; + } else { + delete this.waInstances[instance]; + this.eventEmitter.emit('remove.instance', instance, 'inner'); + } } }, 1000 * 60 * time); } @@ -156,7 +165,7 @@ export class WAMonitoringService { }, 3600 * 1000 * 2); } - private async cleaningUp(instanceName: string) { + public async cleaningUp(instanceName: string) { if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) { await this.repository.dbServer.connect(); const collections: any[] = await this.dbInstance.collections(); diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 9fa90841..ac912af6 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -30,7 +30,7 @@ import makeWASocket, { WAMessageUpdate, WASocket, getAggregateVotesInPollMessage, -} from '@evolution/base'; +} from '@whiskeysockets/baileys'; import { Auth, CleanStoreConf, diff --git a/src/whatsapp/types/wa.types.ts b/src/whatsapp/types/wa.types.ts index fbf2fb99..1686f031 100644 --- a/src/whatsapp/types/wa.types.ts +++ b/src/whatsapp/types/wa.types.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-namespace */ -import { AuthenticationState, WAConnectionState } from '@evolution/base'; +import { AuthenticationState, WAConnectionState } from '@whiskeysockets/baileys'; export enum Events { APPLICATION_STARTUP = 'application.startup', diff --git a/src/whatsapp/whatsapp.module.ts b/src/whatsapp/whatsapp.module.ts index 814753e2..f1cb5444 100644 --- a/src/whatsapp/whatsapp.module.ts +++ b/src/whatsapp/whatsapp.module.ts @@ -27,7 +27,7 @@ import { WebhookRepository } from './repository/webhook.repository'; import { WebhookModel } from './models/webhook.model'; import { AuthRepository } from './repository/auth.repository'; import { WAStartupService } from './services/whatsapp.service'; -import { delay } from '@evolution/base'; +import { delay } from '@whiskeysockets/baileys'; import { Events } from './types/wa.types'; const logger = new Logger('WA MODULE');