diff --git a/cmd/open_im_api/main.go b/cmd/open_im_api/main.go index ff61b1ccb..65ab3aae1 100644 --- a/cmd/open_im_api/main.go +++ b/cmd/open_im_api/main.go @@ -24,8 +24,9 @@ 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_user_info", user.GetUserInfo) //1 + userRouterGroup.POST("/update_user_info", user.UpdateUserInfo) //1 + userRouterGroup.POST("/get_user_info", user.GetUsersInfo) //1 + userRouterGroup.POST("/get_self_user_info", user.GetSelfUserInfo) //1 } //friend routing group friendRouterGroup := r.Group("/friend") diff --git a/internal/api/group/group.go b/internal/api/group/group.go index ef3c599cb..61bc95495 100644 --- a/internal/api/group/group.go +++ b/internal/api/group/group.go @@ -227,11 +227,11 @@ func InviteUserToGroup(c *gin.Context) { resp := api.InviteUserToGroupResp{CommResp: api.CommResp{ErrCode: RpcResp.ErrCode, ErrMsg: RpcResp.ErrMsg}} for _, v := range RpcResp.Id2ResultList { - resp.UserIDResultList = append(resp.UserIDResultList, api.UserIDResult{UserID: v.UserID, Result: v.Result}) + resp.UserIDResultList = append(resp.UserIDResultList, &api.UserIDResult{UserID: v.UserID, Result: v.Result}) } if len(resp.UserIDResultList) == 0 { - resp.UserIDResultList = []api.UserIDResult{} + resp.UserIDResultList = *new([]*api.UserIDResult) } log.NewInfo(req.OperationID, "InviteUserToGroup api return ", resp) diff --git a/internal/api/user/user.go b/internal/api/user/user.go index ebc5decf7..d249486e6 100644 --- a/internal/api/user/user.go +++ b/internal/api/user/user.go @@ -16,8 +16,8 @@ import ( "strings" ) -func GetUserInfo(c *gin.Context) { - params := api.GetUserInfoReq{} +func GetUsersInfo(c *gin.Context) { + params := api.GetUsersInfoReq{} if err := c.BindJSON(¶ms); err != nil { log.NewError("0", "BindJSON failed ", err.Error()) c.JSON(http.StatusBadRequest, gin.H{"errCode": http.StatusBadRequest, "errMsg": err.Error()}) @@ -42,15 +42,20 @@ func GetUserInfo(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"}) return } + var publicUserInfoList []*open_im_sdk.PublicUserInfo + for _, v := range RpcResp.UserInfoList { + publicUserInfoList = append(publicUserInfoList, + &open_im_sdk.PublicUserInfo{UserID: v.UserID, Nickname: v.Nickname, FaceUrl: v.FaceUrl, Gender: v.Gender, AppMangerLevel: v.AppMangerLevel}) + } - resp := api.GetUserInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}, UserInfoList: RpcResp.UserInfoList} + resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}, UserInfoList: publicUserInfoList} resp.Data = jsonData.JsonDataList(resp.UserInfoList) log.NewInfo(req.OperationID, "GetUserInfo api return ", resp) c.JSON(http.StatusOK, resp) } func UpdateUserInfo(c *gin.Context) { - params := api.UpdateUserInfoReq{} + params := api.UpdateSelfUserInfoReq{} if err := c.BindJSON(¶ms); err != nil { log.NewError("0", "BindJSON failed ", err.Error()) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) @@ -80,3 +85,45 @@ func UpdateUserInfo(c *gin.Context) { log.NewInfo(req.OperationID, "UpdateUserInfo api return ", resp) c.JSON(http.StatusOK, resp) } + +func GetSelfUserInfo(c *gin.Context) { + params := api.GetSelfUserInfoReq{} + if err := c.BindJSON(¶ms); err != nil { + log.NewError("0", "BindJSON failed ", err.Error()) + c.JSON(http.StatusBadRequest, gin.H{"errCode": http.StatusBadRequest, "errMsg": err.Error()}) + return + } + req := &rpc.GetUserInfoReq{} + + utils.CopyStructFields(req, ¶ms) + var ok bool + ok, req.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) + 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 + } + req.UserIDList = append(req.UserIDList, req.OpUserID) + log.NewInfo(params.OperationID, "GetUserInfo args ", req.String()) + + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName) + client := rpc.NewUserClient(etcdConn) + RpcResp, err := client.GetUserInfo(context.Background(), req) + if err != nil { + log.NewError(req.OperationID, "GetUserInfo failed ", err.Error(), req.String()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"}) + return + } + if len(RpcResp.UserInfoList) == 1 { + resp := api.GetSelfUserInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}, UserInfoList: RpcResp.UserInfoList[0]} + resp.Data = jsonData.JsonDataList(resp.UserInfoList) + log.NewInfo(req.OperationID, "GetUserInfo api return ", resp) + c.JSON(http.StatusOK, resp) + } else { + resp := api.GetSelfUserInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}} + resp.Data = jsonData.JsonDataList(resp.UserInfoList) + log.NewInfo(req.OperationID, "GetUserInfo api return ", resp) + c.JSON(http.StatusOK, resp) + } + +} diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 322039a75..210c997b5 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -171,7 +171,7 @@ func (s *groupServer) GetJoinedGroupList(ctx context.Context, req *pbGroup.GetJo group, err := imdb.GetGroupInfoByGroupID(v) if num > 0 && owner != nil && err2 == nil && group != nil && err == nil { utils.CopyStructFields(&groupNode, group) - groupNode.CreateTime = group.CreateTime.Unix() + groupNode.CreateTime = uint32(group.CreateTime.Unix()) groupNode.MemberCount = uint32(num) groupNode.OwnerUserID = owner.UserID resp.GroupList = append(resp.GroupList, &groupNode) diff --git a/pkg/base_info/group_api_struct.go b/pkg/base_info/group_api_struct.go index 7c70d5a0e..71301455a 100644 --- a/pkg/base_info/group_api_struct.go +++ b/pkg/base_info/group_api_struct.go @@ -45,7 +45,7 @@ type InviteUserToGroupReq struct { } type InviteUserToGroupResp struct { CommResp - UserIDResultList []UserIDResult `json:"data"` + UserIDResultList []*UserIDResult `json:"data"` } type GetJoinedGroupListReq struct { diff --git a/pkg/base_info/user_api_struct.go b/pkg/base_info/user_api_struct.go index 632c6ea77..3ca191f98 100644 --- a/pkg/base_info/user_api_struct.go +++ b/pkg/base_info/user_api_struct.go @@ -4,17 +4,17 @@ import ( open_im_sdk "Open_IM/pkg/proto/sdk_ws" ) -type GetUserInfoReq struct { +type GetUsersInfoReq struct { OperationID string `json:"operationID" binding:"required"` UserIDList []string `json:"userIDList" binding:"required"` } -type GetUserInfoResp struct { +type GetUsersInfoResp struct { CommResp - UserInfoList []*open_im_sdk.UserInfo `json:"-"` + UserInfoList []*open_im_sdk.PublicUserInfo Data []map[string]interface{} `json:"data"` } -type UpdateUserInfoReq struct { +type UpdateSelfUserInfoReq struct { UserInfo OperationID string `json:"operationID" binding:"required"` } @@ -22,3 +22,13 @@ type UpdateUserInfoReq struct { type UpdateUserInfoResp struct { CommResp } + +type GetSelfUserInfoReq struct { + OperationID string `json:"operationID" binding:"required"` + UserID string `json:"userID" binding:"required"` +} +type GetSelfUserInfoResp struct { + CommResp + UserInfoList *open_im_sdk.UserInfo `json:"-"` + Data []map[string]interface{} `json:"data"` +} diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go index 6b895a8ee..e9bfdcbac 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_member_model.go @@ -92,7 +92,7 @@ func DeleteGroupMemberByGroupIDAndUserID(groupID, userID string) error { if err != nil { return err } - err = dbConn.Table("group_members").Where("group_id=? and user_id=? ", groupID, userID).Delete(&db.GroupMember{}).Error + err = dbConn.Table("group_members").Where("group_id=? and user_id=? ", groupID, userID).Delete(db.GroupMember{}).Error if err != nil { return err } diff --git a/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go b/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go index b1436e17b..84988a3f4 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/group_request_model.go @@ -66,7 +66,7 @@ func DelGroupRequestByGroupIDAndUserID(groupID, userID string) error { if err != nil { return err } - err = dbConn.Table("group_requests").Where("group_id=? and user_id=?", groupID, userID).Delete(&db.GroupRequest{}).Error + err = dbConn.Table("group_requests").Where("group_id=? and user_id=?", groupID, userID).Delete(db.GroupRequest{}).Error if err != nil { return err }