diff --git a/internal/api/user/user.go b/internal/api/user/user.go index d6b733f03..437774972 100644 --- a/internal/api/user/user.go +++ b/internal/api/user/user.go @@ -8,7 +8,6 @@ import ( "Open_IM/pkg/common/log" "Open_IM/pkg/common/token_verify" "Open_IM/pkg/grpc-etcdv3/getcdv3" - cacheRpc "Open_IM/pkg/proto/cache" pbRelay "Open_IM/pkg/proto/relay" open_im_sdk "Open_IM/pkg/proto/sdk_ws" rpc "Open_IM/pkg/proto/user" @@ -19,6 +18,110 @@ import ( "strings" ) +//func GetUsersInfoFromCache(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()}) +// return +// } +// getUserInfoReq := &rpc.GetUserInfoReq{} +// getUserInfoReq.OperationID = params.OperationID +// var ok bool +// ok, getUserInfoReq.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), getUserInfoReq.OperationID) +// if !ok { +// log.NewError(getUserInfoReq.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token")) +// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"}) +// return +// } +// log.NewInfo(params.OperationID, "GetUserInfo args ", getUserInfoReq.String()) +// reqCacheGetUserInfo := &cacheRpc.GetUserInfoReq{} +// utils.CopyStructFields(reqCacheGetUserInfo, ¶ms) +// var userInfoList []*open_im_sdk.UserInfo +// var publicUserInfoList []*open_im_sdk.PublicUserInfo +// etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName) +// cacheClient := cacheRpc.NewCacheClient(etcdConn) +// cacheResp, err := cacheClient.GetUserInfo(context.Background(), reqCacheGetUserInfo) +// if err != nil { +// log.NewError(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "GetUserInfo failed", err.Error()) +// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed: " + err.Error()}) +// return +// } +// if cacheResp.CommonResp.ErrCode != 0 { +// log.NewError(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "GetUserInfo failed", cacheResp.CommonResp) +// resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: cacheResp.CommonResp.ErrCode, ErrMsg: cacheResp.CommonResp.ErrMsg}} +// resp.Data = []map[string]interface{}{} +// log.NewInfo(getUserInfoReq.OperationID, "GetUserInfo api return ", resp) +// c.JSON(http.StatusOK, resp) +// return +// } +// log.NewInfo(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "cacheResp:", cacheResp.String()) +// userInfoList = cacheResp.UserInfoList +// var needCacheUserIDList []string +// for _, userID := range reqCacheGetUserInfo.UserIDList { +// isGetUserInfoFromCache := false +// for _, cacheUser := range userInfoList { +// if cacheUser.UserID == userID { +// isGetUserInfoFromCache = true +// } +// } +// if !isGetUserInfoFromCache { +// needCacheUserIDList = append(needCacheUserIDList, userID) +// } +// } +// if len(needCacheUserIDList) == 0 { +// log.NewInfo(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "get all userInfo from cache success") +// for _, v := range userInfoList { +// publicUserInfoList = append(publicUserInfoList, +// &open_im_sdk.PublicUserInfo{UserID: v.UserID, Nickname: v.Nickname, FaceURL: v.FaceURL, Gender: v.Gender, Ex: v.Ex}) +// } +// resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: cacheResp.CommonResp.ErrCode, ErrMsg: cacheResp.CommonResp.ErrMsg}, UserInfoList: publicUserInfoList} +// resp.Data = jsonData.JsonDataList(resp.UserInfoList) +// log.NewInfo(getUserInfoReq.OperationID, "GetUserInfo api return ", resp) +// c.JSON(http.StatusOK, resp) +// return +// } +// +// log.NewDebug(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "need cache user list", needCacheUserIDList) +// getUserInfoReq.UserIDList = needCacheUserIDList +// 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(), getUserInfoReq) +// if err != nil { +// log.NewError(getUserInfoReq.OperationID, "GetUserInfo failed ", err.Error(), getUserInfoReq.String()) +// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed" + err.Error()}) +// return +// } +// if rpcResp.CommonResp.ErrCode != 0 { +// log.NewError(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "GetUserInfo failed", cacheResp.CommonResp) +// resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: cacheResp.CommonResp.ErrCode, ErrMsg: cacheResp.CommonResp.ErrMsg}} +// resp.Data = []map[string]interface{}{} +// log.NewInfo(getUserInfoReq.OperationID, "GetUserInfo api return ", resp) +// c.JSON(http.StatusOK, resp) +// return +// } +// userInfoList = append(userInfoList, rpcResp.UserInfoList...) +// cacheUpdateUserInfoReq := &cacheRpc.UpdateUserInfoReq{ +// UserInfoList: rpcResp.UserInfoList, +// OperationID: getUserInfoReq.OperationID, +// } +// _, err = cacheClient.UpdateUserInfo(context.Background(), cacheUpdateUserInfoReq) +// if err != nil { +// log.NewError(getUserInfoReq.OperationID, "GetUserInfo failed ", err.Error()) +// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed:" + err.Error()}) +// return +// } +// userInfoList = rpcResp.UserInfoList +// for _, v := range userInfoList { +// publicUserInfoList = append(publicUserInfoList, +// &open_im_sdk.PublicUserInfo{UserID: v.UserID, Nickname: v.Nickname, FaceURL: v.FaceURL, Gender: v.Gender, Ex: v.Ex}) +// } +// resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: rpcResp.CommonResp.ErrCode, ErrMsg: rpcResp.CommonResp.ErrMsg}, UserInfoList: publicUserInfoList} +// resp.Data = jsonData.JsonDataList(resp.UserInfoList) +// log.NewInfo(getUserInfoReq.OperationID, "GetUserInfo api return ", resp) +// c.JSON(http.StatusOK, resp) +//} + func GetUsersInfoFromCache(c *gin.Context) { params := api.GetUsersInfoReq{} if err := c.BindJSON(¶ms); err != nil { @@ -26,100 +129,34 @@ func GetUsersInfoFromCache(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": http.StatusBadRequest, "errMsg": err.Error()}) return } - getUserInfoReq := &rpc.GetUserInfoReq{} - getUserInfoReq.OperationID = params.OperationID + req := &rpc.GetUserInfoReq{} + utils.CopyStructFields(req, ¶ms) var ok bool - ok, getUserInfoReq.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), getUserInfoReq.OperationID) + ok, req.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID) if !ok { - log.NewError(getUserInfoReq.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token")) + 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, "GetUserInfo args ", getUserInfoReq.String()) - reqCacheGetUserInfo := &cacheRpc.GetUserInfoReq{} - utils.CopyStructFields(reqCacheGetUserInfo, ¶ms) - var userInfoList []*open_im_sdk.UserInfo - var publicUserInfoList []*open_im_sdk.PublicUserInfo - etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName) - cacheClient := cacheRpc.NewCacheClient(etcdConn) - cacheResp, err := cacheClient.GetUserInfo(context.Background(), reqCacheGetUserInfo) - if err != nil { - log.NewError(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "GetUserInfo failed", err.Error()) - c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed: " + err.Error()}) - return - } - if cacheResp.CommonResp.ErrCode != 0 { - log.NewError(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "GetUserInfo failed", cacheResp.CommonResp) - resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: cacheResp.CommonResp.ErrCode, ErrMsg: cacheResp.CommonResp.ErrMsg}} - resp.Data = []map[string]interface{}{} - log.NewInfo(getUserInfoReq.OperationID, "GetUserInfo api return ", resp) - c.JSON(http.StatusOK, resp) - return - } - log.NewInfo(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "cacheResp:", cacheResp.String()) - userInfoList = cacheResp.UserInfoList - var needCacheUserIDList []string - for _, userID := range reqCacheGetUserInfo.UserIDList { - isGetUserInfoFromCache := false - for _, cacheUser := range userInfoList { - if cacheUser.UserID == userID { - isGetUserInfoFromCache = true - } - } - if !isGetUserInfoFromCache { - needCacheUserIDList = append(needCacheUserIDList, userID) - } - } - if len(needCacheUserIDList) == 0 { - log.NewInfo(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "get all userInfo from cache success") - for _, v := range userInfoList { - publicUserInfoList = append(publicUserInfoList, - &open_im_sdk.PublicUserInfo{UserID: v.UserID, Nickname: v.Nickname, FaceURL: v.FaceURL, Gender: v.Gender, Ex: v.Ex}) - } - resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: cacheResp.CommonResp.ErrCode, ErrMsg: cacheResp.CommonResp.ErrMsg}, UserInfoList: publicUserInfoList} - resp.Data = jsonData.JsonDataList(resp.UserInfoList) - log.NewInfo(getUserInfoReq.OperationID, "GetUserInfo api return ", resp) - c.JSON(http.StatusOK, resp) - return - } + log.NewInfo(params.OperationID, "GetUserInfo args ", req.String()) - log.NewDebug(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "need cache user list", needCacheUserIDList) - getUserInfoReq.UserIDList = needCacheUserIDList - etcdConn = getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName) + 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(), getUserInfoReq) - if err != nil { - log.NewError(getUserInfoReq.OperationID, "GetUserInfo failed ", err.Error(), getUserInfoReq.String()) - c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed" + err.Error()}) - return - } - if rpcResp.CommonResp.ErrCode != 0 { - log.NewError(getUserInfoReq.OperationID, utils.GetSelfFuncName(), "GetUserInfo failed", cacheResp.CommonResp) - resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: cacheResp.CommonResp.ErrCode, ErrMsg: cacheResp.CommonResp.ErrMsg}} - resp.Data = []map[string]interface{}{} - log.NewInfo(getUserInfoReq.OperationID, "GetUserInfo api return ", resp) - c.JSON(http.StatusOK, resp) - return - } - userInfoList = append(userInfoList, rpcResp.UserInfoList...) - cacheUpdateUserInfoReq := &cacheRpc.UpdateUserInfoReq{ - UserInfoList: rpcResp.UserInfoList, - OperationID: getUserInfoReq.OperationID, - } - _, err = cacheClient.UpdateUserInfo(context.Background(), cacheUpdateUserInfoReq) + RpcResp, err := client.GetUserInfo(context.Background(), req) if err != nil { - log.NewError(getUserInfoReq.OperationID, "GetUserInfo failed ", err.Error()) - c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed:" + err.Error()}) + log.NewError(req.OperationID, "GetUserInfo failed ", err.Error(), req.String()) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"}) return } - userInfoList = rpcResp.UserInfoList - for _, v := range userInfoList { + 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, Ex: v.Ex}) } - resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: rpcResp.CommonResp.ErrCode, ErrMsg: rpcResp.CommonResp.ErrMsg}, UserInfoList: publicUserInfoList} + + resp := api.GetUsersInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}, UserInfoList: publicUserInfoList} resp.Data = jsonData.JsonDataList(resp.UserInfoList) - log.NewInfo(getUserInfoReq.OperationID, "GetUserInfo api return ", resp) + log.NewInfo(req.OperationID, "GetUserInfo api return ", resp) c.JSON(http.StatusOK, resp) } diff --git a/pkg/common/db/model.go b/pkg/common/db/model.go index 2fe0ed367..e50d7dc11 100644 --- a/pkg/common/db/model.go +++ b/pkg/common/db/model.go @@ -71,24 +71,14 @@ func init() { opts := options.CreateIndexes().SetMaxTime(10 * time.Second) dataBase := mongoClient.Database(config.Config.Mongo.DBDatabase) - cCommentMsgModels := []mongo.IndexModel{ - { - Keys: bson.A{bson.D{{"create_time", -1}, {"user_id", -1}}}, - }, - } - result, err := dataBase.Collection(cCommentMsg).Indexes().CreateMany(context.Background(), cCommentMsgModels, opts) - if err != nil { - fmt.Println("mongodb create cCommentMsgModels failed", result, err.Error()) - } - cSendLogModels := []mongo.IndexModel{ { - Keys: bson.M{"user_id": -1, "send_time": -1}, + Keys: bson.M{"user_id": -1}, }, } - result, err = dataBase.Collection(cSendLog).Indexes().CreateMany(context.Background(), cSendLogModels, opts) + result, err := dataBase.Collection(cSendLog).Indexes().CreateMany(context.Background(), cSendLogModels, opts) if err != nil { - fmt.Println("mongodb create cSendLogModels failed", result, err.Error()) + //fmt.Println("mongodb create cSendLogModels failed", result, err.Error()) } cChatModels := []mongo.IndexModel{ @@ -106,12 +96,12 @@ func init() { Keys: bson.M{"work_moment_id": -1}, }, { - Keys: bson.M{"user_id": -1, "create_time": -1}, + Keys: bson.M{"user_id": -1}, }, } result, err = dataBase.Collection(cWorkMoment).Indexes().CreateMany(context.Background(), cWorkMomentModels, opts) if err != nil { - fmt.Println("mongodb create cWorkMomentModels failed", result, err.Error()) + //fmt.Println("mongodb create cWorkMomentModels failed", result, err.Error()) } cTagModels := []mongo.IndexModel{ @@ -119,12 +109,12 @@ func init() { Keys: bson.M{"tag_id": -1}, }, { - Keys: bson.M{"user_id": -1, "tag_id": -1}, + Keys: bson.M{"user_id": -1}, }, } result, err = dataBase.Collection(cTag).Indexes().CreateMany(context.Background(), cTagModels, opts) if err != nil { - fmt.Println("mongodb create cTagModels failed", result, err.Error()) + //fmt.Println("mongodb create cTagModels failed", result, err.Error()) } DB.mongoClient = mongoClient