mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-19 05:21:56 -06:00
feat: Adds method to fetch contacts with last message
- Implements the `fetchContactsWithLastMessage` method in `ChatController` to retrieve contacts and their last messages. - Updates `ChatRouter` to include the new route that calls the above method. - Adds logic in `ChannelStartupService` to fetch contacts and their last messages, improving contact management functionality.
This commit is contained in:
parent
86c603b3a1
commit
cee6498ea0
@ -86,6 +86,11 @@ export class ChatController {
|
|||||||
return await this.waMonitor.waInstances[instanceName].fetchChats();
|
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) {
|
public async sendPresence({ instanceName }: InstanceDto, data: SendPresenceDto) {
|
||||||
logger.verbose('requested sendPresence from ' + instanceName + ' instance');
|
logger.verbose('requested sendPresence from ' + instanceName + ' instance');
|
||||||
return await this.waMonitor.waInstances[instanceName].sendPresence(data);
|
return await this.waMonitor.waInstances[instanceName].sendPresence(data);
|
||||||
|
@ -8,13 +8,17 @@ const logger = new Logger('Socket');
|
|||||||
|
|
||||||
let io: SocketIO;
|
let io: SocketIO;
|
||||||
|
|
||||||
const cors = configService.get<Cors>('CORS').ORIGIN;
|
const origin = configService.get<Cors>('CORS').ORIGIN;
|
||||||
|
const methods = configService.get<Cors>('CORS').METHODS;
|
||||||
|
const credentials = configService.get<Cors>('CORS').CREDENTIALS;
|
||||||
|
|
||||||
export const initIO = (httpServer: Server) => {
|
export const initIO = (httpServer: Server) => {
|
||||||
if (configService.get<Websocket>('WEBSOCKET')?.ENABLED) {
|
if (configService.get<Websocket>('WEBSOCKET')?.ENABLED) {
|
||||||
io = new SocketIO(httpServer, {
|
io = new SocketIO(httpServer, {
|
||||||
cors: {
|
cors: {
|
||||||
origin: cors,
|
origin,
|
||||||
|
methods,
|
||||||
|
credentials,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -253,6 +253,23 @@ export class ChatRouter extends RouterBroker {
|
|||||||
|
|
||||||
return res.status(HttpStatus.OK).json(response);
|
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<InstanceDto>({
|
||||||
|
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) => {
|
.post(this.routerPath('sendPresence'), ...guards, async (req, res) => {
|
||||||
logger.verbose('request received in sendPresence');
|
logger.verbose('request received in sendPresence');
|
||||||
logger.verbose('request body: ');
|
logger.verbose('request body: ');
|
||||||
|
@ -1328,4 +1328,31 @@ export class ChannelStartupService {
|
|||||||
this.logger.verbose('Fetching chats');
|
this.logger.verbose('Fetching chats');
|
||||||
return await this.repository.chat.find({ where: { owner: this.instance.name } });
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user