104 lines
3.2 KiB
PHP
104 lines
3.2 KiB
PHP
<?php
|
|
/**
|
|
* API - Obtener mensajes de una conversación específica
|
|
*/
|
|
|
|
require_once '../config/config.php';
|
|
|
|
// Modo debug: desactivar autenticación si existe el parámetro debug
|
|
$debugMode = isset($_GET['debug']) && $_GET['debug'] === 'true';
|
|
|
|
if (!$debugMode) {
|
|
requireAuthentication();
|
|
}
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
try {
|
|
$user_id = $_GET['user_id'] ?? null;
|
|
|
|
if (!$user_id) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'user_id es requerido']);
|
|
exit;
|
|
}
|
|
|
|
$db = Database::getInstance();
|
|
|
|
// Obtener información del usuario
|
|
$user = $db->fetch(
|
|
"SELECT id, phone_number, name FROM users WHERE id = ?",
|
|
[$user_id]
|
|
);
|
|
|
|
if (!$user) {
|
|
http_response_code(404);
|
|
echo json_encode(['error' => 'Usuario no encontrado']);
|
|
exit;
|
|
}
|
|
|
|
// Obtener todos los mensajes de la conversación
|
|
$conversations = $db->fetchAll(
|
|
"SELECT
|
|
id,
|
|
message_id,
|
|
direction,
|
|
message_type,
|
|
content,
|
|
media_url,
|
|
status,
|
|
created_at
|
|
FROM conversations
|
|
WHERE user_id = ?
|
|
AND (
|
|
message_type = 'text'
|
|
OR (message_type IN ('image', 'audio', 'video', 'document') AND media_url IS NOT NULL AND media_url != '')
|
|
)
|
|
ORDER BY created_at ASC",
|
|
[$user_id]
|
|
);
|
|
|
|
// 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' => (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);
|
|
|
|
// Marcar mensajes como leídos
|
|
$db->query(
|
|
"UPDATE conversations SET status = 'read' WHERE user_id = ? AND direction = 'incoming' AND status != 'read'",
|
|
[$user_id]
|
|
);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'user' => [
|
|
'id' => intval($user['id']),
|
|
'phone_number' => $user['phone_number'],
|
|
'name' => $user['name'] ?? $user['phone_number']
|
|
],
|
|
'conversations' => $conversations,
|
|
'total_conversations' => count($conversations)
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Error in get_conversation_detail.php: " . $e->getMessage());
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Error interno del servidor: ' . $e->getMessage()]);
|
|
}
|
|
?>
|