Fix parse multiples messages updates and creates

This commit is contained in:
Jesus 2024-10-08 17:15:53 +02:00
parent 73e732bf24
commit 394c091810

View File

@ -983,8 +983,10 @@ export class BaileysStartupService extends ChannelStartupService {
for (const received of messages) { for (const received of messages) {
if (received.message?.conversation || received.message?.extendedTextMessage?.text) { if (received.message?.conversation || received.message?.extendedTextMessage?.text) {
const text = received.message?.conversation || received.message?.extendedTextMessage?.text; const text = received.message?.conversation || received.message?.extendedTextMessage?.text;
if (text == 'requestPlaceholder' && !requestId) { if (text == 'requestPlaceholder' && !requestId) {
const messageId = await this.client.requestPlaceholderResend(received.key); const messageId = await this.client.requestPlaceholderResend(received.key);
console.log('requested placeholder resync, id=', messageId); console.log('requested placeholder resync, id=', messageId);
} else if (requestId) { } else if (requestId) {
console.log('Message received from phone, id=', requestId, received); console.log('Message received from phone, id=', requestId, received);
@ -1018,6 +1020,7 @@ export class BaileysStartupService extends ChannelStartupService {
message: received, message: received,
retry: 0, retry: 0,
}); });
continue; continue;
} }
@ -1034,7 +1037,7 @@ export class BaileysStartupService extends ChannelStartupService {
received.message?.pollUpdateMessage || received.message?.pollUpdateMessage ||
!received?.message !received?.message
) { ) {
return; continue;
} }
if (Long.isLong(received.messageTimestamp)) { if (Long.isLong(received.messageTimestamp)) {
@ -1042,7 +1045,7 @@ export class BaileysStartupService extends ChannelStartupService {
} }
if (settings?.groupsIgnore && received.key.remoteJid.includes('@g.us')) { if (settings?.groupsIgnore && received.key.remoteJid.includes('@g.us')) {
return; continue;
} }
const messageRaw = this.prepareMessage(received); const messageRaw = this.prepareMessage(received);
@ -1105,9 +1108,15 @@ export class BaileysStartupService extends ChannelStartupService {
data: messageRaw, data: messageRaw,
}); });
if (received.key.fromMe === false && msg.status === status[3]) { if (received.key.fromMe === false) {
if (msg.status === status[3]) {
this.logger.log(`Update not read messages ${received.key.remoteJid}`);
// is received not read message // is received not read message
await this.updateChatUnreadMessages(received.key.remoteJid); await this.updateChatUnreadMessages(received.key.remoteJid);
} else if (msg.status === status[4]) {
this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`);
this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp);
}
} }
if (isMedia) { if (isMedia) {
@ -1122,11 +1131,8 @@ export class BaileysStartupService extends ChannelStartupService {
); );
const { buffer, mediaType, fileName, size } = media; const { buffer, mediaType, fileName, size } = media;
const mimetype = mime.getType(fileName).toString(); const mimetype = mime.getType(fileName).toString();
const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName); const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName);
await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, {
'Content-Type': mimetype, 'Content-Type': mimetype,
}); });
@ -1193,7 +1199,7 @@ export class BaileysStartupService extends ChannelStartupService {
}; };
if (contactRaw.remoteJid === 'status@broadcast') { if (contactRaw.remoteJid === 'status@broadcast') {
return; continue;
} }
if (contact) { if (contact) {
@ -1214,7 +1220,7 @@ export class BaileysStartupService extends ChannelStartupService {
update: contactRaw, update: contactRaw,
}); });
return; continue;
} }
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw); this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
@ -1241,11 +1247,11 @@ export class BaileysStartupService extends ChannelStartupService {
}, },
'messages.update': async (args: WAMessageUpdate[], settings: any) => { 'messages.update': async (args: WAMessageUpdate[], settings: any) => {
const readChatToUpdate: Record<string, true> = {}; // remoteJid[] const readChatToUpdate: Record<string, true> = {}; // {remoteJid: true}
for await (const { key, update } of args) { for await (const { key, update } of args) {
if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) { if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) {
return; continue;
} }
if (status[update.status] === 'READ' && key.fromMe) { if (status[update.status] === 'READ' && key.fromMe) {
@ -1260,6 +1266,7 @@ export class BaileysStartupService extends ChannelStartupService {
if (key.remoteJid !== 'status@broadcast') { if (key.remoteJid !== 'status@broadcast') {
let pollUpdates: any; let pollUpdates: any;
if (update.pollUpdates) { if (update.pollUpdates) {
const pollCreation = await this.getMessage(key); const pollCreation = await this.getMessage(key);
@ -1282,7 +1289,7 @@ export class BaileysStartupService extends ChannelStartupService {
}); });
if (!findMessage) { if (!findMessage) {
return; continue;
} }
if (update.message === null && update.status === undefined) { if (update.message === null && update.status === undefined) {
@ -1311,26 +1318,14 @@ export class BaileysStartupService extends ChannelStartupService {
); );
} }
return; continue;
} else if (update.status !== undefined && status[update.status] !== findMessage.status) { } else if (update.status !== undefined && status[update.status] !== findMessage.status) {
if (!key.fromMe && key.remoteJid) { if (!key.fromMe && key.remoteJid) {
readChatToUpdate[key.remoteJid] = true; readChatToUpdate[key.remoteJid] = true;
if (status[update.status] === status[4]) { if (status[update.status] === status[4]) {
// Mark to read old messages this.logger.log(`Update as read ${key.remoteJid} - ${findMessage.messageTimestamp}`);
await this.prismaRepository.message.updateMany({ this.updateMessagesReadedByTimestamp(key.remoteJid, findMessage.messageTimestamp);
where: {
AND: [
{ key: { path: ['remoteJid'], equals: key.remoteJid } },
{ key: { path: ['fromMe'], equals: false } },
{ messageTimestamp: { lt: findMessage.messageTimestamp } }, // Delivered messages
{
OR: [{ status: null }, { status: status[3] }],
},
],
},
data: { status: status[4] },
});
} }
} }
@ -3750,6 +3745,30 @@ export class BaileysStartupService extends ChannelStartupService {
} }
} }
private async updateMessagesReadedByTimestamp(remoteJid: string, timestamp?: number): Promise<number> {
if (timestamp === undefined || timestamp === null) return 0;
const result = await this.prismaRepository.message.updateMany({
where: {
AND: [
{ key: { path: ['remoteJid'], equals: remoteJid } },
{ key: { path: ['fromMe'], equals: false } },
{ messageTimestamp: { lte: timestamp } },
{
OR: [{ status: null }, { status: status[3] }],
},
],
},
data: { status: status[4] },
});
if (result) {
return result.count;
}
return 0;
}
private async updateChatUnreadMessages(remoteJid: string): Promise<number> { private async updateChatUnreadMessages(remoteJid: string): Promise<number> {
const [chat, unreadMessages] = await Promise.all([ const [chat, unreadMessages] = await Promise.all([
this.prismaRepository.chat.findFirst({ where: { remoteJid } }), this.prismaRepository.chat.findFirst({ where: { remoteJid } }),