From 7a9e0af8ed720008ccd2a009c8025da6198e682b Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 5 Jul 2023 13:22:06 +0800 Subject: [PATCH] sendNotificationWithName --- internal/rpc/group/group.go | 2 +- pkg/rpcclient/msg.go | 37 ++++++++++++++++++++++++++--- pkg/rpcclient/notification/group.go | 6 ++--- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 0d242201d..92acd9945 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -55,7 +55,7 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e pbGroup.RegisterGroupServer(server, &groupServer{ GroupDatabase: database, User: userRpcClient, - Notification: notification.NewGroupNotificationSender(database, &msgRpcClient, func(ctx context.Context, userIDs []string) ([]notification.CommonUser, error) { + Notification: notification.NewGroupNotificationSender(database, &msgRpcClient, &userRpcClient, func(ctx context.Context, userIDs []string) ([]notification.CommonUser, error) { users, err := userRpcClient.GetUsersInfo(ctx, userIDs) if err != nil { return nil, err diff --git a/pkg/rpcclient/msg.go b/pkg/rpcclient/msg.go index 8c08d0db4..ee09eaba6 100644 --- a/pkg/rpcclient/msg.go +++ b/pkg/rpcclient/msg.go @@ -151,6 +151,7 @@ type NotificationSender struct { contentTypeConf map[int32]config.NotificationConf sessionTypeConf map[int32]int32 sendMsg func(ctx context.Context, req *msg.SendMsgReq) (*msg.SendMsgResp, error) + getUserInfo func(ctx context.Context, userID string) (*sdkws.UserInfo, error) } type NewNotificationSenderOptions func(*NotificationSender) @@ -167,6 +168,12 @@ func WithRpcClient(msgRpcClient *MessageRpcClient) NewNotificationSenderOptions } } +func WithUserRpcClient(userRpcClient *UserRpcClient) NewNotificationSenderOptions { + return func(s *NotificationSender) { + s.getUserInfo = userRpcClient.GetUserInfo + } +} + func NewNotificationSender(opts ...NewNotificationSenderOptions) *NotificationSender { notificationSender := &NotificationSender{contentTypeConf: newContentTypeConf(), sessionTypeConf: newSessionTypeConf()} for _, opt := range opts { @@ -175,15 +182,40 @@ func NewNotificationSender(opts ...NewNotificationSenderOptions) *NotificationSe return notificationSender } -func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, sendID, recvID string, contentType, sesstionType int32, m proto.Message, opts ...utils.OptionsOpt) (err error) { +type notificationOpt struct { + WithRpcGetUsername bool +} + +type NotificationOptions func(*notificationOpt) + +func WithRpcGetUserName() NotificationOptions { + return func(opt *notificationOpt) { + opt.WithRpcGetUsername = true + } +} + +func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, sendID, recvID string, contentType, sesstionType int32, m proto.Message, opts ...NotificationOptions) (err error) { n := sdkws.NotificationElem{Detail: utils.StructToJsonString(m)} content, err := json.Marshal(&n) if err != nil { log.ZError(ctx, "MsgClient Notification json.Marshal failed", err, "sendID", sendID, "recvID", recvID, "contentType", contentType, "msg", m) return err } + notificationOpt := ¬ificationOpt{} + for _, opt := range opts { + opt(notificationOpt) + } var req msg.SendMsgReq var msg sdkws.MsgData + if notificationOpt.WithRpcGetUsername && s.getUserInfo != nil { + userInfo, err := s.getUserInfo(ctx, sendID) + if err != nil { + log.ZWarn(ctx, "getUserInfo failed", err, "sendID", sendID) + } else { + msg.SenderNickname = userInfo.Nickname + msg.SenderFaceURL = userInfo.FaceURL + } + } var offlineInfo sdkws.OfflinePushInfo var title, desc, ex string msg.SendID = sendID @@ -198,7 +230,6 @@ func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, s msg.CreateTime = utils.GetCurrentTimestampByMill() msg.ClientMsgID = utils.GetMsgID(sendID) options := config.GetOptionsByNotification(s.contentTypeConf[contentType]) - options = utils.WithOptions(options, opts...) msg.Options = options offlineInfo.Title = title offlineInfo.Desc = desc @@ -214,6 +245,6 @@ func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, s return err } -func (s *NotificationSender) Notification(ctx context.Context, sendID, recvID string, contentType int32, m proto.Message, opts ...utils.OptionsOpt) error { +func (s *NotificationSender) Notification(ctx context.Context, sendID, recvID string, contentType int32, m proto.Message, opts ...NotificationOptions) error { return s.NotificationWithSesstionType(ctx, sendID, recvID, contentType, s.sessionTypeConf[contentType], m, opts...) } diff --git a/pkg/rpcclient/notification/group.go b/pkg/rpcclient/notification/group.go index 5175f3740..ee3ed8075 100644 --- a/pkg/rpcclient/notification/group.go +++ b/pkg/rpcclient/notification/group.go @@ -16,9 +16,9 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" ) -func NewGroupNotificationSender(db controller.GroupDatabase, msgRpcClient *rpcclient.MessageRpcClient, fn func(ctx context.Context, userIDs []string) ([]CommonUser, error)) *GroupNotificationSender { +func NewGroupNotificationSender(db controller.GroupDatabase, msgRpcClient *rpcclient.MessageRpcClient, userRpcClient *rpcclient.UserRpcClient, fn func(ctx context.Context, userIDs []string) ([]CommonUser, error)) *GroupNotificationSender { return &GroupNotificationSender{ - NotificationSender: rpcclient.NewNotificationSender(rpcclient.WithRpcClient(msgRpcClient)), + NotificationSender: rpcclient.NewNotificationSender(rpcclient.WithRpcClient(msgRpcClient), rpcclient.WithUserRpcClient(userRpcClient)), getUsersInfo: fn, db: db, } @@ -264,7 +264,7 @@ func (g *GroupNotificationSender) GroupInfoSetAnnouncementNotification(ctx conte if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { return err } - return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupInfoSetAnnouncementNotification, tips) + return g.Notification(ctx, mcontext.GetOpUserID(ctx), tips.Group.GroupID, constant.GroupInfoSetAnnouncementNotification, tips, rpcclient.WithRpcGetUserName()) } func (g *GroupNotificationSender) JoinGroupApplicationNotification(ctx context.Context, req *pbGroup.JoinGroupReq) (err error) {