Skip to content

Commit faba3ab

Browse files
committed
update
1 parent 28924f3 commit faba3ab

3 files changed

Lines changed: 42 additions & 10 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@adiwajshing/keyed-db": "^0.2.4",
4545
"@hapi/boom": "^10.0.1",
4646
"@prisma/client": "^5.19.1",
47-
"@whiskeysockets/baileys": "7.0.0-rc.8",
47+
"@whiskeysockets/baileys": "7.0.0-rc.9",
4848
"axios": "^1.7.7",
4949
"class-validator": "^0.14.0",
5050
"cross-env": "^7.0.3",

src/utils/to-base64.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { isBase64 } from 'class-validator';
2+
3+
type BufferLike = Buffer | { type: 'Buffer'; data: number[] } | undefined | null;
4+
5+
// eslint-disable-next-line prettier/prettier
6+
export const toBase64 = (value: { type: 'Buffer'; data: number[] } | BufferLike): any | null => {
7+
if (!value) return null;
8+
9+
if (isBase64(value)) {
10+
return value;
11+
}
12+
13+
if (isBase64(value?.['data'])) {
14+
return value['data'];
15+
}
16+
17+
if (Buffer.isBuffer(value)) {
18+
return value.toString('base64');
19+
}
20+
21+
if (typeof value === 'object' && value.type === 'Buffer' && Array.isArray(value.data)) {
22+
return Buffer.from(value.data).toString('base64');
23+
}
24+
25+
console.error(Error('Formato de buffer desconhecido'));
26+
return null;
27+
};

src/whatsapp/services/whatsapp.service.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import makeWASocket, {
4141
AnyMessageContent,
4242
BaileysEventMap,
4343
BufferedEventData,
44+
BufferJSON,
4445
Chat,
4546
ConnectionState,
4647
Contact,
@@ -123,7 +124,6 @@ import {
123124
GroupUpdateParticipantDto,
124125
} from '../dto/group.dto';
125126
import Long from 'long';
126-
import NodeCache from 'node-cache';
127127
import {
128128
AuthState,
129129
AuthStateProvider,
@@ -150,6 +150,7 @@ import {
150150
writeFileSync,
151151
} from 'fs';
152152
import { fetchLatestBaileysVersionV2 } from '../../utils/wa-version';
153+
import { toBase64 } from '../../utils/to-base64';
153154

154155
type InstanceQrCode = {
155156
count: number;
@@ -475,7 +476,7 @@ export class WAStartupService {
475476

476477
private async getMessage(key: PrismType.Message, full = false) {
477478
try {
478-
key.instanceId = this.instance.id;
479+
// key.instanceId = this.instance.id;
479480
const message = await this.repository.message.findFirst({
480481
where: key as any,
481482
});
@@ -489,6 +490,13 @@ export class WAStartupService {
489490
[message.messageType]: message.content,
490491
},
491492
};
493+
494+
if (webMessageInfo.message[message.messageType]?.mediaKey) {
495+
webMessageInfo.message[message.messageType].mediaKey = toBase64(
496+
webMessageInfo.message?.[message.messageType]?.mediaKey as any,
497+
);
498+
}
499+
492500
if (full) {
493501
return webMessageInfo;
494502
}
@@ -888,7 +896,7 @@ export class WAStartupService {
888896

889897
if (this.databaseOptions.DB_OPTIONS.NEW_MESSAGE) {
890898
const { id } = await this.repository.message.create({
891-
data: JSON.parse(JSON.stringify(messageRaw) as any),
899+
data: JSON.parse(JSON.stringify(messageRaw, BufferJSON.replacer) as any),
892900
});
893901
messageRaw.id = id;
894902
}
@@ -1355,7 +1363,7 @@ export class WAStartupService {
13551363
})();
13561364
if (this.databaseOptions.DB_OPTIONS.NEW_MESSAGE) {
13571365
const { id } = await this.repository.message.create({
1358-
data: JSON.parse(JSON.stringify(messageSent)) as any,
1366+
data: JSON.parse(JSON.stringify(messageSent, BufferJSON.replacer)) as any,
13591367
});
13601368
messageSent.id = id;
13611369
}
@@ -2195,10 +2203,6 @@ export class WAStartupService {
21952203
throw 'The message is not of the media type';
21962204
}
21972205

2198-
if (typeof mediaMessage['mediaKey'] === 'object') {
2199-
msg.message = JSON.parse(JSON.stringify(msg.message));
2200-
}
2201-
22022206
const stream = await downloadMediaMessage(
22032207
{ key: msg?.key, message: msg?.message },
22042208
'stream',
@@ -2244,7 +2248,8 @@ export class WAStartupService {
22442248
if (inner) {
22452249
return;
22462250
}
2247-
throw new BadRequestException(error.toString());
2251+
const boom = error as Boom;
2252+
throw new BadRequestException(error.toString(), boom?.output);
22482253
}
22492254
}
22502255

0 commit comments

Comments
 (0)