Skip to content

Commit 394c091

Browse files
author
Jesus
committed
Fix parse multiples messages updates and creates
1 parent 73e732b commit 394c091

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

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

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,10 @@ export class BaileysStartupService extends ChannelStartupService {
983983
for (const received of messages) {
984984
if (received.message?.conversation || received.message?.extendedTextMessage?.text) {
985985
const text = received.message?.conversation || received.message?.extendedTextMessage?.text;
986+
986987
if (text == 'requestPlaceholder' && !requestId) {
987988
const messageId = await this.client.requestPlaceholderResend(received.key);
989+
988990
console.log('requested placeholder resync, id=', messageId);
989991
} else if (requestId) {
990992
console.log('Message received from phone, id=', requestId, received);
@@ -1018,6 +1020,7 @@ export class BaileysStartupService extends ChannelStartupService {
10181020
message: received,
10191021
retry: 0,
10201022
});
1023+
10211024
continue;
10221025
}
10231026

@@ -1034,15 +1037,15 @@ export class BaileysStartupService extends ChannelStartupService {
10341037
received.message?.pollUpdateMessage ||
10351038
!received?.message
10361039
) {
1037-
return;
1040+
continue;
10381041
}
10391042

10401043
if (Long.isLong(received.messageTimestamp)) {
10411044
received.messageTimestamp = received.messageTimestamp?.toNumber();
10421045
}
10431046

10441047
if (settings?.groupsIgnore && received.key.remoteJid.includes('@g.us')) {
1045-
return;
1048+
continue;
10461049
}
10471050

10481051
const messageRaw = this.prepareMessage(received);
@@ -1105,9 +1108,15 @@ export class BaileysStartupService extends ChannelStartupService {
11051108
data: messageRaw,
11061109
});
11071110

1108-
if (received.key.fromMe === false && msg.status === status[3]) {
1109-
// is received not read message
1110-
await this.updateChatUnreadMessages(received.key.remoteJid);
1111+
if (received.key.fromMe === false) {
1112+
if (msg.status === status[3]) {
1113+
this.logger.log(`Update not read messages ${received.key.remoteJid}`);
1114+
// is received not read message
1115+
await this.updateChatUnreadMessages(received.key.remoteJid);
1116+
} else if (msg.status === status[4]) {
1117+
this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`);
1118+
this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp);
1119+
}
11111120
}
11121121

11131122
if (isMedia) {
@@ -1122,11 +1131,8 @@ export class BaileysStartupService extends ChannelStartupService {
11221131
);
11231132

11241133
const { buffer, mediaType, fileName, size } = media;
1125-
11261134
const mimetype = mime.getType(fileName).toString();
1127-
11281135
const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName);
1129-
11301136
await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, {
11311137
'Content-Type': mimetype,
11321138
});
@@ -1193,7 +1199,7 @@ export class BaileysStartupService extends ChannelStartupService {
11931199
};
11941200

11951201
if (contactRaw.remoteJid === 'status@broadcast') {
1196-
return;
1202+
continue;
11971203
}
11981204

11991205
if (contact) {
@@ -1214,7 +1220,7 @@ export class BaileysStartupService extends ChannelStartupService {
12141220
update: contactRaw,
12151221
});
12161222

1217-
return;
1223+
continue;
12181224
}
12191225

12201226
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
@@ -1241,11 +1247,11 @@ export class BaileysStartupService extends ChannelStartupService {
12411247
},
12421248

12431249
'messages.update': async (args: WAMessageUpdate[], settings: any) => {
1244-
const readChatToUpdate: Record<string, true> = {}; // remoteJid[]
1250+
const readChatToUpdate: Record<string, true> = {}; // {remoteJid: true}
12451251

12461252
for await (const { key, update } of args) {
12471253
if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) {
1248-
return;
1254+
continue;
12491255
}
12501256

12511257
if (status[update.status] === 'READ' && key.fromMe) {
@@ -1260,6 +1266,7 @@ export class BaileysStartupService extends ChannelStartupService {
12601266

12611267
if (key.remoteJid !== 'status@broadcast') {
12621268
let pollUpdates: any;
1269+
12631270
if (update.pollUpdates) {
12641271
const pollCreation = await this.getMessage(key);
12651272

@@ -1282,7 +1289,7 @@ export class BaileysStartupService extends ChannelStartupService {
12821289
});
12831290

12841291
if (!findMessage) {
1285-
return;
1292+
continue;
12861293
}
12871294

12881295
if (update.message === null && update.status === undefined) {
@@ -1311,26 +1318,14 @@ export class BaileysStartupService extends ChannelStartupService {
13111318
);
13121319
}
13131320

1314-
return;
1321+
continue;
13151322
} else if (update.status !== undefined && status[update.status] !== findMessage.status) {
13161323
if (!key.fromMe && key.remoteJid) {
13171324
readChatToUpdate[key.remoteJid] = true;
13181325

13191326
if (status[update.status] === status[4]) {
1320-
// Mark to read old messages
1321-
await this.prismaRepository.message.updateMany({
1322-
where: {
1323-
AND: [
1324-
{ key: { path: ['remoteJid'], equals: key.remoteJid } },
1325-
{ key: { path: ['fromMe'], equals: false } },
1326-
{ messageTimestamp: { lt: findMessage.messageTimestamp } }, // Delivered messages
1327-
{
1328-
OR: [{ status: null }, { status: status[3] }],
1329-
},
1330-
],
1331-
},
1332-
data: { status: status[4] },
1333-
});
1327+
this.logger.log(`Update as read ${key.remoteJid} - ${findMessage.messageTimestamp}`);
1328+
this.updateMessagesReadedByTimestamp(key.remoteJid, findMessage.messageTimestamp);
13341329
}
13351330
}
13361331

@@ -3750,6 +3745,30 @@ export class BaileysStartupService extends ChannelStartupService {
37503745
}
37513746
}
37523747

3748+
private async updateMessagesReadedByTimestamp(remoteJid: string, timestamp?: number): Promise<number> {
3749+
if (timestamp === undefined || timestamp === null) return 0;
3750+
3751+
const result = await this.prismaRepository.message.updateMany({
3752+
where: {
3753+
AND: [
3754+
{ key: { path: ['remoteJid'], equals: remoteJid } },
3755+
{ key: { path: ['fromMe'], equals: false } },
3756+
{ messageTimestamp: { lte: timestamp } },
3757+
{
3758+
OR: [{ status: null }, { status: status[3] }],
3759+
},
3760+
],
3761+
},
3762+
data: { status: status[4] },
3763+
});
3764+
3765+
if (result) {
3766+
return result.count;
3767+
}
3768+
3769+
return 0;
3770+
}
3771+
37533772
private async updateChatUnreadMessages(remoteJid: string): Promise<number> {
37543773
const [chat, unreadMessages] = await Promise.all([
37553774
this.prismaRepository.chat.findFirst({ where: { remoteJid } }),

0 commit comments

Comments
 (0)