fix(baileys): salvar corretamente buffer no db

This commit is contained in:
Willian Coqueiro 2025-10-19 18:53:16 +00:00
parent d58d0b8bff
commit dd21a29ea6

View File

@ -4444,24 +4444,37 @@ export class BaileysStartupService extends ChannelStartupService {
throw new Error('Method not available in the Baileys service'); throw new Error('Method not available in the Baileys service');
} }
private convertLongToNumber(obj: any): any { private deserializeMessageBuffers(obj: any): any {
if (obj === null || obj === undefined) { if (obj === null || obj === undefined) {
return obj; return obj;
} }
if (Long.isLong(obj)) { if (typeof obj === 'object' && !Array.isArray(obj) && !Buffer.isBuffer(obj)) {
return obj.toNumber(); const keys = Object.keys(obj);
const isIndexedObject = keys.every((key) => !isNaN(Number(key)));
if (isIndexedObject && keys.length > 0) {
const values = keys.sort((a, b) => Number(a) - Number(b)).map((key) => obj[key]);
return new Uint8Array(values);
}
} }
// Is Buffer?, converter to Uint8Array
if (Buffer.isBuffer(obj)) {
return new Uint8Array(obj);
}
// Process arrays recursively
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
return obj.map((item) => this.convertLongToNumber(item)); return obj.map((item) => this.deserializeMessageBuffers(item));
} }
// Process objects recursively
if (typeof obj === 'object') { if (typeof obj === 'object') {
const converted: any = {}; const converted: any = {};
for (const key in obj) { for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) { if (Object.prototype.hasOwnProperty.call(obj, key)) {
converted[key] = this.convertLongToNumber(obj[key]); converted[key] = this.deserializeMessageBuffers(obj[key]);
} }
} }
return converted; return converted;
@ -4482,8 +4495,8 @@ export class BaileysStartupService extends ChannelStartupService {
? 'Você' ? 'Você'
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)), : message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)),
status: status[message.status], status: status[message.status],
message: this.convertLongToNumber({ ...message.message }), message: this.deserializeMessageBuffers({ ...message.message }),
contextInfo: this.convertLongToNumber(contentMsg?.contextInfo), contextInfo: this.deserializeMessageBuffers(contentMsg?.contextInfo),
messageType: contentType || 'unknown', messageType: contentType || 'unknown',
messageTimestamp: Long.isLong(message.messageTimestamp) messageTimestamp: Long.isLong(message.messageTimestamp)
? message.messageTimestamp.toNumber() ? message.messageTimestamp.toNumber()