Skip to content

Commit

Permalink
Feat: user max room count
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Nov 12, 2023
1 parent 465c45c commit 5de12cf
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 64 deletions.
40 changes: 27 additions & 13 deletions internal/db/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,41 @@ func WithStatus(status model.RoomStatus) CreateRoomConfig {
}
}

func CreateRoom(name, password string, conf ...CreateRoomConfig) (*model.Room, error) {
var hashedPassword []byte
// if maxCount is 0, it will be ignored
func CreateRoom(name, password string, maxCount int64, conf ...CreateRoomConfig) (*model.Room, error) {
r := &model.Room{
Name: name,
}
for _, c := range conf {
c(r)
}
if password != "" {
var err error
hashedPassword, err = bcrypt.GenerateFromPassword(stream.StringToBytes(password), bcrypt.DefaultCost)
r.HashedPassword, err = bcrypt.GenerateFromPassword(stream.StringToBytes(password), bcrypt.DefaultCost)
if err != nil {
return nil, err
}
}
r := &model.Room{
Name: name,
HashedPassword: hashedPassword,
}
for _, c := range conf {
c(r)

tx := db.Begin()
if maxCount != 0 {
var count int64
tx.Model(&model.Room{}).Where("creator_id = ?", r.CreatorID).Count(&count)
if count >= maxCount {
tx.Rollback()
return nil, errors.New("room count is over limit")
}
}
err := db.Create(r).Error
if err != nil && errors.Is(err, gorm.ErrDuplicatedKey) {
return r, errors.New("room already exists")
err := tx.Create(r).Error
if err != nil {
tx.Rollback()
if errors.Is(err, gorm.ErrDuplicatedKey) {
return r, errors.New("room already exists")
}
return r, err
}
return r, err
tx.Commit()
return r, nil
}

func GetRoomByID(id string) (*model.Room, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/op/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

var roomCache *synccache.SyncCache[string, *Room]

func CreateRoom(name, password string, conf ...db.CreateRoomConfig) (*Room, error) {
r, err := db.CreateRoom(name, password, conf...)
func CreateRoom(name, password string, maxCount int64, conf ...db.CreateRoomConfig) (*Room, error) {
r, err := db.CreateRoom(name, password, maxCount, conf...)
if err != nil {
return nil, err
}
Expand Down
10 changes: 8 additions & 2 deletions internal/op/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type User struct {
model.User
}

func (u *User) CreateRoom(name, password string, conf ...db.CreateRoomConfig) (*model.Room, error) {
func (u *User) CreateRoom(name, password string, conf ...db.CreateRoomConfig) (*Room, error) {
if u.IsBanned() {
return nil, errors.New("user banned")
}
Expand All @@ -28,7 +28,13 @@ func (u *User) CreateRoom(name, password string, conf ...db.CreateRoomConfig) (*
conf = append(conf, db.WithStatus(model.RoomStatusActive))
}
}
return db.CreateRoom(name, password, append(conf, db.WithCreator(&u.User))...)

var maxCount int64
if !u.IsAdmin() {
maxCount = settings.UserMaxRoomCount.Get()
}

return CreateRoom(name, password, maxCount, append(conf, db.WithCreator(&u.User))...)
}

func (u *User) NewMovie(movie *model.BaseMovie) *model.Movie {
Expand Down
5 changes: 3 additions & 2 deletions internal/settings/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ type Int64 struct {
func NewInt64(name string, value int64, group model.SettingGroup) *Int64 {
i := &Int64{
setting: setting{
name: name,
group: group,
name: name,
group: group,
settingType: model.SettingTypeInt64,
},
defaultValue: value,
value: value,
Expand Down
53 changes: 11 additions & 42 deletions internal/settings/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package settings
import (
"fmt"

json "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
"github.com/synctv-org/synctv/internal/db"
"github.com/synctv-org/synctv/internal/model"
Expand Down Expand Up @@ -34,49 +35,17 @@ func SetValue(name string, value any) error {
}

func SetSettingValue(s Setting, value any) error {
switch s.Type() {
case model.SettingTypeBool:
i, ok := s.(BoolSetting)
if !ok {
log.Fatalf("setting %s is not bool", s.Name())
}
v, ok := value.(bool)
if !ok {
return fmt.Errorf("setting %s, value %v is not bool", s.Name(), value)
}
i.Set(v)
case model.SettingTypeInt64:
i, ok := s.(Int64Setting)
if !ok {
log.Fatalf("setting %s is not int64", s.Name())
}
v, ok := value.(int64)
if !ok {
return fmt.Errorf("setting %s, value %v is not int64", s.Name(), value)
}
i.Set(v)
case model.SettingTypeFloat64:
i, ok := s.(Float64Setting)
if !ok {
log.Fatalf("setting %s is not float64", s.Name())
}
v, ok := value.(float64)
if !ok {
return fmt.Errorf("setting %s, value %v is not float64", s.Name(), value)
}
i.Set(v)
case model.SettingTypeString:
i, ok := s.(StringSetting)
if !ok {
log.Fatalf("setting %s is not string", s.Name())
}
v, ok := value.(string)
if !ok {
return fmt.Errorf("setting %s, value %v is not string", s.Name(), value)
}
i.Set(v)
switch s := s.(type) {
case BoolSetting:
return s.Set(json.Wrap(value).ToBool())
case Int64Setting:
return s.Set(json.Wrap(value).ToInt64())
case Float64Setting:
return s.Set(json.Wrap(value).ToFloat64())
case StringSetting:
return s.Set(json.Wrap(value).ToString())
default:
log.Fatalf("unknown setting type: %s", s.Type())
log.Fatalf("unknown setting %s type: %s", s.Name(), s.Type())
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/settings/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
RoomMustNeedPwd = newBoolSetting("room_must_need_pwd", false, model.SettingGroupRoom)
CreateRoomNeedReview = newBoolSetting("create_room_need_review", false, model.SettingGroupRoom)
RoomTTL = newInt64Setting("room_ttl", int64(time.Hour*48), model.SettingGroupRoom)
UserMaxRoomCount = newInt64Setting("user_max_room_count", 3, model.SettingGroupRoom)
)

var (
Expand Down
4 changes: 1 addition & 3 deletions server/handlers/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ func CreateRoom(ctx *gin.Context) {
return
}

r, err := user.CreateRoom(req.RoomName, req.Password, db.WithSetting(req.Setting))
room, err := user.CreateRoom(req.RoomName, req.Password, db.WithSetting(req.Setting))
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}

room, _ := op.LoadOrInitRoomByID(r.ID)

token, err := middlewares.NewAuthRoomToken(user, room)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
Expand Down

0 comments on commit 5de12cf

Please sign in to comment.