2025-12-03 19:29:37 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
2025-12-06 11:53:15 +00:00
|
|
|
"context"
|
2025-12-03 19:29:37 +00:00
|
|
|
|
|
|
|
|
"veza-backend-api/internal/services"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-06 11:53:15 +00:00
|
|
|
// RoomServiceInterface defines the interface for room service operations
|
|
|
|
|
type RoomServiceInterface interface {
|
|
|
|
|
CreateRoom(ctx context.Context, userID uuid.UUID, req services.CreateRoomRequest) (*services.RoomResponse, error)
|
|
|
|
|
GetUserRooms(ctx context.Context, userID uuid.UUID) ([]*services.RoomResponse, error)
|
|
|
|
|
GetRoom(ctx context.Context, roomID uuid.UUID) (*services.RoomResponse, error)
|
|
|
|
|
AddMember(ctx context.Context, roomID, userID uuid.UUID) error
|
|
|
|
|
GetRoomHistory(ctx context.Context, roomID uuid.UUID, limit, offset int) ([]services.ChatMessageResponse, error)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 19:29:37 +00:00
|
|
|
// RoomHandler gère les opérations sur les rooms (conversations)
|
|
|
|
|
type RoomHandler struct {
|
2025-12-06 11:53:15 +00:00
|
|
|
roomService RoomServiceInterface
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
|
commonHandler *CommonHandler
|
2025-12-03 19:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewRoomHandler crée une nouvelle instance de RoomHandler
|
2025-12-06 11:53:15 +00:00
|
|
|
func NewRoomHandler(roomService RoomServiceInterface, logger *zap.Logger) *RoomHandler {
|
2025-12-03 19:29:37 +00:00
|
|
|
return &RoomHandler{
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
roomService: roomService,
|
|
|
|
|
logger: logger,
|
|
|
|
|
commonHandler: NewCommonHandler(logger),
|
2025-12-03 19:29:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateRoom gère la création d'une nouvelle room
|
|
|
|
|
// POST /api/v1/conversations
|
|
|
|
|
func (h *RoomHandler) CreateRoom(c *gin.Context) {
|
|
|
|
|
// Récupérer l'ID utilisateur du contexte
|
|
|
|
|
userIDInterface, exists := c.Get("user_id")
|
|
|
|
|
if !exists {
|
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "User not authenticated"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convertir userID en uuid.UUID
|
|
|
|
|
userID, ok := userIDInterface.(uuid.UUID)
|
|
|
|
|
if !ok {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid user ID type in context"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parser la requête
|
|
|
|
|
var req services.CreateRoomRequest
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
if appErr := h.commonHandler.BindAndValidateJSON(c, &req); appErr != nil {
|
|
|
|
|
RespondWithAppError(c, appErr)
|
2025-12-03 19:29:37 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Valider le type de room si non spécifié
|
|
|
|
|
if req.Type == "" {
|
|
|
|
|
req.Type = "public"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Créer la room
|
|
|
|
|
room, err := h.roomService.CreateRoom(c.Request.Context(), userID, req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Error("failed to create room",
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
zap.String("user_id", userID.String()),
|
|
|
|
|
zap.String("room_name", req.Name))
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create conversation"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.logger.Info("room created successfully",
|
|
|
|
|
zap.String("room_id", room.ID.String()),
|
|
|
|
|
zap.String("user_id", userID.String()),
|
|
|
|
|
zap.String("room_name", req.Name))
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusCreated, room)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetUserRooms récupère toutes les rooms d'un utilisateur
|
|
|
|
|
// GET /api/v1/conversations
|
|
|
|
|
func (h *RoomHandler) GetUserRooms(c *gin.Context) {
|
|
|
|
|
// Récupérer l'ID utilisateur du contexte
|
|
|
|
|
userIDInterface, exists := c.Get("user_id")
|
|
|
|
|
if !exists {
|
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "User not authenticated"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convertir userID en uuid.UUID
|
|
|
|
|
userID, ok := userIDInterface.(uuid.UUID)
|
|
|
|
|
if !ok {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid user ID type in context"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Récupérer les rooms
|
|
|
|
|
rooms, err := h.roomService.GetUserRooms(c.Request.Context(), userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Error("failed to get user rooms",
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
zap.String("user_id", userID.String()))
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch conversations"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"conversations": rooms,
|
|
|
|
|
"total": len(rooms),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetRoom récupère une room par son ID
|
|
|
|
|
// GET /api/v1/conversations/:id
|
|
|
|
|
func (h *RoomHandler) GetRoom(c *gin.Context) {
|
|
|
|
|
// Récupérer l'ID de la room depuis l'URL
|
|
|
|
|
roomIDStr := c.Param("id")
|
|
|
|
|
roomID, err := uuid.Parse(roomIDStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid room ID"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Récupérer la room
|
|
|
|
|
room, err := h.roomService.GetRoom(c.Request.Context(), roomID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Error("failed to get room",
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
zap.String("room_id", roomID.String()))
|
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Conversation not found"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, room)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddMemberRequest représente une requête pour ajouter un membre à une room
|
|
|
|
|
type AddMemberRequest struct {
|
|
|
|
|
UserID uuid.UUID `json:"user_id" binding:"required"` // Changed to UUID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddMember ajoute un membre à une room
|
|
|
|
|
// POST /api/v1/conversations/:id/members
|
|
|
|
|
func (h *RoomHandler) AddMember(c *gin.Context) {
|
|
|
|
|
// Récupérer l'ID de la room depuis l'URL
|
|
|
|
|
roomIDStr := c.Param("id")
|
|
|
|
|
roomID, err := uuid.Parse(roomIDStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid room ID"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parser la requête
|
|
|
|
|
var req AddMemberRequest
|
P0: stabilisation backend/chat/stream + nouvelle base migrations v1
Backend Go:
- Remplacement complet des anciennes migrations par la base V1 alignée sur ORIGIN.
- Durcissement global du parsing JSON (BindAndValidateJSON + RespondWithAppError).
- Sécurisation de config.go, CORS, statuts de santé et monitoring.
- Implémentation des transactions P0 (RBAC, duplication de playlists, social toggles).
- Ajout d’un job worker structuré (emails, analytics, thumbnails) + tests associés.
- Nouvelle doc backend : AUDIT_CONFIG, BACKEND_CONFIG, AUTH_PASSWORD_RESET, JOB_WORKER_*.
Chat server (Rust):
- Refonte du pipeline JWT + sécurité, audit et rate limiting avancé.
- Implémentation complète du cycle de message (read receipts, delivered, edit/delete, typing).
- Nettoyage des panics, gestion d’erreurs robuste, logs structurés.
- Migrations chat alignées sur le schéma UUID et nouvelles features.
Stream server (Rust):
- Refonte du moteur de streaming (encoding pipeline + HLS) et des modules core.
- Transactions P0 pour les jobs et segments, garanties d’atomicité.
- Documentation détaillée de la pipeline (AUDIT_STREAM_*, DESIGN_STREAM_PIPELINE, TRANSACTIONS_P0_IMPLEMENTATION).
Documentation & audits:
- TRIAGE.md et AUDIT_STABILITY.md à jour avec l’état réel des 3 services.
- Cartographie complète des migrations et des transactions (DB_MIGRATIONS_*, DB_TRANSACTION_PLAN, AUDIT_DB_TRANSACTIONS, TRANSACTION_TESTS_PHASE3).
- Scripts de reset et de cleanup pour la lab DB et la V1.
Ce commit fige l’ensemble du travail de stabilisation P0 (UUID, backend, chat et stream) avant les phases suivantes (Coherence Guardian, WS hardening, etc.).
2025-12-06 10:14:38 +00:00
|
|
|
if appErr := h.commonHandler.BindAndValidateJSON(c, &req); appErr != nil {
|
|
|
|
|
RespondWithAppError(c, appErr)
|
2025-12-03 19:29:37 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ajouter le membre
|
|
|
|
|
if err := h.roomService.AddMember(c.Request.Context(), roomID, req.UserID); err != nil {
|
|
|
|
|
h.logger.Error("failed to add member to room",
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
zap.String("room_id", roomID.String()),
|
|
|
|
|
zap.String("user_id", req.UserID.String()))
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to add member"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.logger.Info("member added to room",
|
|
|
|
|
zap.String("room_id", roomID.String()),
|
|
|
|
|
zap.String("user_id", req.UserID.String()))
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Member added successfully"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetRoomHistory récupère l'historique des messages d'une room
|
|
|
|
|
// GET /api/v1/conversations/:id/history
|
|
|
|
|
func (h *RoomHandler) GetRoomHistory(c *gin.Context) {
|
|
|
|
|
conversationIDStr := c.Param("id")
|
|
|
|
|
conversationID, err := uuid.Parse(conversationIDStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid conversation ID"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
limit := c.DefaultQuery("limit", "50")
|
|
|
|
|
offset := c.DefaultQuery("offset", "0")
|
|
|
|
|
|
|
|
|
|
limitInt, err := strconv.Atoi(limit)
|
|
|
|
|
if err != nil || limitInt <= 0 {
|
|
|
|
|
limitInt = 50
|
|
|
|
|
}
|
|
|
|
|
offsetInt, err := strconv.Atoi(offset)
|
|
|
|
|
if err != nil || offsetInt < 0 {
|
|
|
|
|
offsetInt = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
messages, err := h.roomService.GetRoomHistory(c.Request.Context(), conversationID, limitInt, offsetInt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.logger.Error("failed to get room history",
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
zap.String("conversation_id", conversationID.String()))
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get conversation history"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"messages": messages})
|
|
|
|
|
}
|