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
4 changes: 2 additions & 2 deletions internal/handlers/chat_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (h *ChatHandler) GetUserDirectChats(w http.ResponseWriter, r *http.Request)
return
}

chats, err := h.DirectChatService.GetUserDirectChats(userID)
chats, err := h.DirectChatService.GetUserDirectChatsWithSenderNames(userID)
if err != nil {
http.Error(w, "Error getting direct chats: "+err.Error(), http.StatusInternalServerError)
return
Expand Down Expand Up @@ -401,7 +401,7 @@ func (h *ChatHandler) GetChat(w http.ResponseWriter, r *http.Request) {
return
}

chat, err := h.DirectChatService.GetDirectChat(chatID)
chat, err := h.DirectChatService.GetDirectChatWithSenderName(chatID)
if err != nil {
http.Error(w, "Error getting chat: "+err.Error(), http.StatusInternalServerError)
return
Expand Down
15 changes: 8 additions & 7 deletions internal/models/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import "time"

// Message representa un mensaje enviado por un usuario
type Message struct {
ID string `json:"id" firestore:"id"`
Content string `json:"content" firestore:"content"`
UserID string `json:"userId" firestore:"userId"`
RoomID string `json:"roomId" firestore:"roomId"`
CreatedAt time.Time `json:"createdAt" firestore:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" firestore:"updatedAt"`
IsDeleted bool `json:"isDeleted" firestore:"isDeleted"`
ID string `json:"id" firestore:"id"`
Content string `json:"content" firestore:"content"`
UserID string `json:"userId" firestore:"userId"`
RoomID string `json:"roomId" firestore:"roomId"`
CreatedAt time.Time `json:"createdAt" firestore:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" firestore:"updatedAt"`
IsDeleted bool `json:"isDeleted" firestore:"isDeleted"`
DisplayName string `json:"displayName,omitempty" firestore:"-"` // Excluido de Firestore
}

// MessageResponse es la respuesta que incluye un mensaje y el nombre del usuario que lo envió
Expand Down
91 changes: 91 additions & 0 deletions internal/services/directchat_service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package services

import (
"context"

"github.com/Parchat/backend/internal/models"
"github.com/Parchat/backend/internal/repositories"
)
Expand Down Expand Up @@ -34,6 +36,37 @@ func (s *DirectChatService) GetUserDirectChats(userID string) ([]models.DirectCh
return s.DirectChatRepo.GetUserDirectChats(userID)
}

// GetUserDirectChatsWithSenderNames obtiene todos los chats directos con nombres de remitentes
func (s *DirectChatService) GetUserDirectChatsWithSenderNames(userID string) ([]models.DirectChat, error) {
chats, err := s.DirectChatRepo.GetUserDirectChats(userID)
if err != nil {
return nil, err
}

ctx := context.Background()
client := s.DirectChatRepo.FirestoreClient.Client

// Para cada chat, obtener el nombre del remitente del último mensaje
for i := range chats {
if chats[i].LastMessage != nil && chats[i].LastMessage.UserID != "" {
userDoc, err := client.Collection("users").Doc(chats[i].LastMessage.UserID).Get(ctx)
if err == nil {
var user models.User
if err := userDoc.DataTo(&user); err == nil {
// Crear una copia del mensaje para añadir el displayName
messageCopy := *chats[i].LastMessage
// Agregar el displayName a la estructura Message (se serializa como JSON aunque no esté en la estructura)
messageCopy.DisplayName = user.DisplayName
// Reemplazar el mensaje original con la copia que incluye displayName
chats[i].LastMessage = &messageCopy
}
}
}
}

return chats, nil
}

// GetDirectChatMessages obtiene los mensajes de un chat directo
func (s *DirectChatService) GetDirectChatMessages(directChatID string, limit int) ([]models.MessageResponse, error) {
return s.MessageRepo.GetDirectChatMessages(directChatID, limit)
Expand All @@ -43,3 +76,61 @@ func (s *DirectChatService) GetDirectChatMessages(directChatID string, limit int
func (s *DirectChatService) FindOrCreateDirectChat(userID1, userID2 string) (*models.DirectChat, error) {
return s.DirectChatRepo.FindOrCreateDirectChat(userID1, userID2)
}

// FindOrCreateDirectChatWithSenderName encuentra o crea un chat directo e incluye el nombre del remitente
func (s *DirectChatService) FindOrCreateDirectChatWithSenderName(userID1, userID2 string) (*models.DirectChat, error) {
chat, err := s.DirectChatRepo.FindOrCreateDirectChat(userID1, userID2)
if err != nil {
return nil, err
}

// Añadir el displayName al último mensaje si existe
if chat.LastMessage != nil && chat.LastMessage.UserID != "" {
ctx := context.Background()
client := s.DirectChatRepo.FirestoreClient.Client

userDoc, err := client.Collection("users").Doc(chat.LastMessage.UserID).Get(ctx)
if err == nil {
var user models.User
if err := userDoc.DataTo(&user); err == nil {
// Crear una copia del mensaje para añadir el displayName
messageCopy := *chat.LastMessage
// Agregar el displayName a la estructura Message
messageCopy.DisplayName = user.DisplayName
// Reemplazar el mensaje original con la copia
chat.LastMessage = &messageCopy
}
}
}

return chat, nil
}

// GetDirectChatWithSenderName obtiene un chat directo por su ID e incluye el nombre del remitente del último mensaje
func (s *DirectChatService) GetDirectChatWithSenderName(directChatID string) (*models.DirectChat, error) {
chat, err := s.DirectChatRepo.GetDirectChat(directChatID)
if err != nil {
return nil, err
}

// Añadir el displayName al último mensaje si existe
if chat.LastMessage != nil && chat.LastMessage.UserID != "" {
ctx := context.Background()
client := s.DirectChatRepo.FirestoreClient.Client

userDoc, err := client.Collection("users").Doc(chat.LastMessage.UserID).Get(ctx)
if err == nil {
var user models.User
if err := userDoc.DataTo(&user); err == nil {
// Crear una copia del mensaje para añadir el displayName
messageCopy := *chat.LastMessage
// Agregar el displayName a la estructura Message
messageCopy.DisplayName = user.DisplayName
// Reemplazar el mensaje original con la copia
chat.LastMessage = &messageCopy
}
}
}

return chat, nil
}