Opt: admin api: ban user or room

pull/31/head
zijiren233 1 year ago
parent b8238d29c3
commit 185ca2c8e7

@ -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
}

@ -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
}

@ -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

@ -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
}

@ -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
}

@ -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

Loading…
Cancel
Save