From ac6d04c31dae9d13dbd08566b11ea85874c7fbfc Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Sat, 24 Jan 2026 01:17:22 -0500 Subject: [PATCH] up --- api/react_message.php | 32 ++++++++++++++------------------ api/webhook.php | 40 +++++++++++++++++++++++++++++++++++++--- conversations.php | 6 ++++-- 3 files changed, 55 insertions(+), 23 deletions(-) diff --git a/api/react_message.php b/api/react_message.php index caec598..469a7e5 100644 --- a/api/react_message.php +++ b/api/react_message.php @@ -47,26 +47,22 @@ try { error_log('sendReaction failed: ' . $e->getMessage()); } - // Registrar la reacción como mensaje saliente en la BD - $msgData = [ - 'user_id' => $target['user_id'], - 'direction' => 'outgoing', - 'message_type' => 'reaction', - 'content' => $emoji, - 'reaction_to_message_id' => $message_id, - 'reaction_emoji' => $emoji, - 'status' => ($resp ? 'sent' : 'failed'), - 'created_at' => date('Y-m-d H:i:s') - ]; + // Registrar la reacción en el mensaje objetivo (no crear un nuevo "reaction" como mensaje saliente) + try { + $update = ['reaction_emoji' => $emoji, 'reaction_to_message_id' => $message_id, 'updated_at' => date('Y-m-d H:i:s')]; + // If send failed, optionally record a status on target (not mandatory) + if (!$resp) $update['reaction_status'] = 'failed'; - // If we got an id from WhatsApp response, save it - if (is_array($resp) && isset($resp['conversations'][0]['id'])) { - $msgData['message_id'] = $resp['conversations'][0]['id']; + $db->update('conversations', $update, 'id = :id', ['id' => $target['id']]); + + // Return updated row for convenience + $updated = $db->query('SELECT * FROM conversations WHERE id = ? LIMIT 1', [$target['id']])->fetch(); + + echo json_encode(['success' => true, 'message' => 'Reacción aplicada', 'updated' => $updated]); + } catch (Exception $e) { + http_response_code(500); + echo json_encode(['success' => false, 'error' => 'No se pudo guardar la reacción: ' . $e->getMessage()]); } - - $db->insert('conversations', $msgData); - - echo json_encode(['success' => true, 'message' => 'Reacción enviada', 'saved' => $msgData]); } catch (Exception $e) { http_response_code(500); echo json_encode(['success' => false, 'error' => $e->getMessage()]); diff --git a/api/webhook.php b/api/webhook.php index e2d2ae8..db916f7 100644 --- a/api/webhook.php +++ b/api/webhook.php @@ -247,9 +247,43 @@ class WhatsAppWebhook { $saveData['reply_to_message_id'] = $message['context']['id']; } - // Añadir campos extra (si se detectaron) - if (isset($extraFields) && is_array($extraFields)) { - $saveData = array_merge($saveData, $extraFields); + // Special handling for reaction messages: apply reaction to referenced message instead of creating separate record + if (isset($extraFields) && is_array($extraFields) && isset($messageType) && $messageType === 'reaction' && !empty($reactionTo)) { + try { + // Try to update the referenced message (by message_id or id) + $updated = $this->db->update( + 'conversations', + ['reaction_emoji' => $emoji, 'reaction_to_message_id' => $reactionTo, 'updated_at' => date('Y-m-d H:i:s')], + 'message_id = :mid OR id = :mid', + ['mid' => $reactionTo] + ); + + // Optionally create a small notification about the reaction + try { + $this->db->insert('notifications', [ + 'user_id' => $user['id'], + 'type' => 'reaction', + 'message' => "Reacción recibida: {$emoji}", + 'data' => json_encode(['message_id' => $reactionTo, 'emoji' => $emoji]), + 'is_read' => 0, + 'created_at' => date('Y-m-d H:i:s') + ]); + } catch (Exception $ne) { + error_log('Failed to create reaction notification: ' . $ne->getMessage()); + } + + // We handled the reaction by updating the target message - skip creating a separate reaction record + continue; + } catch (Exception $e) { + // If update failed, fallback to saving the reaction as a message + error_log('Failed to apply reaction update: ' . $e->getMessage()); + $saveData = array_merge($saveData, $extraFields); + } + } else { + // Añadir campos extra (si se detectaron) + if (isset($extraFields) && is_array($extraFields)) { + $saveData = array_merge($saveData, $extraFields); + } } // Filtrar campos según columnas existentes para evitar errores en esquemas antiguos diff --git a/conversations.php b/conversations.php index 14c792a..ae7ac28 100644 --- a/conversations.php +++ b/conversations.php @@ -805,10 +805,10 @@
- @@ -2746,6 +2746,8 @@ const sendBtn = document.getElementById('send-btn'); // ensure send button is enabled if (sendBtn) sendBtn.disabled = false; + // Update mic/send visibility (hide mic when a file is selected) + if (this._updateSendMicVisibility) this._updateSendMicVisibility(); } formatFileSize(bytes) {