Skip to content

Commit a82669b

Browse files
committed
Improve chat update and fix unread messages
1 parent 72a33ae commit a82669b

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

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

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
126126
import { spawn } from 'child_process';
127127
import { isArray, isBase64, isURL } from 'class-validator';
128128
import { randomBytes } from 'crypto';
129-
import cuid from 'cuid';
130129
import EventEmitter2 from 'eventemitter2';
131130
import ffmpeg from 'fluent-ffmpeg';
132131
import FormData from 'form-data';
@@ -1137,29 +1136,25 @@ export class BaileysStartupService extends ChannelStartupService {
11371136
}
11381137
const existingChat = await this.prismaRepository.chat.findFirst({
11391138
where: { instanceId: this.instanceId, remoteJid: received.key.remoteJid },
1139+
select: { id: true, name: true },
11401140
});
11411141

1142-
if (existingChat) {
1143-
const chatToInsert = {
1144-
remoteJid: received.key.remoteJid,
1145-
instanceId: this.instanceId,
1146-
name: received.pushName || '',
1147-
unreadMessages: 0,
1148-
};
1149-
1150-
this.sendDataWebhook(Events.CHATS_UPSERT, [chatToInsert]);
1142+
if (
1143+
existingChat &&
1144+
received.pushName &&
1145+
existingChat.name !== received.pushName &&
1146+
received.pushName.trim().length > 0
1147+
) {
1148+
this.sendDataWebhook(Events.CHATS_UPSERT, [{ ...existingChat, name: received.pushName }]);
11511149
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
1152-
try {
1153-
await this.prismaRepository.chat.update({
1154-
where: {
1155-
id: existingChat.id,
1156-
},
1157-
data: chatToInsert,
1158-
});
1159-
}
1160-
catch(error){
1161-
console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`);
1162-
}
1150+
try {
1151+
await this.prismaRepository.chat.update({
1152+
where: { id: existingChat.id },
1153+
data: { name: received.pushName },
1154+
});
1155+
} catch (error) {
1156+
console.log(`Chat insert record ignored: ${received.key.remoteJid} - ${this.instanceId}`);
1157+
}
11631158
}
11641159
}
11651160

@@ -1495,13 +1490,12 @@ export class BaileysStartupService extends ChannelStartupService {
14951490
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CHATS) {
14961491
try {
14971492
await this.prismaRepository.chat.update({
1498-
where: {
1499-
id: existingChat.id,
1500-
},
1493+
where: {
1494+
id: existingChat.id,
1495+
},
15011496
data: chatToInsert,
15021497
});
1503-
}
1504-
catch(error){
1498+
} catch (error) {
15051499
console.log(`Chat insert record ignored: ${chatToInsert.remoteJid} - ${chatToInsert.instanceId}`);
15061500
}
15071501
}

0 commit comments

Comments
 (0)