mejoras
This commit is contained in:
+37
-26
@@ -22,12 +22,16 @@ try {
|
||||
echo json_encode(['error' => 'user_id es requerido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
// Paginación: limit y before (timestamp) - cargamos N mensajes anteriores a 'before'
|
||||
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 50;
|
||||
$limit = max(1, min(200, $limit));
|
||||
$before = !empty($_GET['before']) ? $_GET['before'] : null; // expect timestamp string
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
// Obtener mensajes del usuario (de ambas tablas para compatibilidad)
|
||||
$conversations = $db->fetchAll(
|
||||
"SELECT
|
||||
|
||||
// Construir consulta: obtener mensajes ordenados DESC (más recientes primero) y limitar
|
||||
$sql = "SELECT
|
||||
id,
|
||||
user_id,
|
||||
COALESCE(content, message_text) as content,
|
||||
@@ -41,25 +45,29 @@ try {
|
||||
COALESCE(c.is_read, 0) as is_read
|
||||
FROM conversations c
|
||||
LEFT JOIN users u ON c.user_id = u.id
|
||||
WHERE user_id = :user_id
|
||||
UNION ALL
|
||||
SELECT
|
||||
id,
|
||||
user_id,
|
||||
message_text as content,
|
||||
NULL as message_id,
|
||||
NULL as media_url,
|
||||
NULL as user_phone,
|
||||
direction,
|
||||
message_type,
|
||||
status,
|
||||
created_at,
|
||||
1 as is_read
|
||||
FROM conversations
|
||||
WHERE user_id = :user_id
|
||||
ORDER BY created_at ASC",
|
||||
['user_id' => $userId]
|
||||
);
|
||||
WHERE user_id = :user_id";
|
||||
|
||||
$params = ['user_id' => $userId];
|
||||
if ($before) {
|
||||
$sql .= " AND created_at < :before";
|
||||
$params['before'] = $before;
|
||||
}
|
||||
|
||||
$sql .= " ORDER BY created_at DESC LIMIT " . $limit;
|
||||
|
||||
$conversations = $db->fetchAll($sql, $params);
|
||||
|
||||
// Si no hay mensajes, devolver array vacío
|
||||
if (empty($conversations)) {
|
||||
echo json_encode(['success' => true, 'data' => [], 'has_more' => false]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Queremos devolver los mensajes en orden cronológico ascendente para la UI
|
||||
$conversations = array_reverse($conversations);
|
||||
|
||||
// Indicar si hay más mensajes anteriores (si la consulta original devolvió exactamente $limit, podría haber más)
|
||||
$hasMore = count($conversations) >= 1 && count($conversations) == $limit ? true : false;
|
||||
|
||||
// Si no hay mensajes en conversations, intentar con conversations
|
||||
if (empty($conversations)) {
|
||||
@@ -115,8 +123,11 @@ try {
|
||||
'created_at' => $msg['created_at']
|
||||
];
|
||||
}, $conversations);
|
||||
|
||||
echo json_encode($conversations);
|
||||
|
||||
// earliest message timestamp (para paginación hacia atrás)
|
||||
$earliest = $conversations[0]['created_at'] ?? null;
|
||||
|
||||
echo json_encode(['success' => true, 'data' => $conversations, 'has_more' => $hasMore, 'earliest' => $earliest]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in get_user_conversations.php: " . $e->getMessage());
|
||||
|
||||
Reference in New Issue
Block a user