Skip to content

Commit 427c994

Browse files
committedFeb 3, 2025
Merge branch 'release/2.2.3'
2 parents db9cdbf + da74611 commit 427c994

File tree

7 files changed

+83
-79
lines changed

7 files changed

+83
-79
lines changed
 

‎CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 2.2.3 (2025-02-03 11:52)
2+
3+
### Fixed
4+
5+
* Fix cache in local file system
6+
* Update Baileys Version
7+
18
# 2.2.2 (2025-01-31 06:55)
29

310
### Features

‎Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM node:20-alpine AS builder
33
RUN apk update && \
44
apk add git ffmpeg wget curl bash openssl
55

6-
LABEL version="2.2.2" description="Api to control whatsapp features through http requests."
6+
LABEL version="2.2.3" description="Api to control whatsapp features through http requests."
77
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
88
LABEL contact="contato@atendai.com"
99

‎package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "evolution-api",
3-
"version": "2.2.2",
3+
"version": "2.2.3",
44
"description": "Rest api for communication with WhatsApp",
55
"main": "./dist/main.js",
66
"type": "commonjs",

‎src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,7 @@ export class BaileysStartupService extends ChannelStartupService {
21632163
const cache = this.configService.get<CacheConf>('CACHE');
21642164
if (!cache.REDIS.ENABLED && !cache.LOCAL.ENABLED) group = await this.findGroup({ groupJid: sender }, 'inner');
21652165
else group = await this.getGroupMetadataCache(sender);
2166+
// group = await this.findGroup({ groupJid: sender }, 'inner');
21662167
} catch (error) {
21672168
throw new NotFoundException('Group not found');
21682169
}
@@ -3551,25 +3552,31 @@ export class BaileysStartupService extends ChannelStartupService {
35513552
const messageId = response.message?.protocolMessage?.key?.id;
35523553
if (messageId) {
35533554
const isLogicalDeleted = configService.get<Database>('DATABASE').DELETE_DATA.LOGICAL_MESSAGE_DELETE;
3554-
let message = await this.prismaRepository.message.findUnique({
3555-
where: { id: messageId },
3555+
let message = await this.prismaRepository.message.findFirst({
3556+
where: {
3557+
key: {
3558+
path: ['id'],
3559+
equals: messageId,
3560+
},
3561+
},
35563562
});
35573563
if (isLogicalDeleted) {
35583564
if (!message) return response;
35593565
const existingKey = typeof message?.key === 'object' && message.key !== null ? message.key : {};
35603566
message = await this.prismaRepository.message.update({
3561-
where: { id: messageId },
3567+
where: { id: message.id },
35623568
data: {
35633569
key: {
35643570
...existingKey,
35653571
deleted: true,
35663572
},
3573+
status: 'DELETED',
35673574
},
35683575
});
35693576
} else {
35703577
await this.prismaRepository.message.deleteMany({
35713578
where: {
3572-
id: messageId,
3579+
id: message.id,
35733580
},
35743581
});
35753582
}
@@ -3578,7 +3585,7 @@ export class BaileysStartupService extends ChannelStartupService {
35783585
instanceId: message.instanceId,
35793586
key: message.key,
35803587
messageType: message.messageType,
3581-
status: message.status,
3588+
status: 'DELETED',
35823589
source: message.source,
35833590
messageTimestamp: message.messageTimestamp,
35843591
pushName: message.pushName,

‎src/api/integrations/chatbot/dify/services/dify.service.ts

+23-43
Original file line numberDiff line numberDiff line change
@@ -224,63 +224,43 @@ export class DifyService {
224224
headers: {
225225
Authorization: `Bearer ${dify.apiKey}`,
226226
},
227-
responseType: 'stream',
228227
});
229228

230229
let conversationId;
231230
let answer = '';
232231

233-
const stream = response.data;
234-
const reader = new Readable().wrap(stream);
232+
const data = response.data.replaceAll('data: ', '');
235233

236-
reader.on('data', (chunk) => {
237-
const data = chunk.toString().replace(/data:\s*/g, '');
234+
const events = data.split('\n').filter((line) => line.trim() !== '');
238235

239-
if (data.trim() === '' || !data.startsWith('{')) {
240-
return;
241-
}
242-
243-
try {
244-
const events = data.split('\n').filter((line) => line.trim() !== '');
236+
for (const eventString of events) {
237+
if (eventString.trim().startsWith('{')) {
238+
const event = JSON.parse(eventString);
245239

246-
for (const eventString of events) {
247-
if (eventString.trim().startsWith('{')) {
248-
const event = JSON.parse(eventString);
249-
250-
if (event?.event === 'agent_message') {
251-
console.log('event:', event);
252-
conversationId = conversationId ?? event?.conversation_id;
253-
answer += event?.answer;
254-
}
255-
}
240+
if (event?.event === 'agent_message') {
241+
console.log('event:', event);
242+
conversationId = conversationId ?? event?.conversation_id;
243+
answer += event?.answer;
256244
}
257-
} catch (error) {
258-
console.error('Error parsing stream data:', error);
259245
}
260-
});
261-
262-
reader.on('end', async () => {
263-
if (instance.integration === Integration.WHATSAPP_BAILEYS)
264-
await instance.client.sendPresenceUpdate('paused', remoteJid);
246+
}
265247

266-
const message = answer;
248+
if (instance.integration === Integration.WHATSAPP_BAILEYS)
249+
await instance.client.sendPresenceUpdate('paused', remoteJid);
267250

268-
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
251+
const message = answer;
269252

270-
await this.prismaRepository.integrationSession.update({
271-
where: {
272-
id: session.id,
273-
},
274-
data: {
275-
status: 'opened',
276-
awaitUser: true,
277-
sessionId: conversationId,
278-
},
279-
});
280-
});
253+
await this.sendMessageWhatsApp(instance, remoteJid, message, settings);
281254

282-
reader.on('error', (error) => {
283-
console.error('Error reading stream:', error);
255+
await this.prismaRepository.integrationSession.update({
256+
where: {
257+
id: session.id,
258+
},
259+
data: {
260+
status: 'opened',
261+
awaitUser: true,
262+
sessionId: conversationId,
263+
},
284264
});
285265

286266
return;

‎src/utils/use-multi-file-auth-state-prisma.ts

+35-25
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from
55
import fs from 'fs/promises';
66
import path from 'path';
77

8-
// const fixFileName = (file: string): string | undefined => {
9-
// if (!file) {
10-
// return undefined;
11-
// }
12-
// const replacedSlash = file.replace(/\//g, '__');
13-
// const replacedColon = replacedSlash.replace(/:/g, '-');
14-
// return replacedColon;
15-
// };
8+
const fixFileName = (file: string): string | undefined => {
9+
if (!file) {
10+
return undefined;
11+
}
12+
const replacedSlash = file.replace(/\//g, '__');
13+
const replacedColon = replacedSlash.replace(/:/g, '-');
14+
return replacedColon;
15+
};
1616

1717
export async function keyExists(sessionId: string): Promise<any> {
1818
try {
@@ -63,14 +63,14 @@ async function deleteAuthKey(sessionId: string): Promise<any> {
6363
}
6464
}
6565

66-
// async function fileExists(file: string): Promise<any> {
67-
// try {
68-
// const stat = await fs.stat(file);
69-
// if (stat.isFile()) return true;
70-
// } catch (error) {
71-
// return;
72-
// }
73-
// }
66+
async function fileExists(file: string): Promise<any> {
67+
try {
68+
const stat = await fs.stat(file);
69+
if (stat.isFile()) return true;
70+
} catch (error) {
71+
return;
72+
}
73+
}
7474

7575
export default async function useMultiFileAuthStatePrisma(
7676
sessionId: string,
@@ -80,16 +80,19 @@ export default async function useMultiFileAuthStatePrisma(
8080
saveCreds: () => Promise<void>;
8181
}> {
8282
const localFolder = path.join(INSTANCE_DIR, sessionId);
83-
// const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
83+
const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
8484
await fs.mkdir(localFolder, { recursive: true });
8585

8686
async function writeData(data: any, key: string): Promise<any> {
8787
const dataString = JSON.stringify(data, BufferJSON.replacer);
8888

8989
if (key != 'creds') {
90-
return await cache.hSet(sessionId, key, data);
91-
// await fs.writeFile(localFile(key), dataString);
92-
// return;
90+
if (process.env.CACHE_REDIS_ENABLED === 'true') {
91+
return await cache.hSet(sessionId, key, data);
92+
} else {
93+
await fs.writeFile(localFile(key), dataString);
94+
return;
95+
}
9396
}
9497
await saveKey(sessionId, dataString);
9598
return;
@@ -100,9 +103,13 @@ export default async function useMultiFileAuthStatePrisma(
100103
let rawData;
101104

102105
if (key != 'creds') {
103-
return await cache.hGet(sessionId, key);
104-
// if (!(await fileExists(localFile(key)))) return null;
105-
// rawData = await fs.readFile(localFile(key), { encoding: 'utf-8' });
106+
if (process.env.CACHE_REDIS_ENABLED === 'true') {
107+
return await cache.hGet(sessionId, key);
108+
} else {
109+
if (!(await fileExists(localFile(key)))) return null;
110+
rawData = await fs.readFile(localFile(key), { encoding: 'utf-8' });
111+
return JSON.parse(rawData, BufferJSON.reviver);
112+
}
106113
} else {
107114
rawData = await getAuthKey(sessionId);
108115
}
@@ -117,8 +124,11 @@ export default async function useMultiFileAuthStatePrisma(
117124
async function removeData(key: string): Promise<any> {
118125
try {
119126
if (key != 'creds') {
120-
return await cache.hDelete(sessionId, key);
121-
// await fs.unlink(localFile(key));
127+
if (process.env.CACHE_REDIS_ENABLED === 'true') {
128+
return await cache.hDelete(sessionId, key);
129+
} else {
130+
await fs.unlink(localFile(key));
131+
}
122132
} else {
123133
await deleteAuthKey(sessionId);
124134
}

0 commit comments

Comments
 (0)
Please sign in to comment.