Chat API (Real-Time Messaging)

Controlador: app/Http/Controllers/Api/ChatApiController.php (286 líneas)

Usa Pusher para mensajería en tiempo real, con modelos ChMessage y ChFavorite (UUIDs).


GET /api/chat/contacts (protegida)

Lista de contactos del usuario con último mensaje y conteo de no leídos.

Response:

{
  "success": true,
  "data": [
    {
      "id": 5,
      "name": "María López",
      "username": "marialop",
      "imagen_url": "https://dominio.com/perfiles/uuid.jpg",
      "is_online": true,
      "last_seen": "hace 2 minutos",
      "last_message": {
        "body": "¡Hola! ¿Cómo estás?",
        "created_at": "2025-06-20T...",
        "attachment": null
      },
      "unread_count": 3
    }
  ]
}

Flujo: Obtiene todos los usuarios con los que ha intercambiado mensajes (from_id o to_id en ch_messages), incluye último mensaje y cuenta mensajes no vistos (seen = 0).


POST /api/chat/messages (protegida)

Obtener historial de mensajes con un contacto.

Request:

{
  "contact_id": 5
}

Response:

{
  "success": true,
  "data": [
    {
      "id": "uuid-xxx",
      "from_id": 1,
      "to_id": 5,
      "body": "¡Hola!",
      "attachment": null,
      "seen": true,
      "created_at": "2025-06-20T..."
    },
    {
      "id": "uuid-yyy",
      "from_id": 5,
      "to_id": 1,
      "body": null,
      "attachment": {
        "new_name": "attachment_uuid.jpg",
        "old_name": "foto.jpg",
        "type": "image"
      },
      "attachment_url": "/storage/attachments/attachment_uuid.jpg",
      "seen": true,
      "created_at": "2025-06-20T..."
    }
  ]
}

Los adjuntos se guardan como JSON en el campo attachment. Las URLs se construyen con ruta relativa /storage/attachments/.


POST /api/chat/send (protegida)

Enviar un mensaje (con o sin adjunto).