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

403 lines
15 KiB

3 years ago
package group
import (
3 years ago
"Open_IM/pkg/cms_api_struct"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
openIMHttp "Open_IM/pkg/common/http"
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"
)
2 years ago
func GetGroupByID(c *gin.Context) {
3 years ago
var (
2 years ago
req cms_api_struct.GetGroupByIDRequest
resp cms_api_struct.GetGroupByIDResponse
2 years ago
reqPb pbGroup.GetGroupByIDReq
3 years ago
)
if err := c.ShouldBindQuery(&req); err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "ShouldBindQuery failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
2 years ago
reqPb.GroupID = req.GroupID
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pbGroup.NewGroupClient(etcdConn)
2 years ago
respPb, err := client.GetGroupByID(context.Background(), &reqPb)
3 years ago
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetGroupById failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
2 years ago
utils.CopyStructFields(&resp, respPb.CMSGroup.GroupInfo)
resp.GroupOwnerID = respPb.CMSGroup.GroupOwnerUserID
resp.GroupOwnerName = respPb.CMSGroup.GroupOwnerUserName
3 years ago
log.NewInfo("", utils.GetSelfFuncName(), "req: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
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
)
if err := c.ShouldBindQuery(&req); err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "ShouldBindQuery failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
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)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetConn == 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.GetGroups(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetUserInfo failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
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)
resp.CurrentPage = int(respPb.Pagination.PageNumber)
resp.ShowNumber = int(respPb.Pagination.ShowNumber)
3 years ago
log.NewInfo("", utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
3 years ago
func GetGroupByName(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.GetGroupRequest
resp cms_api_struct.GetGroupResponse
3 years ago
reqPb pbGroup.GetGroupReq
)
if err := c.ShouldBindQuery(&req); err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "ShouldBindQuery failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
reqPb.GroupName = req.GroupName
3 years ago
reqPb.Pagination = &commonPb.RequestPagination{}
utils.CopyStructFields(&reqPb.Pagination, req)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetConn == 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.GetGroup(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetGroup failed", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrServer, nil)
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.CurrentPage = int(respPb.Pagination.PageNumber)
resp.ShowNumber = int(respPb.Pagination.ShowNumber)
3 years ago
resp.GroupNums = int(respPb.GroupNums)
3 years ago
log.NewInfo("", utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
func CreateGroup(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.CreateGroupRequest
3 years ago
_ cms_api_struct.CreateGroupResponse
3 years ago
reqPb pbGroup.CreateGroupReq
)
if err := c.BindJSON(&req); err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
reqPb.GroupInfo = &commonPb.GroupInfo{}
3 years ago
reqPb.GroupInfo.GroupName = req.GroupName
3 years ago
reqPb.GroupInfo.CreatorUserID = req.GroupMasterId
3 years ago
reqPb.OwnerUserID = req.GroupMasterId
reqPb.OpUserID = req.GroupMasterId
3 years ago
for _, v := range req.GroupMembers {
reqPb.InitMemberList = append(reqPb.InitMemberList, &pbGroup.GroupAddMemberInfo{
3 years ago
UserID: v,
3 years ago
RoleLevel: 1,
})
}
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pbGroup.NewGroupClient(etcdConn)
3 years ago
_, err := client.CreateGroup(context.Background(), &reqPb)
3 years ago
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "CreateGroup failed", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrServer, nil)
return
}
3 years ago
openIMHttp.RespHttp200(c, constant.OK, nil)
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
)
3 years ago
if err := c.ShouldBindQuery(&req); err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "ShouldBindQuery failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
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
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetConn == 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())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
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)
3 years ago
for _, groupMembers := range respPb.Members {
2 years ago
resp.GroupMembers = append(resp.GroupMembers, *groupMembers)
3 years ago
}
3 years ago
log.NewInfo("", utils.GetSelfFuncName(), "req: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
3 years ago
func AddGroupMembers(c *gin.Context) {
var (
3 years ago
req cms_api_struct.RemoveGroupMembersRequest
resp cms_api_struct.RemoveGroupMembersResponse
3 years ago
reqPb pbGroup.AddGroupMembersCMSReq
)
if err := c.BindJSON(&req); err != nil {
2 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
2 years ago
reqPb.OperationID = utils.OperationIDGenerator()
3 years ago
log.NewInfo("", utils.GetSelfFuncName(), "req: ", req)
2 years ago
reqPb.UserIDList = req.Members
reqPb.GroupID = req.GroupId
etcdConn := getcdv3.GetConn(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.GetConn == 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.AddGroupMembersCMS(context.Background(), &reqPb)
if err != nil {
2 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "AddGroupMembersCMS failed", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
resp.Success = respPb.Success
resp.Failed = respPb.Failed
3 years ago
log.NewInfo("", utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
3 years ago
func RemoveGroupMembers(c *gin.Context) {
var (
3 years ago
req cms_api_struct.RemoveGroupMembersRequest
resp cms_api_struct.RemoveGroupMembersResponse
3 years ago
reqPb pbGroup.RemoveGroupMembersCMSReq
)
if err := c.BindJSON(&req); err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
2 years ago
reqPb.UserIDList = req.Members
reqPb.GroupID = req.GroupId
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetConn == 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.RemoveGroupMembersCMS(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "RemoveGroupMembersCMS failed", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
resp.Success = respPb.Success
resp.Failed = respPb.Failed
3 years ago
log.NewInfo("", utils.GetSelfFuncName(), "req: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
2 years ago
func SetGroupOwner(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.SetGroupMasterRequest
_ cms_api_struct.SetGroupMasterResponse
3 years ago
reqPb pbGroup.OperateUserRoleReq
3 years ago
)
if err := c.BindJSON(&req); err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
2 years ago
reqPb.GroupID = req.GroupId
reqPb.UserID = req.UserId
3 years ago
reqPb.RoleLevel = constant.GroupOwner
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pbGroup.NewGroupClient(etcdConn)
3 years ago
_, err := client.OperateUserRole(context.Background(), &reqPb)
3 years ago
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "DeleteGroup failed", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrServer, nil)
return
}
openIMHttp.RespHttp200(c, constant.OK, nil)
}
3 years ago
func SetGroupOrdinaryUsers(c *gin.Context) {
var (
3 years ago
req cms_api_struct.SetGroupMemberRequest
_ cms_api_struct.AdminLoginResponse
3 years ago
reqPb pbGroup.OperateUserRoleReq
)
if err := c.BindJSON(&req); err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
2 years ago
reqPb.GroupID = req.GroupId
reqPb.UserID = req.UserId
3 years ago
reqPb.RoleLevel = constant.GroupOrdinaryUsers
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pbGroup.NewGroupClient(etcdConn)
_, err := client.OperateUserRole(context.Background(), &reqPb)
if err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "DeleteGroup failed", err.Error())
openIMHttp.RespHttp200(c, err, nil)
return
}
openIMHttp.RespHttp200(c, constant.OK, nil)
}
3 years ago
3 years ago
func AlterGroupInfo(c *gin.Context) {
var (
3 years ago
req cms_api_struct.AlterGroupInfoRequest
_ cms_api_struct.SetGroupMasterResponse
3 years ago
reqPb pbGroup.SetGroupInfoReq
)
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
if err := c.BindJSON(&req); err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OpUserID = c.MustGet("userID").(string)
2 years ago
reqPb.GroupInfoForSet = &commonPb.GroupInfoForSet{
3 years ago
GroupID: req.GroupID,
GroupName: req.GroupName,
Introduction: req.Introduction,
Notification: req.Notification,
FaceURL: req.ProfilePhoto,
3 years ago
}
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, reqPb.OperationID)
if etcdConn == nil {
errMsg := reqPb.OperationID + "getcdv3.GetConn == nil"
log.NewError(reqPb.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
client := pbGroup.NewGroupClient(etcdConn)
_, err := client.SetGroupInfo(context.Background(), &reqPb)
if err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "DeleteGroup failed", err.Error())
openIMHttp.RespHttp200(c, err, nil)
return
}
openIMHttp.RespHttp200(c, constant.OK, nil)
3 years ago
}