Skip to content

Commit

Permalink
Enhance settings and integrate Baileys controller for WhatsApp functi…
Browse files Browse the repository at this point in the history
…onality

- Added `wavoipToken` field to `Setting` model in both MySQL and PostgreSQL schemas.
- Updated `package.json` and `package-lock.json` to include `mime-types` and `socket.io-client` dependencies.
- Introduced `BaileysController` and `BaileysRouter` for handling WhatsApp interactions.
- Refactored media type handling to use `mime-types` instead of `mime` across various services.
- Updated DTOs and validation schemas to accommodate the new `wavoipToken` field.
- Implemented voice call functionalities using the Wavoip service in the Baileys integration.
- Enhanced event handling in the WebSocket controller to support new features.
  • Loading branch information
DavidsonGomes committed Jan 16, 2025
1 parent 616ae0a commit 5404672
Show file tree
Hide file tree
Showing 30 changed files with 748 additions and 36 deletions.
95 changes: 95 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"long": "^5.2.3",
"mediainfo.js": "^0.3.4",
"mime": "^4.0.0",
"mime-types": "^2.1.35",
"minio": "^8.0.3",
"multer": "^1.4.5-lts.1",
"node-cache": "^5.1.2",
Expand All @@ -93,6 +94,7 @@
"redis": "^4.7.0",
"sharp": "^0.32.6",
"socket.io": "^4.8.1",
"socket.io-client": "^4.8.1",
"tsup": "^8.3.5"
},
"devDependencies": {
Expand All @@ -101,6 +103,7 @@
"@types/express": "^4.17.18",
"@types/json-schema": "^7.0.15",
"@types/mime": "^4.0.0",
"@types/mime-types": "^2.1.4",
"@types/node": "^22.10.5",
"@types/node-cron": "^3.0.11",
"@types/qrcode": "^1.5.5",
Expand Down
1 change: 1 addition & 0 deletions prisma/mysql-schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ model Setting {
readMessages Boolean @default(false)
readStatus Boolean @default(false)
syncFullHistory Boolean @default(false)
wavoipToken String? @db.VarChar(100)
createdAt DateTime? @default(dbgenerated("CURRENT_TIMESTAMP")) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- A unique constraint covering the columns `[remoteJid,instanceId]` on the table `Chat` will be added. If there are existing duplicate values, this will fail.
*/
-- AlterTable
ALTER TABLE "Setting" ADD COLUMN "wavoipToken" VARCHAR(100);

-- CreateIndex
CREATE UNIQUE INDEX "Chat_remoteJid_instanceId_key" ON "Chat"("remoteJid", "instanceId");
1 change: 1 addition & 0 deletions prisma/postgresql-schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ model Setting {
readMessages Boolean @default(false) @db.Boolean
readStatus Boolean @default(false) @db.Boolean
syncFullHistory Boolean @default(false) @db.Boolean
wavoipToken String? @db.VarChar(100)
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
Expand Down
1 change: 1 addition & 0 deletions src/api/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export class InstanceController {
readMessages: instanceData.readMessages === true,
readStatus: instanceData.readStatus === true,
syncFullHistory: instanceData.syncFullHistory === true,
wavoipToken: instanceData.wavoipToken || '',
};

await this.settingsService.create(instance, settings);
Expand Down
1 change: 1 addition & 0 deletions src/api/dto/instance.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class InstanceDto extends IntegrationDto {
readMessages?: boolean;
readStatus?: boolean;
syncFullHistory?: boolean;
wavoipToken?: string;
// proxy
proxyHost?: string;
proxyPort?: string;
Expand Down
1 change: 1 addition & 0 deletions src/api/dto/settings.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export class SettingsDto {
readMessages?: boolean;
readStatus?: boolean;
syncFullHistory?: boolean;
wavoipToken?: string;
}
4 changes: 3 additions & 1 deletion src/api/integrations/channel/channel.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { Router } from 'express';

import { EvolutionRouter } from './evolution/evolution.router';
import { MetaRouter } from './meta/meta.router';
import { BaileysRouter } from './whatsapp/baileys.router';

export class ChannelRouter {
public readonly router: Router;

constructor(configService: any) {
constructor(configService: any, ...guards: any[]) {
this.router = Router();

this.router.use('/', new EvolutionRouter(configService).router);
this.router.use('/', new MetaRouter(configService).router);
this.router.use('/baileys', new BaileysRouter(...guards).router);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { BadRequestException, InternalServerErrorException } from '@exceptions';
import { status } from '@utils/renderStatus';
import { isURL } from 'class-validator';
import EventEmitter2 from 'eventemitter2';
import mime from 'mime';
import mimeTypes from 'mime-types';
import { v4 } from 'uuid';

export class EvolutionStartupService extends ChannelStartupService {
Expand Down Expand Up @@ -396,7 +396,7 @@ export class EvolutionStartupService extends ChannelStartupService {
mediaMessage.fileName = 'video.mp4';
}

let mimetype: string;
let mimetype: string | false;

const prepareMedia: any = {
caption: mediaMessage?.caption,
Expand All @@ -407,9 +407,9 @@ export class EvolutionStartupService extends ChannelStartupService {
};

if (isURL(mediaMessage.media)) {
mimetype = mime.getType(mediaMessage.media);
mimetype = mimeTypes.lookup(mediaMessage.media);
} else {
mimetype = mime.getType(mediaMessage.fileName);
mimetype = mimeTypes.lookup(mediaMessage.fileName);
}

prepareMedia.mimetype = mimetype;
Expand Down Expand Up @@ -449,7 +449,7 @@ export class EvolutionStartupService extends ChannelStartupService {
number = number.replace(/\D/g, '');
const hash = `${number}-${new Date().getTime()}`;

let mimetype: string;
let mimetype: string | false;

const prepareMedia: any = {
fileName: `${hash}.mp4`,
Expand All @@ -458,9 +458,9 @@ export class EvolutionStartupService extends ChannelStartupService {
};

if (isURL(audio)) {
mimetype = mime.getType(audio);
mimetype = mimeTypes.lookup(audio);
} else {
mimetype = mime.getType(prepareMedia.fileName);
mimetype = mimeTypes.lookup(prepareMedia.fileName);
}

prepareMedia.mimetype = mimetype;
Expand Down
14 changes: 7 additions & 7 deletions src/api/integrations/channel/meta/whatsapp.business.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { arrayUnique, isURL } from 'class-validator';
import EventEmitter2 from 'eventemitter2';
import FormData from 'form-data';
import { createReadStream } from 'fs';
import mime from 'mime';
import mimeTypes from 'mime-types';
import { join } from 'path';

export class BusinessStartupService extends ChannelStartupService {
Expand Down Expand Up @@ -1017,7 +1017,7 @@ export class BusinessStartupService extends ChannelStartupService {
mediaMessage.fileName = 'video.mp4';
}

let mimetype: string;
let mimetype: string | false;

const prepareMedia: any = {
caption: mediaMessage?.caption,
Expand All @@ -1028,11 +1028,11 @@ export class BusinessStartupService extends ChannelStartupService {
};

if (isURL(mediaMessage.media)) {
mimetype = mime.getType(mediaMessage.media);
mimetype = mimeTypes.lookup(mediaMessage.media);
prepareMedia.id = mediaMessage.media;
prepareMedia.type = 'link';
} else {
mimetype = mime.getType(mediaMessage.fileName);
mimetype = mimeTypes.lookup(mediaMessage.fileName);
const id = await this.getIdMedia(prepareMedia);
prepareMedia.id = id;
prepareMedia.type = 'id';
Expand Down Expand Up @@ -1075,7 +1075,7 @@ export class BusinessStartupService extends ChannelStartupService {
number = number.replace(/\D/g, '');
const hash = `${number}-${new Date().getTime()}`;

let mimetype: string;
let mimetype: string | false;

const prepareMedia: any = {
fileName: `${hash}.mp3`,
Expand All @@ -1084,11 +1084,11 @@ export class BusinessStartupService extends ChannelStartupService {
};

if (isURL(audio)) {
mimetype = mime.getType(audio);
mimetype = mimeTypes.lookup(audio);
prepareMedia.id = audio;
prepareMedia.type = 'link';
} else {
mimetype = mime.getType(prepareMedia.fileName);
mimetype = mimeTypes.lookup(prepareMedia.fileName);
const id = await this.getIdMedia(prepareMedia);
prepareMedia.id = id;
prepareMedia.type = 'id';
Expand Down
Loading

0 comments on commit 5404672

Please sign in to comment.