diff --git a/internal/common/check/access.go b/internal/common/check/access.go index 4f0df2643..71254b903 100644 --- a/internal/common/check/access.go +++ b/internal/common/check/access.go @@ -11,4 +11,5 @@ func Access(ctx context.Context, ownerUserID string) (err error) { return err } return tokenverify.CheckAccessV3(ctx, ownerUserID) + } diff --git a/internal/common/check/msg.go b/internal/common/check/msg.go new file mode 100644 index 000000000..c4f1dfc03 --- /dev/null +++ b/internal/common/check/msg.go @@ -0,0 +1,26 @@ +package check + +import ( + "Open_IM/pkg/common/config" + discoveryRegistry "Open_IM/pkg/discoveryregistry" + "Open_IM/pkg/proto/msg" + "context" + "google.golang.org/grpc" +) + +type MsgCheck struct { + zk discoveryRegistry.SvcDiscoveryRegistry +} + +func (m *MsgCheck) getConn() (*grpc.ClientConn, error) { + return m.zk.GetConn(config.Config.RpcRegisterName.OpenImMsgName) +} + +func (m *MsgCheck) SendMsg(ctx context.Context, req *msg.SendMsgReq) (*msg.SendMsgResp, error) { + cc, err := m.getConn() + if err != nil { + return nil, err + } + resp, err := msg.NewMsgClient(cc).SendMsg(ctx, req) + return resp, err +} diff --git a/internal/common/notification/c.go b/internal/common/notification/c.go index 5dac5fed4..098bdcf56 100644 --- a/internal/common/notification/c.go +++ b/internal/common/notification/c.go @@ -4,8 +4,10 @@ import ( "Open_IM/internal/common/check" "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" + "Open_IM/pkg/common/tracelog" "Open_IM/pkg/proto/msg" "Open_IM/pkg/proto/sdkws" + utils2 "Open_IM/pkg/utils" "context" utils "github.com/OpenIMSDK/open_utils" ) @@ -13,6 +15,7 @@ import ( type Check struct { user *check.UserCheck group *check.GroupChecker + msg *check.MsgCheck } type NotificationMsg struct { @@ -26,28 +29,33 @@ type NotificationMsg struct { SenderFaceURL string } -func (c *Check) Notification(ctx context.Context, n *NotificationMsg) { +func (c *Check) Notification(ctx context.Context, notificationMsg *NotificationMsg) { + var err error + defer func() { + tracelog.SetCtxDebug(ctx, utils2.GetFuncName(1), err, "notificationMsg", notificationMsg) + }() + var req msg.SendMsgReq var msg sdkws.MsgData var offlineInfo sdkws.OfflinePushInfo var title, desc, ex string var pushSwitch, unReadCount bool var reliabilityLevel int - msg.SendID = n.SendID - msg.RecvID = n.RecvID - msg.Content = n.Content - msg.MsgFrom = n.MsgFrom - msg.ContentType = n.ContentType - msg.SessionType = n.SessionType + msg.SendID = notificationMsg.SendID + msg.RecvID = notificationMsg.RecvID + msg.Content = notificationMsg.Content + msg.MsgFrom = notificationMsg.MsgFrom + msg.ContentType = notificationMsg.ContentType + msg.SessionType = notificationMsg.SessionType msg.CreateTime = utils.GetCurrentTimestampByMill() - msg.ClientMsgID = utils.GetMsgID(n.SendID) + msg.ClientMsgID = utils.GetMsgID(notificationMsg.SendID) msg.Options = make(map[string]bool, 7) - msg.SenderNickname = n.SenderNickname - msg.SenderFaceURL = n.SenderFaceURL - switch n.SessionType { + msg.SenderNickname = notificationMsg.SenderNickname + msg.SenderFaceURL = notificationMsg.SenderFaceURL + switch notificationMsg.SessionType { case constant.GroupChatType, constant.SuperGroupChatType: msg.RecvID = "" - msg.GroupID = n.RecvID + msg.GroupID = notificationMsg.RecvID } offlineInfo.IOSBadgeCount = config.Config.IOSPush.BadgeCount offlineInfo.IOSPushSound = config.Config.IOSPush.PushSound @@ -280,9 +288,5 @@ func (c *Check) Notification(ctx context.Context, n *NotificationMsg) { msg.OfflinePushInfo = &offlineInfo req.MsgData = &msg - _, err := sendMsg(context.Background(), &req) -} - -func sendMsg(ctx context.Context, req *msg.SendMsgReq) (*msg.SendMsgResp, error) { - + _, err = c.msg.SendMsg(ctx, &req) } diff --git a/internal/common/notification/friend.go b/internal/common/notification/friend.go index 684700154..7a521c329 100644 --- a/internal/common/notification/friend.go +++ b/internal/common/notification/friend.go @@ -148,12 +148,6 @@ func (c *Check) BlackDeletedNotification(ctx context.Context, req *pbFriend.Remo c.friendNotification(ctx, req.OwnerUserID, req.BlackUserID, constant.BlackDeletedNotification, &blackDeletedTips) } -// send to myself -func (c *Check) UserInfoUpdatedNotification(ctx context.Context, opUserID string, changedUserID string) { - selfInfoUpdatedTips := sdkws.UserInfoUpdatedTips{UserID: changedUserID} - c.friendNotification(ctx, opUserID, changedUserID, constant.UserInfoUpdatedNotification, &selfInfoUpdatedTips) -} - func (c *Check) FriendInfoUpdatedNotification(ctx context.Context, changedUserID string, needNotifiedUserID string, opUserID string) { selfInfoUpdatedTips := sdkws.UserInfoUpdatedTips{UserID: changedUserID} c.friendNotification(ctx, opUserID, needNotifiedUserID, constant.FriendInfoUpdatedNotification, &selfInfoUpdatedTips) diff --git a/internal/common/notification/user.go b/internal/common/notification/user.go new file mode 100644 index 000000000..01d5fcee8 --- /dev/null +++ b/internal/common/notification/user.go @@ -0,0 +1,13 @@ +package notification + +import ( + "Open_IM/pkg/common/constant" + "Open_IM/pkg/proto/sdkws" + "context" +) + +// send to myself +func (c *Check) UserInfoUpdatedNotification(ctx context.Context, opUserID string, changedUserID string) { + selfInfoUpdatedTips := sdkws.UserInfoUpdatedTips{UserID: changedUserID} + c.friendNotification(ctx, opUserID, changedUserID, constant.UserInfoUpdatedNotification, &selfInfoUpdatedTips) +} diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 3b99ede9d..a73e82233 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -3,7 +3,7 @@ package friend import ( "Open_IM/internal/common/check" "Open_IM/internal/common/convert" - chat "Open_IM/internal/common/notification" + "Open_IM/internal/common/notification" "Open_IM/internal/common/rpcserver" "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" @@ -26,6 +26,8 @@ type friendServer struct { *rpcserver.RpcServer controller.FriendInterface controller.BlackInterface + notification *notification.Check + userCheck *check.UserCheck } func NewFriendServer(port int) *friendServer { @@ -105,7 +107,7 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbFriend.Apply if req.ToUserID == req.FromUserID { return nil, constant.ErrCanNotAddYourself.Wrap() } - if _, err := check.GetUsersInfo(ctx, req.ToUserID, req.FromUserID); err != nil { + if _, err := s.userCheck.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}, true); err != nil { return nil, err } in1, in2, err := s.FriendInterface.CheckIn(ctx, req.FromUserID, req.ToUserID) @@ -118,7 +120,7 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbFriend.Apply if err = s.FriendInterface.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil { return nil, err } - chat.FriendApplicationAddNotification(ctx, req) + s.notification.FriendApplicationAddNotification(ctx, req) return resp, nil } @@ -128,7 +130,7 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbFriend.ImportFr if err := tokenverify.CheckAdmin(ctx); err != nil { return nil, err } - if _, err := check.NewUserCheck().GetUsersInfos(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...), true); err != nil { + if _, err := s.userCheck.GetUsersInfos(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...), true); err != nil { return nil, err } @@ -157,7 +159,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbFriend.Res if err != nil { return nil, err } - chat.FriendApplicationAgreedNotification(ctx, req) + s.notification.FriendApplicationAgreedNotification(ctx, req) return resp, nil } if req.HandleResult == constant.FriendResponseRefuse { @@ -165,7 +167,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbFriend.Res if err != nil { return nil, err } - chat.FriendApplicationRefusedNotification(ctx, req) + s.notification.FriendApplicationRefusedNotification(ctx, req) return resp, nil } return nil, constant.ErrArgs.Wrap("req.HandleResult != -1/1") @@ -184,7 +186,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbFriend.DeleteFri if err := s.FriendInterface.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil { return nil, err } - chat.FriendDeletedNotification(ctx, req) + s.notification.FriendDeletedNotification(ctx, req) return resp, nil } @@ -201,7 +203,7 @@ func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbFriend.SetFri if err := s.FriendInterface.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil { return nil, err } - chat.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID) + s.notification.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID) return resp, nil } diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index d25d331d5..658de155d 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -1,8 +1,9 @@ package user import ( + "Open_IM/internal/common/check" "Open_IM/internal/common/convert" - chat "Open_IM/internal/common/notification" + "Open_IM/internal/common/notification" "Open_IM/internal/common/rpcserver" "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" @@ -25,6 +26,8 @@ import ( type userServer struct { *rpcserver.RpcServer controller.UserInterface + notification *notification.Check + userCheck *check.UserCheck } func NewUserServer(port int) *userServer { @@ -226,11 +229,11 @@ func (s *userServer) UpdateUserInfo(ctx context.Context, req *pbuser.UpdateUserI } go func() { for _, v := range friends { - chat.FriendInfoUpdatedNotification(ctx, req.UserInfo.UserID, v.FriendUser.UserID, tracelog.GetOpUserID(ctx)) + s.notification.FriendInfoUpdatedNotification(ctx, req.UserInfo.UserID, v.FriendUser.UserID, tracelog.GetOpUserID(ctx)) } }() - chat.UserInfoUpdatedNotification(ctx, tracelog.GetOpUserID(ctx), req.UserInfo.UserID) + s.notification.UserInfoUpdatedNotification(ctx, tracelog.GetOpUserID(ctx), req.UserInfo.UserID) return resp, nil } @@ -246,7 +249,7 @@ func (s *userServer) SetGlobalRecvMessageOpt(ctx context.Context, req *pbuser.Se if err := s.UpdateByMap(ctx, req.UserID, m); err != nil { return nil, err } - chat.UserInfoUpdatedNotification(ctx, req.UserID, req.UserID) + s.notification.UserInfoUpdatedNotification(ctx, req.UserID, req.UserID) return resp, nil }