mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-26 10:28:38 -06:00
Merge branch 'main' of https://github.com/instantsol/evolution-api
This commit is contained in:
commit
3aaee9ebb3
@ -11,21 +11,27 @@ const logger = new Logger('KwikController');
|
|||||||
export class KwikController {
|
export class KwikController {
|
||||||
constructor(private readonly waMonitor: WAMonitoringService) {}
|
constructor(private readonly waMonitor: WAMonitoringService) {}
|
||||||
|
|
||||||
private isTextMessage(message: any) {
|
private isTextMessage(messageType: any) {
|
||||||
return message.messageType === 'conversation' || message.messageType === 'extendedTextMessage';
|
return ['senderKeyDistributionMessage', 'conversation', 'extendedTextMessage'].includes(messageType);
|
||||||
}
|
}
|
||||||
public async fetchChats({ instanceName }: InstanceDto, limit: number, skip: number, sort: any) {
|
public async fetchChats({ instanceName }: InstanceDto, limit: number, skip: number, sort: any) {
|
||||||
const db = configService.get<Database>('DATABASE');
|
const db = configService.get<Database>('DATABASE');
|
||||||
const connection = dbserver.getClient().db(db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api');
|
const connection = dbserver.getClient().db(db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api');
|
||||||
const messages = connection.collection('messages');
|
const messages = connection.collection('messages');
|
||||||
const pipeline: Document[] = [
|
const pipeline: Document[] = [
|
||||||
{ $match: { owner: instanceName } },
|
{ $sort: { 'key.remoteJid': -1, 'messageTimestamp': -1 }},
|
||||||
{
|
{
|
||||||
$group: {
|
$group: {
|
||||||
_id: '$key.remoteJid',
|
_id: '$key.remoteJid',
|
||||||
lastAllMsgTimestamp: { $max: '$messageTimestamp' }
|
owner: { $first: '$owner'},
|
||||||
|
message: {$first: '$message'},
|
||||||
|
lastAllMsgTimestamp: { $first: '$messageTimestamp' },
|
||||||
|
name: {$first: '$pushName'},
|
||||||
|
fromMe: {$first: '$key.fromMe'},
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
|
{ $match: { owner: instanceName } },
|
||||||
|
{ $sort: { 'lastAllMsgTimestamp': -1 }} ,
|
||||||
];
|
];
|
||||||
|
|
||||||
if (sort === 'asc') {
|
if (sort === 'asc') {
|
||||||
@ -44,12 +50,7 @@ export class KwikController {
|
|||||||
|
|
||||||
const msgs = await messages.aggregate(pipeline).toArray();
|
const msgs = await messages.aggregate(pipeline).toArray();
|
||||||
const chat_id_list = msgs.map(m=>m._id)
|
const chat_id_list = msgs.map(m=>m._id)
|
||||||
const chat_promises = connection.collection('chats').find({ id: { $in: chat_id_list}}).toArray()
|
|
||||||
const last_messages_promises = connection.collection('messages').find({
|
|
||||||
owner: instanceName,
|
|
||||||
messageTimestamp: { $in : msgs.map(m=>m.lastAllMsgTimestamp)},
|
|
||||||
"key.remoteJid": { $in: chat_id_list}
|
|
||||||
}).toArray()
|
|
||||||
const contacts_promises = connection.collection('contacts').find({
|
const contacts_promises = connection.collection('contacts').find({
|
||||||
owner: instanceName,
|
owner: instanceName,
|
||||||
id: { $in: chat_id_list},
|
id: { $in: chat_id_list},
|
||||||
@ -57,32 +58,30 @@ export class KwikController {
|
|||||||
|
|
||||||
const group_promises = chat_id_list.filter(g => g.includes('@g.us')).map(g=> this.waMonitor.waInstances[instanceName].findGroup({groupJid: g}, "inner"))
|
const group_promises = chat_id_list.filter(g => g.includes('@g.us')).map(g=> this.waMonitor.waInstances[instanceName].findGroup({groupJid: g}, "inner"))
|
||||||
|
|
||||||
const [chats_solved, last_messages_solved, contacts_solved, ...groups_solved] = await Promise.all([chat_promises, last_messages_promises, contacts_promises, ...group_promises])
|
const [contacts_solved, ...groups_solved] = await Promise.all([contacts_promises, ...group_promises])
|
||||||
|
|
||||||
const chats = Object.fromEntries(chats_solved.map(m => ([m.id, m])))
|
|
||||||
const last_messages = Object.fromEntries(last_messages_solved.map(m => ([m.key.remoteJid, m])))
|
|
||||||
const contacts = Object.fromEntries(contacts_solved.map(c => ([c.id, c])))
|
const contacts = Object.fromEntries(contacts_solved.map(c => ([c.id, c])))
|
||||||
const groups = Object.fromEntries(groups_solved.map(g => {if (g) return [g.id, g]}))
|
const groups = Object.fromEntries(groups_solved.map(g => {if (g) return [g.id, g]}))
|
||||||
|
|
||||||
|
|
||||||
const mm = msgs.map((msg) => {
|
const mm = msgs.map((msg) => {
|
||||||
const chat = chats[String(msg._id)]
|
const [messageType, lastMsg] = Object.entries(msg.message)[0] || ['none', '']
|
||||||
const lastMsg = last_messages[String(msg._id)]
|
|
||||||
|
|
||||||
const chat_data = {
|
const chat_data = {
|
||||||
id: chat.id,
|
id: msg._id,
|
||||||
labels: chat.labels,
|
labels: [],
|
||||||
owner: chat.owner,
|
owner: msg.owner,
|
||||||
last_message_timestamp: msg.lastAllMsgTimestamp,
|
last_message_timestamp: msg.lastAllMsgTimestamp,
|
||||||
message: this.isTextMessage(lastMsg) ? lastMsg.message : null,
|
message: this.isTextMessage(messageType) ? msg.message : null,
|
||||||
message_type: lastMsg.messageType,
|
message_type: messageType,
|
||||||
|
fromMe: msg.fromMe,
|
||||||
phone_num: null,
|
phone_num: null,
|
||||||
profile_picture: null,
|
profile_picture: null,
|
||||||
name: null,
|
name: null,
|
||||||
|
sender: msg.name,
|
||||||
type: null,
|
type: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const info = chat.id.split('@');
|
const info = msg._id.split('@');
|
||||||
if (info[1] == 'g.us') {
|
if (info[1] == 'g.us') {
|
||||||
chat_data.type = 'GROUP';
|
chat_data.type = 'GROUP';
|
||||||
const group = groups[String(msg._id)]
|
const group = groups[String(msg._id)]
|
||||||
|
Loading…
Reference in New Issue
Block a user