Improve chat update and fix unread messages

This commit is contained in:
Jesus 2024-12-28 07:47:56 +01:00
parent 72a33ae59f
commit a82669b6fa

View File

@ -126,7 +126,6 @@ import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
import { spawn } from 'child_process'; import { spawn } from 'child_process';
import { isArray, isBase64, isURL } from 'class-validator'; import { isArray, isBase64, isURL } from 'class-validator';
import { randomBytes } from 'crypto'; import { randomBytes } from 'crypto';
import cuid from 'cuid';
import EventEmitter2 from 'eventemitter2'; import EventEmitter2 from 'eventemitter2';
import ffmpeg from 'fluent-ffmpeg'; import ffmpeg from 'fluent-ffmpeg';
import FormData from 'form-data'; import FormData from 'form-data';
@ -1137,28 +1136,24 @@ export class BaileysStartupService extends ChannelStartupService {
} }
const existingChat = await this.prismaRepository.chat.findFirst({ const existingChat = await this.prismaRepository.chat.findFirst({
where: { instanceId: this.instanceId, remoteJid: received.key.remoteJid }, where: { instanceId: this.instanceId, remoteJid: received.key.remoteJid },
select: { id: true, name: true },
}); });
if (existingChat) { if (
const chatToInsert = { existingChat &&
remoteJid: received.key.remoteJid, received.pushName &&
instanceId: this.instanceId, existingChat.name !== received.pushName &&
name: received.pushName || '', received.pushName.trim().length > 0
unreadMessages: 0, ) {
}; this.sendDataWebhook(Events.CHATS_UPSERT, [{ ...existingChat, name: received.pushName }]);
this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]);
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) { if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
try { try {
await this.prismaRepository.chat.update({ await this.prismaRepository.chat.update({
where: { where: { id: existingChat.id },
id: existingChat.id, data: { name: received.pushName },
},
data: chatToInsert,
}); });
} } catch (error) {
catch(error){ console.log(`Chat insert record ignored: ${received.key.remoteJid} - ${this.instanceId}`);
console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`);
} }
} }
} }
@ -1500,8 +1495,7 @@ export class BaileysStartupService extends ChannelStartupService {
}, },
data: chatToInsert, data: chatToInsert,
}); });
} } catch (error) {
catch(error){
console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`); console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`);
} }
} }