From 5baf05ba5e497f38e1816a3037d85f5f1eb90d62 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Tue, 14 Feb 2023 22:04:03 +0800 Subject: [PATCH] Error code standardization --- internal/common/check/black.go | 21 ++++++++ internal/common/check/conversation.go | 4 ++ internal/common/check/friend.go | 5 ++ internal/common/check/user.go | 4 ++ internal/rpc/msg/send_msg.go | 77 ++++++++------------------- internal/rpc/msg/send_pull.go | 14 ++--- internal/rpc/msg/server.go | 9 +++- pkg/proto/user/user.proto | 10 +++- 8 files changed, 79 insertions(+), 65 deletions(-) create mode 100644 internal/common/check/black.go diff --git a/internal/common/check/black.go b/internal/common/check/black.go new file mode 100644 index 000000000..3e15bec0b --- /dev/null +++ b/internal/common/check/black.go @@ -0,0 +1,21 @@ +package check + +import ( + discoveryRegistry "Open_IM/pkg/discoveryregistry" + "context" +) + +type BlackChecker struct { + zk discoveryRegistry.SvcDiscoveryRegistry +} + +func NewBlackChecker(zk discoveryRegistry.SvcDiscoveryRegistry) *BlackChecker { + return &BlackChecker{ + zk: zk, + } +} + +// possibleBlackUserID是否被userID拉黑,也就是是否在userID的黑名单中 +func (b *BlackChecker) IsBlocked(ctx context.Context, possibleBlackUserID, userID string) (bool, error) { + +} diff --git a/internal/common/check/conversation.go b/internal/common/check/conversation.go index 5d6e25537..c56330c5b 100644 --- a/internal/common/check/conversation.go +++ b/internal/common/check/conversation.go @@ -29,3 +29,7 @@ func (c *ConversationChecker) ModifyConversationField(ctx context.Context, req * func (c *ConversationChecker) getConn() (*grpc.ClientConn, error) { return c.zk.GetConn(config.Config.RpcRegisterName.OpenImConversationName) } + +func (c *ConversationChecker) GetSingleConversationRecvMsgOpt(ctx context.Context, userID, conversationID string) (int32, error) { + +} diff --git a/internal/common/check/friend.go b/internal/common/check/friend.go index 6c258a38c..6c4a50a48 100644 --- a/internal/common/check/friend.go +++ b/internal/common/check/friend.go @@ -34,3 +34,8 @@ func (f *FriendChecker) GetFriendsInfo(ctx context.Context, ownerUserID, friendU func (f *FriendChecker) getConn() (*grpc.ClientConn, error) { return f.zk.GetConn(config.Config.RpcRegisterName.OpenImFriendName) } + +// possibleFriendUserID是否在userID的好友中 +func (f *FriendChecker) IsFriend(ctx context.Context, possibleFriendUserID, userID string) (bool, error) { + +} diff --git a/internal/common/check/user.go b/internal/common/check/user.go index 8178cbf1a..5e5b38ee7 100644 --- a/internal/common/check/user.go +++ b/internal/common/check/user.go @@ -102,3 +102,7 @@ func (u *UserCheck) GetPublicUserInfoMap(ctx context.Context, userIDs []string, return e.UserID }), nil } + +func (u *UserCheck) GetUserGlobalMsgRecvOpt(ctx context.Context, userID string) (int32, error) { + +} diff --git a/internal/rpc/msg/send_msg.go b/internal/rpc/msg/send_msg.go index c9ec6bd8c..150da4ae1 100644 --- a/internal/rpc/msg/send_msg.go +++ b/internal/rpc/msg/send_msg.go @@ -1,10 +1,8 @@ package msg import ( - "Open_IM/internal/common/check" "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" - "Open_IM/pkg/common/db/localcache" "Open_IM/pkg/common/tracelog" "Open_IM/pkg/proto/msg" "Open_IM/pkg/proto/sdkws" @@ -58,8 +56,8 @@ type MsgCallBackResp struct { } } -func userIsMuteAndIsAdminInGroup(ctx context.Context, groupID, userID string) (isMute bool, err error) { - groupMemberInfo, err := check.NewGroupChecker().GetGroupMemberInfo(ctx, groupID, userID) +func (m *msgServer) userIsMuteAndIsAdminInGroup(ctx context.Context, groupID, userID string) (isMute bool, err error) { + groupMemberInfo, err := m.Group.GetGroupMemberInfo(ctx, groupID, userID) if err != nil { return false, err } @@ -70,14 +68,14 @@ func userIsMuteAndIsAdminInGroup(ctx context.Context, groupID, userID string) (i } // 如果禁言了,再看下是否群管理员 -func groupIsMuted(ctx context.Context, groupID string, userID string) (bool, bool, error) { - groupInfo, err := check.NewGroupChecker().GetGroupInfo(ctx, groupID) +func (m *msgServer) groupIsMuted(ctx context.Context, groupID string, userID string) (bool, bool, error) { + groupInfo, err := m.Group.GetGroupInfo(ctx, groupID) if err != nil { return false, false, err } if groupInfo.Status == constant.GroupStatusMuted { - groupMemberInfo, err := check.NewGroupChecker().GetGroupMemberInfo(ctx, groupID, userID) + groupMemberInfo, err := m.Group.GetGroupMemberInfo(ctx, groupID, userID) if err != nil { return false, false, err } @@ -86,24 +84,10 @@ func groupIsMuted(ctx context.Context, groupID string, userID string) (bool, boo return false, false, nil } -func GetGroupMemberIDs(ctx context.Context, groupID string) (groupMemberIDs []string, err error) { - return localcache.NewGroupMemberIDsLocalCache().GetGroupMemberIDs(ctx, groupID) +func (m *msgServer) GetGroupMemberIDs(ctx context.Context, groupID string) (groupMemberIDs []string, err error) { + return m.GroupLocalCache.GetGroupMemberIDs(ctx, groupID) } -//func GetGroupInfo(ctx context.Context, groupID string) (*sdkws.GroupInfo, error) { -// return check.NewGroupChecker().GetGroupInfo(ctx, groupID) -// -// -//} - -//func GetGroupMemberInfo(ctx context.Context, groupID string, userID string) (*sdkws.GroupMemberFullInfo, error) { -// check.NewGroupChecker().GetGroupMemberInfo -//} - -//func (m *msgServer)GetSuperGroupMsg(ctx context.Context, groupID string, seq uint32) (*sdkws.MsgData, error) { -// return m.MsgInterface.GetSuperGroupMsg(ctx, groupID, seq) -//} - func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgReq) ([]string, error) { switch data.MsgData.SessionType { case constant.SingleChatType: @@ -113,7 +97,7 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe if data.MsgData.ContentType <= constant.NotificationEnd && data.MsgData.ContentType >= constant.NotificationBegin { return nil, nil } - black, err := IsBlocked(data.MsgData.SendID, data.MsgData.RecvID) + black, err := m.black.IsBlocked(ctx, data.MsgData.SendID, data.MsgData.RecvID) if err != nil { return nil, err } @@ -121,7 +105,7 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe return nil, constant.ErrBlockedByPeer.Wrap() } if *config.Config.MessageVerify.FriendVerify { - friend, err := IsFriend(data.MsgData.SendID, data.MsgData.RecvID) + friend, err := m.friend.IsFriend(ctx, data.MsgData.SendID, data.MsgData.RecvID) if err != nil { return nil, err } @@ -135,7 +119,7 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe if utils.IsContain(data.MsgData.SendID, config.Config.Manager.AppManagerUid) { return nil, nil } - userIDList, err := GetGroupMemberIDs(ctx, data.MsgData.GroupID) + userIDList, err := m.GetGroupMemberIDs(ctx, data.MsgData.GroupID) if err != nil { return nil, err } @@ -148,7 +132,7 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe if !utils.IsContain(data.MsgData.SendID, userIDList) { return nil, constant.ErrNotInGroupYet.Wrap() } - isMute, err := userIsMuteAndIsAdminInGroup(ctx, data.MsgData.GroupID, data.MsgData.SendID) + isMute, err := m.userIsMuteAndIsAdminInGroup(ctx, data.MsgData.GroupID, data.MsgData.SendID) if err != nil { return nil, err } @@ -156,7 +140,7 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe return nil, constant.ErrMutedInGroup.Wrap() } - isMute, isAdmin, err := groupIsMuted(ctx, data.MsgData.GroupID, data.MsgData.SendID) + isMute, isAdmin, err := m.groupIsMuted(ctx, data.MsgData.GroupID, data.MsgData.SendID) if err != nil { return nil, err } @@ -169,7 +153,7 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe } return userIDList, nil case constant.SuperGroupChatType: - groupInfo, err := check.NewGroupChecker().GetGroupInfo(ctx, data.MsgData.GroupID) + groupInfo, err := m.Group.GetGroupInfo(ctx, data.MsgData.GroupID) if err != nil { return nil, err } @@ -199,7 +183,7 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe return nil, nil } - userIDList, err := GetGroupMemberIDs(ctx, data.MsgData.GroupID) + userIDList, err := m.GetGroupMemberIDs(ctx, data.MsgData.GroupID) if err != nil { return nil, err } @@ -213,7 +197,7 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe return nil, constant.ErrNotInGroupYet.Wrap() } } - isMute, err := userIsMuteAndIsAdminInGroup(ctx, data.MsgData.GroupID, data.MsgData.SendID) + isMute, err := m.userIsMuteAndIsAdminInGroup(ctx, data.MsgData.GroupID, data.MsgData.SendID) if err != nil { return nil, err } @@ -221,7 +205,7 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe return nil, constant.ErrMutedInGroup.Wrap() } - isMute, isAdmin, err := groupIsMuted(ctx, data.MsgData.GroupID, data.MsgData.SendID) + isMute, isAdmin, err := m.groupIsMuted(ctx, data.MsgData.GroupID, data.MsgData.SendID) if err != nil { return nil, err } @@ -289,27 +273,8 @@ func GetMsgID(sendID string) string { return utils.Md5(t + "-" + sendID + "-" + strconv.Itoa(rand.Int())) } -func GetUserGlobalMsgRecvOpt(userID string) (int32, error) { - -} - -// possibleBlackUserID是否被userID拉黑,也就是是否在userID的黑名单中 -func IsBlocked(possibleBlackUserID, userID string) (bool, error) { - -} - -// possibleFriendUserID是否在userID的好友中 -func IsFriend(possibleFriendUserID, userID string) (bool, error) { - -} - -// 没找到不返回错误 -func GetSingleConversationRecvMsgOpt(userID, conversationID string) (int32, error) { - -} - -func modifyMessageByUserMessageReceiveOpt(userID, sourceID string, sessionType int, pb *msg.SendMsgReq) (bool, error) { - opt, err := GetUserGlobalMsgRecvOpt(userID) +func (m *msgServer) modifyMessageByUserMessageReceiveOpt(ctx context.Context, userID, sourceID string, sessionType int, pb *msg.SendMsgReq) (bool, error) { + opt, err := m.User.GetUserGlobalMsgRecvOpt(ctx, userID) if err != nil { return false, err } @@ -325,7 +290,7 @@ func modifyMessageByUserMessageReceiveOpt(userID, sourceID string, sessionType i return true, nil } conversationID := utils.GetConversationIDBySessionType(sourceID, sessionType) - singleOpt, err := GetSingleConversationRecvMsgOpt(userID, conversationID) + singleOpt, err := m.Conversation.GetSingleConversationRecvMsgOpt(ctx, userID, conversationID) if err != nil { return false, err } @@ -334,7 +299,7 @@ func modifyMessageByUserMessageReceiveOpt(userID, sourceID string, sessionType i return true, nil case constant.NotReceiveMessage: if utils.IsContainInt(int(pb.MsgData.ContentType), ExcludeContentType) { - return true + return true, nil } return false, nil case constant.ReceiveNotNotifyMessage: @@ -378,7 +343,7 @@ func (m *msgServer) sendMsgToGroupOptimization(ctx context.Context, list []strin options[k] = v } groupPB.MsgData.Options = options - isSend, err := modifyMessageByUserMessageReceiveOpt(v, groupPB.MsgData.GroupID, constant.GroupChatType, groupPB) + isSend, err := m.modifyMessageByUserMessageReceiveOpt(ctx, v, groupPB.MsgData.GroupID, constant.GroupChatType, groupPB) if err != nil { wg.Done() return err diff --git a/internal/rpc/msg/send_pull.go b/internal/rpc/msg/send_pull.go index 4493f9768..904e4f402 100644 --- a/internal/rpc/msg/send_pull.go +++ b/internal/rpc/msg/send_pull.go @@ -15,7 +15,7 @@ import ( func (m *msgServer) sendMsgSuperGroupChat(ctx context.Context, req *msg.SendMsgReq) (resp *msg.SendMsgResp, err error) { promePkg.PromeInc(promePkg.WorkSuperGroupChatMsgRecvSuccessCounter) // callback - if err = callbackBeforeSendGroupMsg(req); err != nil { + if err = CallbackBeforeSendGroupMsg(ctx, req); err != nil { return nil, err } @@ -29,7 +29,7 @@ func (m *msgServer) sendMsgSuperGroupChat(ctx context.Context, req *msg.SendMsgR return nil, err } // callback - if err = callbackAfterSendGroupMsg(req); err != nil { + if err = CallbackAfterSendGroupMsg(ctx, req); err != nil { return nil, err } @@ -60,7 +60,7 @@ func (m *msgServer) sendMsgNotification(ctx context.Context, req *msg.SendMsgReq func (m *msgServer) sendMsgSingleChat(ctx context.Context, req *msg.SendMsgReq) (resp *msg.SendMsgResp, err error) { promePkg.PromeInc(promePkg.SingleChatMsgRecvSuccessCounter) - if err = callbackBeforeSendSingleMsg(req); err != nil { + if err = CallbackBeforeSendSingleMsg(ctx, req); err != nil { return nil, err } _, err = m.messageVerification(ctx, req) @@ -84,7 +84,7 @@ func (m *msgServer) sendMsgSingleChat(ctx context.Context, req *msg.SendMsgReq) return nil, constant.ErrInternalServer.Wrap("insert to mq") } } - err = callbackAfterSendSingleMsg(req) + err = CallbackAfterSendSingleMsg(ctx, req) if err != nil { return nil, err } @@ -98,7 +98,7 @@ func (m *msgServer) sendMsgSingleChat(ctx context.Context, req *msg.SendMsgReq) func (m *msgServer) sendMsgGroupChat(ctx context.Context, req *msg.SendMsgReq) (resp *msg.SendMsgResp, err error) { // callback promePkg.PromeInc(promePkg.GroupChatMsgRecvSuccessCounter) - err = callbackBeforeSendGroupMsg(req) + err = CallbackBeforeSendGroupMsg(ctx, req) if err != nil { return nil, err } @@ -163,7 +163,7 @@ func (m *msgServer) sendMsgGroupChat(ctx context.Context, req *msg.SendMsgReq) ( wg.Wait() // callback - err = callbackAfterSendGroupMsg(req) + err = CallbackAfterSendGroupMsg(ctx, req) if err != nil { return nil, err } @@ -234,7 +234,7 @@ func (m *msgServer) SendMsg(ctx context.Context, req *msg.SendMsgReq) (resp *msg return nil, constant.ErrMessageHasReadDisable.Wrap() } m.encapsulateMsgData(req.MsgData) - if err := callbackMsgModify(req); err != nil { + if err := CallbackMsgModify(ctx, req); err != nil { return nil, err } switch req.MsgData.SessionType { diff --git a/internal/rpc/msg/server.go b/internal/rpc/msg/server.go index 28d5c14a2..1700d4e72 100644 --- a/internal/rpc/msg/server.go +++ b/internal/rpc/msg/server.go @@ -3,6 +3,7 @@ package msg import ( "Open_IM/internal/common/check" "Open_IM/pkg/common/db/controller" + "Open_IM/pkg/common/db/localcache" "Open_IM/pkg/common/db/relation" tablerelation "Open_IM/pkg/common/db/table/relation" discoveryRegistry "Open_IM/pkg/discoveryregistry" @@ -19,6 +20,9 @@ type msgServer struct { Group *check.GroupChecker User *check.UserCheck Conversation *check.ConversationChecker + friend *check.FriendChecker + *localcache.GroupLocalCache + black *check.BlackChecker } type deleteMsg struct { @@ -41,7 +45,10 @@ func Start(client *openKeeper.ZkClient, server *grpc.Server) error { User: check.NewUserCheck(client), Group: check.NewGroupChecker(client), //MsgInterface: controller.MsgInterface(), - RegisterCenter: client, + RegisterCenter: client, + GroupLocalCache: localcache.NewGroupMemberIDsLocalCache(client), + black: check.NewBlackChecker(client), + friend: check.NewFriendChecker(client), } s.initPrometheus() msg.RegisterMsgServer(server, s) diff --git a/pkg/proto/user/user.proto b/pkg/proto/user/user.proto index 3fb70a6a2..085cdafb7 100644 --- a/pkg/proto/user/user.proto +++ b/pkg/proto/user/user.proto @@ -128,7 +128,13 @@ message userRegisterResp { } +message getGlobalRecvMessageOptReq{ + string userID = 1; +} +message getGlobalRecvMessageOptResp{ + int32 globalRecvMsgOpt = 1; +} service user { //获取指定的用户信息 全字段 @@ -137,8 +143,10 @@ service user { rpc updateUserInfo(updateUserInfoReq) returns(updateUserInfoResp); //设置用户消息接收选项 rpc setGlobalRecvMessageOpt(setGlobalRecvMessageOptReq) returns(setGlobalRecvMessageOptResp); + //获取用户消息接收选项 没找到不返回错误 + rpc getGlobalRecvMessageOpt(getGlobalRecvMessageOptReq) returns(getGlobalRecvMessageOptResp); //检查userID是否存在 - rpc accountCheck(accountCheckReq)returns(accountCheckResp); + rpc accountCheck(accountCheckReq) returns (accountCheckResp); //翻页(或指定userID,昵称)拉取用户信息 全字段 rpc getPaginationUsers(getPaginationUsersReq) returns (getPaginationUsersResp); //用户注册