Feat: Sincronizar contactos y grupos, mejorar nombres en MCP
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m8s

- Agregar listeners contacts.upsert y contacts.update
- Agregar listeners groups.upsert y groups.update
- Guardar contactos del historial en messaging-history.set
- Modificar MCP para hacer JOIN con contacts y group_metadata
- Ahora los chats muestran nombres reales en lugar de números/IDs
This commit is contained in:
2025-12-04 14:51:09 -06:00
parent 42f0dcafc1
commit 67b54d4ad9
2 changed files with 185 additions and 21 deletions

View File

@@ -687,10 +687,17 @@ export async function handleToolCall(toolName: string, args: Record<string, any>
}
const result = await query(
`SELECT id, jid, name, is_group, unread_count, last_message_at, last_message_type
FROM chats
WHERE instance_id = $1
ORDER BY last_message_at DESC NULLS LAST
`SELECT
c.id, c.jid, c.is_group, c.unread_count, c.last_message_at, c.last_message_type,
CASE
WHEN c.is_group THEN COALESCE(gm.subject, c.name, c.jid)
ELSE COALESCE(ct.name, ct.push_name, c.name, SPLIT_PART(c.jid, '@', 1))
END as name
FROM chats c
LEFT JOIN contacts ct ON c.instance_id = ct.instance_id AND c.jid = ct.jid
LEFT JOIN group_metadata gm ON c.instance_id = gm.instance_id AND c.jid = gm.jid
WHERE c.instance_id = $1
ORDER BY c.last_message_at DESC NULLS LAST
LIMIT $2`,
[instanceId, Math.min(limit, 100)]
)
@@ -717,9 +724,17 @@ export async function handleToolCall(toolName: string, args: Record<string, any>
}
const result = await query(
`SELECT id, jid, name, is_group, unread_count, last_message_at, last_message_type
FROM chats
WHERE id = $1 AND instance_id = $2`,
`SELECT
c.id, c.jid, c.is_group, c.unread_count, c.last_message_at, c.last_message_type,
CASE
WHEN c.is_group THEN COALESCE(gm.subject, c.name, c.jid)
ELSE COALESCE(ct.name, ct.push_name, c.name, SPLIT_PART(c.jid, '@', 1))
END as name,
gm.participants as group_participants
FROM chats c
LEFT JOIN contacts ct ON c.instance_id = ct.instance_id AND c.jid = ct.jid
LEFT JOIN group_metadata gm ON c.instance_id = gm.instance_id AND c.jid = gm.jid
WHERE c.id = $1 AND c.instance_id = $2`,
[chatId, instanceId]
)
@@ -728,17 +743,24 @@ export async function handleToolCall(toolName: string, args: Record<string, any>
}
const chat = result.rows[0]
const response: any = {
id: chat.id,
jid: chat.jid,
name: chat.name,
isGroup: chat.is_group,
unreadCount: chat.unread_count || 0,
lastMessageAt: chat.last_message_at,
lastMessageType: chat.last_message_type
}
// Include participants for groups
if (chat.is_group && chat.group_participants) {
response.participants = chat.group_participants
}
return mcpSuccess({
ok: true,
chat: {
id: chat.id,
jid: chat.jid,
name: chat.name,
isGroup: chat.is_group,
unreadCount: chat.unread_count || 0,
lastMessageAt: chat.last_message_at,
lastMessageType: chat.last_message_type
}
chat: response
})
}
@@ -750,11 +772,18 @@ export async function handleToolCall(toolName: string, args: Record<string, any>
}
const result = await query(
`SELECT id, jid, name, is_group, unread_count, last_message_at
FROM chats
WHERE instance_id = $1
AND (name ILIKE $2 OR jid ILIKE $2)
ORDER BY last_message_at DESC NULLS LAST
`SELECT
c.id, c.jid, c.is_group, c.unread_count, c.last_message_at,
CASE
WHEN c.is_group THEN COALESCE(gm.subject, c.name, c.jid)
ELSE COALESCE(ct.name, ct.push_name, c.name, SPLIT_PART(c.jid, '@', 1))
END as name
FROM chats c
LEFT JOIN contacts ct ON c.instance_id = ct.instance_id AND c.jid = ct.jid
LEFT JOIN group_metadata gm ON c.instance_id = gm.instance_id AND c.jid = gm.jid
WHERE c.instance_id = $1
AND (c.name ILIKE $2 OR c.jid ILIKE $2 OR ct.name ILIKE $2 OR ct.push_name ILIKE $2 OR gm.subject ILIKE $2)
ORDER BY c.last_message_at DESC NULLS LAST
LIMIT $3`,
[instanceId, `%${searchQuery}%`, Math.min(limit, 50)]
)