From c59dc129a8e5c13b241ea5974dedfcb8c1001598 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Mon, 18 Apr 2022 14:27:12 +0800 Subject: [PATCH] /user/get_users_online_status --- cmd/open_im_api/main.go | 8 ++-- internal/api/manage/management_user.go | 8 +++- internal/api/user/user.go | 62 ++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/cmd/open_im_api/main.go b/cmd/open_im_api/main.go index 122f4d055..bbeb522e3 100644 --- a/cmd/open_im_api/main.go +++ b/cmd/open_im_api/main.go @@ -38,9 +38,10 @@ func main() { // user routing group, which handles user registration and login services userRouterGroup := r.Group("/user") { - userRouterGroup.POST("/update_user_info", user.UpdateUserInfo) //1 - userRouterGroup.POST("/get_users_info", user.GetUsersInfo) //1 - userRouterGroup.POST("/get_self_user_info", user.GetSelfUserInfo) //1 + userRouterGroup.POST("/update_user_info", user.UpdateUserInfo) //1 + userRouterGroup.POST("/get_users_info", user.GetUsersInfo) //1 + userRouterGroup.POST("/get_self_user_info", user.GetSelfUserInfo) //1 + userRouterGroup.POST("/get_users_online_status", user.GetUsersOnlineStatus) //1 } //friend routing group friendRouterGroup := r.Group("/friend") @@ -86,6 +87,7 @@ func main() { groupRouterGroup.POST("/cancel_mute_group", group.CancelMuteGroup) groupRouterGroup.POST("/set_group_member_nickname", group.SetGroupMemberNickname) + } //certificate authRouterGroup := r.Group("/auth") diff --git a/internal/api/manage/management_user.go b/internal/api/manage/management_user.go index 5bcc19be0..1ff802cc4 100644 --- a/internal/api/manage/management_user.go +++ b/internal/api/manage/management_user.go @@ -118,6 +118,7 @@ func AccountCheck(c *gin.Context) { log.NewInfo(req.OperationID, "AccountCheck api return", resp) c.JSON(http.StatusOK, resp) } + func GetUsersOnlineStatus(c *gin.Context) { params := api.GetUsersOnlineStatusReq{} if err := c.BindJSON(¶ms); err != nil { @@ -133,6 +134,12 @@ func GetUsersOnlineStatus(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"}) return } + if len(config.Config.Manager.AppManagerUid) == 0 { + log.NewError(req.OperationID, "Manager == 0") + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "Manager == 0"}) + return + } + req.OpUserID = config.Config.Manager.AppManagerUid[0] log.NewInfo(params.OperationID, "GetUsersOnlineStatus args ", req.String()) var wsResult []*pbRelay.GetUsersOnlineStatusResp_SuccessResult var respResult []*pbRelay.GetUsersOnlineStatusResp_SuccessResult @@ -176,5 +183,4 @@ func GetUsersOnlineStatus(c *gin.Context) { } log.NewInfo(req.OperationID, "GetUsersOnlineStatus api return", resp) c.JSON(http.StatusOK, resp) - } diff --git a/internal/api/user/user.go b/internal/api/user/user.go index 1756a9f8d..b3abbbab4 100644 --- a/internal/api/user/user.go +++ b/internal/api/user/user.go @@ -4,9 +4,11 @@ import ( jsonData "Open_IM/internal/utils" api "Open_IM/pkg/base_info" "Open_IM/pkg/common/config" + "Open_IM/pkg/common/constant" "Open_IM/pkg/common/log" "Open_IM/pkg/common/token_verify" "Open_IM/pkg/grpc-etcdv3/getcdv3" + pbRelay "Open_IM/pkg/proto/relay" open_im_sdk "Open_IM/pkg/proto/sdk_ws" rpc "Open_IM/pkg/proto/user" "Open_IM/pkg/utils" @@ -127,3 +129,63 @@ func GetSelfUserInfo(c *gin.Context) { } } + +func GetUsersOnlineStatus(c *gin.Context) { + params := api.GetUsersOnlineStatusReq{} + if err := c.BindJSON(¶ms); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + req := &pbRelay.GetUsersOnlineStatusReq{} + utils.CopyStructFields(req, ¶ms) + var ok bool + ok, req.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID) + if !ok { + log.NewError(req.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token")) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"}) + return + } + log.NewInfo(params.OperationID, "GetUsersOnlineStatus args ", req.String()) + var wsResult []*pbRelay.GetUsersOnlineStatusResp_SuccessResult + var respResult []*pbRelay.GetUsersOnlineStatusResp_SuccessResult + flag := false + 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.NewInfo(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) + } + resp := api.GetUsersOnlineStatusResp{CommResp: api.CommResp{ErrCode: 0, ErrMsg: ""}, SuccessResult: respResult} + if len(respResult) == 0 { + resp.SuccessResult = []*pbRelay.GetUsersOnlineStatusResp_SuccessResult{} + } + log.NewInfo(req.OperationID, "GetUsersOnlineStatus api return", resp) + c.JSON(http.StatusOK, resp) +}