This commit is contained in:
Pedro Howat 2024-08-27 11:18:19 -03:00
parent 3aaee9ebb3
commit a26e062936

View File

@ -19,19 +19,19 @@ export class KwikController {
const connection = dbserver.getClient().db(db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api');
const messages = connection.collection('messages');
const pipeline: Document[] = [
{ $sort: { 'key.remoteJid': -1, 'messageTimestamp': -1 }},
{ $sort: { 'key.remoteJid': -1, messageTimestamp: -1 } },
{
$group: {
_id: '$key.remoteJid',
owner: { $first: '$owner'},
message: {$first: '$message'},
owner: { $first: '$owner' },
message: { $first: '$message' },
lastAllMsgTimestamp: { $first: '$messageTimestamp' },
name: {$first: '$pushName'},
fromMe: {$first: '$key.fromMe'},
name: { $first: '$pushName' },
fromMe: { $first: '$key.fromMe' },
},
},
{ $match: { owner: instanceName } },
{ $sort: { 'lastAllMsgTimestamp': -1 }} ,
},
{ $match: { owner: instanceName } },
{ $sort: { lastAllMsgTimestamp: -1 } },
];
if (sort === 'asc') {
@ -49,58 +49,67 @@ export class KwikController {
}
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 contacts_promises = connection.collection('contacts').find({
owner: instanceName,
id: { $in: chat_id_list},
}).toArray()
const group_promises = chat_id_list.filter(g => g.includes('@g.us')).map(g=> this.waMonitor.waInstances[instanceName].findGroup({groupJid: g}, "inner"))
const [contacts_solved, ...groups_solved] = await Promise.all([contacts_promises, ...group_promises])
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 mm = msgs.map((msg) => {
const [messageType, lastMsg] = Object.entries(msg.message)[0] || ['none', '']
const chat_data = {
id: msg._id,
labels: [],
owner: msg.owner,
last_message_timestamp: msg.lastAllMsgTimestamp,
message: this.isTextMessage(messageType) ? msg.message : null,
message_type: messageType,
fromMe: msg.fromMe,
phone_num: null,
profile_picture: null,
name: null,
sender: msg.name,
type: null,
};
const info = msg._id.split('@');
if (info[1] == 'g.us') {
chat_data.type = 'GROUP';
const group = groups[String(msg._id)]
if (group){
chat_data.name = group.subject;
chat_data.profile_picture = group.pictureUrl;
}
} else {
const contact = contacts[String(msg._id)]
chat_data.type = 'CONTACT';
chat_data.phone_num = info[0];
if (contact) {
chat_data.name = contact.pushName;
chat_data.profile_picture = contact.profilePictureUrl;
}
}
return chat_data;
const contacts_promises = connection
.collection('contacts')
.find({
owner: instanceName,
id: { $in: chat_id_list },
})
.toArray();
const group_promises = chat_id_list
.filter((g) => g.includes('@g.us'))
.map((g) => this.waMonitor.waInstances[instanceName].findGroup({ groupJid: g }, 'inner'));
const [contacts_solved, ...groups_solved] = await Promise.all([contacts_promises, ...group_promises]);
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 mm = msgs.map((msg) => {
const [messageType, lastMsg] = Object.entries(msg.message)[0] || ['none', ''];
const chat_data = {
id: msg._id,
labels: [],
owner: msg.owner,
last_message_timestamp: msg.lastAllMsgTimestamp,
message: this.isTextMessage(messageType) ? msg.message : null,
message_type: messageType,
fromMe: msg.fromMe,
phone_num: null,
profile_picture: null,
name: null,
sender: msg.name,
type: null,
};
const info = msg._id.split('@');
if (info[1] == 'g.us') {
chat_data.type = 'GROUP';
const group = groups[String(msg._id)];
if (group) {
chat_data.name = group.subject;
chat_data.profile_picture = group.pictureUrl;
}
} else {
const contact = contacts[String(msg._id)];
chat_data.type = 'CONTACT';
chat_data.phone_num = info[0];
if (contact) {
chat_data.name = contact.pushName;
chat_data.profile_picture = contact.profilePictureUrl;
}
}
return chat_data;
});
return mm;
}