Merge branch 'v2.0.0' of github.com:EvolutionAPI/evolution-api into v2.0.0

This commit is contained in:
Davidson Gomes 2024-10-07 13:37:56 -03:00
commit f2bb01a640
3 changed files with 45 additions and 37 deletions

View File

@ -123,6 +123,7 @@ model Chat {
updatedAt DateTime? @updatedAt @db.Timestamp updatedAt DateTime? @updatedAt @db.Timestamp
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
instanceId String instanceId String
unreadMessages Int @default(0)
} }
model Contact { model Contact {

View File

@ -627,7 +627,12 @@ export class BaileysStartupService extends ChannelStartupService {
const chatsToInsert = chats const chatsToInsert = chats
.filter((chat) => !existingChatIdSet?.has(chat.id)) .filter((chat) => !existingChatIdSet?.has(chat.id))
.map((chat) => ({ remoteJid: chat.id, instanceId: this.instanceId, name: chat.name })); .map((chat) => ({
remoteJid: chat.id,
instanceId: this.instanceId,
name: chat.name,
unreadMessages: chat.unreadCount !== undefined ? chat.unreadCount : 0,
}));
this.sendDataWebhook(Events.CHATS_UPSERT, chatsToInsert); this.sendDataWebhook(Events.CHATS_UPSERT, chatsToInsert);
@ -661,6 +666,7 @@ export class BaileysStartupService extends ChannelStartupService {
instanceId: this.instanceId, instanceId: this.instanceId,
remoteJid: chat.id, remoteJid: chat.id,
name: chat.name, name: chat.name,
unreadMessages: typeof chat.unreadCount === 'number' ? chat.unreadCount : 0,
}, },
data: { remoteJid: chat.id }, data: { remoteJid: chat.id },
}); });
@ -1231,6 +1237,8 @@ export class BaileysStartupService extends ChannelStartupService {
}, },
'messages.update': async (args: WAMessageUpdate[], settings: any) => { 'messages.update': async (args: WAMessageUpdate[], settings: any) => {
const unreadChatToUpdate: Record<string, number> = {}; // {remoteJid: readedMessages}
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; return;
@ -1273,8 +1281,6 @@ export class BaileysStartupService extends ChannelStartupService {
return; return;
} }
if (status[update.status] === 'READ' && !key.fromMe) return;
if (update.message === null && update.status === undefined) { if (update.message === null && update.status === undefined) {
this.sendDataWebhook(Events.MESSAGES_DELETE, key); this.sendDataWebhook(Events.MESSAGES_DELETE, key);
@ -1302,6 +1308,17 @@ export class BaileysStartupService extends ChannelStartupService {
} }
return; return;
} else if (update.status !== undefined && status[update.status] !== findMessage.status) {
if (!unreadChatToUpdate[key.remoteJid!]) {
unreadChatToUpdate[key.remoteJid!] = 0;
}
unreadChatToUpdate[key.remoteJid!]++;
this.prismaRepository.message.update({
where: { id: findMessage.id },
data: { status: status[update.status] },
});
} }
const message: any = { const message: any = {
@ -1323,6 +1340,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 +2034,13 @@ export class BaileysStartupService extends ChannelStartupService {
const mimetype = mime.getType(fileName).toString(); 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, { await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, {
'Content-Type': mimetype, 'Content-Type': mimetype,

View File

@ -614,9 +614,7 @@ export class ChannelStartupService {
: this.createJid(query.where?.remoteJid) : this.createJid(query.where?.remoteJid)
: null; : null;
let result; const result = await this.prismaRepository.$queryRaw`
if (remoteJid) {
result = await this.prismaRepository.$queryRaw`
SELECT SELECT
"Chat"."id", "Chat"."id",
"Chat"."remoteJid", "Chat"."remoteJid",
@ -625,12 +623,13 @@ export class ChannelStartupService {
"Chat"."createdAt", "Chat"."createdAt",
"Chat"."updatedAt", "Chat"."updatedAt",
"Contact"."pushName", "Contact"."pushName",
"Contact"."profilePicUrl" "Contact"."profilePicUrl",
"Contact"."unreadMessages"
FROM "Chat" FROM "Chat"
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid' INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid" LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
WHERE "Chat"."instanceId" = ${this.instanceId} WHERE "Chat"."instanceId" = ${this.instanceId}
AND "Chat"."remoteJid" = ${remoteJid} ${remoteJid ? 'AND "Chat"."remoteJid" = ${remoteJid}' : ''}
GROUP BY GROUP BY
"Chat"."id", "Chat"."id",
"Chat"."remoteJid", "Chat"."remoteJid",
@ -639,36 +638,10 @@ export class ChannelStartupService {
"Chat"."createdAt", "Chat"."createdAt",
"Chat"."updatedAt", "Chat"."updatedAt",
"Contact"."pushName", "Contact"."pushName",
"Contact"."profilePicUrl" "Contact"."profilePicUrl",
"Contact"."unreadMessages"
ORDER BY "Chat"."updatedAt" DESC; ORDER BY "Chat"."updatedAt" DESC;
`; `;
} else {
result = await this.prismaRepository.$queryRaw`
SELECT
"Chat"."id",
"Chat"."remoteJid",
"Chat"."name",
"Chat"."labels",
"Chat"."createdAt",
"Chat"."updatedAt",
"Contact"."pushName",
"Contact"."profilePicUrl"
FROM "Chat"
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
WHERE "Chat"."instanceId" = ${this.instanceId}
GROUP BY
"Chat"."id",
"Chat"."remoteJid",
"Chat"."name",
"Chat"."labels",
"Chat"."createdAt",
"Chat"."updatedAt",
"Contact"."pushName",
"Contact"."profilePicUrl"
ORDER BY "Chat"."updatedAt" DESC;
`;
}
return result; return result;
} }