Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

채팅방 내 Plain Text 메시지 발송 시, 저장 로직을 추가합니다. #100

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ linters-settings:
# Checks the number of lines in a function.
# If lower than 0, disable the check.
# Default: 60
lines: 150
lines: 180
# Checks the number of statements in a function.
# If lower than 0, disable the check.
# Default: 40
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func NewRouter(app *firebaseinfra.FirebaseApp) (*echo.Echo, error) {
}

upgrader := wschat.NewDefaultUpgrader()
wsServerV2 := wschat.NewWSServer(upgrader, authService, *mediaService)
wsServerV2 := wschat.NewWSServer(upgrader, authService, *mediaService, *chatService)

go wsServerV2.LoopOverClientMessages()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/pet-sitter/pets-next-door-api

go 1.23
go 1.23.0

require (
firebase.google.com/go v3.13.0+incompatible
Expand Down
13 changes: 13 additions & 0 deletions internal/domain/chat/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,16 @@ func ToUserChatRoomMessageView(
) *MessageCursorView {
return createMessageCursorView(row, hasNext, hasPrev, nextMessageID, prevMessageID)
}

func ToChatRoomMessage(
row databasegen.SaveChatMessageRow,
) *Message {
return &Message{
ID: row.ID,
UserID: row.UserID,
RoomID: row.RoomID,
MessageType: row.MessageType,
Content: row.Content,
CreatedAt: row.CreatedAt,
}
}
49 changes: 49 additions & 0 deletions internal/infra/database/gen/chats.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions internal/service/chat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,39 @@ func (s *ChatService) HasNextMessages(

return hasNext, nil
}

// 채팅 메시지를 저장합니다.
func (s *ChatService) SaveChatMessage(
ctx context.Context, userID, roomID uuid.UUID, messageType, content string,
) (*chat.Message, error) {
chatMessageID, uuidError := uuid.NewV7()
if uuidError != nil {
return nil, pnd.ErrUnknown(fmt.Errorf("failed to generate UUID: %w", uuidError))
}

tx, transactionError := s.conn.BeginTx(ctx)
defer tx.Rollback()

if transactionError != nil {
return nil, transactionError
}

q := databasegen.New(tx)
row, databaseGenError := q.SaveChatMessage(ctx, databasegen.SaveChatMessageParams{
ID: chatMessageID,
UserID: userID,
RoomID: roomID,
MessageType: messageType,
Content: content,
})

if databaseGenError != nil {
return nil, databaseGenError
}

if err := tx.Commit(); err != nil {
return nil, err
}

return chat.ToChatRoomMessage(row), nil
}
25 changes: 25 additions & 0 deletions internal/wschat/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@ type WSServer struct {

authService service.AuthService
mediaService service.MediaService
chatService service.ChatService
}

func NewWSServer(
upgrader websocket.Upgrader,
authService service.AuthService,
mediaService service.MediaService,
chatService service.ChatService,
) *WSServer {
return &WSServer{
clients: make(map[uuid.UUID]WSClient),
broadcast: make(chan MessageRequest),
upgrader: upgrader,
authService: authService,
mediaService: mediaService,
chatService: chatService,
}
}

Expand Down Expand Up @@ -101,6 +104,7 @@ func (s *WSServer) HandleConnections(
}

// Broadcast messages to all clients
// TODO : 각 메시지 별 타입에 따라, 메시지 처리하고 있음 (plain, media) 저장로직 추가해야 함
func (s *WSServer) LoopOverClientMessages() {
log.Info().Msg("Looping over client messages")
ctx := context.Background()
Expand All @@ -121,6 +125,27 @@ func (s *WSServer) LoopOverClientMessages() {
var msg MessageResponse
switch msgReq.MessageType {
case "plain":
saveChatMessage, err := s.chatService.SaveChatMessage(
ctx,
msgReq.Sender.ID,
msgReq.Room.ID,
msgReq.MessageType,
msgReq.Message,
)
if err != nil {
log.Error().Err(err).Msg("Failed to save chat message")
msg = NewErrorMessageResponse(
msgReq.MessageID,
msgReq.Sender,
msgReq.Room,
"Failed to save chat message",
time.Now(),
)

break
}

log.Info().Msg("Save chat message: " + saveChatMessage.ID.String())
msg = NewPlainMessageResponse(
msgReq.MessageID,
msgReq.Sender,
Expand Down
11 changes: 11 additions & 0 deletions queries/chats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,14 @@ WHERE chat_messages.deleted_at IS NULL
AND room_id = $2
ORDER BY chat_messages.created_at DESC
LIMIT $1;

-- name: SaveChatMessage :one
INSERT INTO chat_messages
(id,
user_id,
room_id,
message_type,
content,
created_at)
VALUES ($1, $2, $3, $4, $5, NOW())
RETURNING id, user_id, room_id, message_type, content, created_at;
Loading