Feat: create room

This commit is contained in:
Grail Finder
2025-05-09 11:24:33 +03:00
parent e20118acea
commit 2b4bf2ec29
6 changed files with 39 additions and 18 deletions

View File

@ -75,6 +75,10 @@ func saveFullInfo(fi *models.FullInfo) error {
if err := saveState(fi.State.Username, fi.State); err != nil {
return err
}
// can room be nil?
// if fi.Room == nil {
// return nil
// }
if err := saveRoom(fi.Room); err != nil {
return err
}
@ -126,19 +130,24 @@ func getAllNames() []string {
return names
}
// can room exists without state? I think no
func getFullInfoByCtx(ctx context.Context) (*models.FullInfo, error) {
resp := &models.FullInfo{}
state, err := getStateByCtx(ctx)
if err != nil {
return nil, err
}
resp.State = state
if state.RoomID == "" {
return resp, nil
}
room, err := getRoomByID(state.RoomID)
if err != nil {
log.Warn("failed to find room despite knowing room_id;",
"room_id", state.RoomID)
return nil, err
}
resp := &models.FullInfo{
State: state,
Room: room,
}
resp.Room = room
return resp, nil
}

View File

@ -72,16 +72,20 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
state := models.MakeTestState(cleanName)
state.State.Username = cleanName
// state := models.MakeTestState(cleanName)
// state.State.Username = cleanName
userstate := models.InitState(cleanName)
fi := &models.FullInfo{
State: userstate,
}
// save state to cache
// if err := saveState(cleanName, state.State); err != nil {
if err := saveFullInfo(state); err != nil {
if err := saveState(cleanName, userstate); err != nil {
// if err := saveFullInfo(fi); err != nil {
log.Error("failed to save state", "error", err)
abortWithError(w, err.Error())
return
}
tmpl.ExecuteTemplate(w, "base", state)
tmpl.ExecuteTemplate(w, "base", fi)
}
func makeCookie(username string, remote string) (*http.Cookie, error) {
@ -152,7 +156,7 @@ func cacheSetSession(key string, session *models.Session) error {
}
func updateRoomInSession(ctx context.Context, roomID string) (context.Context, error) {
s, ok := ctx.Value("session").(models.Session)
s, ok := ctx.Value(models.CtxSessionKey).(*models.Session)
if !ok {
return context.TODO(), errors.New("failed to extract session from ctx")
}

View File

@ -23,8 +23,16 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) {
return
}
ctx := context.WithValue(r.Context(), "current_room", room.ID)
ctx, err = updateRoomInSession(ctx, room.ID)
fi, err := getFullInfoByCtx(ctx)
if err != nil {
msg := "failed to get full info from ctx"
log.Error(msg, "error", err)
abortWithError(w, msg)
return
}
fi.State.RoomID = room.ID
fi.Room = room
if err := saveFullInfo(fi); err != nil {
msg := "failed to set current room to session"
log.Error(msg, "error", err)
abortWithError(w, msg)
@ -41,7 +49,7 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
tmpl.ExecuteTemplate(w, "base", nil)
tmpl.ExecuteTemplate(w, "base", fi)
}
func HandleRoomEnter(w http.ResponseWriter, r *http.Request) {

View File

@ -41,6 +41,7 @@ func LogRequests(next http.Handler) http.Handler {
func GetSession(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO: move
cookieName := "session_token"
sessionCookie, err := r.Cookie(cookieName)
if err != nil {