mirror of https://github.com/synctv-org/synctv
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.
217 lines
5.5 KiB
Go
217 lines
5.5 KiB
Go
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/provider"
|
|
"github.com/synctv-org/synctv/internal/provider/providers"
|
|
"github.com/synctv-org/synctv/server/middlewares"
|
|
"github.com/synctv-org/synctv/server/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func Me(ctx *gin.Context) {
|
|
user := ctx.MustGet("user").(*op.User)
|
|
|
|
ctx.JSON(http.StatusOK, model.NewApiDataResp(&model.UserInfoResp{
|
|
ID: user.ID,
|
|
Username: user.Username,
|
|
Role: user.Role,
|
|
CreatedAt: user.CreatedAt.UnixMilli(),
|
|
}))
|
|
}
|
|
|
|
func LoginUser(ctx *gin.Context) {
|
|
req := model.LoginUserReq{}
|
|
if err := model.Decode(ctx, &req); err != nil {
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
|
|
user, err := op.LoadUserByUsername(req.Username)
|
|
if err != nil {
|
|
if err == op.ErrUserBanned || err == op.ErrUserPending {
|
|
ctx.AbortWithStatusJSON(http.StatusForbidden, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
|
|
if ok := user.CheckPassword(req.Password); !ok {
|
|
ctx.AbortWithStatusJSON(http.StatusForbidden, model.NewApiErrorStringResp("password incorrect"))
|
|
return
|
|
}
|
|
|
|
token, err := middlewares.NewAuthUserToken(user)
|
|
if err != nil {
|
|
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
|
|
"token": token,
|
|
}))
|
|
}
|
|
|
|
func LogoutUser(ctx *gin.Context) {
|
|
user := ctx.MustGet("user").(*op.User)
|
|
|
|
err := op.CompareAndDeleteUser(user)
|
|
if err != nil {
|
|
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func UserRooms(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("order", "desc") == "desc"
|
|
|
|
scopes := []func(db *gorm.DB) *gorm.DB{
|
|
db.WhereCreatorID(user.ID),
|
|
}
|
|
|
|
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))
|
|
}
|
|
|
|
switch ctx.DefaultQuery("sort", "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 sort"))
|
|
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 "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 SetUsername(ctx *gin.Context) {
|
|
user := ctx.MustGet("user").(*op.User)
|
|
|
|
var req model.SetUsernameReq
|
|
if err := model.Decode(ctx, &req); err != nil {
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
|
|
err := user.SetUsername(req.Username)
|
|
if err != nil {
|
|
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func SetUserPassword(ctx *gin.Context) {
|
|
user := ctx.MustGet("user").(*op.User)
|
|
|
|
var req model.SetUserPasswordReq
|
|
if err := model.Decode(ctx, &req); err != nil {
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
|
|
err := user.SetPassword(req.Password)
|
|
if err != nil {
|
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
|
|
token, err := middlewares.NewAuthUserToken(user)
|
|
if err != nil {
|
|
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
|
|
"token": token,
|
|
}))
|
|
}
|
|
|
|
func UserBindProviders(ctx *gin.Context) {
|
|
user := ctx.MustGet("user").(*op.User)
|
|
|
|
up, err := db.GetBindProviders(user.ID)
|
|
if err != nil {
|
|
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
|
|
return
|
|
}
|
|
|
|
m := providers.EnabledProvider()
|
|
|
|
resp := make(model.UserBindProviderResp, len(up))
|
|
|
|
for _, v := range up {
|
|
if _, ok := m.Load(v.Provider); ok {
|
|
resp[v.Provider] = struct {
|
|
ProviderUserID string "json:\"providerUserID\""
|
|
CreatedAt int64 "json:\"createdAt\""
|
|
}{
|
|
ProviderUserID: v.ProviderUserID,
|
|
CreatedAt: v.CreatedAt.UnixMilli(),
|
|
}
|
|
}
|
|
}
|
|
|
|
m.Range(func(p provider.OAuth2Provider, pi provider.ProviderInterface) bool {
|
|
if _, ok := resp[p]; !ok {
|
|
resp[p] = struct {
|
|
ProviderUserID string "json:\"providerUserID\""
|
|
CreatedAt int64 "json:\"createdAt\""
|
|
}{
|
|
ProviderUserID: "",
|
|
CreatedAt: 0,
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
|
|
ctx.JSON(http.StatusOK, resp)
|
|
}
|