File size: 1,094 Bytes
ba5de78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package controllers

import (
	"log"
	http_error "whatsapp-backend/models/error"
	"whatsapp-backend/services"
	"whatsapp-backend/utils"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

type SocketController interface {
	HandleWebSocket(ctx *gin.Context)
}

type socketController struct {
	socketService services.SocketService
}

func NewSocketController(socketService services.SocketService) SocketController {
	return &socketController{socketService: socketService}
}

// HandleWebSocket godoc
// @Summary      Connect to WebSocket
// @Description  Upgrade HTTP connection to WebSocket for real-time updates
// @Tags         whatsapp
// @Security     BearerAuth
// @Success      101
// @Router       /whatsapp/socket [get]
func (c *socketController) HandleWebSocket(ctx *gin.Context) {
	userID, exists := ctx.Get("user_id")
	if !exists {
		log.Println("[WebSocket] User ID not found in context")
		utils.SendResponse[any, any](ctx, nil, nil, http_error.UNAUTHORIZED)
		return
	}
	accountID := userID.(uuid.UUID)
	c.socketService.HandleConnection(ctx.Writer, ctx.Request, accountID)
}