Feat: roomlist & join room

This commit is contained in:
Grail Finder
2025-05-09 14:39:33 +03:00
parent 45761446e5
commit 6c9c86f02b
10 changed files with 141 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import (
"errors"
"golias/models"
"golias/utils"
"strings"
)
func createRoom(ctx context.Context, req *models.RoomReq) (*models.Room, error) {
@ -22,11 +23,13 @@ func createRoom(ctx context.Context, req *models.RoomReq) (*models.Room, error)
}
func saveRoom(room *models.Room) error {
key := models.CacheRoomPrefix + room.ID
data, err := json.Marshal(room)
if err != nil {
return err
}
memcache.Set(models.CacheRoomPrefix+room.ID, data)
log.Debug("saved room", "room", room, "key", key)
return nil
}
@ -208,3 +211,24 @@ func joinTeam(ctx context.Context, role, team string) (*models.FullInfo, error)
}
return fi, nil
}
// get all rooms
func listPublicRooms() []*models.Room {
cacheMap := memcache.GetAll()
publicRooms := []*models.Room{}
// no way to know if room is public until unmarshal -_-;
for key, value := range cacheMap {
if strings.HasPrefix(key, models.CacheRoomPrefix) {
room := &models.Room{}
if err := json.Unmarshal(value, &room); err != nil {
log.Warn("failed to unmarshal room", "error", err)
continue
}
log.Debug("consider room for list", "room", room, "key", key)
if room.IsPublic {
publicRooms = append(publicRooms, room)
}
}
}
return publicRooms
}