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/user/user.go

403 lines
15 KiB

3 years ago
package user
import (
"Open_IM/pkg/cms_api_struct"
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
openIMHttp "Open_IM/pkg/common/http"
3 years ago
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
3 years ago
commonPb "Open_IM/pkg/proto/sdk_ws"
pb "Open_IM/pkg/proto/user"
3 years ago
"Open_IM/pkg/utils"
"context"
3 years ago
"fmt"
3 years ago
"net/http"
"strings"
3 years ago
"github.com/gin-gonic/gin"
3 years ago
)
3 years ago
func GetUserById(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.GetUserRequest
resp cms_api_struct.GetUserResponse
3 years ago
reqPb pb.GetUserByIdReq
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)
3 years ago
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb, &req)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
3 years ago
respPb, err := client.GetUserById(context.Background(), &reqPb)
3 years ago
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
if respPb.User.UserId == "" {
openIMHttp.RespHttp200(c, constant.OK, nil)
3 years ago
return
}
3 years ago
utils.CopyStructFields(&resp, respPb.User)
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
3 years ago
func GetUsersByName(c *gin.Context) {
var (
3 years ago
req cms_api_struct.GetUsersByNameRequest
resp cms_api_struct.GetUsersByNameResponse
3 years ago
reqPb pb.GetUsersByNameReq
)
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
if err := c.ShouldBindQuery(&req); err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "ShouldBindQuery failed", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.UserName = req.UserName
reqPb.Pagination = &commonPb.RequestPagination{
PageNumber: int32(req.PageNumber),
ShowNumber: int32(req.ShowNumber),
}
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
respPb, err := client.GetUsersByName(context.Background(), &reqPb)
if err != nil {
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "rpc", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
utils.CopyStructFields(&resp.Users, respPb.Users)
resp.ShowNumber = int(respPb.Pagination.ShowNumber)
resp.CurrentPage = int(respPb.Pagination.CurrentPage)
resp.UserNums = respPb.UserNums
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
}
3 years ago
func GetUsers(c *gin.Context) {
var (
3 years ago
req cms_api_struct.GetUsersRequest
resp cms_api_struct.GetUsersResponse
reqPb pb.GetUsersReq
3 years ago
)
reqPb.Pagination = &commonPb.RequestPagination{}
if err := c.ShouldBindQuery(&req); err != nil {
3 years ago
log.NewError("0", "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
utils.CopyStructFields(&reqPb.Pagination, &req)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
respPb, err := client.GetUsers(context.Background(), &reqPb)
if err != nil {
3 years ago
openIMHttp.RespHttp200(c, err, resp)
3 years ago
return
3 years ago
}
3 years ago
utils.CopyStructFields(&resp.Users, respPb.User)
resp.ShowNumber = int(respPb.Pagination.ShowNumber)
resp.CurrentPage = int(respPb.Pagination.CurrentPage)
3 years ago
resp.UserNums = respPb.UserNums
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
3 years ago
func ResignUser(c *gin.Context) {
var (
req cms_api_struct.ResignUserRequest
resp cms_api_struct.ResignUserResponse
reqPb pb.ResignUserReq
)
if err := c.ShouldBind(&req); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": http.StatusBadRequest, "errMsg": err.Error()})
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb, &req)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
_, err := client.ResignUser(context.Background(), &reqPb)
if err != nil {
3 years ago
openIMHttp.RespHttp200(c, err, resp)
3 years ago
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
}
3 years ago
func AlterUser(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.AlterUserRequest
resp cms_api_struct.AlterUserResponse
reqPb pb.AlterUserReq
3 years ago
)
3 years ago
if err := c.BindJSON(&req); err != nil {
3 years ago
log.NewError("0", "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, resp)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb, &req)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
3 years ago
_, err := client.AlterUser(context.Background(), &reqPb)
3 years ago
if err != nil {
log.NewError(reqPb.OperationID, "microserver failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
}
3 years ago
openIMHttp.RespHttp200(c, constant.OK, nil)
3 years ago
}
func AddUser(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.AddUserRequest
reqPb pb.AddUserReq
3 years ago
)
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)
3 years ago
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb, &req)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
3 years ago
_, err := client.AddUser(context.Background(), &reqPb)
3 years ago
if err != nil {
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
3 years ago
}
3 years ago
openIMHttp.RespHttp200(c, constant.OK, nil)
3 years ago
}
func BlockUser(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.BlockUserRequest
resp cms_api_struct.BlockUserResponse
reqPb pb.BlockUserReq
3 years ago
)
3 years ago
if err := c.BindJSON(&req); err != nil {
fmt.Println(err)
3 years ago
log.NewError("0", "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, resp)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb, &req)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
3 years ago
fmt.Println(reqPb)
_, err := client.BlockUser(context.Background(), &reqPb)
3 years ago
if err != nil {
3 years ago
openIMHttp.RespHttp200(c, err, resp)
3 years ago
return
3 years ago
}
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
func UnblockUser(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.UnblockUserRequest
resp cms_api_struct.UnBlockUserResponse
reqPb pb.UnBlockUserReq
3 years ago
)
if err := c.ShouldBind(&req); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, constant.ErrArgs, resp)
3 years ago
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb, &req)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
3 years ago
_, err := client.UnBlockUser(context.Background(), &reqPb)
3 years ago
if err != nil {
3 years ago
openIMHttp.RespHttp200(c, err, resp)
3 years ago
return
3 years ago
}
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
func GetBlockUsers(c *gin.Context) {
3 years ago
var (
req cms_api_struct.GetBlockUsersRequest
3 years ago
resp cms_api_struct.GetBlockUsersResponse
3 years ago
reqPb pb.GetBlockUsersReq
respPb *pb.GetBlockUsersResp
)
3 years ago
reqPb.Pagination = &commonPb.RequestPagination{}
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, resp)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
utils.CopyStructFields(&reqPb.Pagination, &req)
3 years ago
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "blockUsers", reqPb.Pagination, req)
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
respPb, err := client.GetBlockUsers(context.Background(), &reqPb)
if err != nil {
3 years ago
log.NewError(reqPb.OperationID, utils.GetSelfFuncName(), "GetBlockUsers rpc", err.Error())
openIMHttp.RespHttp200(c, err, resp)
3 years ago
return
3 years ago
}
3 years ago
for _, v := range respPb.BlockUsers {
3 years ago
resp.BlockUsers = append(resp.BlockUsers, cms_api_struct.BlockUser{
3 years ago
UserResponse: cms_api_struct.UserResponse{
UserId: v.User.UserId,
ProfilePhoto: v.User.ProfilePhoto,
Nickname: v.User.Nickname,
IsBlock: v.User.IsBlock,
CreateTime: v.User.CreateTime,
3 years ago
},
BeginDisableTime: v.BeginDisableTime,
EndDisableTime: v.EndDisableTime,
})
}
3 years ago
resp.ShowNumber = int(respPb.Pagination.ShowNumber)
resp.CurrentPage = int(respPb.Pagination.CurrentPage)
3 years ago
resp.UserNums = respPb.UserNums
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
3 years ago
3 years ago
func GetBlockUserById(c *gin.Context) {
3 years ago
var (
3 years ago
req cms_api_struct.GetBlockUserRequest
resp cms_api_struct.GetBlockUserResponse
3 years ago
reqPb pb.GetBlockUserByIdReq
3 years ago
)
if err := c.ShouldBindQuery(&req); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
reqPb.UserId = req.UserId
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
3 years ago
respPb, err := client.GetBlockUserById(context.Background(), &reqPb)
3 years ago
if err != nil {
log.NewError(reqPb.OperationID, "GetBlockUserById rpc failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
resp.EndDisableTime = respPb.BlockUser.EndDisableTime
resp.BeginDisableTime = respPb.BlockUser.BeginDisableTime
utils.CopyStructFields(&resp, respPb.BlockUser.User)
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
3 years ago
openIMHttp.RespHttp200(c, constant.OK, resp)
3 years ago
}
3 years ago
func DeleteUser(c *gin.Context) {
var (
req cms_api_struct.DeleteUserRequest
reqPb pb.DeleteUserReq
)
if err := c.BindJSON(&req); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
openIMHttp.RespHttp200(c, constant.ErrArgs, nil)
return
}
reqPb.OperationID = utils.OperationIDGenerator()
log.NewInfo(reqPb.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
reqPb.UserId = req.UserId
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName, 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 := pb.NewUserClient(etcdConn)
_, err := client.DeleteUser(context.Background(), &reqPb)
if err != nil {
log.NewError(reqPb.OperationID, "DeleteUser rpc failed ", err.Error())
3 years ago
openIMHttp.RespHttp200(c, err, nil)
3 years ago
return
}
openIMHttp.RespHttp200(c, constant.OK, nil)
}