Update chat_window.php
This commit is contained in:
+38
-18
@@ -496,8 +496,18 @@ if (empty($user_id)) {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
|
||||
// Manejar respuestas que no contienen JSON válido
|
||||
try {
|
||||
const text = await response.text();
|
||||
if (!text || text.trim() === '') {
|
||||
throw new Error('Empty response body');
|
||||
}
|
||||
return JSON.parse(text);
|
||||
} catch (err) {
|
||||
console.error('apiCall parse error for', url, err);
|
||||
throw new Error('Invalid JSON response from ' + url + ': ' + err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -516,6 +526,11 @@ if (empty($user_id)) {
|
||||
};
|
||||
return text.toString().replace(/[&<>"']/g, function(m) { return map[m]; });
|
||||
}
|
||||
|
||||
// Helper: validar que una URL de media sea útil (no null/undefined/empty)
|
||||
function isValidMediaUrl(u) {
|
||||
return (typeof u === 'string') && u.trim() !== '' && u !== 'null' && u !== 'undefined';
|
||||
}
|
||||
|
||||
// Cargar datos del usuario y mensajes
|
||||
async function loadUserData() {
|
||||
@@ -570,9 +585,9 @@ if (empty($user_id)) {
|
||||
const isOutgoing = msg.direction === 'outgoing';
|
||||
const messageClass = isOutgoing ? 'message-outgoing' : 'message-incoming';
|
||||
|
||||
// Debug: ver todos los datos del mensaje (prioriza media_url_external)
|
||||
const has_media_external = !!(msg.media_url_external && msg.media_url_external.trim() !== '');
|
||||
const effective_media = msg.media_url_external || msg.local_file || msg.local_thumb || msg.media_url || '';
|
||||
// Debug: ver todos los datos del mensaje (prioriza media_url_external y valida URLs)
|
||||
const has_media_external = isValidMediaUrl(msg.media_url_external);
|
||||
const effective_media = isValidMediaUrl(msg.media_url_external) ? msg.media_url_external : (isValidMediaUrl(msg.local_file) ? msg.local_file : (isValidMediaUrl(msg.local_thumb) ? msg.local_thumb : (isValidMediaUrl(msg.media_url) ? msg.media_url : '')));
|
||||
console.log('Procesando mensaje:', {
|
||||
id: msg.id,
|
||||
type: msg.message_type,
|
||||
@@ -583,7 +598,7 @@ if (empty($user_id)) {
|
||||
|
||||
// Verificar si es multimedia (validando principalmente media_url_external o archivos locales)
|
||||
let contentHtml = '';
|
||||
const hasMediaUrl = (msg.media_url_external && msg.media_url_external.trim() !== '') || (msg.local_file && msg.local_file.trim() !== '') || (msg.local_thumb && msg.local_thumb.trim() !== '');
|
||||
const hasMediaUrl = isValidMediaUrl(msg.media_url_external) || isValidMediaUrl(msg.local_file) || isValidMediaUrl(msg.local_thumb);
|
||||
const isMultimedia = hasMediaUrl && msg.message_type && msg.message_type !== 'text';
|
||||
|
||||
if (isMultimedia) {
|
||||
@@ -593,20 +608,20 @@ if (empty($user_id)) {
|
||||
case 'image':
|
||||
// Determinar miniatura y URL completa (prioriza media_url_external o archivos locales)
|
||||
let thumbUrl = '';
|
||||
if (msg.local_thumb) {
|
||||
if (isValidMediaUrl(msg.local_thumb)) {
|
||||
thumbUrl = `/${msg.local_thumb}`;
|
||||
} else if (msg.local_file) {
|
||||
} else if (isValidMediaUrl(msg.local_file)) {
|
||||
thumbUrl = `/${msg.local_file}`;
|
||||
} else if (msg.media_url_external) {
|
||||
} else if (isValidMediaUrl(msg.media_url_external)) {
|
||||
thumbUrl = msg.media_url_external;
|
||||
} else if (msg.media_url) {
|
||||
} else if (isValidMediaUrl(msg.media_url)) {
|
||||
thumbUrl = msg.media_url;
|
||||
}
|
||||
|
||||
let fullUrl = '';
|
||||
if (msg.local_file) {
|
||||
if (isValidMediaUrl(msg.local_file)) {
|
||||
fullUrl = `/api/version/media-url.php?local=${encodeURIComponent(msg.local_file)}`;
|
||||
} else if (msg.media_url_external) {
|
||||
} else if (isValidMediaUrl(msg.media_url_external)) {
|
||||
if (msg.media_url_external.indexOf('api/get_media.php?id=') !== -1) {
|
||||
const parts = msg.media_url_external.split('id=');
|
||||
const mid = parts[1] ? decodeURIComponent(parts[1]) : '';
|
||||
@@ -621,14 +636,14 @@ if (empty($user_id)) {
|
||||
fullUrl = '';
|
||||
}
|
||||
|
||||
if (fullUrl && fullUrl.trim() !== '') {
|
||||
if (isValidMediaUrl(fullUrl)) {
|
||||
contentHtml = `
|
||||
<a href="#" onclick="openImageModal(${JSON.stringify(fullUrl)}); return false;" title="Abrir imagen">
|
||||
<img src="${thumbUrl || fullUrl}" alt="Imagen" class="message-media-image">
|
||||
<img src="${escapeHtml(thumbUrl || fullUrl)}" alt="Imagen" class="message-media-image">
|
||||
</a>`;
|
||||
} else if (thumbUrl && thumbUrl.trim() !== '') {
|
||||
} else if (isValidMediaUrl(thumbUrl)) {
|
||||
// Mostrar solo la miniatura si no hay URL externa/local para abrir
|
||||
contentHtml = `<img src="${thumbUrl}" alt="Imagen" class="message-media-image">`;
|
||||
contentHtml = `<img src="${escapeHtml(thumbUrl)}" alt="Imagen" class="message-media-image">`;
|
||||
} else {
|
||||
// No hay media disponible
|
||||
contentHtml = `<div class="text-muted"><em>Imagen no disponible</em></div>`;
|
||||
@@ -651,8 +666,8 @@ if (empty($user_id)) {
|
||||
case 'document':
|
||||
const filename = msg.filename || msg.content || 'Documento';
|
||||
const fileIcon = getFileIcon(filename);
|
||||
// Construir URL de descarga usando media_url_external o archivo local (requerimos media_url_external)
|
||||
let docSource = msg.media_url_external || msg.local_file || null;
|
||||
// Construir URL de descarga usando media_url_external o archivo local (validar fuentes)
|
||||
let docSource = isValidMediaUrl(msg.media_url_external) ? msg.media_url_external : (isValidMediaUrl(msg.local_file) ? msg.local_file : null);
|
||||
let docUrl = null;
|
||||
if (docSource && docSource.indexOf && docSource.indexOf('api/get_media.php?id=') !== -1) {
|
||||
const parts = docSource.split('id=');
|
||||
@@ -1311,6 +1326,11 @@ if (empty($user_id)) {
|
||||
|
||||
// Modal para ver imágenes en grande
|
||||
function openImageModal(imageUrl) {
|
||||
if (!isValidMediaUrl(imageUrl)) {
|
||||
showAlert('Media no disponible', 'warning');
|
||||
return;
|
||||
}
|
||||
|
||||
// Crear modal si no existe
|
||||
let modal = document.getElementById('imageModal');
|
||||
if (!modal) {
|
||||
|
||||
Reference in New Issue
Block a user