conversation

This commit is contained in:
wangchuxiao
2023-04-26 14:10:12 +08:00
parent 0dc16d54a5
commit 16231ee077
8 changed files with 261 additions and 65 deletions
+58 -13
View File
@@ -1,7 +1,13 @@
package rpcclient
import (
"context"
"fmt"
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
sdkws "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"google.golang.org/grpc"
)
@@ -9,30 +15,69 @@ type MetaClient struct {
// contains filtered or unexported fields
client discoveryregistry.SvcDiscoveryRegistry
rpcRegisterName string
getUsersInfo func(ctx context.Context, userIDs []string) ([]CommonUser, error)
}
func NewMetaClient(client discoveryregistry.SvcDiscoveryRegistry, rpcRegisterName string) *MetaClient {
return &MetaClient{
func NewMetaClient(client discoveryregistry.SvcDiscoveryRegistry, rpcRegisterName string, opts ...MetaClientOptions) *MetaClient {
c := &MetaClient{
client: client,
rpcRegisterName: rpcRegisterName,
}
for _, opt := range opts {
opt(c)
}
return c
}
type MetaClientOptions func(*MetaClient)
func WithDBFunc(fn func(ctx context.Context, userIDs []string) (users []*relationTb.UserModel, err error)) MetaClientOptions {
return func(s *MetaClient) {
f := func(ctx context.Context, userIDs []string) (result []CommonUser, err error) {
users, err := fn(ctx, userIDs)
if err != nil {
return nil, err
}
for _, user := range users {
result = append(result, user)
}
return result, nil
}
s.getUsersInfo = f
}
}
func WithRpcFunc(fn func(ctx context.Context, userIDs []string) ([]*sdkws.UserInfo, error)) MetaClientOptions {
return func(s *MetaClient) {
f := func(ctx context.Context, userIDs []string) (result []CommonUser, err error) {
users, err := fn(ctx, userIDs)
if err != nil {
return nil, err
}
for _, user := range users {
result = append(result, user)
}
return result, err
}
s.getUsersInfo = f
}
}
func (m *MetaClient) getFaceURLAndName(userID string) (faceURL, nickname string, err error) {
users, err := m.getUsersInfo(context.Background(), []string{userID})
if err != nil {
return "", "", err
}
if len(users) == 0 {
return "", "", errs.ErrRecordNotFound.Wrap(fmt.Sprintf("notification user %s not found", userID))
}
return users[0].GetFaceURL(), users[0].GetNickname(), nil
}
func (m *MetaClient) getConn() (*grpc.ClientConn, error) {
return m.client.GetConn(m.rpcRegisterName)
}
type NotificationMsg struct {
SendID string
RecvID string
Content []byte
MsgFrom int32
ContentType int32
SessionType int32
SenderNickname string
SenderFaceURL string
}
type CommonUser interface {
GetNickname() string
GetFaceURL() string
+20 -15
View File
@@ -2,6 +2,7 @@ package rpcclient
import (
"context"
"encoding/json"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
@@ -9,6 +10,7 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"github.com/golang/protobuf/proto"
)
type MsgClient struct {
@@ -46,26 +48,29 @@ func (m *MsgClient) PullMessageBySeqList(ctx context.Context, req *sdkws.PullMes
return resp, err
}
func (c *MsgClient) Notification(ctx context.Context, notificationMsg *NotificationMsg) error {
var err error
func (c *MsgClient) Notification(ctx context.Context, sendID, recvID string, contentType, sessionType int32, m proto.Message, cfg config.NotificationConf, opts ...utils.OptionsOpt) error {
content, err := json.Marshal(m)
if err != nil {
return err
}
var req msg.SendMsgReq
var msg sdkws.MsgData
var offlineInfo sdkws.OfflinePushInfo
var title, desc, ex string
var pushEnable, unReadCount bool
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.SendID = sendID
msg.RecvID = recvID
msg.Content = content
msg.MsgFrom = constant.SysMsgType
msg.ContentType = contentType
msg.SessionType = sessionType
msg.CreateTime = utils.GetCurrentTimestampByMill()
msg.ClientMsgID = utils.GetMsgID(notificationMsg.SendID)
msg.Options = make(map[string]bool, 7)
msg.SenderNickname = notificationMsg.SenderNickname
msg.SenderFaceURL = notificationMsg.SenderFaceURL
utils.SetSwitchFromOptions(msg.Options, constant.IsUnreadCount, unReadCount)
utils.SetSwitchFromOptions(msg.Options, constant.IsOfflinePush, pushEnable)
msg.ClientMsgID = utils.GetMsgID(sendID)
// msg.Options = make(map[string]bool, 7)
// todo notification get sender name and face url
// msg.SenderNickname, msg.SenderFaceURL, err = c.getFaceURLAndName(sendID)
options := config.GetOptionsByNotification(cfg)
options = utils.WithOptions(options, opts...)
msg.Options = options
offlineInfo.Title = title
offlineInfo.Desc = desc
offlineInfo.Ex = ex
+4 -20
View File
@@ -2,13 +2,12 @@ package notification
import (
"context"
"encoding/json"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
"github.com/golang/protobuf/proto"
)
type ConversationNotificationSender struct {
@@ -19,21 +18,6 @@ func NewConversationNotificationSender(client discoveryregistry.SvcDiscoveryRegi
return &ConversationNotificationSender{rpcclient.NewMsgClient(client)}
}
func (c *ConversationNotificationSender) SetConversationNotification(ctx context.Context, sendID, recvID string, contentType int, m proto.Message) {
var err error
var n rpcclient.NotificationMsg
n.SendID = sendID
n.RecvID = recvID
n.ContentType = int32(contentType)
n.SessionType = constant.SingleChatType
n.MsgFrom = constant.SysMsgType
n.Content, err = json.Marshal(m)
if err != nil {
return
}
c.Notification(ctx, &n)
}
// SetPrivate调用
func (c *ConversationNotificationSender) ConversationSetPrivateNotification(ctx context.Context, sendID, recvID string, isPrivateChat bool) {
tips := &sdkws.ConversationSetPrivateTips{
@@ -41,7 +25,7 @@ func (c *ConversationNotificationSender) ConversationSetPrivateNotification(ctx
SendID: sendID,
IsPrivate: isPrivateChat,
}
c.SetConversationNotification(ctx, sendID, recvID, constant.ConversationPrivateChatNotification, tips)
c.Notification(ctx, sendID, recvID, constant.ConversationPrivateChatNotification, constant.SingleChatType, tips, config.Config.Notification.ConversationSetPrivate)
}
// 会话改变
@@ -49,7 +33,7 @@ func (c *ConversationNotificationSender) ConversationChangeNotification(ctx cont
tips := &sdkws.ConversationUpdateTips{
UserID: userID,
}
c.SetConversationNotification(ctx, userID, userID, constant.ConversationOptChangeNotification, tips)
c.Notification(ctx, userID, userID, constant.ConversationChangeNotification, constant.SingleChatType, tips, config.Config.Notification.ConversationChanged)
}
// 会话未读数同步
@@ -59,5 +43,5 @@ func (c *ConversationNotificationSender) ConversationUnreadChangeNotification(ct
ConversationIDList: []string{conversationID},
UpdateUnreadCountTime: updateUnreadCountTime,
}
c.SetConversationNotification(ctx, userID, userID, constant.ConversationUnreadNotification, tips)
c.Notification(ctx, userID, userID, constant.ConversationUnreadNotification, constant.SingleChatType, tips, config.Config.Notification.ConversationChanged)
}