diff --git a/src/api/controllers/chat.controller.ts b/src/api/controllers/chat.controller.ts index 9d22a8f0..24c048e2 100644 --- a/src/api/controllers/chat.controller.ts +++ b/src/api/controllers/chat.controller.ts @@ -86,6 +86,11 @@ export class ChatController { return await this.waMonitor.waInstances[instanceName].fetchChats(); } + public async fetchContactsWithLastMessage({ instanceName }: InstanceDto) { + logger.verbose('requested fetchContactsWithLastMessage from ' + instanceName + ' instance'); + return await this.waMonitor.waInstances[instanceName].fetchContactsWithLastMessage(); + } + public async sendPresence({ instanceName }: InstanceDto, data: SendPresenceDto) { logger.verbose('requested sendPresence from ' + instanceName + ' instance'); return await this.waMonitor.waInstances[instanceName].sendPresence(data); diff --git a/src/api/integrations/websocket/libs/socket.server.ts b/src/api/integrations/websocket/libs/socket.server.ts index 81f97847..d03c6283 100644 --- a/src/api/integrations/websocket/libs/socket.server.ts +++ b/src/api/integrations/websocket/libs/socket.server.ts @@ -8,13 +8,17 @@ const logger = new Logger('Socket'); let io: SocketIO; -const cors = configService.get('CORS').ORIGIN; +const origin = configService.get('CORS').ORIGIN; +const methods = configService.get('CORS').METHODS; +const credentials = configService.get('CORS').CREDENTIALS; export const initIO = (httpServer: Server) => { if (configService.get('WEBSOCKET')?.ENABLED) { io = new SocketIO(httpServer, { cors: { - origin: cors, + origin, + methods, + credentials, }, }); diff --git a/src/api/routes/chat.router.ts b/src/api/routes/chat.router.ts index 4debf3d1..f4b6426f 100644 --- a/src/api/routes/chat.router.ts +++ b/src/api/routes/chat.router.ts @@ -253,6 +253,23 @@ export class ChatRouter extends RouterBroker { return res.status(HttpStatus.OK).json(response); }) + .get(this.routerPath('fetchContactsWithLastMessage'), ...guards, async (req, res) => { + logger.verbose('request received in fetchContactsWithLastMessage'); + logger.verbose('request body: '); + logger.verbose(req.body); + + logger.verbose('request query: '); + logger.verbose(req.query); + + const response = await this.dataValidate({ + request: req, + schema: null, + ClassRef: InstanceDto, + execute: (instance) => chatController.fetchContactsWithLastMessage(instance), + }); + + return res.status(HttpStatus.OK).json(response); + }) .post(this.routerPath('sendPresence'), ...guards, async (req, res) => { logger.verbose('request received in sendPresence'); logger.verbose('request body: '); diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index 702b8b81..8462d0d2 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -1328,4 +1328,31 @@ export class ChannelStartupService { this.logger.verbose('Fetching chats'); return await this.repository.chat.find({ where: { owner: this.instance.name } }); } + + public async fetchContactsWithLastMessage() { + this.logger.verbose('Searching for contacts with last message'); + const contacts = await this.repository.contact.find({ where: { owner: this.instance.name } }); + const result = []; + + for (const contact of contacts) { + // Buscar a Ășltima mensagem desse contato + const messages = await this.repository.message.find({ + where: { + owner: this.instance.name, + key: { remoteJid: contact.id }, + }, + limit: 1, + }); + if (messages && messages.length > 0) { + result.push({ + id: contact.id, + pushName: contact?.pushName ?? null, + profilePictureUrl: contact?.profilePictureUrl ?? null, + owner: contact.owner, + lastMessage: messages[0], + }); + } + } + return result; + } }