import axios from 'axios'; import { WhatsAppMessage, Conversation, Msg, Participant } from '../types'; const conversations = new Map(); async function loadMessages( chatId: string, openWaUrl: string ): Promise { console.log(`[conversationStore] Loading messages for ${chatId}`); const { data } = await axios.post(`${openWaUrl}/loadAndGetAllMessagesInChat`, { args: { chatId, includeMe: true, includeNotifications: true, }, }); const msgs: WhatsAppMessage[] = data?.response || data || []; return msgs; } function mapMessage(m: WhatsAppMessage): Msg { return { id: m.id, from: m.from, to: m.to, ts: (m as any).timestamp || (m as any).t, type: ((m as any).type as any) || 'chat', text: (m as any).text || (m as any).caption || (m as any).body, mediaUrl: (m as any).cloudUrl || (m as any).clientUrl, mentions: ((m as any).mentionedJidList as any) || [], meta: { ack: (m as any).ack || 0, hasReaction: (m as any).hasReaction || false, isQuoted: !!(m as any).quotedMsg, }, }; } export async function getConversation( chatId: string, openWaUrl: string ): Promise { console.log(`[conversationStore] Retrieving conversation for ${chatId}`); let conv = conversations.get(chatId); if (!conv) { conv = await buildConversation(chatId, openWaUrl); } return conv; } export function listConversations(): Conversation[] { console.log('[conversationStore] Listing conversations'); return Array.from(conversations.values()); } export async function buildConversation( chatId: string, openWaUrl: string ): Promise { console.log(`[conversationStore] Building conversation for ${chatId}`); const rawMessages = await loadMessages(chatId, openWaUrl); const now = Date.now(); const first = rawMessages[0]; const chat = first?.chat; const title = chat?.formattedTitle || chat?.name || chatId; const isGroup = chat?.isGroup || false; const unreadCount = chat?.unreadCount || 0; const participantsMap = new Map(); if (chat?.contact) { const c = chat.contact; participantsMap.set(c.id, { id: c.id, name: c.pushname || c.name || '', isMe: c.isMe, }); } if (isGroup && chat?.groupMetadata?.participants) { for (const p of chat.groupMetadata.participants as any[]) { const c = p.contact || {}; const id = c.id || p.id; participantsMap.set(id, { id, name: c.pushname || c.name || '', isMe: c.isMe || false, isAdmin: p.isAdmin || p.isSuperAdmin, }); } } for (const m of rawMessages) { const s = m.sender; if (s && !participantsMap.has(s.id)) { participantsMap.set(s.id, { id: s.id, name: s.pushname || s.name || '', isMe: s.isMe, }); } } const messages: Msg[] = rawMessages.slice(-20).map(mapMessage); messages.sort((a, b) => a.ts - b.ts); const conv: Conversation = { chatId, title, isGroup, unreadCount, participants: Array.from(participantsMap.values()), messages, createdAt: conversations.get(chatId)?.createdAt || now, }; conversations.set(chatId, conv); return conv; } export function deleteConversation(chatId: string): boolean { console.log(`[conversationStore] Deleting conversation ${chatId}`); return conversations.delete(chatId); } export async function addMessageToConversation( chatId: string, msg: WhatsAppMessage, openWaUrl: string ): Promise { console.log(`[conversationStore] Adding message to ${chatId}`); const conv = await getConversation(chatId, openWaUrl); const mapped = mapMessage(msg); conv.messages.push(mapped); if (conv.messages.length > 20) conv.messages.shift(); const s = msg.sender; if (s && !conv.participants.some((p) => p.id === s.id)) { conv.participants.push({ id: s.id, name: s.pushname || s.name || '', isMe: s.isMe, }); } return conv; }