From 24a44114c15a1fbd834cae7ca7b9252c26fa438a Mon Sep 17 00:00:00 2001 From: away <1819625428@qq.com> Date: Mon, 28 Jun 2021 15:27:35 +0800 Subject: [PATCH] group api --- src/api/group/create_group.go | 59 ++++ src/api/group/get_group_applicationList.go | 95 ++++++ src/api/group/get_groups_info.go | 53 ++++ src/api/group/group.go | 306 ++++++++++++++++++++ src/api/group/group_application_response.go | 87 ++++++ src/api/group/join_group.go | 47 +++ src/api/group/quit_group.go | 46 +++ src/api/group/set_group_info.go | 53 ++++ src/api/group/transfer_group_owner.go | 65 +++++ 9 files changed, 811 insertions(+) create mode 100644 src/api/group/create_group.go create mode 100644 src/api/group/get_group_applicationList.go create mode 100644 src/api/group/get_groups_info.go create mode 100644 src/api/group/group.go create mode 100644 src/api/group/group_application_response.go create mode 100644 src/api/group/join_group.go create mode 100644 src/api/group/quit_group.go create mode 100644 src/api/group/set_group_info.go create mode 100644 src/api/group/transfer_group_owner.go diff --git a/src/api/group/create_group.go b/src/api/group/create_group.go new file mode 100644 index 000000000..7e91d946d --- /dev/null +++ b/src/api/group/create_group.go @@ -0,0 +1,59 @@ +package group + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + pb "Open_IM/src/proto/group" + "context" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type paramsCreateGroupStruct struct { + MemberList []*pb.GroupAddMemberInfo `json:"memberList"` + GroupName string `json:"name"` + Introduction string `json:"introduction"` + Notification string `json:"notification"` + FaceUrl string `json:"faceUrl"` + OperationID string `json:"operationID" binding:"required"` +} + +func CreateGroup(c *gin.Context) { + log.Info("", "", "api create group init ....") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := pb.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := paramsCreateGroupStruct{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pb.CreateGroupReq{ + MemberList: params.MemberList, + GroupName: params.GroupName, + Introduction: params.Introduction, + Notification: params.Notification, + FaceUrl: params.FaceUrl, + OperationID: params.OperationID, + Token: c.Request.Header.Get("token"), + } + log.Info(req.Token, req.OperationID, "api create group is server,params=%s", req.String()) + RpcResp, err := client.CreateGroup(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "err=%s,call create group rpc server failed", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"}) + return + } + log.InfoByArgs("call create group rpc server success,args=%s", RpcResp.String()) + if RpcResp.ErrorCode == 0 { + resp := gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg, "data": gin.H{"groupID": RpcResp.GroupID}} + c.JSON(http.StatusOK, resp) + } else { + c.JSON(http.StatusOK, gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg}) + } + log.InfoByArgs("api create group success return,get args=%s,return args=%s", req.String(), RpcResp.String()) +} diff --git a/src/api/group/get_group_applicationList.go b/src/api/group/get_group_applicationList.go new file mode 100644 index 000000000..cbadd6396 --- /dev/null +++ b/src/api/group/get_group_applicationList.go @@ -0,0 +1,95 @@ +package group + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + "Open_IM/src/proto/group" + "Open_IM/src/utils" + "context" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type paramsGroupApplicationList struct { + OperationID string `json:"operationID" binding:"required"` +} + +func newUserRegisterReq(params *paramsGroupApplicationList) *group.GetGroupApplicationListReq { + pbData := group.GetGroupApplicationListReq{ + OperationID: params.OperationID, + } + return &pbData +} + +type paramsGroupApplicationListRet struct { + GroupID string `json:"groupID"` + FromUserID string `json:"fromUserID"` + FromUserNickName string `json:"fromUserNickName"` + FromUserFaceUrl string `json:"fromUserFaceUrl"` + ToUserID string `json:"toUserID"` + AddTime int64 `json:"addTime"` + RequestMsg string `json:"requestMsg"` + HandledMsg string `json:"handledMsg"` + Type int32 `json:"type"` + HandleStatus int32 `json:"handleStatus"` + HandleResult int32 `json:"handleResult"` +} + +func GetGroupApplicationList(c *gin.Context) { + log.Info("", "", "api GetGroupApplicationList init ....") + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := group.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := paramsGroupApplicationList{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + pbData := newUserRegisterReq(¶ms) + + token := c.Request.Header.Get("token") + if claims, err := utils.ParseToken(token); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "token validate err"}) + return + } else { + pbData.UID = claims.UID + } + + log.Info("", "", "api GetGroupApplicationList is server, [data: %s]", pbData.String()) + reply, err := client.GetGroupApplicationList(context.Background(), pbData) + if err != nil { + log.Error("", "", "api GetGroupApplicationList call rpc fail, [data: %s] [err: %s]", pbData.String(), err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()}) + return + } + log.Info("", "", "api GetGroupApplicationList call rpc success, [data: %s] [reply: %s]", pbData.String(), reply.String()) + + var userReq []paramsGroupApplicationListRet + for i := 0; i < len(reply.Data.User); i++ { + req := paramsGroupApplicationListRet{} + req.GroupID = reply.Data.User[i].GroupID + req.FromUserID = reply.Data.User[i].FromUserID + req.FromUserNickName = reply.Data.User[i].FromUserNickName + req.FromUserFaceUrl = reply.Data.User[i].FromUserFaceUrl + req.ToUserID = reply.Data.User[i].ToUserID + req.RequestMsg = reply.Data.User[i].RequestMsg + req.HandledMsg = reply.Data.User[i].HandledMsg + req.Type = reply.Data.User[i].Type + req.HandleStatus = reply.Data.User[i].HandleStatus + req.HandleResult = reply.Data.User[i].HandleResult + userReq = append(userReq, req) + } + + c.JSON(http.StatusOK, gin.H{ + "errCode": reply.ErrCode, + "errMsg": reply.ErrMsg, + "data": gin.H{ + "count": reply.Data.Count, + "user": userReq, + }, + }) + +} diff --git a/src/api/group/get_groups_info.go b/src/api/group/get_groups_info.go new file mode 100644 index 000000000..7301c5ac9 --- /dev/null +++ b/src/api/group/get_groups_info.go @@ -0,0 +1,53 @@ +package group + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + pb "Open_IM/src/proto/group" + "context" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type paramsGetGroupInfo struct { + GroupIDList []string `json:"groupIDList" binding:"required"` + OperationID string `json:"operationID" binding:"required"` +} + +func GetGroupsInfo(c *gin.Context) { + log.Info("", "", "api get groups info init ....") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := pb.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := paramsGetGroupInfo{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pb.GetGroupsInfoReq{ + GroupIDList: params.GroupIDList, + Token: c.Request.Header.Get("token"), + OperationID: params.OperationID, + } + log.Info(req.Token, req.OperationID, "get groups info is server,params=%s", req.String()) + RpcResp, err := client.GetGroupsInfo(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "call get groups info rpc server failed,err=%s", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"}) + return + } + log.InfoByArgs("call get groups info rpc server success,info=%s", RpcResp.String()) + if RpcResp.ErrorCode == 0 { + c.JSON(http.StatusOK, gin.H{ + "errCode": RpcResp.ErrorCode, + "errMsg": RpcResp.ErrorMsg, + "data": RpcResp.Data, + }) + } else { + c.JSON(http.StatusOK, gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg}) + } +} diff --git a/src/api/group/group.go b/src/api/group/group.go new file mode 100644 index 000000000..e62e5a340 --- /dev/null +++ b/src/api/group/group.go @@ -0,0 +1,306 @@ +package group + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + pb "Open_IM/src/proto/group" + "context" + "fmt" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type InviteUserToGroupReq struct { + GroupID string `json:"groupID"` + UidList []string `json:"uidList" binding:"required"` + Reason string `json:"reason"` + OperationID string `json:"operationID" binding:"required"` +} + +type GetJoinedGroupListReq struct { + OperationID string `json:"operationID" binding:"required"` +} + +type KickGroupMemberReq struct { + GroupID string `json:"groupID"` + UidList []string `json:"uidList" binding:"required"` + Reason string `json:"reason"` + OperationID string `json:"operationID" binding:"required"` +} + +func KickGroupMember(c *gin.Context) { + log.Info("", "", "KickGroupMember start....") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := pb.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := KickGroupMemberReq{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + + req := &pb.KickGroupMemberReq{ + OperationID: params.OperationID, + GroupID: params.GroupID, + Token: c.Request.Header.Get("token"), + UidList: params.UidList, + } + log.Info(req.Token, req.OperationID, "recv req: ", req.String()) + + RpcResp, err := client.KickGroupMember(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "GetGroupMemberList failed, err: ", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()}) + return + } + + type KickGroupMemberResp struct { + ErrorCode int32 `json:"errCode"` + ErrorMsg string `json:"errMsg"` + Data []Id2Result `json:"data"` + } + + var memberListResp KickGroupMemberResp + memberListResp.ErrorMsg = RpcResp.ErrorMsg + memberListResp.ErrorCode = RpcResp.ErrorCode + for _, v := range RpcResp.Id2Result { + memberListResp.Data = append(memberListResp.Data, + Id2Result{UId: v.UId, + Result: v.Result}) + } + c.JSON(http.StatusOK, memberListResp) +} + +type GetGroupMembersInfoReq struct { + GroupID string `json:"groupID"` + MemberList []string `json:"memberList"` + OperationID string `json:"operationID"` +} +type GetGroupMembersInfoResp struct { + ErrorCode int32 `json:"errCode"` + ErrorMsg string `json:"errMsg"` + Data []MemberResult `json:"data"` +} + +func GetGroupMembersInfo(c *gin.Context) { + log.Info("", "", "GetGroupMembersInfo start....") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := pb.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := GetGroupMembersInfoReq{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + + req := &pb.GetGroupMembersInfoReq{ + OperationID: params.OperationID, + GroupID: params.GroupID, + MemberList: params.MemberList, + Token: c.Request.Header.Get("token"), + } + log.Info(req.Token, req.OperationID, "recv req: ", len(params.MemberList)) + + RpcResp, err := client.GetGroupMembersInfo(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "GetGroupMemberList failed, err: ", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()}) + return + } + + var memberListResp GetGroupMembersInfoResp + memberListResp.ErrorMsg = RpcResp.ErrorMsg + memberListResp.ErrorCode = RpcResp.ErrorCode + for _, v := range RpcResp.MemberList { + memberListResp.Data = append(memberListResp.Data, + MemberResult{GroupId: req.GroupID, + UserId: v.UserId, + Role: v.Role, + JoinTime: uint64(v.JoinTime), + Nickname: v.NickName, + FaceUrl: v.FaceUrl}) + } + c.JSON(http.StatusOK, memberListResp) +} + +type GetGroupMemberListReq struct { + GroupID string `json:"groupID"` + Filter int32 `json:"filter"` + NextSeq int32 `json:"nextSeq"` + OperationID string `json:"operationID"` +} + +type MemberResult struct { + GroupId string `json:"groupID"` + UserId string `json:"userId"` + Role int32 `json:"role"` + JoinTime uint64 `json:"joinTime"` + Nickname string `json:"nickName"` + FaceUrl string `json:"faceUrl"` +} + +func GetGroupMemberList(c *gin.Context) { + log.Info("", "", "GetGroupMemberList start....") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := pb.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := GetGroupMemberListReq{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pb.GetGroupMemberListReq{ + OperationID: params.OperationID, + Filter: params.Filter, + NextSeq: params.NextSeq, + GroupID: params.GroupID, + Token: c.Request.Header.Get("token"), + } + log.Info(req.Token, req.OperationID, "recv req: ", req.String()) + RpcResp, err := client.GetGroupMemberList(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "GetGroupMemberList failed, err: ", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()}) + return + } + + type GetGroupMemberListResp struct { + ErrorCode int32 `json:"errCode"` + ErrorMsg string `json:"errMsg"` + NextSeq int32 `json:"nextSeq"` + Data []MemberResult `json:"data"` + } + + var memberListResp GetGroupMemberListResp + memberListResp.ErrorMsg = RpcResp.ErrorMsg + memberListResp.ErrorCode = RpcResp.ErrorCode + memberListResp.NextSeq = RpcResp.NextSeq + for _, v := range RpcResp.MemberList { + memberListResp.Data = append(memberListResp.Data, + MemberResult{GroupId: req.GroupID, + UserId: v.UserId, + Role: v.Role, + JoinTime: uint64(v.JoinTime), + Nickname: v.NickName, + FaceUrl: v.FaceUrl}) + } + c.JSON(http.StatusOK, memberListResp) + +} + +type groupResult struct { + GroupId string `json:"groupId"` + GroupName string `json:"groupName"` + Notification string `json:"notification"` + Introduction string `json:"introduction"` + FaceUrl string `json:"faceUrl"` + OwnerId string `json:"ownerId"` + CreateTime uint64 `json:"createTime"` + MemberCount uint32 `json:"memberCount"` +} + +func GetJoinedGroupList(c *gin.Context) { + log.Info("", "", "GetJoinedGroupList start....") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + fmt.Println("config: ", etcdConn, config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := pb.NewGroupClient(etcdConn) + + defer etcdConn.Close() + + params := GetJoinedGroupListReq{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pb.GetJoinedGroupListReq{ + OperationID: params.OperationID, + Token: c.Request.Header.Get("token"), + } + log.Info(req.Token, req.OperationID, "recv req: ", req.String()) + + RpcResp, err := client.GetJoinedGroupList(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "GetJoinedGroupList failed, err: ", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()}) + return + } + + type GetJoinedGroupListResp struct { + ErrorCode int32 `json:"errCode"` + ErrorMsg string `json:"errMsg"` + Data []groupResult `json:"data"` + } + + var GroupListResp GetJoinedGroupListResp + GroupListResp.ErrorCode = RpcResp.ErrorCode + GroupListResp.ErrorMsg = RpcResp.ErrorMsg + for _, v := range RpcResp.GroupList { + GroupListResp.Data = append(GroupListResp.Data, + groupResult{GroupId: v.GroupId, GroupName: v.GroupName, + Notification: v.Notification, + Introduction: v.Introduction, + FaceUrl: v.FaceUrl, + OwnerId: v.OwnerId, + CreateTime: v.CreateTime, + MemberCount: v.MemberCount}) + } + c.JSON(http.StatusOK, GroupListResp) +} + +type Id2Result struct { + UId string `json:"uid"` + Result int32 `json:"result"` +} + +func InviteUserToGroup(c *gin.Context) { + log.Info("", "", "InviteUserToGroup start....") + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := pb.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := InviteUserToGroupReq{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pb.InviteUserToGroupReq{ + OperationID: params.OperationID, + GroupID: params.GroupID, + Reason: params.Reason, + UidList: params.UidList, + Token: c.Request.Header.Get("token"), + } + log.Info(req.Token, req.OperationID, "recv req: ", req.String()) + + RpcResp, err := client.InviteUserToGroup(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "InviteUserToGroup failed, err: ", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()}) + return + } + + type InviteUserToGroupResp struct { + ErrorCode int32 `json:"errCode"` + ErrorMsg string `json:"errMsg"` + I2R []Id2Result `json:"data"` + } + + var iResp InviteUserToGroupResp + iResp.ErrorMsg = RpcResp.ErrorMsg + iResp.ErrorCode = RpcResp.ErrorCode + for _, v := range RpcResp.Id2Result { + iResp.I2R = append(iResp.I2R, Id2Result{UId: v.UId, Result: v.Result}) + } + + //resp := gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg, "data": RpcResp.Id2Result} + c.JSON(http.StatusOK, iResp) +} diff --git a/src/api/group/group_application_response.go b/src/api/group/group_application_response.go new file mode 100644 index 000000000..efd290376 --- /dev/null +++ b/src/api/group/group_application_response.go @@ -0,0 +1,87 @@ +package group + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + "Open_IM/src/proto/group" + "Open_IM/src/utils" + "context" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type paramsGroupApplicationResponse struct { + OperationID string `json:"operationID" binding:"required"` + GroupID string `json:"groupID" binding:"required"` + FromUserID string `json:"fromUserID" binding:"required"` + FromUserNickName string `json:"fromUserNickName"` + FromUserFaceUrl string `json:"fromUserFaceUrl"` + ToUserID string `json:"toUserID" binding:"required"` + ToUserNickName string `json:"toUserNickName"` + ToUserFaceUrl string `json:"toUserFaceUrl"` + AddTime int64 `json:"addTime"` + RequestMsg string `json:"requestMsg"` + HandledMsg string `json:"handledMsg"` + Type int32 `json:"type"` + HandleStatus int32 `json:"handleStatus"` + HandleResult int32 `json:"handleResult"` +} + +func newGroupApplicationResponse(params *paramsGroupApplicationResponse) *group.GroupApplicationResponseReq { + pbData := group.GroupApplicationResponseReq{ + OperationID: params.OperationID, + GroupID: params.GroupID, + FromUserID: params.FromUserID, + FromUserNickName: params.FromUserNickName, + FromUserFaceUrl: params.FromUserFaceUrl, + ToUserID: params.ToUserID, + ToUserNickName: params.ToUserNickName, + ToUserFaceUrl: params.ToUserFaceUrl, + AddTime: params.AddTime, + RequestMsg: params.RequestMsg, + HandledMsg: params.HandledMsg, + Type: params.Type, + HandleStatus: params.HandleStatus, + HandleResult: params.HandleResult, + } + return &pbData +} + +func ApplicationGroupResponse(c *gin.Context) { + log.Info("", "", "api GroupApplicationResponse init ....") + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := group.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := paramsGroupApplicationResponse{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + pbData := newGroupApplicationResponse(¶ms) + + token := c.Request.Header.Get("token") + if claims, err := utils.ParseToken(token); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "token validate err"}) + return + } else { + pbData.OwnerID = claims.UID + } + + log.Info("", "", "api GroupApplicationResponse is server, [data: %s]", pbData.String()) + reply, err := client.GroupApplicationResponse(context.Background(), pbData) + if err != nil { + log.Error("", "", "api GroupApplicationResponse call rpc fail, [data: %s] [err: %s]", pbData.String(), err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()}) + return + } + log.Info("", "", "api GroupApplicationResponse call rpc success, [data: %s] [reply: %s]", pbData.String(), reply.String()) + + c.JSON(http.StatusOK, gin.H{ + "errCode": reply.ErrCode, + "errMsg": reply.ErrMsg, + }) + +} diff --git a/src/api/group/join_group.go b/src/api/group/join_group.go new file mode 100644 index 000000000..4b25d1ccf --- /dev/null +++ b/src/api/group/join_group.go @@ -0,0 +1,47 @@ +package group + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + pb "Open_IM/src/proto/group" + "context" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type paramsJoinGroup struct { + GroupID string `json:"groupID" binding:"required"` + Message string `json:"message"` + OperationID string `json:"operationID" binding:"required"` +} + +func JoinGroup(c *gin.Context) { + log.Info("", "", "api join group init....") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := pb.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := paramsJoinGroup{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pb.JoinGroupReq{ + GroupID: params.GroupID, + Message: params.Message, + Token: c.Request.Header.Get("token"), + OperationID: params.OperationID, + } + log.Info(req.Token, req.OperationID, "api join group is server,params=%s", req.String()) + RpcResp, err := client.JoinGroup(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "call join group rpc server failed,err=%s", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"}) + return + } + log.InfoByArgs("call join group rpc server success,args=%s", RpcResp.String()) + c.JSON(http.StatusOK, gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg}) +} diff --git a/src/api/group/quit_group.go b/src/api/group/quit_group.go new file mode 100644 index 000000000..9d1812588 --- /dev/null +++ b/src/api/group/quit_group.go @@ -0,0 +1,46 @@ +package group + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + pb "Open_IM/src/proto/group" + "context" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type paramsQuitGroup struct { + GroupID string `json:"groupID" binding:"required"` + OperationID string `json:"operationID" binding:"required"` +} + +func QuitGroup(c *gin.Context) { + log.Info("", "", "api quit group init ....") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := pb.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := paramsQuitGroup{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pb.QuitGroupReq{ + GroupID: params.GroupID, + OperationID: params.OperationID, + Token: c.Request.Header.Get("token"), + } + log.Info(req.Token, req.OperationID, "api quit group is server,params=%s", req.String()) + RpcResp, err := client.QuitGroup(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "call quit group rpc server failed,err=%s", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"}) + return + } + log.InfoByArgs("call quit group rpc server success,args=%s", RpcResp.String()) + c.JSON(http.StatusOK, gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg}) + log.InfoByArgs("api quit group success return,get args=%s,return args=%s", req.String(), RpcResp.String()) +} diff --git a/src/api/group/set_group_info.go b/src/api/group/set_group_info.go new file mode 100644 index 000000000..fcaa804cc --- /dev/null +++ b/src/api/group/set_group_info.go @@ -0,0 +1,53 @@ +package group + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + pb "Open_IM/src/proto/group" + "context" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type paramsSetGroupInfo struct { + GroupID string `json:"groupId" binding:"required"` + GroupName string `json:"groupName"` + Notification string `json:"notification"` + Introduction string `json:"introduction"` + FaceUrl string `json:"faceUrl"` + OperationID string `json:"operationID" binding:"required"` +} + +func SetGroupInfo(c *gin.Context) { + log.Info("", "", "api set group info init...") + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := pb.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := paramsSetGroupInfo{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pb.SetGroupInfoReq{ + GroupID: params.GroupID, + GroupName: params.GroupName, + Notification: params.Notification, + Introduction: params.Introduction, + FaceUrl: params.FaceUrl, + Token: c.Request.Header.Get("token"), + OperationID: params.OperationID, + } + log.Info(req.Token, req.OperationID, "api set group info is server,params=%s", req.String()) + RpcResp, err := client.SetGroupInfo(context.Background(), req) + if err != nil { + log.Error(req.Token, req.OperationID, "call set group info rpc server failed,err=%s", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"}) + return + } + log.InfoByArgs("call set group info rpc server success,args=%s", RpcResp.String()) + c.JSON(http.StatusOK, gin.H{"errCode": RpcResp.ErrorCode, "errMsg": RpcResp.ErrorMsg}) +} diff --git a/src/api/group/transfer_group_owner.go b/src/api/group/transfer_group_owner.go new file mode 100644 index 000000000..f5c3eabfa --- /dev/null +++ b/src/api/group/transfer_group_owner.go @@ -0,0 +1,65 @@ +package group + +import ( + "Open_IM/src/common/config" + "Open_IM/src/common/log" + "Open_IM/src/proto/group" + "Open_IM/src/utils" + "context" + "github.com/gin-gonic/gin" + "github.com/skiffer-git/grpc-etcdv3/getcdv3" + "net/http" + "strings" +) + +type paramsTransferGroupOwner struct { + OperationID string `json:"operationID" binding:"required"` + GroupID string `json:"groupID" binding:"required"` + UID string `json:"uid" binding:"required"` +} + +func newTransferGroupOwnerReq(params *paramsTransferGroupOwner) *group.TransferGroupOwnerReq { + pbData := group.TransferGroupOwnerReq{ + OperationID: params.OperationID, + GroupID: params.GroupID, + NewOwner: params.UID, + } + return &pbData +} + +func TransferGroupOwner(c *gin.Context) { + log.Info("", "", "api TransferGroupOwner init ....") + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName) + client := group.NewGroupClient(etcdConn) + defer etcdConn.Close() + + params := paramsTransferGroupOwner{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + pbData := newTransferGroupOwnerReq(¶ms) + + token := c.Request.Header.Get("token") + if claims, err := utils.ParseToken(token); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "token validate err"}) + return + } else { + pbData.OldOwner = claims.UID + } + + log.Info("", "", "api TransferGroupOwner is server, [data: %s]", pbData.String()) + reply, err := client.TransferGroupOwner(context.Background(), pbData) + if err != nil { + log.Error("", "", "api TransferGroupOwner call rpc fail, [data: %s] [err: %s]", pbData.String(), err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()}) + return + } + log.Info("", "", "api TransferGroupOwner call rpc success, [data: %s] [reply: %s]", pbData.String(), reply.String()) + + c.JSON(http.StatusOK, gin.H{ + "errCode": reply.ErrCode, + "errMsg": reply.ErrMsg, + }) + +}