From 185ca2c8e7caac56725933a70c6098945d1efcb4 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Tue, 7 Nov 2023 11:20:09 +0800 Subject: [PATCH] Opt: admin api: ban user or room --- internal/db/room.go | 13 ++++--------- internal/model/room.go | 8 ++------ internal/op/room.go | 2 +- internal/op/rooms.go | 15 ++++++++++++--- internal/op/users.go | 19 +++++++++++++++++++ server/handlers/admin.go | 36 +++++++----------------------------- 6 files changed, 45 insertions(+), 48 deletions(-) diff --git a/internal/db/room.go b/internal/db/room.go index b8b1e72..2a20d58 100644 --- a/internal/db/room.go +++ b/internal/db/room.go @@ -77,15 +77,6 @@ func GetRoomByID(id string) (*model.Room, error) { return r, err } -func GetRoomAndCreatorByID(id string) (*model.Room, error) { - r := &model.Room{} - err := db.Preload("Creator").Where("id = ?", id).First(r).Error - if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { - return r, errors.New("room not found") - } - return r, err -} - func ChangeRoomSetting(roomID string, setting model.Settings) error { err := db.Model(&model.Room{}).Where("id = ?", roomID).Update("setting", setting).Error if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { @@ -197,3 +188,7 @@ func SetRoomStatus(roomID string, status model.RoomStatus) error { } return err } + +func SetRoomStatusByCreator(userID string, status model.RoomStatus) error { + return db.Model(&model.Room{}).Where("creator_id = ?", userID).Update("status", status).Error +} diff --git a/internal/model/room.go b/internal/model/room.go index 93cd722..82a0f01 100644 --- a/internal/model/room.go +++ b/internal/model/room.go @@ -14,7 +14,6 @@ type RoomStatus string const ( RoomStatusBanned RoomStatus = "banned" RoomStatusPending RoomStatus = "pending" - RoomStatusStopped RoomStatus = "stopped" RoomStatusActive RoomStatus = "active" ) @@ -39,7 +38,8 @@ func (r *Room) BeforeCreate(tx *gorm.DB) error { } type Settings struct { - Hidden bool `json:"hidden"` + Hidden bool `json:"hidden"` + CanPushMovie bool `gorm:"default:true" json:"canPushMovie"` } func (r *Room) NeedPassword() bool { @@ -58,10 +58,6 @@ func (r *Room) IsPending() bool { return r.Status == RoomStatusPending } -func (r *Room) IsStopped() bool { - return r.Status == RoomStatusStopped -} - func (r *Room) IsActive() bool { return r.Status == RoomStatusActive } diff --git a/internal/op/room.go b/internal/op/room.go index 7fd6cf9..0c760a8 100644 --- a/internal/op/room.go +++ b/internal/op/room.go @@ -194,7 +194,7 @@ func (r *Room) SetRoomStatus(status model.RoomStatus) error { } r.Status = status switch status { - case model.RoomStatusBanned, model.RoomStatusStopped, model.RoomStatusPending: + case model.RoomStatusBanned, model.RoomStatusPending: return CompareAndCloseRoom(r) } return nil diff --git a/internal/op/rooms.go b/internal/op/rooms.go index e106189..b1cc681 100644 --- a/internal/op/rooms.go +++ b/internal/op/rooms.go @@ -41,7 +41,6 @@ func InitRoom(room *model.Room) (*Room, error) { var ( ErrRoomPending = errors.New("room pending, please wait for admin to approve") - ErrRoomStopped = errors.New("room stopped") ErrRoomBanned = errors.New("room banned") ) @@ -51,8 +50,6 @@ func LoadOrInitRoom(room *model.Room) (*Room, error) { return nil, ErrRoomBanned case model.RoomStatusPending: return nil, ErrRoomPending - case model.RoomStatusStopped: - return nil, ErrRoomStopped } t := time.Duration(settings.RoomTTL.Get()) i, loaded := roomCache.LoadOrStore(room.ID, &Room{ @@ -214,3 +211,15 @@ func GetRoomHeapInCacheWithoutHidden() []*RoomInfo { }) return rooms.Slice() } + +func SetRoomStatus(roomID string, status model.RoomStatus) error { + err := db.SetRoomStatus(roomID, status) + if err != nil { + return err + } + e, loaded := roomCache.LoadAndDelete(roomID) + if loaded { + e.Value().close() + } + return nil +} diff --git a/internal/op/users.go b/internal/op/users.go index 337885b..a66fc6d 100644 --- a/internal/op/users.go +++ b/internal/op/users.go @@ -121,5 +121,24 @@ func SetRoleByID(userID string, role model.Role) error { return err } userCache.Remove(userID) + + err = db.SetRoomStatusByCreator(userID, model.RoomStatusBanned) + if err != nil { + return err + } + + switch role { + case model.RoleBanned: + roomCache.Range(func(key string, value *synccache.Entry[*Room]) bool { + v := value.Value() + if v.CreatorID == userID { + if roomCache.CompareAndDelete(key, value) { + v.close() + } + } + return true + }) + } + return nil } diff --git a/server/handlers/admin.go b/server/handlers/admin.go index da2a1ad..2abc025 100644 --- a/server/handlers/admin.go +++ b/server/handlers/admin.go @@ -1,7 +1,6 @@ package handlers import ( - "errors" "net/http" "github.com/gin-gonic/gin" @@ -166,17 +165,9 @@ func BanUser(ctx *gin.Context) { return } - u, err := op.GetUserById(req.ID) + u, err := db.GetUserByID(req.ID) if err != nil { - if errors.Is(err, op.ErrUserPending) { - err = db.SetRoleByID(req.ID, dbModel.RoleBanned) - if err != nil { - ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) - return - } - } else { - ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) - } + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) return } @@ -189,7 +180,7 @@ func BanUser(ctx *gin.Context) { return } - err = u.SetRole(dbModel.RoleBanned) + err = op.SetRoleByID(req.ID, dbModel.RoleBanned) if err != nil { ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) return @@ -297,21 +288,13 @@ func BanRoom(ctx *gin.Context) { return } - room, err := op.LoadOrInitRoomByID(req.Id) + r, err := db.GetRoomByID(req.Id) if err != nil { - if errors.Is(err, op.ErrRoomPending) || errors.Is(err, op.ErrRoomStopped) { - err = db.SetRoomStatus(req.Id, dbModel.RoomStatusBanned) - if err != nil { - ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) - return - } - } else { - ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) - } + ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) return } - creator, err := db.GetUserByID(room.CreatorID) + creator, err := db.GetUserByID(r.CreatorID) if err != nil { ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err)) return @@ -327,12 +310,7 @@ func BanRoom(ctx *gin.Context) { return } - if room.IsBanned() { - ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("room is already banned")) - return - } - - err = room.SetRoomStatus(dbModel.RoomStatusBanned) + err = op.SetRoomStatus(req.Id, dbModel.RoomStatusBanned) if err != nil { ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err)) return