Skip to content

Commit

Permalink
feat: exit room
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Oct 12, 2024
1 parent bfff86d commit d132b6d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/handlers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ func initUser(user *gin.RouterGroup, needAuthUser *gin.RouterGroup) {

needAuthRoom.POST("/delete", UserDeleteRoom)

needAuthRoom.POST("/exit", UserExitRoom)

needAuthRoom.GET("/joined", UserCheckJoinedRoom)
}
}
Expand Down
26 changes: 26 additions & 0 deletions server/handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,29 @@ func UserSignupPassword(ctx *gin.Context) {

handleUserToken(ctx, user.Value())
}

func UserExitRoom(ctx *gin.Context) {
user := ctx.MustGet("user").(*op.UserEntry).Value()
log := ctx.MustGet("log").(*logrus.Entry)

var req model.IdReq
if err := model.Decode(ctx, &req); err != nil {
log.Errorf("failed to decode request: %v", err)
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
return
}

roomE, err := op.LoadOrInitRoomByID(req.Id)
if err == nil {
err = roomE.Value().DeleteMember(user.ID)
} else {
err = db.DeleteRoomMember(req.Id, user.ID)
}
if err != nil {
log.Errorf("failed to delete room member: %v", err)
ctx.AbortWithStatusJSON(http.StatusInternalServerError, model.NewApiErrorResp(err))
return
}

ctx.Status(http.StatusNoContent)
}

0 comments on commit d132b6d

Please sign in to comment.