management_user.go 6.9 KB
Newer Older
programor_guo's avatar
programor_guo 已提交
1 2 3 4 5 6 7 8 9
/*
** description("").
** copyright('open-im,www.open-im.io').
** author("fg,Gordon@tuoyun.net").
** time(2021/9/15 10:28).
 */
package manage

import (
10
	"Open_IM/pkg/common/config"
programor_guo's avatar
programor_guo 已提交
11
	"Open_IM/pkg/common/constant"
12
	"Open_IM/pkg/common/log"
programor_guo's avatar
programor_guo 已提交
13
	"Open_IM/pkg/common/token_verify"
14
	"Open_IM/pkg/grpc-etcdv3/getcdv3"
programor_guo's avatar
programor_guo 已提交
15
	pbRelay "Open_IM/pkg/proto/relay"
programor_guo's avatar
programor_guo 已提交
16
	pbUser "Open_IM/pkg/proto/user"
programor_guo's avatar
programor_guo 已提交
17
	"Open_IM/pkg/utils"
programor_guo's avatar
programor_guo 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

type paramsDeleteUsers struct {
	OperationID   string   `json:"operationID" binding:"required"`
	DeleteUidList []string `json:"deleteUidList" binding:"required"`
}
type paramsGetAllUsersUid struct {
	OperationID string `json:"operationID" binding:"required"`
}
programor_guo's avatar
programor_guo 已提交
31 32 33 34
type paramsGetUsersOnlineStatus struct {
	OperationID string   `json:"operationID" binding:"required"`
	UserIDList  []string `json:"userIDList" binding:"required,lte=200"`
}
programor_guo's avatar
programor_guo 已提交
35 36 37 38
type paramsAccountCheck struct {
	OperationID string   `json:"operationID" binding:"required"`
	UserIDList  []string `json:"userIDList" binding:"required,lte=100"`
}
programor_guo's avatar
programor_guo 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

func DeleteUser(c *gin.Context) {
	params := paramsDeleteUsers{}
	if err := c.BindJSON(&params); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
		return
	}
	log.InfoByKv("DeleteUser req come here", params.OperationID, "DeleteUidList", params.DeleteUidList)
	etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
	client := pbUser.NewUserClient(etcdConn)
	//defer etcdConn.Close()

	req := &pbUser.DeleteUsersReq{
		OperationID:   params.OperationID,
		DeleteUidList: params.DeleteUidList,
		Token:         c.Request.Header.Get("token"),
	}
	RpcResp, err := client.DeleteUsers(context.Background(), req)
	if err != nil {
programor_guo's avatar
programor_guo 已提交
58
		log.NewError(req.OperationID, "call delete users rpc server failed", err.Error())
programor_guo's avatar
programor_guo 已提交
59 60 61
		c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call delete users rpc server failed"})
		return
	}
programor_guo's avatar
programor_guo 已提交
62 63 64 65
	failedUidList := make([]string, 0)
	for _, v := range RpcResp.FailedUidList {
		failedUidList = append(failedUidList, v)
	}
programor_guo's avatar
programor_guo 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	log.InfoByKv("call delete user rpc server is success", params.OperationID, "resp args", RpcResp.String())
	resp := gin.H{"errCode": RpcResp.CommonResp.ErrorCode, "errMsg": RpcResp.CommonResp.ErrorMsg, "failedUidList": RpcResp.FailedUidList}
	c.JSON(http.StatusOK, resp)
}
func GetAllUsersUid(c *gin.Context) {
	params := paramsGetAllUsersUid{}
	if err := c.BindJSON(&params); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
		return
	}
	log.InfoByKv("GetAllUsersUid req come here", params.OperationID)
	etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
	client := pbUser.NewUserClient(etcdConn)
	//defer etcdConn.Close()

	req := &pbUser.GetAllUsersUidReq{
		OperationID: params.OperationID,
		Token:       c.Request.Header.Get("token"),
	}
	RpcResp, err := client.GetAllUsersUid(context.Background(), req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error(), "uidList": []string{}})
		return
	}
	log.InfoByKv("call GetAllUsersUid rpc server is success", params.OperationID, "resp args", RpcResp.String())
	resp := gin.H{"errCode": RpcResp.CommonResp.ErrorCode, "errMsg": RpcResp.CommonResp.ErrorMsg, "uidList": RpcResp.UidList}
	c.JSON(http.StatusOK, resp)

programor_guo's avatar
programor_guo 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
}
func AccountCheck(c *gin.Context) {
	params := paramsAccountCheck{}
	if err := c.BindJSON(&params); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
		return
	}
	log.InfoByKv("AccountCheck req come here", params.OperationID, params.UserIDList)
	etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
	client := pbUser.NewUserClient(etcdConn)
	//defer etcdConn.Close()

	req := &pbUser.AccountCheckReq{
		OperationID: params.OperationID,
		Token:       c.Request.Header.Get("token"),
		UidList:     params.UserIDList,
	}
	RpcResp, err := client.AccountCheck(context.Background(), req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
		return
	}
	log.InfoByKv("call AccountCheck rpc server is success", params.OperationID, "resp args", RpcResp.String())
	resp := gin.H{"errCode": RpcResp.CommonResp.ErrorCode, "errMsg": RpcResp.CommonResp.ErrorMsg, "Result": RpcResp.Result}
	c.JSON(http.StatusOK, resp)

programor_guo's avatar
programor_guo 已提交
120
}
programor_guo's avatar
programor_guo 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
func GetUsersOnlineStatus(c *gin.Context) {
	params := paramsGetUsersOnlineStatus{}
	if err := c.BindJSON(&params); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
		return
	}
	claims, err := token_verify.ParseToken(c.Request.Header.Get("token"))
	if err != nil {
		log.ErrorByKv("parse token failed", params.OperationID, "err", err.Error())
		c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": err.Error()})
		return
	}
	if !utils.IsContain(claims.UID, config.Config.Manager.AppManagerUid) {
		log.ErrorByKv(" Authentication failed", params.OperationID, "args", c)
		c.JSON(http.StatusBadRequest, gin.H{"errCode": 402, "errMsg": "not authorized"})
		return
	}
	req := &pbRelay.GetUsersOnlineStatusReq{
		OperationID: params.OperationID,
		UserIDList:  params.UserIDList,
	}
	var wsResult []*pbRelay.GetUsersOnlineStatusResp_SuccessResult
	var respResult []*pbRelay.GetUsersOnlineStatusResp_SuccessResult
	flag := false
	log.NewDebug(params.OperationID, "GetUsersOnlineStatus req come here", params.UserIDList)
	grpcCons := getcdv3.GetConn4Unique(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOnlineMessageRelayName)
	for _, v := range grpcCons {
		client := pbRelay.NewOnlineMessageRelayServiceClient(v)
		reply, err := client.GetUsersOnlineStatus(context.Background(), req)
		if err != nil {
			log.NewError(params.OperationID, "GetUsersOnlineStatus rpc  err", req.String(), err.Error())
			continue
		} else {
			if reply.ErrCode == 0 {
				wsResult = append(wsResult, reply.SuccessResult...)
			}
		}
	}
	log.NewDebug(params.OperationID, "call GetUsersOnlineStatus rpc server is success", wsResult)
	//Online data merge of each node
	for _, v1 := range params.UserIDList {
		flag = false
		temp := new(pbRelay.GetUsersOnlineStatusResp_SuccessResult)
		for _, v2 := range wsResult {
			if v2.UserID == v1 {
				flag = true
				temp.UserID = v1
				temp.Status = constant.OnlineStatus
				temp.DetailPlatformStatus = append(temp.DetailPlatformStatus, v2.DetailPlatformStatus...)
			}

		}
		if !flag {
			temp.UserID = v1
			temp.Status = constant.OfflineStatus
		}
		respResult = append(respResult, temp)
	}
	log.NewDebug(params.OperationID, "Finished merged data", respResult)
	resp := gin.H{"errCode": 0, "errMsg": "", "successResult": respResult}
	c.JSON(http.StatusOK, resp)

}