You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
synctv/server/handlers/admin.go

321 lines
8.5 KiB
Go

1 year ago
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/synctv-org/synctv/internal/db"
dbModel "github.com/synctv-org/synctv/internal/model"
"github.com/synctv-org/synctv/internal/op"
"github.com/synctv-org/synctv/internal/settings"
"github.com/synctv-org/synctv/server/model"
"gorm.io/gorm"
)
1 year ago
func EditAdminSettings(ctx *gin.Context) {
// user := ctx.MustGet("user").(*op.User)
req := model.AdminSettingsReq{}
if err := model.Decode(ctx, &req); err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
1 year ago
for k, v := range req {
err := settings.SetValue(k, v)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
}
ctx.Status(http.StatusNoContent)
1 year ago
}
func AdminSettings(ctx *gin.Context) {
// user := ctx.MustGet("user").(*op.User)
group := ctx.Param("group")
if group == "" {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("group is required"))
return
}
s, ok := settings.GroupSettings[dbModel.SettingGroup(group)]
if !ok {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("group not found"))
return
}
resp := make(gin.H, len(s))
for _, v := range s {
resp[v.Name()] = v.Interface()
}
ctx.JSON(http.StatusOK, model.NewApiDataResp(resp))
}
func Users(ctx *gin.Context) {
// user := ctx.MustGet("user").(*op.User)
page, pageSize, err := GetPageAndPageSize(ctx)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
var desc = ctx.DefaultQuery("sort", "desc") == "desc"
scopes := []func(db *gorm.DB) *gorm.DB{}
switch ctx.DefaultQuery("role", "user") {
case "admin":
scopes = append(scopes, db.WhereRole(dbModel.RoleAdmin))
case "user":
scopes = append(scopes, db.WhereRole(dbModel.RoleUser))
case "pending":
scopes = append(scopes, db.WhereRole(dbModel.RolePending))
case "banned":
scopes = append(scopes, db.WhereRole(dbModel.RoleBanned))
default:
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support role"))
return
}
switch ctx.DefaultQuery("order", "name") {
case "createdAt":
if desc {
scopes = append(scopes, db.OrderByCreatedAtDesc)
} else {
scopes = append(scopes, db.OrderByCreatedAtAsc)
}
case "name":
if desc {
scopes = append(scopes, db.OrderByDesc("username"))
} else {
scopes = append(scopes, db.OrderByAsc("username"))
}
default:
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support order"))
return
}
if keyword := ctx.Query("keyword"); keyword != "" {
// search mode, all, name, id
switch ctx.DefaultQuery("search", "all") {
case "all":
scopes = append(scopes, db.WhereUsernameLikeOrIDIn(keyword, db.GerUsersIDByIDLike(keyword)))
case "name":
scopes = append(scopes, db.WhereUsernameLike(keyword))
case "id":
scopes = append(scopes, db.WhereIDIn(db.GerUsersIDByIDLike(keyword)))
}
}
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
"total": db.GetAllUserCount(scopes...),
"list": genUserListResp(append(scopes, db.Paginate(page, pageSize))...),
}))
}
func genUserListResp(scopes ...func(db *gorm.DB) *gorm.DB) []*model.UserInfoResp {
us := db.GetAllUsers(scopes...)
resp := make([]*model.UserInfoResp, len(us))
for i, v := range us {
resp[i] = &model.UserInfoResp{
ID: v.ID,
Username: v.Username,
Role: v.Role,
CreatedAt: v.CreatedAt.UnixMilli(),
}
}
return resp
}
func ApprovePendingUser(ctx *gin.Context) {
req := model.UserIDReq{}
if err := model.Decode(ctx, &req); err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
user, err := db.GetUserByID(req.ID)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
if !user.IsPending() {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("user is not pending"))
return
}
err = db.SetRoleByID(req.ID, dbModel.RoleUser)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
return
}
ctx.Status(http.StatusNoContent)
}
func BanUser(ctx *gin.Context) {
user := ctx.MustGet("user").(*op.User)
req := model.UserIDReq{}
if err := model.Decode(ctx, &req); err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
u, err := db.GetUserByID(req.ID)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
if u.ID == user.ID {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot ban yourself"))
return
}
if u.IsRoot() {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot ban root user"))
return
}
err = op.SetRoleByID(req.ID, dbModel.RoleBanned)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
return
}
ctx.Status(http.StatusNoContent)
}
func Rooms(ctx *gin.Context) {
// user := ctx.MustGet("user").(*op.User)
page, pageSize, err := GetPageAndPageSize(ctx)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
var desc = ctx.DefaultQuery("sort", "desc") == "desc"
scopes := []func(db *gorm.DB) *gorm.DB{}
switch ctx.DefaultQuery("status", "active") {
case "active":
scopes = append(scopes, db.WhereStatus(dbModel.RoomStatusActive))
case "pending":
scopes = append(scopes, db.WhereStatus(dbModel.RoomStatusPending))
case "banned":
scopes = append(scopes, db.WhereStatus(dbModel.RoomStatusBanned))
default:
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support status"))
return
}
switch ctx.DefaultQuery("order", "name") {
case "createdAt":
if desc {
scopes = append(scopes, db.OrderByCreatedAtDesc)
} else {
scopes = append(scopes, db.OrderByCreatedAtAsc)
}
case "name":
if desc {
scopes = append(scopes, db.OrderByDesc("name"))
} else {
scopes = append(scopes, db.OrderByAsc("name"))
}
default:
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("not support order"))
return
}
if keyword := ctx.Query("keyword"); keyword != "" {
// search mode, all, name, creator
switch ctx.DefaultQuery("search", "all") {
case "all":
scopes = append(scopes, db.WhereRoomNameLikeOrCreatorInOrIDLike(keyword, db.GerUsersIDByUsernameLike(keyword), keyword))
case "name":
scopes = append(scopes, db.WhereRoomNameLike(keyword))
case "creator":
scopes = append(scopes, db.WhereCreatorIDIn(db.GerUsersIDByUsernameLike(keyword)))
case "id":
scopes = append(scopes, db.WhereIDLike(keyword))
}
}
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
"total": db.GetAllRoomsCount(scopes...),
"list": genRoomListResp(append(scopes, db.Paginate(page, pageSize))...),
}))
}
func ApprovePendingRoom(ctx *gin.Context) {
req := model.RoomIDReq{}
if err := model.Decode(ctx, &req); err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
room, err := db.GetRoomByID(req.Id)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
if !room.IsPending() {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("room is not pending"))
return
}
err = db.SetRoomStatus(req.Id, dbModel.RoomStatusActive)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
return
}
ctx.Status(http.StatusNoContent)
}
func BanRoom(ctx *gin.Context) {
user := ctx.MustGet("user").(*op.User)
req := model.RoomIDReq{}
if err := model.Decode(ctx, &req); err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
r, err := db.GetRoomByID(req.Id)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
creator, err := db.GetUserByID(r.CreatorID)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
if creator.ID == user.ID {
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorStringResp("cannot ban yourself"))
return
}
if creator.IsAdmin() {
ctx.AbortWithStatusJSON(http.StatusForbidden, model.NewApiErrorStringResp("no permission"))
return
}
err = op.SetRoomStatus(req.Id, dbModel.RoomStatusBanned)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
return
}
ctx.Status(http.StatusNoContent)
}