This commit is contained in:
Pedro Howat 2024-08-26 17:44:10 -03:00
commit 3aaee9ebb3

View File

@ -11,21 +11,27 @@ const logger = new Logger('KwikController');
export class KwikController {
constructor(private readonly waMonitor: WAMonitoringService) {}
private isTextMessage(message: any) {
return message.messageType === 'conversation' || message.messageType === 'extendedTextMessage';
private isTextMessage(messageType: any) {
return ['senderKeyDistributionMessage', 'conversation', 'extendedTextMessage'].includes(messageType);
}
public async fetchChats({ instanceName }: InstanceDto, limit: number, skip: number, sort: any) {
const db = configService.get<Database>('DATABASE');
const connection = dbserver.getClient().db(db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api');
const messages = connection.collection('messages');
const pipeline: Document[] = [
{ $match: { owner: instanceName } },
{ $sort: { 'key.remoteJid': -1, 'messageTimestamp': -1 }},
{
$group: {
_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') {
@ -44,12 +50,7 @@ export class KwikController {
const msgs = await messages.aggregate(pipeline).toArray();
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({
owner: instanceName,
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 [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 groups = Object.fromEntries(groups_solved.map(g => {if (g) return [g.id, g]}))
const mm = msgs.map((msg) => {
const chat = chats[String(msg._id)]
const lastMsg = last_messages[String(msg._id)]
const [messageType, lastMsg] = Object.entries(msg.message)[0] || ['none', '']
const chat_data = {
id: chat.id,
labels: chat.labels,
owner: chat.owner,
id: msg._id,
labels: [],
owner: msg.owner,
last_message_timestamp: msg.lastAllMsgTimestamp,
message: this.isTextMessage(lastMsg) ? lastMsg.message : null,
message_type: lastMsg.messageType,
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 = chat.id.split('@');
const info = msg._id.split('@');
if (info[1] == 'g.us') {
chat_data.type = 'GROUP';
const group = groups[String(msg._id)]