feat: room info and pwd check

api-v2
zijiren233 1 year ago
parent 8c7b942ec6
commit fe2c9365dd

@ -168,14 +168,20 @@ func initRoom(room *gin.RouterGroup, needAuthUser *gin.RouterGroup, needAuthRoom
needAuthUser.POST("/login", LoginRoom)
needAuthUser.GET("/joined", UserCheckJoinedRoom)
needAuthRoom.GET("/me", RoomMe)
needAuthRoom.GET("/info", RoomInfo)
needAuthRoom.GET("/ws", NewWebSocketHandler(utils.NewWebSocketServer()))
needAuthWithoutGuestRoom.GET("/settings", RoomPiblicSettings)
needAuthWithoutGuestRoom.GET("/members", RoomMembers)
needAuthWithoutGuestRoom.POST("/pwd/check", CheckRoomPassword)
{
needAuthRoomAdmin := needAuthRoom.Group("/admin", middlewares.AuthRoomAdminMiddleware)
needAuthRoomCreator := needAuthRoom.Group("/admin", middlewares.AuthRoomCreatorMiddleware)

@ -34,7 +34,7 @@ func RoomMe(ctx *gin.Context) {
room := ctx.MustGet("room").(*op.RoomEntry).Value()
log := ctx.MustGet("log").(*logrus.Entry)
rur, err := room.LoadOrCreateMember(user.ID)
member, err := room.LoadMember(user.ID)
if err != nil {
log.Errorf("room me failed: %v", err)
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
@ -44,11 +44,44 @@ func RoomMe(ctx *gin.Context) {
ctx.JSON(http.StatusOK, model.NewApiDataResp(&model.RoomMeResp{
UserID: user.ID,
RoomID: room.ID,
JoinAt: rur.CreatedAt.UnixMilli(),
Status: rur.Status,
Role: rur.Role,
Permissions: rur.Permissions,
AdminPermissions: rur.AdminPermissions,
JoinAt: member.CreatedAt.UnixMilli(),
Status: member.Status,
Role: member.Role,
Permissions: member.Permissions,
AdminPermissions: member.AdminPermissions,
}))
}
func RoomInfo(ctx *gin.Context) {
user := ctx.MustGet("user").(*op.UserEntry).Value()
room := ctx.MustGet("room").(*op.RoomEntry).Value()
log := ctx.MustGet("log").(*logrus.Entry)
member, err := room.LoadMember(user.ID)
if err != nil {
log.Errorf("room me failed: %v", err)
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
return
}
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
"id": room.ID,
"name": room.Name,
"needPassword": room.NeedPassword(),
"creator": op.GetUserName(room.CreatorID),
"createdAt": room.CreatedAt.UnixMilli(),
"status": room.Status,
"enabledGuest": room.EnabledGuest(),
"member": gin.H{
"id": user.ID,
"username": user.Username,
"joinAt": member.CreatedAt.UnixMilli(),
"status": member.Status,
"role": member.Role,
"permissions": member.Permissions,
"adminPermissions": member.AdminPermissions,
},
}))
}
@ -330,6 +363,14 @@ func LoginRoom(ctx *gin.Context) {
room := roomE.Value()
if member, err := room.LoadMember(user.ID); err == nil {
if member.Status == dbModel.RoomMemberStatusPending {
ctx.AbortWithStatusJSON(http.StatusForbidden, model.NewApiErrorStringResp("status is pending, need admin to approve"))
return
}
if member.Status == dbModel.RoomMemberStatusBanned {
ctx.AbortWithStatusJSON(http.StatusForbidden, model.NewApiErrorStringResp("status is banned"))
return
}
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
"status": member.Status,
"role": member.Role,
@ -362,6 +403,15 @@ func LoginRoom(ctx *gin.Context) {
return
}
if member.Status == dbModel.RoomMemberStatusPending {
ctx.AbortWithStatusJSON(http.StatusForbidden, model.NewApiErrorStringResp("status is pending, need admin to approve"))
return
}
if member.Status == dbModel.RoomMemberStatusBanned {
ctx.AbortWithStatusJSON(http.StatusForbidden, model.NewApiErrorStringResp("status is banned"))
return
}
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
"status": member.Status,
"role": member.Role,
@ -370,6 +420,22 @@ func LoginRoom(ctx *gin.Context) {
}))
}
func CheckRoomPassword(ctx *gin.Context) {
room := ctx.MustGet("room").(*op.RoomEntry).Value()
log := ctx.MustGet("log").(*logrus.Entry)
req := model.CheckRoomPasswordReq{}
if err := model.Decode(ctx, &req); err != nil {
log.Errorf("check room password failed: %v", err)
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
"valid": room.CheckPassword(req.Password),
}))
}
func DeleteRoom(ctx *gin.Context) {
room := ctx.MustGet("room").(*op.RoomEntry)
user := ctx.MustGet("user").(*op.UserEntry).Value()

@ -138,3 +138,15 @@ func (s *SetRoomSettingReq) Decode(ctx *gin.Context) error {
func (s *SetRoomSettingReq) Validate() error {
return nil
}
type CheckRoomPasswordReq struct {
Password string `json:"password"`
}
func (c *CheckRoomPasswordReq) Decode(ctx *gin.Context) error {
return json.NewDecoder(ctx.Request.Body).Decode(c)
}
func (c *CheckRoomPasswordReq) Validate() error {
return nil
}

Loading…
Cancel
Save