Skip to content

Commit

Permalink
feat: 添加退出群组
Browse files Browse the repository at this point in the history
  • Loading branch information
danmuking committed Apr 15, 2024
1 parent e30fb46 commit 1379fe8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
6 changes: 6 additions & 0 deletions domain/vo/req/quit_group_req.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package req

type QuitGroupReq struct {
// 房间di
ID int64 `json:"id" binding:"required"`
}
1 change: 1 addition & 0 deletions routes/init_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func initGin() {
apiGroup.POST("/create", service.CreateGroupService)
apiGroup.DELETE("/:id", service.DeleteGroupService)
apiGroup.POST("/join", service.JoinGroupService)
apiGroup.POST("/quit", service.QuitGroupService)
}

apiContact := router.Group("/api/contact")
Expand Down
85 changes: 85 additions & 0 deletions service/group_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"DiTing-Go/pkg/resp"
"context"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"time"
)

Expand Down Expand Up @@ -429,3 +430,87 @@ func JoinGroupService(c *gin.Context) {
resp.SuccessResponseWithMsg(c, "success")
return
}

// QuitGroupService 退出群聊
//
// @Summary 退出群聊
// @Produce json
// @Param id body int true "房间id"
// @Success 200 {object} resp.ResponseData "成功"
// @Failure 500 {object} resp.ResponseData "内部错误"
// @Router /api/group/create [post]
func QuitGroupService(c *gin.Context) {
uid := c.GetInt64("uid")
quitGroupReq := req.QuitGroupReq{}
if err := c.ShouldBind(&quitGroupReq); err != nil {
resp.ErrorResponse(c, "参数错误")
global.Logger.Errorf("参数错误: %v", err)
c.Abort()
return
}

ctx := context.Background()
tx := global.Query.Begin()
// 群聊是否存在
room := global.Query.Room
roomTx := tx.Room.WithContext(ctx)
_, err := roomTx.Where(room.ID.Eq(quitGroupReq.ID)).First()
if err != nil {
if err.Error() != gorm.ErrRecordNotFound.Error() {
global.Logger.Errorf("查询房间失败 %s", err)
}
resp.ErrorResponse(c, "群聊不存在")
c.Abort()
return
}
// 用户是否在群聊中
groupMember := global.Query.GroupMember
groupMemberTx := tx.GroupMember.WithContext(ctx)
_, err = groupMemberTx.Where(groupMember.UID.Eq(uid), groupMember.GroupID.Eq(quitGroupReq.ID)).First()
if err != nil {
if err.Error() == gorm.ErrRecordNotFound.Error() {
resp.ErrorResponse(c, "未加入群聊")
c.Abort()
return
}
resp.ErrorResponse(c, "退出群聊失败")
global.Logger.Errorf("查询群组成员表失败 %s", err)
c.Abort()
return
}
// 删除会话表
contact := global.Query.Contact
contactTx := tx.Contact.WithContext(ctx)
if _, err := contactTx.Where(contact.UID.Eq(uid), contact.RoomID.Eq(quitGroupReq.ID)).Delete(); err != nil {
resp.ErrorResponse(c, "退出群聊失败")
global.Logger.Errorf("删除会话表失败 %s", err)
c.Abort()
return
}
// 删除群组成员表
// 查询群组
roomGroup := global.Query.RoomGroup
roomGroupTx := tx.RoomGroup.WithContext(ctx)
roomGroupR, err := roomGroupTx.Where(roomGroup.RoomID.Eq(quitGroupReq.ID)).First()
if err != nil {
resp.ErrorResponse(c, "退出群聊失败")
global.Logger.Errorf("查询群聊失败 %s", err)
c.Abort()
return
}
if _, err := groupMemberTx.Where(groupMember.UID.Eq(uid), groupMember.GroupID.Eq(roomGroupR.ID)).Delete(); err != nil {
resp.ErrorResponse(c, "退出群聊失败")
global.Logger.Errorf("删除群组成员表失败 %s", err)
c.Abort()
return
}

if err := tx.Commit(); err != nil {
global.Logger.Errorf("事务提交失败 %s", err)
resp.ErrorResponse(c, "退出群聊失败")
c.Abort()
return
}
resp.SuccessResponseWithMsg(c, "success")
return
}

0 comments on commit 1379fe8

Please sign in to comment.