mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-26 10:28:38 -06:00
linting
This commit is contained in:
parent
3aaee9ebb3
commit
a26e062936
@ -19,19 +19,19 @@ export class KwikController {
|
|||||||
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[] = [
|
||||||
{ $sort: { 'key.remoteJid': -1, 'messageTimestamp': -1 }},
|
{ $sort: { 'key.remoteJid': -1, messageTimestamp: -1 } },
|
||||||
{
|
{
|
||||||
$group: {
|
$group: {
|
||||||
_id: '$key.remoteJid',
|
_id: '$key.remoteJid',
|
||||||
owner: { $first: '$owner'},
|
owner: { $first: '$owner' },
|
||||||
message: {$first: '$message'},
|
message: { $first: '$message' },
|
||||||
lastAllMsgTimestamp: { $first: '$messageTimestamp' },
|
lastAllMsgTimestamp: { $first: '$messageTimestamp' },
|
||||||
name: {$first: '$pushName'},
|
name: { $first: '$pushName' },
|
||||||
fromMe: {$first: '$key.fromMe'},
|
fromMe: { $first: '$key.fromMe' },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ $match: { owner: instanceName } },
|
{ $match: { owner: instanceName } },
|
||||||
{ $sort: { 'lastAllMsgTimestamp': -1 }} ,
|
{ $sort: { lastAllMsgTimestamp: -1 } },
|
||||||
];
|
];
|
||||||
|
|
||||||
if (sort === 'asc') {
|
if (sort === 'asc') {
|
||||||
@ -49,58 +49,67 @@ 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 contacts_promises = connection.collection('contacts').find({
|
const contacts_promises = connection
|
||||||
owner: instanceName,
|
.collection('contacts')
|
||||||
id: { $in: chat_id_list},
|
.find({
|
||||||
}).toArray()
|
owner: instanceName,
|
||||||
|
id: { $in: chat_id_list },
|
||||||
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;
|
|
||||||
})
|
})
|
||||||
|
.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;
|
return mm;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user