This commit is contained in:
lizandrogd
2026-01-21 12:37:26 -05:00
parent 3f6e723fac
commit 43e54a55c0
4 changed files with 84 additions and 24 deletions
+48 -19
View File
@@ -63,25 +63,54 @@ try {
// Formatear mensajes
$conversations = array_map(function($msg) {
return [
'id' => intval($msg['id']),
'message_id' => $msg['message_id'] ?? '',
'direction' => $msg['direction'],
'message_type' => $msg['message_type'],
'content' => $msg['content'] ?? '',
'media_url' => null,
'local_file' => $msg['local_file'] ?? null,
'local_thumb' => $msg['local_thumb'] ?? null,
'media_url_external' => (isset($msg['media_url']) && $msg['media_url']) ? (preg_match('#^https?://#i', $msg['media_url']) ? $msg['media_url'] : ("api/get_media.php?id=" . urlencode($msg['media_url']))) : null,
'filename' => $msg['filename'] ?? null,
'mime_type' => $msg['mime_type'] ?? null,
'status' => $msg['status'] ?? 'sent',
'created_at' => $msg['created_at'],
'time' => date('H:i', strtotime($msg['created_at'])),
'date' => date('d/m/Y', strtotime($msg['created_at']))
];
}, $conversations);
// Build external URL if available (media id -> api/get_media proxy)
$external = (isset($msg['media_url']) && $msg['media_url']) ? (preg_match('#^https?://#i', $msg['media_url']) ? $msg['media_url'] : ("api/get_media.php?id=" . urlencode($msg['media_url']))) : null;
// Fallback: if no external URL but content is JSON with link, extract it
$contentStr = $msg['content'] ?? '';
if (!$external && $contentStr) {
$j = json_decode($contentStr, true);
if (is_array($j)) {
// Look for common shapes: image.link, image.url, document.link, audio.link
$candidates = [];
foreach (['image','document','audio','video'] as $k) {
if (isset($j[$k]) && is_array($j[$k])) {
if (!empty($j[$k]['link'])) $candidates[] = $j[$k]['link'];
if (!empty($j[$k]['url'])) $candidates[] = $j[$k]['url'];
if (!empty($j[$k]['id'])) $candidates[] = $j[$k]['id'];
}
}
// also top-level 'link'
if (!empty($j['link'])) $candidates[] = $j['link'];
// first valid candidate wins
foreach ($candidates as $cand) {
if ($cand) {
// if looks like id or url
$external = preg_match('#^https?://#i', $cand) ? $cand : (strpos($cand, 'http') === false ? "api/get_media.php?id=" . urlencode($cand) : $cand);
break;
}
}
}
}
return [
'id' => intval($msg['id']),
'message_id' => $msg['message_id'] ?? '',
'direction' => $msg['direction'],
'message_type' => $msg['message_type'],
'content' => $msg['content'] ?? '',
'media_url' => null,
'local_file' => $msg['local_file'] ?? null,
'local_thumb' => $msg['local_thumb'] ?? null,
'media_url_external' => $external,
'filename' => $msg['filename'] ?? null,
'mime_type' => $msg['mime_type'] ?? null,
'status' => $msg['status'] ?? 'sent',
'created_at' => $msg['created_at'],
'time' => date('H:i', strtotime($msg['created_at'])),
'date' => date('d/m/Y', strtotime($msg['created_at']))
];
}, $conversations);
// Marcar mensajes como leídos
$db->query(
"UPDATE conversations SET status = 'read' WHERE user_id = ? AND direction = 'incoming' AND status != 'read'",