This commit is contained in:
Lizandro Guarnizo
2026-01-27 00:12:04 -05:00
parent a086e4a19a
commit 267805d5cd
6 changed files with 404 additions and 54 deletions
+39 -17
View File
@@ -27,6 +27,8 @@ try {
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 50;
$limit = max(1, min(200, $limit));
$before = !empty($_GET['before']) ? $_GET['before'] : null; // expect timestamp string
// We also support a 'since' parameter to fetch only messages strictly newer than a timestamp
$since = !empty($_GET['since']) ? $_GET['since'] : null;
// Validar formato de 'before' si se proporcionó (evitar SQL/parse errors por input malformado)
if (!is_null($before)) {
@@ -38,10 +40,19 @@ try {
}
}
// Validar 'since' si se pasó
if (!is_null($since)) {
if (!preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $since)) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'since timestamp inválido']);
exit;
}
}
$db = Database::getInstance();
// Construir consulta: obtener mensajes ordenados DESC (más recientes primero) y limitar
// Evitar ambigüedades prefijando columnas con el alias de la tabla (c)
// Construir consulta: by default paginamos hacia atrás (before) y ordenamos DESC
// Pero cuando se solicita 'since' queremos sólo mensajes más nuevos => orden ASC (para retornar cronológicamente)
$sql = "SELECT
c.id as id,
c.user_id as user_id,
@@ -65,20 +76,28 @@ try {
$beforeId = isset($_GET['before_id']) ? intval($_GET['before_id']) : null;
$params = ['user_id' => $userId];
if ($before) {
$sql .= " AND (c.created_at < :before_lt";
$params['before_lt'] = $before;
if ($beforeId) {
// Incluir mensajes con mismo timestamp pero id menor (paginación estable)
// Usamos placeholder distinto para evitar duplicar el mismo nombre en la query
$sql .= " OR (c.created_at = :before_eq AND c.id < :before_id)";
$params['before_eq'] = $before;
$params['before_id'] = $beforeId;
}
$sql .= ")";
}
$sql .= " ORDER BY c.created_at DESC, c.id DESC LIMIT " . $limit;
if (!is_null($since)) {
// If 'since' is provided, return only messages strictly newer than 'since' ordered ascending
$sql .= " AND c.created_at > :since";
$params['since'] = $since;
$sql .= " ORDER BY c.created_at ASC, c.id ASC LIMIT " . $limit;
} else {
if ($before) {
$sql .= " AND (c.created_at < :before_lt";
$params['before_lt'] = $before;
if ($beforeId) {
// Incluir mensajes con mismo timestamp pero id menor (paginación estable)
// Usamos placeholder distinto para evitar duplicar el mismo nombre en la query
$sql .= " OR (c.created_at = :before_eq AND c.id < :before_id)";
$params['before_eq'] = $before;
$params['before_id'] = $beforeId;
}
$sql .= ")";
}
$sql .= " ORDER BY c.created_at DESC, c.id DESC LIMIT " . $limit;
}
// Log params/consulta para diagnóstico si algo falla (no sensible)
error_log('get_user_messages.php - params: ' . json_encode(['user_id' => $userId, 'limit' => $limit, 'before' => $before]));
@@ -107,8 +126,11 @@ try {
exit;
}
// Queremos devolver los mensajes en orden cronológico ascendente para la UI
$conversations = array_reverse($conversations);
// Si se solicitó 'since' ya devolvimos en orden ASC (cronológico). Si no, revertimos
$sinceProvided = !is_null($since);
if (!$sinceProvided) {
$conversations = array_reverse($conversations);
}
// Recalcular usando el resultado final para decidir paginación
$finalCount = is_array($conversations) ? count($conversations) : 0;
+33
View File
@@ -62,3 +62,36 @@
[2026-01-21 22:05:03] Request: GET /api/version/media-url.php?id=3932473397057815 GET:{"id":"3932473397057815"} POST:[]
[2026-01-21 22:05:03] Graph API request to https://graph.facebook.com/v22.0/3932473397057815
[2026-01-21 22:05:03] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=3932473397057815&source=getMedia&ext=1769051403&hash=ARm7CptuN3CH6WN5V1RmuW6rK9iIipE_KE1-FLbj3RQuHQ
[2026-01-26 23:32:27] Request: GET /api/version/media-url.php?id=744809485358700 GET:{"id":"744809485358700"} POST:[]
[2026-01-26 23:32:27] Graph API request to https://graph.facebook.com/v22.0/744809485358700
[2026-01-26 23:32:28] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=744809485358700&source=getMedia&ext=1769488648&hash=ARk9shLmP6iyMkiJ5uh9-PvYmJ5nHAtiDbFy7aHsq4xfZw
[2026-01-26 23:32:53] Request: GET /api/version/media-url.php?id=744809485358700 GET:{"id":"744809485358700"} POST:[]
[2026-01-26 23:32:53] Graph API request to https://graph.facebook.com/v22.0/744809485358700
[2026-01-26 23:32:53] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=744809485358700&source=getMedia&ext=1769488673&hash=ARnB_yLpfd4re-Gg3VWlmVMeBBUddIMvtwT9H1cyNVDm8w
[2026-01-26 23:38:15] Request: GET /api/version/media-url.php?id=744809485358700 GET:{"id":"744809485358700"} POST:[]
[2026-01-26 23:38:15] Graph API request to https://graph.facebook.com/v22.0/744809485358700
[2026-01-26 23:38:15] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=744809485358700&source=getMedia&ext=1769488995&hash=ARmtsV162AJYLSTw4c5sSRcOqeNkSp3OpDEzblPHqrS-Pw
[2026-01-26 23:40:43] Request: GET /api/version/media-url.php?id=744809485358700 GET:{"id":"744809485358700"} POST:[]
[2026-01-26 23:40:43] Graph API request to https://graph.facebook.com/v22.0/744809485358700
[2026-01-26 23:40:43] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=744809485358700&source=getMedia&ext=1769489143&hash=ARl38NJPww5Bl_g5a1g_M760-mftREt_0azvDz7X_0_PRA
[2026-01-26 23:48:33] Request: GET /api/version/media-url.php?id=744809485358700 GET:{"id":"744809485358700"} POST:[]
[2026-01-26 23:48:33] Graph API request to https://graph.facebook.com/v22.0/744809485358700
[2026-01-26 23:48:33] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=744809485358700&source=getMedia&ext=1769489613&hash=ARn_ITiMj4Y1h8iPaKANBlnlUGWppfK74dzTfG34VaW5tQ
[2026-01-26 23:48:59] Request: GET /api/version/media-url.php?id=744809485358700 GET:{"id":"744809485358700"} POST:[]
[2026-01-26 23:48:59] Graph API request to https://graph.facebook.com/v22.0/744809485358700
[2026-01-26 23:48:59] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=744809485358700&source=getMedia&ext=1769489639&hash=ARnKGDn0Cde1v5uNe54sObAocewL41pdZPPO_pJ_v7Y2fA
[2026-01-26 23:53:51] Request: GET /api/version/media-url.php?id=1387253256108589 GET:{"id":"1387253256108589"} POST:[]
[2026-01-26 23:53:51] Graph API request to https://graph.facebook.com/v22.0/1387253256108589
[2026-01-26 23:53:52] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=1387253256108589&source=getMedia&ext=1769489932&hash=ARn_m8oP_45Y5WQnFTSdS8Z1GkIl7WlVVouVsoyAk5HiGw
[2026-01-26 23:54:25] Request: GET /api/version/media-url.php?id=744809485358700 GET:{"id":"744809485358700"} POST:[]
[2026-01-26 23:54:25] Graph API request to https://graph.facebook.com/v22.0/744809485358700
[2026-01-26 23:54:25] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=744809485358700&source=getMedia&ext=1769489965&hash=ARmxdxTaoFR0Nng0iIoNK7adP9k4U7yzGDk6Ph-ei_uXkA
[2026-01-27 00:00:16] Request: GET /api/version/media-url.php?id=744809485358700 GET:{"id":"744809485358700"} POST:[]
[2026-01-27 00:00:16] Graph API request to https://graph.facebook.com/v22.0/744809485358700
[2026-01-27 00:00:16] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=744809485358700&source=getMedia&ext=1769490316&hash=ARmpmDemk-gGukePX4CalHS0lLm_I-07G2copLrKz2KViw
[2026-01-27 00:03:27] Request: GET /api/version/media-url.php?id=744809485358700 GET:{"id":"744809485358700"} POST:[]
[2026-01-27 00:03:27] Graph API request to https://graph.facebook.com/v22.0/744809485358700
[2026-01-27 00:03:27] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=744809485358700&source=getMedia&ext=1769490507&hash=ARn3anEAozRDy-NURAxHAGPhDXOeZjPOREUSpjXnYcvj9w
[2026-01-27 00:10:48] Request: GET /api/version/media-url.php?id=744809485358700 GET:{"id":"744809485358700"} POST:[]
[2026-01-27 00:10:48] Graph API request to https://graph.facebook.com/v22.0/744809485358700
[2026-01-27 00:10:49] Proxying Facebook media URL with Authorization to https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=744809485358700&source=getMedia&ext=1769490949&hash=ARmzuq6pJSn6HumhTkmzK4COU2MX1aVjnFuz7CqAMzTzdA