Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/pkg/websocket/hub.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package websocket

import (
"github.com/Parchat/backend/internal/config"
"github.com/Parchat/backend/internal/repositories"
)

Expand Down Expand Up @@ -32,13 +33,17 @@ type Hub struct {
messageRepo *repositories.MessageRepository
roomRepo *repositories.RoomRepository
directChatRepo *repositories.DirectChatRepository

// Firestore client
firestoreClient *config.FirestoreClient
}

// NewHub inicializa un nuevo Hub
func NewHub(
messageRepo *repositories.MessageRepository,
roomRepo *repositories.RoomRepository,
directChatRepo *repositories.DirectChatRepository,
client *config.FirestoreClient,
) *Hub {
return &Hub{
clients: make(map[*Client]bool),
Expand All @@ -49,6 +54,7 @@ func NewHub(
messageRepo: messageRepo,
roomRepo: roomRepo,
directChatRepo: directChatRepo,
firestoreClient: client,
}
}

Expand Down
21 changes: 21 additions & 0 deletions internal/pkg/websocket/websocket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package websocket

import (
"context"
"encoding/json"
"log"
"time"
Expand Down Expand Up @@ -143,6 +144,16 @@ func (c *Client) ReadPump() {
log.Printf("Error updating last message: %v", err)
}

// Obtener el displayName del usuario
ctx := context.Background()
userDoc, err := c.hub.firestoreClient.Client.Collection("users").Doc(c.userID).Get(ctx)
if err == nil {
var user models.User
if err := userDoc.DataTo(&user); err == nil {
chatMsg.DisplayName = user.DisplayName
}
}

// Convertir el mensaje de vuelta a JSON para difundir
payload, _ := json.Marshal(chatMsg)
wsMessage.Payload = payload
Expand Down Expand Up @@ -200,6 +211,16 @@ func (c *Client) ReadPump() {
log.Printf("Error updating last message in direct chat: %v", err)
}

// Obtener el displayName del usuario
ctx := context.Background()
userDoc, err := c.hub.firestoreClient.Client.Collection("users").Doc(c.userID).Get(ctx)
if err == nil {
var user models.User
if err := userDoc.DataTo(&user); err == nil {
chatMsg.DisplayName = user.DisplayName
}
}

// Convertir el mensaje de vuelta a JSON para difundir
payload, _ := json.Marshal(chatMsg)
wsMessage.Payload = payload
Expand Down