sendNotificationWithName

This commit is contained in:
wangchuxiao 2023-07-05 13:22:06 +08:00
parent 1ac6279569
commit 7a9e0af8ed
3 changed files with 38 additions and 7 deletions

View File

@ -55,7 +55,7 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
pbGroup.RegisterGroupServer(server, &groupServer{ pbGroup.RegisterGroupServer(server, &groupServer{
GroupDatabase: database, GroupDatabase: database,
User: userRpcClient, 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) users, err := userRpcClient.GetUsersInfo(ctx, userIDs)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -151,6 +151,7 @@ type NotificationSender struct {
contentTypeConf map[int32]config.NotificationConf contentTypeConf map[int32]config.NotificationConf
sessionTypeConf map[int32]int32 sessionTypeConf map[int32]int32
sendMsg func(ctx context.Context, req *msg.SendMsgReq) (*msg.SendMsgResp, error) 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) 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 { func NewNotificationSender(opts ...NewNotificationSenderOptions) *NotificationSender {
notificationSender := &NotificationSender{contentTypeConf: newContentTypeConf(), sessionTypeConf: newSessionTypeConf()} notificationSender := &NotificationSender{contentTypeConf: newContentTypeConf(), sessionTypeConf: newSessionTypeConf()}
for _, opt := range opts { for _, opt := range opts {
@ -175,15 +182,40 @@ func NewNotificationSender(opts ...NewNotificationSenderOptions) *NotificationSe
return notificationSender 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)} n := sdkws.NotificationElem{Detail: utils.StructToJsonString(m)}
content, err := json.Marshal(&n) content, err := json.Marshal(&n)
if err != nil { if err != nil {
log.ZError(ctx, "MsgClient Notification json.Marshal failed", err, "sendID", sendID, "recvID", recvID, "contentType", contentType, "msg", m) log.ZError(ctx, "MsgClient Notification json.Marshal failed", err, "sendID", sendID, "recvID", recvID, "contentType", contentType, "msg", m)
return err return err
} }
notificationOpt := &notificationOpt{}
for _, opt := range opts {
opt(notificationOpt)
}
var req msg.SendMsgReq var req msg.SendMsgReq
var msg sdkws.MsgData 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 offlineInfo sdkws.OfflinePushInfo
var title, desc, ex string var title, desc, ex string
msg.SendID = sendID msg.SendID = sendID
@ -198,7 +230,6 @@ func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, s
msg.CreateTime = utils.GetCurrentTimestampByMill() msg.CreateTime = utils.GetCurrentTimestampByMill()
msg.ClientMsgID = utils.GetMsgID(sendID) msg.ClientMsgID = utils.GetMsgID(sendID)
options := config.GetOptionsByNotification(s.contentTypeConf[contentType]) options := config.GetOptionsByNotification(s.contentTypeConf[contentType])
options = utils.WithOptions(options, opts...)
msg.Options = options msg.Options = options
offlineInfo.Title = title offlineInfo.Title = title
offlineInfo.Desc = desc offlineInfo.Desc = desc
@ -214,6 +245,6 @@ func (s *NotificationSender) NotificationWithSesstionType(ctx context.Context, s
return err 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...) return s.NotificationWithSesstionType(ctx, sendID, recvID, contentType, s.sessionTypeConf[contentType], m, opts...)
} }

View File

@ -16,9 +16,9 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "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{ return &GroupNotificationSender{
NotificationSender: rpcclient.NewNotificationSender(rpcclient.WithRpcClient(msgRpcClient)), NotificationSender: rpcclient.NewNotificationSender(rpcclient.WithRpcClient(msgRpcClient), rpcclient.WithUserRpcClient(userRpcClient)),
getUsersInfo: fn, getUsersInfo: fn,
db: db, db: db,
} }
@ -264,7 +264,7 @@ func (g *GroupNotificationSender) GroupInfoSetAnnouncementNotification(ctx conte
if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil { if err := g.fillOpUser(ctx, &tips.OpUser, tips.Group.GroupID); err != nil {
return err 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) { func (g *GroupNotificationSender) JoinGroupApplicationNotification(ctx context.Context, req *pbGroup.JoinGroupReq) (err error) {