refactor: integrate chat persistence into Room
Move SaveMessage/LoadMessages logic into Room as private methods. NewPersistentRoom auto-loads history and auto-saves on Send, removing the need for handlers to coordinate persistence separately.
This commit is contained in:
@@ -85,9 +85,9 @@ func HandleGamePage(store *connect4.Store, sm *scs.SessionManager, queries *repo
|
||||
}
|
||||
|
||||
g := gi.GetGame()
|
||||
msgs := chat.LoadMessages(queries, gameID)
|
||||
room := chat.NewPersistentRoom(nil, "", queries, gameID)
|
||||
|
||||
if err := pages.GamePage(g, myColor, msgs, c4ChatConfig(gameID)).Render(r.Context(), w); err != nil {
|
||||
if err := pages.GamePage(g, myColor, room.Messages(), c4ChatConfig(gameID)).Render(r.Context(), w); err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
@@ -111,7 +111,7 @@ func HandleGameEvents(store *connect4.Store, nc *nats.Conn, sm *scs.SessionManag
|
||||
))
|
||||
|
||||
chatCfg := c4ChatConfig(gameID)
|
||||
room := chat.NewRoom(nc, "connect4.chat."+gameID, chat.LoadMessages(queries, gameID))
|
||||
room := chat.NewPersistentRoom(nc, "connect4.chat."+gameID, queries, gameID)
|
||||
|
||||
// Send initial render
|
||||
sendGameComponents(sse, gi, myColor, room, chatCfg)
|
||||
@@ -228,9 +228,7 @@ func HandleSendChat(store *connect4.Store, nc *nats.Conn, sm *scs.SessionManager
|
||||
Message: signals.ChatMsg,
|
||||
Time: time.Now().UnixMilli(),
|
||||
}
|
||||
chat.SaveMessage(queries, gameID, msg)
|
||||
|
||||
room := chat.NewRoom(nc, "connect4.chat."+gameID, nil)
|
||||
room := chat.NewPersistentRoom(nc, "connect4.chat."+gameID, queries, gameID)
|
||||
room.Send(msg)
|
||||
|
||||
sse := datastar.NewSSE(w, r)
|
||||
|
||||
Reference in New Issue
Block a user