fix work moments bug

pull/218/head
wangchuxiao 3 years ago
parent 1889a283ee
commit 59468eb162

@ -8,6 +8,7 @@ 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"
@ -18,110 +19,6 @@ import (
"strings"
)
//func GetUsersInfoFromCache(c *gin.Context) {
// params := api.GetUsersInfoReq{}
// if err := c.BindJSON(&params); 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, &params)
// 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(&params); err != nil {
@ -129,34 +26,100 @@ func GetUsersInfoFromCache(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": http.StatusBadRequest, "errMsg": err.Error()})
return
}
req := &rpc.GetUserInfoReq{}
utils.CopyStructFields(req, &params)
getUserInfoReq := &rpc.GetUserInfoReq{}
getUserInfoReq.OperationID = params.OperationID
var ok bool
ok, req.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
ok, getUserInfoReq.OpUserID = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), getUserInfoReq.OperationID)
if !ok {
log.NewError(req.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token"))
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 ", req.String())
log.NewInfo(params.OperationID, "GetUserInfo args ", getUserInfoReq.String())
reqCacheGetUserInfo := &cacheRpc.GetUserInfoFromCacheReq{}
utils.CopyStructFields(reqCacheGetUserInfo, &params)
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.GetUserInfoFromCache(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
}
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImUserName)
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(), req)
rpcResp, err := client.GetUserInfo(context.Background(), getUserInfoReq)
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"})
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
}
var publicUserInfoList []*open_im_sdk.PublicUserInfo
for _, v := range RpcResp.UserInfoList {
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.UpdateUserInfoToCacheReq{
UserInfoList: rpcResp.UserInfoList,
OperationID: getUserInfoReq.OperationID,
}
_, err = cacheClient.UpdateUserInfoToCache(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 := 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)
log.NewInfo(getUserInfoReq.OperationID, "GetUserInfo api return ", resp)
c.JSON(http.StatusOK, resp)
}
@ -206,55 +169,6 @@ func GetUsersInfo(c *gin.Context) {
c.JSON(http.StatusOK, resp)
}
//func UpdateUserInfo(c *gin.Context) {
// params := api.UpdateSelfUserInfoReq{}
// if err := c.BindJSON(&params); err != nil {
// log.NewError("0", "BindJSON failed ", err.Error())
// c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
// return
// }
// req := &rpc.UpdateUserInfoReq{UserInfo: &open_im_sdk.UserInfo{}}
// utils.CopyStructFields(req.UserInfo, &params)
//
// req.OperationID = params.OperationID
// 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, "UpdateUserInfo 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.UpdateUserInfo(context.Background(), req)
// if err != nil {
// log.NewError(req.OperationID, "UpdateUserInfo failed ", err.Error(), req.String())
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"})
// return
// }
// if RpcResp.CommonResp.ErrCode != 0 {
// resp := api.UpdateUserInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}}
// log.NewInfo(req.OperationID, "UpdateUserInfo api return ", resp)
// c.JSON(http.StatusOK, resp)
// return
// }
// var updateUserInfoReq = &cacheRpc.UpdateUserInfoReq{UserInfoList: []*open_im_sdk.UserInfo{}}
// updateUserInfoReq.UserInfoList[0] = req.UserInfo
// etcdConn = getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName)
// cacheClient := cacheRpc.NewCacheClient(etcdConn)
// cacheResp, err := cacheClient.UpdateUserInfo(context.Background(), updateUserInfoReq)
// if err != nil {
// log.NewError(req.OperationID, "UpdateUserInfo failed ", err.Error(), req.String())
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"})
// return
// }
// resp := api.UpdateUserInfoResp{CommResp: api.CommResp{ErrCode: cacheResp.CommonResp.ErrCode, ErrMsg: cacheResp.CommonResp.ErrMsg}}
// log.NewInfo(req.OperationID, "UpdateUserInfo api return ", resp)
// c.JSON(http.StatusOK, resp)
//}
func UpdateUserInfo(c *gin.Context) {
params := api.UpdateSelfUserInfoReq{}
if err := c.BindJSON(&params); err != nil {
@ -283,11 +197,60 @@ func UpdateUserInfo(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"})
return
}
if RpcResp.CommonResp.ErrCode != 0 {
resp := api.UpdateUserInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}}
log.NewInfo(req.OperationID, "UpdateUserInfo api return ", resp)
c.JSON(http.StatusOK, resp)
return
}
var updateUserInfoReq = &cacheRpc.UpdateUserInfoToCacheReq{UserInfoList: []*open_im_sdk.UserInfo{}}
updateUserInfoReq.UserInfoList[0] = req.UserInfo
etcdConn = getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName)
cacheClient := cacheRpc.NewCacheClient(etcdConn)
cacheResp, err := cacheClient.UpdateUserInfoToCache(context.Background(), updateUserInfoReq)
if err != nil {
log.NewError(req.OperationID, "UpdateUserInfo failed ", err.Error(), req.String())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"})
return
}
resp := api.UpdateUserInfoResp{CommResp: api.CommResp{ErrCode: cacheResp.CommonResp.ErrCode, ErrMsg: cacheResp.CommonResp.ErrMsg}}
log.NewInfo(req.OperationID, "UpdateUserInfo api return ", resp)
c.JSON(http.StatusOK, resp)
}
//func UpdateUserInfo(c *gin.Context) {
// params := api.UpdateSelfUserInfoReq{}
// if err := c.BindJSON(&params); err != nil {
// log.NewError("0", "BindJSON failed ", err.Error())
// c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
// return
// }
// req := &rpc.UpdateUserInfoReq{UserInfo: &open_im_sdk.UserInfo{}}
// utils.CopyStructFields(req.UserInfo, &params)
//
// req.OperationID = params.OperationID
// 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, "UpdateUserInfo 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.UpdateUserInfo(context.Background(), req)
// if err != nil {
// log.NewError(req.OperationID, "UpdateUserInfo failed ", err.Error(), req.String())
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "call rpc server failed"})
// return
// }
// resp := api.UpdateUserInfoResp{CommResp: api.CommResp{ErrCode: RpcResp.CommonResp.ErrCode, ErrMsg: RpcResp.CommonResp.ErrMsg}}
// 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(&params); err != nil {

@ -4,6 +4,7 @@ import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db"
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbCache "Open_IM/pkg/proto/cache"
@ -46,6 +47,14 @@ func (s *cacheServer) Run() {
log.NewInfo("0", "listen network success, ", registerAddress, listener)
defer listener.Close()
//grpc server
//to cache
err = SyncDB2Cache()
if err != nil {
log.NewError("", err.Error(), "db to cache failed")
panic(err.Error())
}
srv := grpc.NewServer()
defer srv.GracefulStop()
pbCache.RegisterCacheServer(srv, s)
@ -62,14 +71,80 @@ func (s *cacheServer) Run() {
log.NewInfo("0", "message cms rpc success")
}
func (s *cacheServer) GetUserInfo(_ context.Context, req *pbCache.GetUserInfoReq) (resp *pbCache.GetUserInfoResp, err error) {
func SyncDB2Cache() error {
var err error
log.NewInfo("0", utils.GetSelfFuncName())
userList, err := imdb.GetAllUser()
if err != nil {
return utils.Wrap(err, "")
}
err = updateAllUserToCache(userList)
err = updateAllFriendToCache(userList)
err = updateAllBlackListToCache(userList)
return err
}
func updateAllUserToCache(userList []db.User) error {
for _, userInfo := range userList {
userInfoPb := &commonPb.UserInfo{
UserID: userInfo.UserID,
Nickname: userInfo.Nickname,
FaceURL: userInfo.FaceURL,
Gender: userInfo.Gender,
PhoneNumber: userInfo.PhoneNumber,
Birth: uint32(userInfo.Birth.Unix()),
Email: userInfo.Email,
Ex: userInfo.Ex,
CreateTime: uint32(userInfo.CreateTime.Unix()),
AppMangerLevel: userInfo.AppMangerLevel,
}
if err := db.DB.SetUserInfoToCache(userInfoPb); err != nil {
log.NewError("0", utils.GetSelfFuncName(), "set userInfo to cache failed", err.Error())
}
}
log.NewInfo("0", utils.GetSelfFuncName(), "ok")
return nil
}
func updateAllFriendToCache(userList []db.User) error {
log.NewInfo("0", utils.GetSelfFuncName())
for _, user := range userList {
friendIDList, err := imdb.GetFriendIDListByUserID(user.UserID)
if err != nil {
log.NewError("0", utils.GetSelfFuncName(), err.Error())
continue
}
if err := db.DB.AddFriendToCache(user.UserID, friendIDList); err != nil {
log.NewError("0", utils.GetSelfFuncName(), err.Error())
}
}
log.NewInfo("0", utils.GetSelfFuncName(), "ok")
return nil
}
func updateAllBlackListToCache(userList []db.User) error {
log.NewInfo("0", utils.GetSelfFuncName())
for _, user := range userList {
blackIDList, err := imdb.GetBlackIDListByUserID(user.UserID)
if err != nil {
log.NewError("", utils.GetSelfFuncName(), err.Error())
continue
}
if err := db.DB.AddBlackUserToCache(user.UserID, blackIDList); err != nil {
log.NewError("0", utils.GetSelfFuncName(), err.Error())
}
}
log.NewInfo("0", utils.GetSelfFuncName(), "ok")
return nil
}
func (s *cacheServer) GetUserInfoFromCache(_ context.Context, req *pbCache.GetUserInfoFromCacheReq) (resp *pbCache.GetUserInfoFromCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.GetUserInfoResp{
UserInfoList: []*commonPb.UserInfo{},
resp = &pbCache.GetUserInfoFromCacheResp{
CommonResp: &pbCache.CommonResp{},
}
for _, userID := range req.UserIDList {
userInfo, err := db.DB.GetUserInfo(userID)
userInfo, err := db.DB.GetUserInfoFromCache(userID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "get userInfo from cache failed", err.Error())
continue
@ -80,39 +155,98 @@ func (s *cacheServer) GetUserInfo(_ context.Context, req *pbCache.GetUserInfoReq
return resp, nil
}
func (s *cacheServer) UpdateUserInfo(_ context.Context, req *pbCache.UpdateUserInfoReq) (resp *pbCache.UpdateUserInfoResp, err error) {
func (s *cacheServer) UpdateUserInfoToCache(_ context.Context, req *pbCache.UpdateUserInfoToCacheReq) (resp *pbCache.UpdateUserInfoToCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.UpdateUserInfoResp{
resp = &pbCache.UpdateUserInfoToCacheResp{
CommonResp: &pbCache.CommonResp{},
}
for _, userInfo := range req.UserInfoList {
if err := db.DB.SetUserInfo(userInfo); err != nil {
if err := db.DB.SetUserInfoToCache(userInfo); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "set userInfo to cache failed", err.Error())
return resp, nil
}
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil
}
func (s *cacheServer) GetBlackList(_ context.Context, req *pbCache.GetBlackListReq) (resp *pbCache.GetBlackListResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
func (s *cacheServer) GetFriendIDListFromCache(_ context.Context, req *pbCache.GetFriendIDListFromCacheReq) (resp *pbCache.GetFriendIDListFromCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.GetFriendIDListFromCacheResp{CommonResp: &pbCache.CommonResp{}}
friendIDList, err := db.DB.GetFriendIDListFromCache(req.UserID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetFriendIDListFromCache", err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
return
}
resp.UserIDList = friendIDList
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return nil, nil
}
func (s *cacheServer) UpdateBlackList(_ context.Context, req *pbCache.UpdateBlackListReq) (resp *pbCache.UpdateBlackListResp, err error) {
func (s *cacheServer) AddFriendToCache(_ context.Context, req *pbCache.AddFriendToCacheReq) (resp *pbCache.AddFriendToCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.AddFriendToCacheResp{CommonResp: &pbCache.CommonResp{}}
if err := db.DB.AddFriendToCache(req.UserID, []string{req.FriendID}); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "AddFriendToCache failed", err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
return resp, nil
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil
}
func (s *cacheServer) ReduceFriendFromCache(_ context.Context, req *pbCache.ReduceFriendFromCacheReq) (resp *pbCache.ReduceFriendFromCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.ReduceFriendFromCacheResp{CommonResp: &pbCache.CommonResp{}}
if err := db.DB.ReduceFriendToCache(req.UserID, req.FriendID); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "AddFriendToCache failed", err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
return resp, nil
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return nil, nil
}
func (s *cacheServer) GetFriendInfo(_ context.Context, req *pbCache.GetFriendInfoReq) (resp *pbCache.GetFriendInfoResp, err error) {
func (s *cacheServer) GetBlackIDListFromCache(_ context.Context, req *pbCache.GetBlackIDListFromCacheReq) (resp *pbCache.GetBlackIDListFromCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.GetBlackIDListFromCacheResp{CommonResp: &pbCache.CommonResp{}}
blackUserIDList, err := db.DB.GetBlackListFromCache(req.UserID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "AddFriendToCache failed", err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
return resp, nil
}
resp.UserIDList = blackUserIDList
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return nil, nil
}
func (s *cacheServer) UpdateFriendInfo(_ context.Context, req *pbCache.UpdateFriendInfoReq) (resp *pbCache.UpdateFriendInfoResp, err error) {
func (s *cacheServer) AddBlackUserToCache(_ context.Context, req *pbCache.AddBlackUserToCacheReq) (resp *pbCache.AddBlackUserToCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.AddBlackUserToCacheResp{CommonResp: &pbCache.CommonResp{}}
if err := db.DB.AddBlackUserToCache(req.UserID, []string{req.BlackUserID}); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
return resp, nil
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil
}
func (s *cacheServer) ReduceBlackUserFromCache(_ context.Context, req *pbCache.ReduceBlackUserFromCacheReq) (resp *pbCache.ReduceBlackUserFromCacheResp, err error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp = &pbCache.ReduceBlackUserFromCacheResp{CommonResp: &pbCache.CommonResp{}}
if err := db.DB.ReduceBlackUserFromCache(req.UserID, []string{req.BlackUserID}); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
resp.CommonResp.ErrCode = constant.ErrDB.ErrCode
resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg
return resp, nil
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return nil, nil
}

@ -56,3 +56,16 @@ func GetBlackListByUserID(ownerUserID string) ([]db.Black, error) {
}
return blackListUsersInfo, nil
}
func GetBlackIDListByUserID(ownerUserID string) ([]string, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var blackIDList []string
err = dbConn.Table("blacks").Where("owner_user_id=?", ownerUserID).Pluck("block_user_id", &blackIDList).Error
if err != nil {
return nil, err
}
return blackIDList, nil
}

@ -60,6 +60,16 @@ func DeleteUser(userID string) (i int64) {
return i
}
func GetAllUser() ([]db.User, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {
return nil, err
}
var userList []db.User
err = dbConn.Table("users").Find(&userList).Error
return userList, err
}
func GetUserByUserID(userID string) (*db.User, error) {
dbConn, err := db.DB.MysqlDB.DefaultGormDB()
if err != nil {

@ -9,15 +9,17 @@ import (
)
const (
AccountTempCode = "ACCOUNT_TEMP_CODE"
accountTempCode = "ACCOUNT_TEMP_CODE"
resetPwdTempCode = "RESET_PWD_TEMP_CODE"
userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
appleDeviceToken = "DEVICE_TOKEN"
userMinSeq = "REDIS_USER_MIN_SEQ:"
uidPidToken = "UID_PID_TOKEN_STATUS:"
conversationReceiveMessageOpt = "CON_RECV_MSG_OPT:"
GetuiToken = "GETUI"
UserInfoCache = "USER_INFO_CACHE:"
getuiToken = "GETUI"
userInfoCache = "USER_INFO_CACHE:"
friendRelationCache = "FRIEND_RELATION_CACHE:"
blackListCache = "BLACK_LIST_CACHE:"
)
func (d *DataBases) Exec(cmd string, key interface{}, args ...interface{}) (interface{}, error) {
@ -40,16 +42,16 @@ func (d *DataBases) Exec(cmd string, key interface{}, args ...interface{}) (inte
return con.Do(cmd, params...)
}
func (d *DataBases) JudgeAccountEXISTS(account string) (bool, error) {
key := AccountTempCode + account
key := accountTempCode + account
return redis.Bool(d.Exec("EXISTS", key))
}
func (d *DataBases) SetAccountCode(account string, code, ttl int) (err error) {
key := AccountTempCode + account
key := accountTempCode + account
_, err = d.Exec("SET", key, code, "ex", ttl)
return err
}
func (d *DataBases) GetAccountCode(account string) (string, error) {
key := AccountTempCode + account
key := accountTempCode + account
return redis.String(d.Exec("GET", key))
}
@ -150,27 +152,32 @@ func (d *DataBases) GetMultiConversationMsgOpt(userID string, conversationIDs []
}
func (d *DataBases) SetGetuiToken(token string, expireTime int64) error {
_, err := d.Exec("SET", GetuiToken, token, "ex", expireTime)
_, err := d.Exec("SET", getuiToken, token, "ex", expireTime)
return err
}
func (d *DataBases) GetGetuiToken() (string, error) {
result, err := redis.String(d.Exec("GET", GetuiToken))
result, err := redis.String(d.Exec("GET", getuiToken))
return result, err
}
func (d *DataBases) SetUserInfo(userInfo *pbCommon.UserInfo) error {
func (d *DataBases) SearchContentType() {
}
func (d *DataBases) SetUserInfoToCache(userInfo *pbCommon.UserInfo) error {
b, _ := json.Marshal(&userInfo)
m := map[string]interface{}{}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
_, err := d.Exec("hmset", UserInfoCache+userInfo.UserID, redis.Args{}.Add().AddFlat(m)...)
_, err := d.Exec("hmset", userInfoCache+userInfo.UserID, redis.Args{}.Add().AddFlat(m)...)
return err
}
func (d *DataBases) GetUserInfo(userID string) (*pbCommon.UserInfo, error) {
result, err := redis.String(d.Exec("HGETALL", UserInfoCache+userID))
func (d *DataBases) GetUserInfoFromCache(userID string) (*pbCommon.UserInfo, error) {
result, err := redis.String(d.Exec("hgetall", userInfoCache+userID))
log2.NewInfo("", result)
if err != nil {
return nil, err
}
@ -178,3 +185,33 @@ func (d *DataBases) GetUserInfo(userID string) (*pbCommon.UserInfo, error) {
err = json.Unmarshal([]byte(result), userInfo)
return userInfo, err
}
func (d *DataBases) AddFriendToCache(userID string, friendIDList ...interface{}) error {
_, err := redis.String(d.Exec("SADD", friendRelationCache+userID, friendIDList...))
return err
}
func (d *DataBases) ReduceFriendToCache(userID string, friendIDList ...interface{}) error {
_, err := redis.String(d.Exec("SREM", friendRelationCache+userID, friendIDList...))
return err
}
func (d *DataBases) GetFriendIDListFromCache(userID string) ([]string, error) {
result, err := redis.Strings(d.Exec("SMEMBERS", friendRelationCache+userID))
return result, err
}
func (d *DataBases) AddBlackUserToCache(userID string, blackList ...interface{}) error {
_, err := redis.String(d.Exec("SADD", blackListCache+userID, blackList...))
return err
}
func (d *DataBases) ReduceBlackUserFromCache(userID string, blackList ...interface{}) error {
_, err := redis.String(d.Exec("SREM", blackListCache+userID, blackList...))
return err
}
func (d *DataBases) GetBlackListFromCache(userID string) ([]string, error) {
result, err := redis.Strings(d.Exec("SMEMBERS", blackListCache+userID))
return result, err
}

File diff suppressed because it is too large Load Diff

@ -8,71 +8,100 @@ message CommonResp{
string errMsg = 2;
}
message GetUserInfoReq{
message GetUserInfoFromCacheReq{
repeated string userIDList = 1;
string operationID = 3;
string operationID = 2;
}
message GetUserInfoResp{
message GetUserInfoFromCacheResp{
CommonResp commonResp = 1;
repeated server_api_params.UserInfo userInfoList = 2;
}
message UpdateUserInfoReq{
message UpdateUserInfoToCacheReq{
repeated server_api_params.UserInfo userInfoList = 1;
string operationID = 2;
}
message UpdateUserInfoResp{
message UpdateUserInfoToCacheResp{
CommonResp commonResp = 1;
}
message GetFriendInfoReq {
message GetFriendIDListFromCacheReq {
string userID = 1;
string operationID = 2;
}
message GetFriendInfoResp {
repeated server_api_params.FriendInfo friendInfoList = 1;
message GetFriendIDListFromCacheResp {
repeated string userIDList = 1;
CommonResp commonResp = 2;
}
message UpdateBlackListReq {
repeated server_api_params.BlackInfo blackList = 1;
string operationID = 2;
message AddFriendToCacheReq {
string userID = 1;
string friendID = 2;
string operationID = 3;
}
message AddFriendToCacheResp {
CommonResp commonResp = 1;
}
message ReduceFriendFromCacheReq {
string userID = 1;
string friendID = 2;
string operationID = 3;
}
message UpdateBlackListResp {
message ReduceFriendFromCacheResp {
CommonResp commonResp = 1;
}
message GetBlackListReq {
message GetBlackIDListFromCacheReq {
string userID = 1;
string operationID = 2;
}
message GetBlackListResp {
repeated server_api_params.BlackInfo blackList = 1;
message GetBlackIDListFromCacheResp {
repeated string userIDList = 1;
CommonResp commonResp = 2;
}
message AddBlackUserToCacheReq {
string userID = 1;
string blackUserID = 2;
string operationID = 3;
}
message AddBlackUserToCacheResp {
CommonResp commonResp = 1;
}
message UpdateFriendInfoReq{
repeated server_api_params.FriendInfo friendInfoList = 1;
string operationID = 2;
message ReduceBlackUserFromCacheReq {
string userID = 1;
string blackUserID = 2;
string operationID = 3;
}
message UpdateFriendInfoResp{
message ReduceBlackUserFromCacheResp {
CommonResp commonResp = 1;
}
service cache{
rpc GetUserInfo(GetUserInfoReq) returns(GetUserInfoResp);
rpc UpdateUserInfo(UpdateUserInfoReq) returns(UpdateUserInfoResp);
rpc GetFriendInfo(GetFriendInfoReq) returns(GetFriendInfoResp);
rpc UpdateFriendInfo(UpdateFriendInfoReq) returns(UpdateFriendInfoResp);
rpc UpdateBlackList(UpdateBlackListReq) returns(UpdateBlackListResp);
rpc GetBlackList(GetBlackListReq) returns(GetBlackListResp);
// userInfo
rpc GetUserInfoFromCache(GetUserInfoFromCacheReq) returns(GetUserInfoFromCacheResp);
rpc UpdateUserInfoToCache(UpdateUserInfoToCacheReq) returns(UpdateUserInfoToCacheResp);
// friendInfo
rpc GetFriendIDListFromCache(GetFriendIDListFromCacheReq) returns(GetFriendIDListFromCacheResp);
rpc AddFriendToCache(AddFriendToCacheReq) returns(AddFriendToCacheResp);
rpc ReduceFriendFromCache(ReduceFriendFromCacheReq) returns(ReduceFriendFromCacheResp);
// blackList
rpc GetBlackIDListFromCache(GetBlackIDListFromCacheReq) returns(GetBlackIDListFromCacheResp);
rpc AddBlackUserToCache(AddBlackUserToCacheReq) returns(AddBlackUserToCacheResp);
rpc ReduceBlackUserFromCache(ReduceBlackUserFromCacheReq) returns(ReduceBlackUserFromCacheResp);
}

Loading…
Cancel
Save