mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-22 20:12:02 -06:00
feat: Added Get Last Message and Archive for Chat
Added Get Last Message and Archive for Chat
This commit is contained in:
commit
0186fff67d
@ -508,6 +508,7 @@ export const archiveChatSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
chat: { type: 'string' },
|
||||
lastMessage: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
@ -528,7 +529,7 @@ export const archiveChatSchema: JSONSchema7 = {
|
||||
},
|
||||
archive: { type: 'boolean', enum: [true, false] },
|
||||
},
|
||||
required: ['lastMessage', 'archive'],
|
||||
required: ['archive'],
|
||||
};
|
||||
|
||||
export const deleteMessageSchema: JSONSchema7 = {
|
||||
|
@ -53,13 +53,14 @@ export class ReadMessageDto {
|
||||
read_messages: Key[];
|
||||
}
|
||||
|
||||
class LastMessage {
|
||||
export class LastMessage {
|
||||
key: Key;
|
||||
messageTimestamp?: number;
|
||||
}
|
||||
|
||||
export class ArchiveChatDto {
|
||||
lastMessage: LastMessage;
|
||||
lastMessage?: LastMessage;
|
||||
chat?: string;
|
||||
archive: boolean;
|
||||
}
|
||||
|
||||
|
@ -72,6 +72,7 @@ import {
|
||||
ArchiveChatDto,
|
||||
DeleteMessage,
|
||||
getBase64FromMediaMessageDto,
|
||||
LastMessage,
|
||||
NumberBusiness,
|
||||
OnWhatsAppDto,
|
||||
PrivacySettingDto,
|
||||
@ -2330,20 +2331,56 @@ export class WAStartupService {
|
||||
}
|
||||
}
|
||||
|
||||
public async getLastMessage(number: string) {
|
||||
const messages = await this.fetchMessages({
|
||||
where: {
|
||||
key: {
|
||||
remoteJid: number,
|
||||
},
|
||||
owner: this.instance.name,
|
||||
},
|
||||
});
|
||||
|
||||
let lastMessage = messages.pop();
|
||||
|
||||
for (const message of messages) {
|
||||
if (message.messageTimestamp >= lastMessage.messageTimestamp) {
|
||||
lastMessage = message;
|
||||
}
|
||||
}
|
||||
|
||||
return lastMessage as unknown as LastMessage;
|
||||
}
|
||||
|
||||
public async archiveChat(data: ArchiveChatDto) {
|
||||
this.logger.verbose('Archiving chat');
|
||||
try {
|
||||
data.lastMessage.messageTimestamp = data.lastMessage?.messageTimestamp ?? Date.now();
|
||||
let last_message = data.lastMessage;
|
||||
let number = data.chat;
|
||||
|
||||
if (!last_message && number) {
|
||||
last_message = await this.getLastMessage(number);
|
||||
} else {
|
||||
last_message = data.lastMessage;
|
||||
last_message.messageTimestamp = last_message?.messageTimestamp ?? Date.now();
|
||||
number = last_message?.key?.remoteJid;
|
||||
}
|
||||
|
||||
if (!last_message || Object.keys(last_message).length === 0) {
|
||||
throw new NotFoundException('Last message not found');
|
||||
}
|
||||
console.log(last_message);
|
||||
|
||||
await this.client.chatModify(
|
||||
{
|
||||
archive: data.archive,
|
||||
lastMessages: [data.lastMessage],
|
||||
lastMessages: [last_message],
|
||||
},
|
||||
data.lastMessage.key.remoteJid,
|
||||
this.createJid(number),
|
||||
);
|
||||
|
||||
return {
|
||||
chatId: data.lastMessage.key.remoteJid,
|
||||
chatId: number,
|
||||
archived: true,
|
||||
};
|
||||
} catch (error) {
|
||||
|
Loading…
Reference in New Issue
Block a user