diff --git a/internal/op/rooms.go b/internal/op/rooms.go index 94613da..84c79bd 100644 --- a/internal/op/rooms.go +++ b/internal/op/rooms.go @@ -83,6 +83,14 @@ func LoadOrInitRoomByID(id uint) (*Room, error) { return r, nil } +func ClientNum(roomID uint) int64 { + r, ok := roomCache.Load(roomID) + if ok { + return r.ClientNum() + } + return 0 +} + func HasRoom(roomID uint) bool { _, ok := roomCache.Load(roomID) if ok { diff --git a/server/handlers/room.go b/server/handlers/room.go index da77b6a..4722b74 100644 --- a/server/handlers/room.go +++ b/server/handlers/room.go @@ -129,19 +129,11 @@ func RoomList(ctx *gin.Context) { } func genRoomsResp(resp []*model.RoomListResp, scopes ...func(db *gorm.DB) *gorm.DB) []*model.RoomListResp { - var clientNum int64 for _, r := range db.GetAllRooms(scopes...) { - room, err := op.LoadRoomByID(r.ID) - if err != nil { - clientNum = 0 - } else { - clientNum = room.ClientNum() - } - resp = append(resp, &model.RoomListResp{ RoomId: r.ID, RoomName: r.Name, - PeopleNum: clientNum, + PeopleNum: op.ClientNum(r.ID), NeedPassword: len(r.HashedPassword) != 0, Creator: op.GetUserName(r.CreatorID), CreatedAt: r.CreatedAt.UnixMilli(), @@ -163,15 +155,8 @@ func CheckRoom(ctx *gin.Context) { return } - var peopleNum int64 - - room, err := op.LoadRoomByID(r.ID) - if err == nil { - peopleNum = room.ClientNum() - } - ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{ - "peopleNum": peopleNum, + "peopleNum": op.ClientNum(r.ID), "needPassword": r.NeedPassword(), })) }