Skip to content

Commit

Permalink
Feat: add room must no need password setting
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Apr 19, 2024
1 parent 42d6d92 commit 0f7e0df
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
12 changes: 10 additions & 2 deletions internal/op/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ func (u *User) CreateRoom(name, password string, conf ...db.CreateRoomConfig) (*
if password == "" && settings.RoomMustNeedPwd.Get() {
return nil, errors.New("room must need password")
}
if password != "" && settings.RoomMustNoNeedPwd.Get() {
return nil, errors.New("room must no need password")
}
if settings.CreateRoomNeedReview.Get() {
conf = append(conf, db.WithStatus(model.RoomStatusPending))
} else {
Expand Down Expand Up @@ -233,8 +236,13 @@ func (u *User) SetRoomPassword(room *Room, password string) error {
if !u.HasRoomAdminPermission(room, model.PermissionSetRoomPassword) {
return model.ErrNoPermission
}
if !u.IsAdmin() && password == "" && settings.RoomMustNeedPwd.Get() {
return errors.New("room must need password")
if !u.IsAdmin() {
if password == "" && settings.RoomMustNeedPwd.Get() {
return errors.New("room must need password")
}
if password != "" && settings.RoomMustNoNeedPwd.Get() {
return errors.New("room must no need password")
}
}
return room.SetPassword(password)
}
Expand Down
28 changes: 27 additions & 1 deletion internal/settings/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,38 @@ import (

var (
DisableCreateRoom = NewBoolSetting("disable_create_room", false, model.SettingGroupRoom)
RoomMustNeedPwd = NewBoolSetting("room_must_need_pwd", false, model.SettingGroupRoom)
RoomMustNeedPwd BoolSetting
RoomMustNoNeedPwd BoolSetting
CreateRoomNeedReview = NewBoolSetting("create_room_need_review", false, model.SettingGroupRoom)
// 48 hours
RoomTTL = NewInt64Setting("room_ttl", 48, model.SettingGroupRoom)
)

func init() {
RoomMustNeedPwd = NewBoolSetting(
"room_must_need_pwd",
false,
model.SettingGroupRoom,
WithBeforeSetBool(func(bs BoolSetting, b bool) (bool, error) {
if b && RoomMustNoNeedPwd.Get() {
return false, errors.New("room_must_need_pwd and room_must_no_need_pwd can't be true at the same time")
}
return b, nil
}),
)
RoomMustNoNeedPwd = NewBoolSetting(
"room_must_no_need_pwd",
false,
model.SettingGroupRoom,
WithBeforeSetBool(func(bs BoolSetting, b bool) (bool, error) {
if b && RoomMustNeedPwd.Get() {
return false, errors.New("room_must_need_pwd and room_must_no_need_pwd can't be true at the same time")
}
return b, nil
}),
)
}

var (
DisableUserSignup = NewBoolSetting("disable_user_signup", false, model.SettingGroupUser)
SignupNeedReview = NewBoolSetting("signup_need_review", false, model.SettingGroupUser)
Expand Down

0 comments on commit 0f7e0df

Please sign in to comment.