From bbfe65a90500266fc93003392289b2f27ae7f60f Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Thu, 28 Apr 2022 15:10:42 +0800 Subject: [PATCH] send message add cache --- internal/rpc/cache/cache.go | 2 +- internal/rpc/friend/firend.go | 25 +++++++++++++++++ internal/rpc/msg/send_msg.go | 52 ++++++++++++++++++----------------- 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/internal/rpc/cache/cache.go b/internal/rpc/cache/cache.go index 051469327..c45dec4fa 100644 --- a/internal/rpc/cache/cache.go +++ b/internal/rpc/cache/cache.go @@ -222,7 +222,7 @@ func (s *cacheServer) GetBlackIDListFromCache(_ context.Context, req *pbCache.Ge } resp.UserIDList = blackUserIDList log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String()) - return nil, nil + return resp, nil } func (s *cacheServer) AddBlackUserToCache(_ context.Context, req *pbCache.AddBlackUserToCacheReq) (resp *pbCache.AddBlackUserToCacheResp, err error) { diff --git a/internal/rpc/friend/firend.go b/internal/rpc/friend/firend.go index b570b8106..6c4559f59 100644 --- a/internal/rpc/friend/firend.go +++ b/internal/rpc/friend/firend.go @@ -10,6 +10,7 @@ import ( "Open_IM/pkg/common/token_verify" cp "Open_IM/pkg/common/utils" "Open_IM/pkg/grpc-etcdv3/getcdv3" + cacheRpc "Open_IM/pkg/proto/cache" pbFriend "Open_IM/pkg/proto/friend" sdkws "Open_IM/pkg/proto/sdk_ws" "Open_IM/pkg/utils" @@ -83,6 +84,18 @@ func (s *friendServer) AddBlacklist(ctx context.Context, req *pbFriend.AddBlackl return &pbFriend.AddBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil } log.NewInfo(req.CommID.OperationID, "AddBlacklist rpc ok ", req.CommID.FromUserID, req.CommID.ToUserID) + reqAddBlackUserToCache := &cacheRpc.AddBlackUserToCacheReq{UserID: req.CommID.FromUserID, BlackUserID: req.CommID.ToUserID, OperationID: req.CommID.OperationID} + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName) + cacheClient := cacheRpc.NewCacheClient(etcdConn) + cacheResp, err := cacheClient.AddBlackUserToCache(context.Background(), reqAddBlackUserToCache) + if err != nil { + log.NewError(req.CommID.OperationID, "AddBlackUserToCache rpc call failed ", err.Error()) + return &pbFriend.AddBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: 500, ErrMsg: "AddBlackUserToCache rpc call failed"}}, nil + } + if cacheResp.CommonResp.ErrCode != 0 { + log.NewError(req.CommID.OperationID, "AddBlackUserToCache rpc logic call failed ", cacheResp.String()) + return &pbFriend.AddBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: cacheResp.CommonResp.ErrCode, ErrMsg: cacheResp.CommonResp.ErrMsg}}, nil + } chat.BlackAddedNotification(req) return &pbFriend.AddBlacklistResp{CommonResp: &pbFriend.CommonResp{}}, nil } @@ -330,6 +343,18 @@ func (s *friendServer) RemoveBlacklist(ctx context.Context, req *pbFriend.Remove } log.NewInfo(req.CommID.OperationID, "rpc RemoveBlacklist ok ") + reqReduceBlackUserFromCache := &cacheRpc.ReduceBlackUserFromCacheReq{UserID: req.CommID.FromUserID, BlackUserID: req.CommID.ToUserID, OperationID: req.CommID.OperationID} + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName) + cacheClient := cacheRpc.NewCacheClient(etcdConn) + cacheResp, err := cacheClient.ReduceBlackUserFromCache(context.Background(), reqReduceBlackUserFromCache) + if err != nil { + log.NewError(req.CommID.OperationID, "ReduceBlackUserFromCache rpc call failed ", err.Error()) + return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: 500, ErrMsg: "ReduceBlackUserFromCache rpc call failed"}}, nil + } + if cacheResp.CommonResp.ErrCode != 0 { + log.NewError(req.CommID.OperationID, "ReduceBlackUserFromCache rpc logic call failed ", cacheResp.String()) + return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{ErrCode: cacheResp.CommonResp.ErrCode, ErrMsg: cacheResp.CommonResp.ErrMsg}}, nil + } chat.BlackDeletedNotification(req) return &pbFriend.RemoveBlacklistResp{CommonResp: &pbFriend.CommonResp{}}, nil } diff --git a/internal/rpc/msg/send_msg.go b/internal/rpc/msg/send_msg.go index f87278dc5..b80853436 100644 --- a/internal/rpc/msg/send_msg.go +++ b/internal/rpc/msg/send_msg.go @@ -6,9 +6,9 @@ import ( "Open_IM/pkg/common/db" "Open_IM/pkg/common/log" "Open_IM/pkg/grpc-etcdv3/getcdv3" + cacheRpc "Open_IM/pkg/proto/cache" pbChat "Open_IM/pkg/proto/chat" pbConversation "Open_IM/pkg/proto/conversation" - rpc "Open_IM/pkg/proto/friend" pbGroup "Open_IM/pkg/proto/group" sdk_ws "Open_IM/pkg/proto/sdk_ws" "Open_IM/pkg/utils" @@ -48,36 +48,38 @@ func userRelationshipVerification(data *pbChat.SendMsgReq) (bool, int32, string) return true, 0, "" } log.NewDebug(data.OperationID, config.Config.MessageVerify.FriendVerify) - req := &rpc.IsInBlackListReq{CommID: &rpc.CommID{}} - req.CommID.OperationID = data.OperationID - req.CommID.OpUserID = data.MsgData.RecvID - req.CommID.FromUserID = data.MsgData.RecvID - req.CommID.ToUserID = data.MsgData.SendID - etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImFriendName) - client := rpc.NewFriendClient(etcdConn) - reply, err := client.IsInBlackList(context.Background(), req) + reqGetBlackIDListFromCache := &cacheRpc.GetBlackIDListFromCacheReq{UserID: data.MsgData.RecvID, OperationID: data.OperationID} + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName) + cacheClient := cacheRpc.NewCacheClient(etcdConn) + cacheResp, err := cacheClient.GetBlackIDListFromCache(context.Background(), reqGetBlackIDListFromCache) if err != nil { - log.NewDebug(data.OperationID, "IsInBlackListReq rpc failed, ", req.String(), err.Error()) - } else if reply.Response == true { - log.NewDebug(data.OperationID, "IsInBlackListReq ", req.String()) - return false, 600, "in black list" + log.NewError(data.OperationID, "GetBlackIDListFromCache rpc call failed ", err.Error()) + } else { + if cacheResp.CommonResp.ErrCode != 0 { + log.NewError(data.OperationID, "GetBlackIDListFromCache rpc logic call failed ", cacheResp.String()) + } else { + if utils.IsContain(data.MsgData.SendID, cacheResp.UserIDList) { + return false, 600, "in black list" + } + } } log.NewDebug(data.OperationID, config.Config.MessageVerify.FriendVerify) if config.Config.MessageVerify.FriendVerify { - friendReq := &rpc.IsFriendReq{CommID: &rpc.CommID{}} - friendReq.CommID.OperationID = data.OperationID - friendReq.CommID.OpUserID = data.MsgData.RecvID - friendReq.CommID.FromUserID = data.MsgData.RecvID - friendReq.CommID.ToUserID = data.MsgData.SendID - friendReply, err := client.IsFriend(context.Background(), friendReq) + reqGetFriendIDListFromCache := &cacheRpc.GetFriendIDListFromCacheReq{UserID: data.MsgData.RecvID, OperationID: data.OperationID} + etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName) + cacheClient := cacheRpc.NewCacheClient(etcdConn) + cacheResp, err := cacheClient.GetFriendIDListFromCache(context.Background(), reqGetFriendIDListFromCache) if err != nil { - log.NewDebug(data.OperationID, "IsFriendReq rpc failed, ", req.String(), err.Error()) - return true, 0, "" - } else if friendReply.Response == false { - log.NewDebug(data.OperationID, "not friend ", req.String()) - return friendReply.Response, 601, "not friend" + log.NewError(data.OperationID, "GetFriendIDListFromCache rpc call failed ", err.Error()) + } else { + if cacheResp.CommonResp.ErrCode != 0 { + log.NewError(data.OperationID, "GetFriendIDListFromCache rpc logic call failed ", cacheResp.String()) + } else { + if !utils.IsContain(data.MsgData.SendID, cacheResp.UserIDList) { + return false, 601, "not friend" + } + } } - log.NewDebug(data.OperationID, config.Config.MessageVerify.FriendVerify, friendReply.Response) return true, 0, "" } else { return true, 0, ""