Added unreadMessages to chats

This commit is contained in:
Jesus
2024-10-07 17:39:00 +02:00
parent 49edd174a9
commit edb3875b91
3 changed files with 39 additions and 3 deletions

View File

@@ -661,6 +661,7 @@ export class BaileysStartupService extends ChannelStartupService {
instanceId: this.instanceId,
remoteJid: chat.id,
name: chat.name,
unreadMessages: typeof chat.unreadCount === 'number' ? chat.unreadCount : 0,
},
data: { remoteJid: chat.id },
});
@@ -1231,6 +1232,8 @@ export class BaileysStartupService extends ChannelStartupService {
},
'messages.update': async (args: WAMessageUpdate[], settings: any) => {
const unreadChatToUpdate: Record<string, number> = {}; // {remoteJid: readedMessages}
for await (const { key, update } of args) {
if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) {
return;
@@ -1273,7 +1276,7 @@ export class BaileysStartupService extends ChannelStartupService {
return;
}
if (status[update.status] === 'READ' && !key.fromMe) return;
//if (status[update.status] === 'READ' && !key.fromMe) return;
if (update.message === null && update.status === undefined) {
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
@@ -1302,6 +1305,17 @@ export class BaileysStartupService extends ChannelStartupService {
}
return;
} else if (update.message !== undefined && update.message !== findMessage.status) {
if (unreadChatToUpdate[key.remoteJid!] === undefined) {
unreadChatToUpdate[key.remoteJid!] = 0;
}
unreadChatToUpdate[key.remoteJid!]++;
this.prismaRepository.message.update({
where: { id: findMessage.id },
data: { status: status[update.status] },
});
}
const message: any = {
@@ -1323,6 +1337,17 @@ export class BaileysStartupService extends ChannelStartupService {
});
}
}
for await (const [remoteJid, unreadMessages] of Object.entries(unreadChatToUpdate)) {
const chat = await this.prismaRepository.chat.findFirst({ where: { remoteJid } });
if (chat) {
this.prismaRepository.chat.update({
where: { id: chat.id },
data: { unreadMessages: Math.max(0, chat.unreadMessages - unreadMessages) },
});
}
}
},
};
@@ -2006,7 +2031,13 @@ export class BaileysStartupService extends ChannelStartupService {
const mimetype = mime.getType(fileName).toString();
const fullName = join(`${this.instance.id}`, messageRaw.key.remoteJid, `${messageRaw.key.id}`, mediaType, fileName);
const fullName = join(
`${this.instance.id}`,
messageRaw.key.remoteJid,
`${messageRaw.key.id}`,
mediaType,
fileName,
);
await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, {
'Content-Type': mimetype,

View File

@@ -626,6 +626,7 @@ export class ChannelStartupService {
"Chat"."updatedAt",
"Contact"."pushName",
"Contact"."profilePicUrl"
"Contact"."unreadMessages"
FROM "Chat"
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
@@ -639,7 +640,8 @@ export class ChannelStartupService {
"Chat"."createdAt",
"Chat"."updatedAt",
"Contact"."pushName",
"Contact"."profilePicUrl"
"Contact"."profilePicUrl",
"Contact"."unreadMessages"
ORDER BY "Chat"."updatedAt" DESC;
`;
} else {
@@ -653,6 +655,7 @@ export class ChannelStartupService {
"Chat"."updatedAt",
"Contact"."pushName",
"Contact"."profilePicUrl"
"Contact"."unreadMessages"
FROM "Chat"
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
@@ -666,6 +669,7 @@ export class ChannelStartupService {
"Chat"."updatedAt",
"Contact"."pushName",
"Contact"."profilePicUrl"
"Contact"."unreadMessages"
ORDER BY "Chat"."updatedAt" DESC;
`;
}