You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/cms_api/group/group.go

110 lines
4.3 KiB

3 years ago
package group
import (
3 years ago
"Open_IM/pkg/cms_api_struct"
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/log"
3 years ago
"Open_IM/pkg/grpc-etcdv3/getcdv3"
3 years ago
commonPb "Open_IM/pkg/proto/sdk_ws"
3 years ago
"Open_IM/pkg/utils"
3 years ago
"context"
"net/http"
3 years ago
"strings"
pbGroup "Open_IM/pkg/proto/group"
3 years ago
3 years ago
"github.com/gin-gonic/gin"
)
3 years ago
func GetGroups(c *gin.Context) {
var (
3 years ago
req cms_api_struct.GetGroupsRequest
resp cms_api_struct.GetGroupsResponse
3 years ago
reqPb pbGroup.GetGroupsReq
)
2 years ago
if err := c.BindJSON(&req); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "ShouldBindQuery failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
reqPb.Pagination = &commonPb.RequestPagination{}
3 years ago
utils.CopyStructFields(&reqPb.Pagination, req)
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
2 years ago
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
2 years ago
reqPb.GroupID = req.GroupID
reqPb.GroupName = req.GroupName
3 years ago
client := pbGroup.NewGroupClient(etcdConn)
respPb, err := client.GetGroups(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetUserInfo failed ", err.Error())
2 years ago
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
3 years ago
return
}
3 years ago
for _, v := range respPb.CMSGroups {
2 years ago
groupResp := cms_api_struct.GroupResponse{}
utils.CopyStructFields(&groupResp, v.GroupInfo)
groupResp.GroupOwnerName = v.GroupOwnerUserName
groupResp.GroupOwnerID = v.GroupOwnerUserID
resp.Groups = append(resp.Groups, groupResp)
3 years ago
}
3 years ago
resp.GroupNums = int(respPb.GroupNum)
2 years ago
resp.CurrentPage = int(respPb.Pagination.CurrentPage)
3 years ago
resp.ShowNumber = int(respPb.Pagination.ShowNumber)
2 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg, "data": resp})
3 years ago
}
3 years ago
func GetGroupMembers(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.GetGroupMembersRequest
3 years ago
reqPb pbGroup.GetGroupMembersCMSReq
3 years ago
resp cms_api_struct.GetGroupMembersResponse
3 years ago
)
2 years ago
if err := c.BindJSON(&req); err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
reqPb.Pagination = &commonPb.RequestPagination{
PageNumber: int32(req.PageNumber),
ShowNumber: int32(req.ShowNumber),
}
2 years ago
reqPb.GroupID = req.GroupID
3 years ago
reqPb.UserName = req.UserName
2 years ago
etcdConn := getcdv3.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
2 years ago
errMsg := reqPb.OperationID + "getcdv3.GetDefaultConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pbGroup.NewGroupClient(etcdConn)
respPb, err := client.GetGroupMembersCMS(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetGroupMembersCMS failed:", err.Error())
2 years ago
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
3 years ago
return
}
3 years ago
resp.ResponsePagination = cms_api_struct.ResponsePagination{
CurrentPage: int(respPb.Pagination.CurrentPage),
ShowNumber: int(respPb.Pagination.ShowNumber),
}
resp.MemberNums = int(respPb.MemberNums)
2 years ago
for _, groupMember := range respPb.Members {
memberResp := cms_api_struct.GroupMemberResponse{}
utils.CopyStructFields(&memberResp, groupMember)
resp.GroupMembers = append(resp.GroupMembers, memberResp)
3 years ago
}
3 years ago
log.NewInfo("", utils.GetSelfFuncName(), "req: ", resp)
2 years ago
c.JSON(http.StatusOK, gin.H{"errCode": respPb.CommonResp.ErrCode, "errMsg": respPb.CommonResp.ErrMsg, "data": resp})
3 years ago
}