Opt: get client num

pull/21/head
zijiren233 1 year ago
parent 9eea775185
commit 160a15d7b1

@ -83,6 +83,14 @@ func LoadOrInitRoomByID(id uint) (*Room, error) {
return r, nil 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 { func HasRoom(roomID uint) bool {
_, ok := roomCache.Load(roomID) _, ok := roomCache.Load(roomID)
if ok { if ok {

@ -129,19 +129,11 @@ func RoomList(ctx *gin.Context) {
} }
func genRoomsResp(resp []*model.RoomListResp, scopes ...func(db *gorm.DB) *gorm.DB) []*model.RoomListResp { func genRoomsResp(resp []*model.RoomListResp, scopes ...func(db *gorm.DB) *gorm.DB) []*model.RoomListResp {
var clientNum int64
for _, r := range db.GetAllRooms(scopes...) { 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{ resp = append(resp, &model.RoomListResp{
RoomId: r.ID, RoomId: r.ID,
RoomName: r.Name, RoomName: r.Name,
PeopleNum: clientNum, PeopleNum: op.ClientNum(r.ID),
NeedPassword: len(r.HashedPassword) != 0, NeedPassword: len(r.HashedPassword) != 0,
Creator: op.GetUserName(r.CreatorID), Creator: op.GetUserName(r.CreatorID),
CreatedAt: r.CreatedAt.UnixMilli(), CreatedAt: r.CreatedAt.UnixMilli(),
@ -163,15 +155,8 @@ func CheckRoom(ctx *gin.Context) {
return return
} }
var peopleNum int64
room, err := op.LoadRoomByID(r.ID)
if err == nil {
peopleNum = room.ClientNum()
}
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{ ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
"peopleNum": peopleNum, "peopleNum": op.ClientNum(r.ID),
"needPassword": r.NeedPassword(), "needPassword": r.NeedPassword(),
})) }))
} }

Loading…
Cancel
Save