up
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* API - Obtener mensajes de un usuario específico
|
||||
* Fecha: 4 de enero de 2026
|
||||
*/
|
||||
|
||||
require_once '../config/config.php';
|
||||
|
||||
// Verificar autenticación
|
||||
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 {
|
||||
$userId = intval($_GET['user_id'] ?? 0);
|
||||
|
||||
if (!$userId) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'user_id es requerido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
// Obtener mensajes del usuario (de ambas tablas para compatibilidad)
|
||||
$messages = $db->fetchAll(
|
||||
"SELECT
|
||||
id,
|
||||
user_id,
|
||||
COALESCE(content, message_text) as content,
|
||||
direction,
|
||||
message_type,
|
||||
status,
|
||||
created_at
|
||||
FROM conversations
|
||||
WHERE user_id = :user_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
id,
|
||||
user_id,
|
||||
message_text as content,
|
||||
direction,
|
||||
message_type,
|
||||
status,
|
||||
created_at
|
||||
FROM messages
|
||||
WHERE user_id = :user_id
|
||||
ORDER BY created_at ASC",
|
||||
['user_id' => $userId]
|
||||
);
|
||||
|
||||
// Si no hay mensajes en conversations, intentar con messages
|
||||
if (empty($messages)) {
|
||||
$messages = $db->fetchAll(
|
||||
"SELECT
|
||||
id,
|
||||
user_id,
|
||||
message_text as content,
|
||||
direction,
|
||||
message_type,
|
||||
status,
|
||||
created_at
|
||||
FROM messages
|
||||
WHERE user_id = :user_id
|
||||
ORDER BY created_at ASC",
|
||||
['user_id' => $userId]
|
||||
);
|
||||
}
|
||||
|
||||
// Formatear fechas y limpiar datos
|
||||
$messages = array_map(function($msg) {
|
||||
return [
|
||||
'id' => intval($msg['id']),
|
||||
'user_id' => intval($msg['user_id']),
|
||||
'content' => $msg['content'] ?? '',
|
||||
'direction' => $msg['direction'] ?? 'incoming',
|
||||
'message_type' => $msg['message_type'] ?? 'text',
|
||||
'status' => $msg['status'] ?? 'sent',
|
||||
'created_at' => $msg['created_at']
|
||||
];
|
||||
}, $messages);
|
||||
|
||||
echo json_encode($messages);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in get_user_messages.php: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Error interno del servidor']);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user