Adição de Get Last Message e Archive por Chat

This commit is contained in:
Alan Mosko 2023-07-27 09:23:44 -03:00
parent d344e513c4
commit 52533d4b38
3 changed files with 56 additions and 14 deletions

View File

@ -508,6 +508,7 @@ export const archiveChatSchema: JSONSchema7 = {
$id: v4(), $id: v4(),
type: 'object', type: 'object',
properties: { properties: {
chat: { type: 'string' },
lastMessage: { lastMessage: {
type: 'object', type: 'object',
properties: { properties: {
@ -528,7 +529,7 @@ export const archiveChatSchema: JSONSchema7 = {
}, },
archive: { type: 'boolean', enum: [true, false] }, archive: { type: 'boolean', enum: [true, false] },
}, },
required: ['lastMessage', 'archive'], required: ['archive'],
}; };
export const deleteMessageSchema: JSONSchema7 = { export const deleteMessageSchema: JSONSchema7 = {

View File

@ -53,13 +53,14 @@ export class ReadMessageDto {
read_messages: Key[]; read_messages: Key[];
} }
class LastMessage { export class LastMessage {
key: Key; key: Key;
messageTimestamp?: number; messageTimestamp?: number;
} }
export class ArchiveChatDto { export class ArchiveChatDto {
lastMessage: LastMessage; lastMessage?: LastMessage;
chat?: string;
archive: boolean; archive: boolean;
} }

View File

@ -74,6 +74,7 @@ import {
getBase64FromMediaMessageDto, getBase64FromMediaMessageDto,
NumberBusiness, NumberBusiness,
OnWhatsAppDto, OnWhatsAppDto,
LastMessage,
PrivacySettingDto, PrivacySettingDto,
ReadMessageDto, ReadMessageDto,
WhatsAppNumberDto, WhatsAppNumberDto,
@ -2345,23 +2346,62 @@ export class WAStartupService {
throw new InternalServerErrorException('Read messages fail', error.toString()); throw new InternalServerErrorException('Read messages fail', error.toString());
} }
} }
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?.low >= lastMessage.messageTimestamp?.low
|| message.messageTimestamp >= lastMessage.messageTimestamp
) {
lastMessage = message;
}
}
return lastMessage as unknown as LastMessage;
}
public async archiveChat(data: ArchiveChatDto) { public async archiveChat(data: ArchiveChatDto) {
this.logger.verbose('Archiving chat'); this.logger.verbose('Archiving chat');
try { 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( await this.client.chatModify(
{ {
archive: data.archive, archive: data.archive,
lastMessages: [data.lastMessage], lastMessages: [last_message]
}, },
data.lastMessage.key.remoteJid, this.createJid(number)
); );
return { return {
chatId: data.lastMessage.key.remoteJid, chatId: number,
archived: true, archived: true,
}; };
} catch (error) { } catch (error) {
throw new InternalServerErrorException({ throw new InternalServerErrorException({
archived: false, archived: false,