Skip to content

Commit

Permalink
Merge pull request #1126 from jesus-chacon/update_chats
Browse files Browse the repository at this point in the history
Fix: cuid security deprecation, update libs, lint and improve chat DB update
  • Loading branch information
DavidsonGomes authored Jan 7, 2025
2 parents f971f38 + 236b0f9 commit ca451bf
Show file tree
Hide file tree
Showing 17 changed files with 12,668 additions and 126 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ lerna-debug.log*
# Package
/yarn.lock
/pnpm-lock.yaml
/package-lock.json

# IDEs
.vscode/*
Expand Down
12,517 changes: 12,517 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

75 changes: 37 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,73 +49,72 @@
"homepage": "https://github.com/EvolutionAPI/evolution-api#readme",
"dependencies": {
"@adiwajshing/keyed-db": "^0.2.4",
"@aws-sdk/client-sqs": "^3.569.0",
"@aws-sdk/client-sqs": "^3.723.0",
"@ffmpeg-installer/ffmpeg": "^1.1.0",
"@figuro/chatwoot-sdk": "^1.1.16",
"@hapi/boom": "^10.0.1",
"@paralleldrive/cuid2": "^2.2.2",
"@prisma/client": "^6.1.0",
"@sentry/node": "^8.28.0",
"amqplib": "^0.10.3",
"axios": "^1.6.5",
"@sentry/node": "^8.47.0",
"amqplib": "^0.10.5",
"axios": "^1.7.9",
"baileys": "github:EvolutionAPI/Baileys",
"class-validator": "^0.14.1",
"compression": "^1.7.4",
"compression": "^1.7.5",
"cors": "^2.8.5",
"cuid": "^3.0.0",
"dayjs": "^1.11.7",
"dotenv": "^16.4.5",
"dayjs": "^1.11.13",
"dotenv": "^16.4.7",
"eventemitter2": "^6.4.9",
"express": "^4.18.2",
"express": "^4.21.2",
"express-async-errors": "^3.1.1",
"fluent-ffmpeg": "^2.1.2",
"form-data": "^4.0.0",
"https-proxy-agent": "^7.0.2",
"fluent-ffmpeg": "^2.1.3",
"form-data": "^4.0.1",
"https-proxy-agent": "^7.0.6",
"i18next": "^23.7.19",
"jimp": "^0.16.13",
"json-schema": "^0.4.0",
"jsonschema": "^1.4.1",
"link-preview-js": "^3.0.4",
"link-preview-js": "^3.0.13",
"long": "^5.2.3",
"mediainfo.js": "^0.3.2",
"mime": "^3.0.0",
"minio": "^8.0.1",
"mediainfo.js": "^0.3.4",
"mime": "^4.0.6",
"minio": "^8.0.3",
"multer": "^1.4.5-lts.1",
"node-cache": "^5.1.2",
"node-cron": "^3.0.3",
"openai": "^4.52.7",
"pg": "^8.11.3",
"openai": "^4.77.3",
"pg": "^8.13.1",
"pino": "^8.11.0",
"prisma": "^6.1.0",
"pusher": "^5.2.0",
"qrcode": "^1.5.1",
"qrcode": "^1.5.4",
"qrcode-terminal": "^0.12.0",
"redis": "^4.6.5",
"sharp": "^0.32.2",
"socket.io": "^4.7.1",
"tsup": "^8.2.4",
"uuid": "^9.0.0"
"redis": "^4.7.0",
"sharp": "^0.32.6",
"socket.io": "^4.8.1",
"tsup": "^8.3.5"
},
"devDependencies": {
"@types/compression": "^1.7.2",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/compression": "^1.7.5",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.18",
"@types/json-schema": "^7.0.15",
"@types/mime": "3.0.0",
"@types/node": "^18.15.11",
"@types/mime": "4.0.0",
"@types/node": "^22.10.5",
"@types/node-cron": "^3.0.11",
"@types/qrcode": "^1.5.0",
"@types/qrcode-terminal": "^0.12.0",
"@types/uuid": "^8.3.4",
"@types/qrcode": "^1.5.5",
"@types/qrcode-terminal": "^0.12.2",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-simple-import-sort": "^10.0.0",
"prettier": "^2.8.8",
"prettier": "^3.4.2",
"ts-node-dev": "^2.0.0",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.5.4"
"typescript": "^5.7.2"
}
}
}
5 changes: 4 additions & 1 deletion src/api/controllers/proxy.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import axios from 'axios';
const logger = new Logger('ProxyController');

export class ProxyController {
constructor(private readonly proxyService: ProxyService, private readonly waMonitor: WAMonitoringService) {}
constructor(
private readonly proxyService: ProxyService,
private readonly waMonitor: WAMonitoringService,
) {}

public async createProxy(instance: InstanceDto, data: ProxyDto) {
if (!this.waMonitor.waInstances[instance.instanceName]) {
Expand Down
38 changes: 19 additions & 19 deletions src/api/integrations/channel/meta/whatsapp.business.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ export class BusinessStartupService extends ChannelStartupService {
}

private isMediaMessage(message: any) {
return message.document ||
message.image ||
message.audio ||
message.video
return message.document || message.image || message.audio || message.video;
}

private async post(message: any, params: string) {
Expand Down Expand Up @@ -333,13 +330,17 @@ export class BusinessStartupService extends ChannelStartupService {

const buffer = await axios.get(result.data.url, { headers, responseType: 'arraybuffer' });

const mediaType = message.messages[0].document
? 'document'
: message.messages[0].image
? 'image'
: message.messages[0].audio
? 'audio'
: 'video';
let mediaType;

if (message.messages[0].document) {
mediaType = 'document';
} else if (message.messages[0].image) {
mediaType = 'image';
} else if (message.messages[0].audio) {
mediaType = 'audio';
} else {
mediaType = 'video';
}

const mimetype = result.data?.mime_type || result.headers['content-type'];

Expand Down Expand Up @@ -479,7 +480,7 @@ export class BusinessStartupService extends ChannelStartupService {
message: {
mediaUrl: messageRaw.message.mediaUrl,
...messageRaw,
}
},
},
() => {},
);
Expand Down Expand Up @@ -800,7 +801,7 @@ export class BusinessStartupService extends ChannelStartupService {
}
if (message['media']) {
const isImage = message['mimetype']?.startsWith('image/');

content = {
messaging_product: 'whatsapp',
recipient_type: 'individual',
Expand All @@ -815,7 +816,7 @@ export class BusinessStartupService extends ChannelStartupService {
};
quoted ? (content.context = { message_id: quoted.id }) : content;
return await this.post(content, 'messages');
}
}
if (message['audio']) {
content = {
messaging_product: 'whatsapp',
Expand Down Expand Up @@ -1103,11 +1104,10 @@ export class BusinessStartupService extends ChannelStartupService {

if (file?.buffer) {
mediaData.audio = file.buffer.toString('base64');
}
else if(isURL(mediaData.audio)){
mediaData.audio = mediaData.audio
}
else {
} else if (isURL(mediaData.audio)) {
// DO NOTHING
// mediaData.audio = mediaData.audio;
} else {
console.error('El archivo no tiene buffer o file es undefined');
throw new Error('File or buffer is undefined');
}
Expand Down
47 changes: 21 additions & 26 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
import { BadRequestException, InternalServerErrorException, NotFoundException } from '@exceptions';
import ffmpegPath from '@ffmpeg-installer/ffmpeg';
import { Boom } from '@hapi/boom';
import { createId as cuid } from '@paralleldrive/cuid2';
import { Instance } from '@prisma/client';
import { makeProxyAgent } from '@utils/makeProxyAgent';
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
Expand Down Expand Up @@ -125,7 +126,6 @@ import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
import { spawn } from 'child_process';
import { isArray, isBase64, isURL } from 'class-validator';
import { randomBytes } from 'crypto';
import cuid from 'cuid';
import EventEmitter2 from 'eventemitter2';
import ffmpeg from 'fluent-ffmpeg';
import FormData from 'form-data';
Expand Down Expand Up @@ -1136,29 +1136,25 @@ export class BaileysStartupService extends ChannelStartupService {
}
const existingChat = await this.prismaRepository.chat.findFirst({
where: { instanceId: this.instanceId, remoteJid: received.key.remoteJid },
select: { id: true, name: true },
});

if (existingChat) {
const chatToInsert = {
remoteJid: received.key.remoteJid,
instanceId: this.instanceId,
name: received.pushName || '',
unreadMessages: 0,
};

this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]);
if (
existingChat &&
received.pushName &&
existingChat.name !== received.pushName &&
received.pushName.trim().length > 0
) {
this.sendDataWebhook(Events.CHATS_UPSERT, [{ ...existingChat, name: received.pushName }]);
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
try {
await this.prismaRepository.chat.update({
where: {
id: existingChat.id,
},
data: chatToInsert,
});
}
catch(error){
console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`);
}
try {
await this.prismaRepository.chat.update({
where: { id: existingChat.id },
data: { name: received.pushName },
});
} catch (error) {
console.log(`Chat insert record ignored: ${received.key.remoteJid} - ${this.instanceId}`);
}
}
}

Expand Down Expand Up @@ -1494,13 +1490,12 @@ export class BaileysStartupService extends ChannelStartupService {
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
try {
await this.prismaRepository.chat.update({
where: {
id: existingChat.id,
},
where: {
id: existingChat.id,
},
data: chatToInsert,
});
}
catch(error){
} catch (error) {
console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/api/integrations/event/webhook/webhook.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export class WebhookController extends EventController implements EventControlle
local,
}: EmitData): Promise<void> {
const instance = (await this.get(instanceName)) as wa.LocalWebHook;


const webhookConfig = configService.get<Webhook>('WEBHOOK');
const webhookLocal = instance?.events;
Expand All @@ -86,7 +85,7 @@ export class WebhookController extends EventController implements EventControlle
apikey: apiKey,
};

if (local && !instance || !instance?.enabled) {
if ((local && !instance) || !instance?.enabled) {
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
let baseURL: string;

Expand Down
5 changes: 4 additions & 1 deletion src/api/integrations/event/webhook/webhook.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { instanceSchema, webhookSchema } from '@validate/validate.schema';
import { RequestHandler, Router } from 'express';

export class WebhookRouter extends RouterBroker {
constructor(readonly configService: ConfigService, ...guards: RequestHandler[]) {
constructor(
readonly configService: ConfigService,
...guards: RequestHandler[]
) {
super();
this.router
.post(this.routerPath('set'), ...guards, async (req, res) => {
Expand Down
5 changes: 4 additions & 1 deletion src/api/routes/instance.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { RequestHandler, Router } from 'express';
import { HttpStatus } from './index.router';

export class InstanceRouter extends RouterBroker {
constructor(readonly configService: ConfigService, ...guards: RequestHandler[]) {
constructor(
readonly configService: ConfigService,
...guards: RequestHandler[]
) {
super();
this.router
.post('/create', ...guards, async (req, res) => {
Expand Down
5 changes: 4 additions & 1 deletion src/api/routes/template.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { RequestHandler, Router } from 'express';
import { HttpStatus } from './index.router';

export class TemplateRouter extends RouterBroker {
constructor(readonly configService: ConfigService, ...guards: RequestHandler[]) {
constructor(
readonly configService: ConfigService,
...guards: RequestHandler[]
) {
super();
this.router
.post(this.routerPath('create'), ...guards, async (req, res) => {
Expand Down
Loading

0 comments on commit ca451bf

Please sign in to comment.