Merge remote-tracking branch 'origin/errcode' into errcode

test-errcode
Gordon 2 years ago
commit 5c610fd24d

@ -13,6 +13,7 @@ groupCreated:
groupInfoSet:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -23,6 +24,7 @@ groupInfoSet:
joinGroupApplication:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -32,6 +34,7 @@ joinGroupApplication:
memberQuit:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -41,6 +44,7 @@ memberQuit:
groupApplicationAccepted:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -50,6 +54,7 @@ groupApplicationAccepted:
groupApplicationRejected:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -60,6 +65,7 @@ groupApplicationRejected:
groupOwnerTransferred:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -69,6 +75,7 @@ groupOwnerTransferred:
memberKicked:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -78,6 +85,7 @@ memberKicked:
memberInvited:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -87,6 +95,7 @@ memberInvited:
memberEnter:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -96,6 +105,7 @@ memberEnter:
groupDismissed:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -105,6 +115,7 @@ groupDismissed:
groupMuted:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -114,6 +125,7 @@ groupMuted:
groupCancelMuted:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -126,6 +138,7 @@ groupCancelMuted:
groupMemberMuted:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -135,6 +148,7 @@ groupMemberMuted:
groupMemberCancelMuted:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -144,6 +158,7 @@ groupMemberCancelMuted:
groupMemberInfoSet:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -154,6 +169,7 @@ groupMemberInfoSet:
#############################friend#################################
friendApplicationAdded:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: false
@ -163,6 +179,7 @@ friendApplicationAdded:
friendApplicationApproved:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true
@ -172,6 +189,7 @@ friendApplicationApproved:
friendApplicationRejected:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true
@ -181,6 +199,7 @@ friendApplicationRejected:
friendAdded:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true
@ -190,6 +209,7 @@ friendAdded:
friendDeleted:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true
@ -199,6 +219,7 @@ friendDeleted:
friendRemarkSet:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true
@ -208,6 +229,7 @@ friendRemarkSet:
blackAdded:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true
@ -217,6 +239,7 @@ blackAdded:
blackDeleted:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true
@ -226,6 +249,7 @@ blackDeleted:
friendInfoUpdated:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true
@ -236,6 +260,7 @@ friendInfoUpdated:
#####################user#########################
userInfoUpdated:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true
@ -246,6 +271,7 @@ userInfoUpdated:
#####################conversation#########################
conversationChanged:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true
@ -255,6 +281,7 @@ conversationChanged:
conversationSetPrivate:
isSendMsg: true
reliabilityLevel: 1
unreadCount: false
offlinePush:
enable: true

@ -58,16 +58,16 @@ func StartTransfer(prometheusPort int) error {
extendMsgDatabase := controller.NewExtendMsgDatabase(extendMsgModel, extendMsgCache, tx.NewMongo(mongo.GetClient()))
msgDatabase := controller.NewCommonMsgDatabase(msgDocModel, msgModel)
conversationRpcClient := rpcclient.NewConversationClient(client)
msgTransfer := NewMsgTransfer(chatLogDatabase, extendMsgDatabase, msgDatabase, conversationRpcClient)
groupRpcClient := rpcclient.NewGroupClient(client)
msgTransfer := NewMsgTransfer(chatLogDatabase, extendMsgDatabase, msgDatabase, conversationRpcClient, groupRpcClient)
msgTransfer.initPrometheus()
return msgTransfer.Start(prometheusPort)
}
func NewMsgTransfer(chatLogDatabase controller.ChatLogDatabase,
extendMsgDatabase controller.ExtendMsgDatabase, msgDatabase controller.CommonMsgDatabase,
conversationRpcClient *rpcclient.ConversationClient) *MsgTransfer {
return &MsgTransfer{persistentCH: NewPersistentConsumerHandler(chatLogDatabase), historyCH: NewOnlineHistoryRedisConsumerHandler(msgDatabase, conversationRpcClient),
conversationRpcClient *rpcclient.ConversationClient, groupRpcClient *rpcclient.GroupClient) *MsgTransfer {
return &MsgTransfer{persistentCH: NewPersistentConsumerHandler(chatLogDatabase), historyCH: NewOnlineHistoryRedisConsumerHandler(msgDatabase, conversationRpcClient, groupRpcClient),
historyMongoCH: NewOnlineHistoryMongoConsumerHandler(msgDatabase), modifyCH: NewModifyMsgConsumerHandler(extendMsgDatabase)}
}

@ -63,7 +63,7 @@ type OnlineHistoryRedisConsumerHandler struct {
groupRpcClient *rpcclient.GroupClient
}
func NewOnlineHistoryRedisConsumerHandler(database controller.CommonMsgDatabase, conversationRpcClient *rpcclient.ConversationClient) *OnlineHistoryRedisConsumerHandler {
func NewOnlineHistoryRedisConsumerHandler(database controller.CommonMsgDatabase, conversationRpcClient *rpcclient.ConversationClient, groupRpcClient *rpcclient.GroupClient) *OnlineHistoryRedisConsumerHandler {
var och OnlineHistoryRedisConsumerHandler
och.msgDatabase = database
och.msgDistributionCh = make(chan Cmd2Value) //no buffer channel
@ -73,6 +73,7 @@ func NewOnlineHistoryRedisConsumerHandler(database controller.CommonMsgDatabase,
go och.Run(i)
}
och.conversationRpcClient = conversationRpcClient
och.groupRpcClient = groupRpcClient
och.historyConsumerGroup = kafka.NewMConsumerGroup(&kafka.MConsumerGroupConfig{KafkaVersion: sarama.V2_0_0_0,
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false}, []string{config.Config.Kafka.Ws2mschat.Topic},
config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.ConsumerGroupID.MsgToRedis)
@ -90,11 +91,11 @@ func (och *OnlineHistoryRedisConsumerHandler) Run(channelID int) {
ctxMsgList := msgChannelValue.ctxMsgList
ctx := msgChannelValue.ctx
log.ZDebug(ctx, "msg arrived channel", "channel id", channelID, "msgList length", len(ctxMsgList), "conversationID", msgChannelValue.conversationID)
storageMsgList, notStorageMsgList, storageNotificationList, notStorageNotificationList, modifyMsgList := och.getPushStorageMsgList(msgChannelValue.conversationID, ctxMsgList)
storageMsgList, notStorageMsgList, storageNotificationList, notStorageNotificationList, modifyMsgList := och.getPushStorageMsgList(ctxMsgList)
log.ZDebug(ctx, "msg lens", "storageMsgList", len(storageMsgList), "notStorageMsgList", len(notStorageMsgList),
"storageNotificationList", len(storageNotificationList), "notStorageNotificationList", len(notStorageNotificationList), "modifyMsgList", len(modifyMsgList))
och.handleMsg(ctx, msgChannelValue.conversationID, storageMsgList, notStorageMsgList)
och.handleNotification(ctx, msgChannelValue.conversationID, storageNotificationList, notStorageNotificationList)
och.handleMsg(ctx, utils.GetChatConversationIDByMsg(ctxMsgList[0].message), storageMsgList, notStorageMsgList)
och.handleNotification(ctx, utils.GetNotificationConversationID(ctxMsgList[0].message), storageNotificationList, notStorageNotificationList)
if err := och.msgDatabase.MsgToModifyMQ(ctx, msgChannelValue.conversationID, modifyMsgList); err != nil {
log.ZError(ctx, "msg to modify mq error", err, "conversationID", msgChannelValue.conversationID, "modifyMsgList", modifyMsgList)
}
@ -104,7 +105,7 @@ func (och *OnlineHistoryRedisConsumerHandler) Run(channelID int) {
}
// 获取消息/通知 存储的消息列表, 不存储并且推送的消息列表,
func (och *OnlineHistoryRedisConsumerHandler) getPushStorageMsgList(conversationID string, totalMsgs []*ContextMsg) (storageMsgList, notStorageMsgList, storageNotificatoinList, notStorageNotificationList, modifyMsgList []*sdkws.MsgData) {
func (och *OnlineHistoryRedisConsumerHandler) getPushStorageMsgList(totalMsgs []*ContextMsg) (storageMsgList, notStorageMsgList, storageNotificatoinList, notStorageNotificationList, modifyMsgList []*sdkws.MsgData) {
isStorage := func(msg *sdkws.MsgData) bool {
options2 := utils.Options(msg.Options)
if options2.IsHistory() {
@ -119,19 +120,27 @@ func (och *OnlineHistoryRedisConsumerHandler) getPushStorageMsgList(conversation
for _, v := range totalMsgs {
options := utils.Options(v.message.Options)
if !options.IsNotNotification() {
// 原通知
notificationMsg := proto.Clone(v.message).(*sdkws.MsgData)
// clone msg from notificationMsg
if options.IsSendMsg() {
msg := proto.Clone(v.message).(*sdkws.MsgData)
// 消息
if v.message.Options != nil {
v.message.Options = utils.WithOptions(utils.Options(v.message.Options), utils.WithNotification(false), utils.WithSendMsg(false))
msg.Options = utils.NewMsgOptions()
}
storageMsgList = append(storageMsgList, v.message)
if options.IsOfflinePush() {
v.message.Options = utils.WithOptions(utils.Options(v.message.Options), utils.WithOfflinePush(false))
msg.Options = utils.WithOptions(utils.Options(msg.Options), utils.WithOfflinePush(true))
}
if options.IsUnreadCount() {
v.message.Options = utils.WithOptions(utils.Options(v.message.Options), utils.WithUnreadCount(false))
msg.Options = utils.WithOptions(utils.Options(msg.Options), utils.WithUnreadCount(true))
}
if isStorage(notificationMsg) {
storageNotificatoinList = append(storageNotificatoinList, notificationMsg)
storageMsgList = append(storageMsgList, msg)
}
if isStorage(v.message) {
storageNotificatoinList = append(storageNotificatoinList, v.message)
} else {
notStorageNotificationList = append(notStorageNotificationList, notificationMsg)
notStorageNotificationList = append(notStorageNotificationList, v.message)
}
} else {
if isStorage(v.message) {
@ -155,7 +164,7 @@ func (och *OnlineHistoryRedisConsumerHandler) handleNotification(ctx context.Con
log.ZError(ctx, "notification batch insert to redis error", err, "conversationID", conversationID, "storageList", storageList)
return
}
log.ZDebug(ctx, "success to next topic")
log.ZDebug(ctx, "success to next topic", "conversationID", conversationID)
och.msgDatabase.MsgToMongoMQ(ctx, conversationID, storageList, lastSeq)
och.toPushTopic(ctx, conversationID, storageList)
}

@ -195,9 +195,6 @@ func (c *conversationServer) SetConversations(ctx context.Context, req *pbConver
if req.Conversation.DraftTextTime != nil {
m["draft_text_time"] = req.Conversation.DraftTextTime.Value
}
if req.Conversation.UnreadCount != nil {
m["unread_count"] = req.Conversation.UnreadCount.Value
}
if req.Conversation.AttachedInfo != nil {
m["attached_info"] = req.Conversation.AttachedInfo.Value
}
@ -283,7 +280,8 @@ func (c *conversationServer) CreateGroupChatConversations(ctx context.Context, r
func (c *conversationServer) DelGroupChatConversations(ctx context.Context, req *pbConversation.DelGroupChatConversationsReq) (*pbConversation.DelGroupChatConversationsResp, error) {
if err := c.conversationDatabase.UpdateUsersConversationFiled(ctx, req.OwnerUserID,
utils.GetConversationIDBySessionType(constant.SuperGroupChatType, req.GroupID), map[string]interface{}{"max_seq": req.MaxSeq}); err != nil {
utils.GetConversationIDBySessionType(constant.SuperGroupChatType, req.GroupID),
map[string]interface{}{"max_seq": req.MaxSeq}); err != nil {
return nil, err
}
return &pbConversation.DelGroupChatConversationsResp{}, nil
@ -315,3 +313,11 @@ func (c *conversationServer) GetConversationsHasReadAndMaxSeq(ctx context.Contex
}
return resp, nil
}
func (c *conversationServer) GetUserConversationIDsHash(ctx context.Context, req *pbConversation.GetUserConversationIDsHashReq) (*pbConversation.GetUserConversationIDsHashResp, error) {
hash, err := c.conversationDatabase.GetUserConversationIDsHash(ctx, req.OwnerUserID)
if err != nil {
return nil, err
}
return &pbConversation.GetUserConversationIDsHashResp{Hash: hash}, nil
}

@ -117,7 +117,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res
return nil, err
}
friendRequest := tablerelation.FriendRequestModel{FromUserID: req.ToUserID, ToUserID: req.FromUserID, HandleMsg: req.HandleMsg, HandleResult: req.HandleResult}
friendRequest := tablerelation.FriendRequestModel{FromUserID: req.FromUserID, ToUserID: req.ToUserID, HandleMsg: req.HandleMsg, HandleResult: req.HandleResult}
if req.HandleResult == constant.FriendResponseAgree {
err := s.friendDatabase.AgreeFriendRequest(ctx, &friendRequest)
if err != nil {
@ -222,8 +222,6 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p
if err != nil {
return nil, err
}
log.ZInfo(ctx, "test log out")
log.ZInfo(ctx, "GetPaginationFriendsApplyFrom.xxx", "total", total, "friendRequests", len(friendRequests), "userRpcClient", s.userRpcClient == nil)
resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.userRpcClient.GetUsersInfoMap)
if err != nil {
return nil, err

@ -62,6 +62,7 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
return utils.Slice(users, func(e *sdkws.UserInfo) rpcclient.CommonUser { return e }), nil
}),
conversationRpcClient: rpcclient.NewConversationClient(client),
msgRpcClient: rpcclient.NewMsgClient(client),
})
return nil
}
@ -751,11 +752,12 @@ func (s *groupServer) deleteMemberAndSetConversationSeq(ctx context.Context, gro
if err != nil {
return err
}
maxSeq, ok := resp.MaxSeqs[conevrsationID]
if !ok {
return errs.ErrInternalServer.Wrap("get max seq error")
}
return s.conversationRpcClient.DelGroupChatConversations(ctx, userIDs, groupID, maxSeq)
//log.ZInfo(ctx, "deleteMemberAndSetConversationSeq.GetMaxSeq", "maxSeqs", resp.MaxSeqs, "conevrsationID", conevrsationID)
//maxSeq, ok := resp.MaxSeqs[conevrsationID]
//if !ok {
// return errs.ErrInternalServer.Wrap("get max seq error")
//}
return s.conversationRpcClient.DelGroupChatConversations(ctx, userIDs, groupID, resp.MaxSeqs[conevrsationID])
}
func (s *groupServer) SetGroupInfo(ctx context.Context, req *pbGroup.SetGroupInfoReq) (*pbGroup.SetGroupInfoResp, error) {

@ -62,11 +62,15 @@ func (m *msgServer) sendMsgSingleChat(ctx context.Context, req *msg.SendMsgReq)
if err != nil {
return nil, err
}
var isSend bool = true
conversationID := utils.GetConversationIDByMsg(req.MsgData)
isSend, err := m.modifyMessageByUserMessageReceiveOpt(ctx, req.MsgData.RecvID, conversationID, constant.SingleChatType, req)
if !utils.IsNotification(conversationID) {
isSend, err = m.modifyMessageByUserMessageReceiveOpt(ctx, req.MsgData.RecvID, conversationID, constant.SingleChatType, req)
if err != nil {
return nil, err
}
}
if isSend {
err = m.MsgDatabase.MsgToMQ(ctx, conversationID, req.MsgData)
if err != nil {

@ -32,6 +32,7 @@ type msgServer struct {
friend *rpcclient.FriendClient
black *rpcclient.BlackClient
GroupLocalCache *localcache.GroupLocalCache
ConversationLocalCache *localcache.ConversationLocalCache
MessageLocker MessageLocker
Handlers MessageInterceptorChain
}
@ -39,6 +40,7 @@ type msgServer struct {
func (m *msgServer) addInterceptorHandler(interceptorFunc ...MessageInterceptorFunc) {
m.Handlers = append(m.Handlers, interceptorFunc...)
}
func (m *msgServer) execInterceptorHandler(ctx context.Context, req *msg.SendMsgReq) error {
for _, handler := range m.Handlers {
msgData, err := handler(ctx, req)
@ -49,6 +51,7 @@ func (m *msgServer) execInterceptorHandler(ctx context.Context, req *msg.SendMsg
}
return nil
}
func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error {
rdb, err := cache.NewRedis()
if err != nil {
@ -73,6 +76,7 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
ExtendMsgDatabase: extendMsgDatabase,
RegisterCenter: client,
GroupLocalCache: localcache.NewGroupLocalCache(client),
ConversationLocalCache: localcache.NewConversationLocalCache(client),
black: rpcclient.NewBlackClient(client),
friend: rpcclient.NewFriendClient(client),
MessageLocker: NewLockerMessage(cacheModel),
@ -127,7 +131,7 @@ func (m *msgServer) GetMaxSeq(ctx context.Context, req *sdkws.GetMaxSeqReq) (*sd
if err := tokenverify.CheckAccessV3(ctx, req.UserID); err != nil {
return nil, err
}
conversationIDs, err := m.Conversation.GetConversationIDs(ctx, req.UserID)
conversationIDs, err := m.ConversationLocalCache.GetConversationIDs(ctx, req.UserID)
if err != nil {
return nil, err
}
@ -168,7 +172,6 @@ func (m *msgServer) PullMessageBySeqs(ctx context.Context, req *sdkws.PullMessag
continue
}
resp.Msgs[seq.ConversationID] = &sdkws.PullMsgs{Msgs: notificationMsgs}
}
}
return resp, nil

@ -341,7 +341,7 @@ type Notification struct {
////////////////////////user///////////////////////
UserInfoUpdated NotificationConf `yaml:"userInfoUpdated"`
//////////////////////friend///////////////////////
FriendApplication NotificationConf `yaml:"friendApplicationAdded"`
FriendApplicationAdded NotificationConf `yaml:"friendApplicationAdded"`
FriendApplicationApproved NotificationConf `yaml:"friendApplicationApproved"`
FriendApplicationRejected NotificationConf `yaml:"friendApplicationRejected"`
FriendAdded NotificationConf `yaml:"friendAdded"`
@ -358,10 +358,10 @@ type Notification struct {
func GetOptionsByNotification(cfg NotificationConf) utils.Options {
opts := utils.NewOptions()
if cfg.UnreadCount {
opts = utils.WithOptions(opts, utils.WithUnreadCount())
opts = utils.WithOptions(opts, utils.WithUnreadCount(true))
}
if cfg.OfflinePush.Enable {
opts = utils.WithOptions(opts, utils.WithOfflinePush())
opts = utils.WithOptions(opts, utils.WithOfflinePush(true))
}
switch cfg.ReliabilityLevel {
case constant.UnreliableNotification:

@ -142,7 +142,7 @@ const (
IsSenderConversationUpdate = "senderConversationUpdate"
IsSenderNotificationPush = "senderNotificationPush"
IsReactionFromCache = "reactionFromCache"
IsNotification = "isNotification"
IsNotNotification = "isNotNotification"
IsSendMsg = "isSendMsg"
//GroupStatus

@ -17,6 +17,7 @@ import (
const (
conversationKey = "CONVERSATION:"
conversationIDsKey = "CONVERSATION_IDS:"
conversationIDsHashKey = "CONVERSATION_IDS_HASH:"
recvMsgOptKey = "RECV_MSG_OPT:"
superGroupRecvMsgNotNotifyUserIDsKey = "SUPER_GROUP_RECV_MSG_NOT_NOTIFY_USER_IDS:"
superGroupRecvMsgNotNotifyUserIDsHashKey = "SUPER_GROUP_RECV_MSG_NOT_NOTIFY_USER_IDS_HASH:"
@ -31,6 +32,10 @@ type ConversationCache interface {
// get user's conversationIDs from msgCache
GetUserConversationIDs(ctx context.Context, ownerUserID string) ([]string, error)
DelConversationIDs(userIDs ...string) ConversationCache
GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error)
DelUserConversationIDsHash(ownerUserIDs ...string) ConversationCache
// get one conversation from msgCache
GetConversation(ctx context.Context, ownerUserID, conversationID string) (*relationTb.ConversationModel, error)
DelConvsersations(ownerUserID string, conversationIDs ...string) ConversationCache
@ -107,6 +112,33 @@ func (c *ConversationRedisCache) DelConversationIDs(userIDs ...string) Conversat
return cache
}
func (c *ConversationRedisCache) getUserConversationIDsHashKey(ownerUserID string) string {
return conversationIDsHashKey + ownerUserID
}
func (c *ConversationRedisCache) GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error) {
return getCache(ctx, c.rcClient, c.getUserConversationIDsHashKey(ownerUserID), c.expireTime, func(ctx context.Context) (uint64, error) {
conversationIDs, err := c.GetUserConversationIDs(ctx, ownerUserID)
if err != nil {
return 0, err
}
utils.Sort(conversationIDs, true)
bi := big.NewInt(0)
bi.SetString(utils.Md5(strings.Join(conversationIDs, ";"))[0:8], 16)
return bi.Uint64(), nil
})
}
func (c *ConversationRedisCache) DelUserConversationIDsHash(ownerUserIDs ...string) ConversationCache {
var keys []string
for _, ownerUserID := range ownerUserIDs {
keys = append(keys, c.getUserConversationIDsHashKey(ownerUserID))
}
cache := c.NewCache()
cache.AddKeys(keys...)
return cache
}
func (c *ConversationRedisCache) GetConversation(ctx context.Context, ownerUserID, conversationID string) (*relationTb.ConversationModel, error) {
return getCache(ctx, c.rcClient, c.getConversationKey(ownerUserID, conversationID), c.expireTime, func(ctx context.Context) (*relationTb.ConversationModel, error) {
return c.conversationDB.Take(ctx, ownerUserID, conversationID)

@ -16,7 +16,7 @@ import (
const (
scanCount = 3000
maxRetryTimes = 5
retryInterval = time.Second * 1
retryInterval = time.Millisecond * 100
)
var errIndex = errors.New("err index")
@ -49,7 +49,7 @@ func (m *metaCacheRedis) ExecDel(ctx context.Context) error {
if err := m.rcClient.TagAsDeletedBatch2(ctx, m.keys); err != nil {
if retryTimes >= m.maxRetryTimes {
err = errs.ErrInternalServer.Wrap(fmt.Sprintf("delete cache error: %v, keys: %v, retry times %d, please check redis server", err, m.keys, retryTimes))
log.ZWarn(ctx, "delete cache failed", err, "keys", m.keys)
log.ZWarn(ctx, "delete cache failed, please handle keys", err, "keys", m.keys)
return err
}
retryTimes++
@ -106,11 +106,11 @@ func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin
return t, nil
}
if v == "" {
return t, errs.ErrRecordNotFound.Wrap("msgCache is not found")
return t, errs.ErrRecordNotFound.Wrap("cache is not found")
}
err = json.Unmarshal([]byte(v), &t)
if err != nil {
log.ZError(ctx, "msgCache json.Unmarshal failed", err, "key", key, "value", v, "expire", expire)
log.ZError(ctx, "cache json.Unmarshal failed", err, "key", key, "value", v, "expire", expire)
return t, utils.Wrap(err, "")
}
return t, nil

@ -30,6 +30,7 @@ type ConversationDatabase interface {
SetUsersConversationFiledTx(ctx context.Context, userIDs []string, conversation *relationTb.ConversationModel, filedMap map[string]interface{}) error
CreateGroupChatConversation(ctx context.Context, groupID string, userIDs []string) error
GetConversationIDs(ctx context.Context, userID string) ([]string, error)
GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error)
}
func NewConversationDatabase(conversation relationTb.ConversationModelInterface, cache cache.ConversationCache, tx tx.Tx) ConversationDatabase {
@ -78,7 +79,7 @@ func (c *ConversationDataBase) SetUsersConversationFiledTx(ctx context.Context,
if err != nil {
return err
}
cache = cache.DelConversationIDs(NotUserIDs...)
cache = cache.DelConversationIDs(NotUserIDs...).DelUserConversationIDsHash(NotUserIDs...)
}
return nil
}); err != nil {
@ -103,7 +104,7 @@ func (c *ConversationDataBase) CreateConversation(ctx context.Context, conversat
for _, conversation := range conversations {
userIDs = append(userIDs, conversation.OwnerUserID)
}
return c.cache.DelConversationIDs(userIDs...).ExecDel(ctx)
return c.cache.DelConversationIDs(userIDs...).DelUserConversationIDsHash(userIDs...).ExecDel(ctx)
}
func (c *ConversationDataBase) SyncPeerUserPrivateConversationTx(ctx context.Context, conversations []*relationTb.ConversationModel) error {
@ -131,7 +132,7 @@ func (c *ConversationDataBase) SyncPeerUserPrivateConversationTx(ctx context.Con
if err := conversationTx.Create(ctx, []*relationTb.ConversationModel{&newConversation}); err != nil {
return err
}
cache = cache.DelConversationIDs([]string{v[0]}...)
cache = cache.DelConversationIDs(v[0]).DelUserConversationIDsHash(v[0])
}
}
}
@ -190,7 +191,7 @@ func (c *ConversationDataBase) SetUserConversations(ctx context.Context, ownerUs
if err != nil {
return err
}
cache = cache.DelConversationIDs([]string{ownerUserID}...)
cache = cache.DelConversationIDs(ownerUserID).DelUserConversationIDsHash(ownerUserID)
}
cache = cache.DelConvsersations(ownerUserID, existConversationIDs...)
return nil
@ -218,7 +219,7 @@ func (c *ConversationDataBase) CreateGroupChatConversation(ctx context.Context,
conversation := relationTb.ConversationModel{ConversationType: constant.SuperGroupChatType, GroupID: groupID, OwnerUserID: v, ConversationID: conversationID}
conversations = append(conversations, &conversation)
}
cache = cache.DelConversationIDs(notExistUserIDs...)
cache = cache.DelConversationIDs(notExistUserIDs...).DelUserConversationIDsHash(notExistUserIDs...)
err = c.conversationDB.Create(ctx, conversations)
if err != nil {
return err
@ -240,3 +241,7 @@ func (c *ConversationDataBase) CreateGroupChatConversation(ctx context.Context,
func (c *ConversationDataBase) GetConversationIDs(ctx context.Context, userID string) ([]string, error) {
return c.cache.GetUserConversationIDs(ctx, userID)
}
func (c *ConversationDataBase) GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error) {
return c.cache.GetUserConversationIDsHash(ctx, ownerUserID)
}

@ -313,15 +313,22 @@ func (db *commonMsgDatabase) unmarshalMsg(msgInfo *unRelationTb.MsgInfoModel) (m
return msgPb, nil
}
func (db *commonMsgDatabase) getMsgBySeqs(ctx context.Context, conversationID string, seqs []int64) (seqMsgs []*sdkws.MsgData, err error) {
seqMsgs, unexistSeqs, err := db.findMsgBySeq(ctx, conversationID, seqs)
func (db *commonMsgDatabase) getMsgBySeqs(ctx context.Context, conversationID string, seqs []int64) (totalMsgs []*sdkws.MsgData, err error) {
m := db.msg.GetDocIDSeqsMap(conversationID, seqs)
var totalUnExistSeqs []int64
for docID, seqs := range m {
log.ZDebug(ctx, "getMsgBySeqs", "docID", docID, "seqs", seqs)
seqMsgs, unexistSeqs, err := db.findMsgBySeq(ctx, docID, seqs)
if err != nil {
return nil, err
}
for _, unexistSeq := range unexistSeqs {
seqMsgs = append(seqMsgs, db.msg.GenExceptionMessageBySeqs([]int64{unexistSeq})...)
totalMsgs = append(totalMsgs, seqMsgs...)
totalUnExistSeqs = append(totalUnExistSeqs, unexistSeqs...)
}
return seqMsgs, nil
for _, unexistSeq := range totalUnExistSeqs {
totalMsgs = append(totalMsgs, db.msg.GenExceptionMessageBySeqs([]int64{unexistSeq})...)
}
return totalMsgs, nil
}
func (db *commonMsgDatabase) refetchDelSeqsMsgs(ctx context.Context, conversationID string, delNums, rangeBegin, begin int64) (seqMsgs []*sdkws.MsgData, err error) {
@ -340,8 +347,8 @@ func (db *commonMsgDatabase) refetchDelSeqsMsgs(ctx context.Context, conversatio
}
if len(reFetchSeqs) > 0 {
m := db.msg.GetDocIDSeqsMap(conversationID, reFetchSeqs)
for docID, seq := range m {
msgs, _, err := db.findMsgBySeq(ctx, docID, seq)
for docID, seqs := range m {
msgs, _, err := db.findMsgBySeq(ctx, docID, seqs)
if err != nil {
return nil, err
}
@ -451,7 +458,7 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, conversation
var seqs []int64
for i := end; i > end-num; i-- {
if i >= begin {
seqs = append(seqs, i)
seqs = append([]int64{i}, seqs...)
} else {
break
}

@ -9,22 +9,22 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
)
type ConversationLocalCacheInterface interface {
GetRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error)
GetConversationIDs(ctx context.Context, userID string) ([]string, error)
}
type ConversationLocalCache struct {
lock sync.Mutex
SuperGroupRecvMsgNotNotifyUserIDs map[string][]string
ConversationIDs map[string][]string
SuperGroupRecvMsgNotNotifyUserIDs map[string]Hash
ConversationIDs map[string]Hash
client discoveryregistry.SvcDiscoveryRegistry
}
type Hash struct {
hash uint64
ids []string
}
func NewConversationLocalCache(client discoveryregistry.SvcDiscoveryRegistry) *ConversationLocalCache {
return &ConversationLocalCache{
SuperGroupRecvMsgNotNotifyUserIDs: make(map[string][]string, 0),
ConversationIDs: make(map[string][]string, 0),
SuperGroupRecvMsgNotNotifyUserIDs: make(map[string]Hash),
ConversationIDs: make(map[string]Hash),
client: client,
}
}
@ -50,11 +50,28 @@ func (g *ConversationLocalCache) GetConversationIDs(ctx context.Context, userID
return nil, err
}
client := conversation.NewConversationClient(conn)
resp, err := client.GetConversationIDs(ctx, &conversation.GetConversationIDsReq{
resp, err := client.GetUserConversationIDsHash(ctx, &conversation.GetUserConversationIDsHashReq{
OwnerUserID: userID,
})
if err != nil {
return nil, err
}
g.lock.Lock()
defer g.lock.Unlock()
hash, ok := g.ConversationIDs[userID]
if !ok || hash.hash != resp.Hash {
conversationIDsResp, err := client.GetConversationIDs(ctx, &conversation.GetConversationIDsReq{
UserID: userID,
})
if err != nil {
return nil, err
}
return resp.ConversationIDs, nil
g.ConversationIDs[userID] = Hash{
hash: resp.Hash,
ids: conversationIDsResp.ConversationIDs,
}
return conversationIDsResp.ConversationIDs, nil
}
return hash.ids, nil
}

@ -10,10 +10,6 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group"
)
type GroupLocalCacheInterface interface {
GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error)
}
type GroupLocalCache struct {
lock sync.Mutex
cache map[string]GroupMemberIDsHash

@ -197,7 +197,7 @@ func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(ctx context.Context, docID strin
break
}
}
log.ZDebug(ctx, "msgInfos", "num", len(doc.Msg))
log.ZDebug(ctx, "msgInfos", "num", len(doc.Msg), "docID", docID)
for _, v := range doc.Msg {
var msg sdkws.MsgData
if err := proto.Unmarshal(v.Msg, &msg); err != nil {

File diff suppressed because it is too large Load Diff

@ -165,6 +165,14 @@ message SetConversationsReq {
message SetConversationsResp {
}
message GetUserConversationIDsHashReq {
string ownerUserID = 1;
}
message GetUserConversationIDsHashResp {
uint64 hash = 1;
}
service conversation {
rpc ModifyConversationField(ModifyConversationFieldReq)returns(ModifyConversationFieldResp);
rpc GetConversation(GetConversationReq)returns(GetConversationResp);
@ -180,4 +188,5 @@ service conversation {
rpc GetConversationIDs(GetConversationIDsReq) returns(GetConversationIDsResp);
rpc GetConversationsHasReadAndMaxSeq(GetConversationsHasReadAndMaxSeqReq) returns(GetConversationsHasReadAndMaxSeqResp);
rpc SetConversations(SetConversationsReq) returns(SetConversationsResp);
rpc GetUserConversationIDsHash(GetUserConversationIDsHashReq) returns(GetUserConversationIDsHashResp);
}

@ -114,7 +114,10 @@ func (g *GroupClient) GetOwnerAndAdminInfos(ctx context.Context, groupID string)
GroupID: groupID,
RoleLevels: []int32{constant.GroupOwner, constant.GroupAdmin},
})
return resp.Members, err
if err != nil {
return nil, err
}
return resp.Members, nil
}
func (g *GroupClient) GetOwnerInfo(ctx context.Context, groupID string) (*sdkws.GroupMemberFullInfo, error) {
@ -137,5 +140,8 @@ func (g *GroupClient) GetGroupMemberIDs(ctx context.Context, groupID string) ([]
resp, err := group.NewGroupClient(cc).GetGroupMemberUserIDs(ctx, &group.GetGroupMemberUserIDsReq{
GroupID: groupID,
})
return resp.UserIDs, err
if err != nil {
return nil, err
}
return resp.UserIDs, nil
}

@ -41,7 +41,7 @@ func newContentTypeConf() map[int32]config.NotificationConf {
// user
constant.UserInfoUpdatedNotification: config.Config.Notification.UserInfoUpdated,
// friend
constant.FriendApplicationNotification: config.Config.Notification.FriendApplication,
constant.FriendApplicationNotification: config.Config.Notification.FriendApplicationAdded,
constant.FriendApplicationApprovedNotification: config.Config.Notification.FriendApplicationApproved,
constant.FriendApplicationRejectedNotification: config.Config.Notification.FriendApplicationRejected,
constant.FriendAddedNotification: config.Config.Notification.FriendAdded,

@ -285,7 +285,7 @@ func (g *GroupNotificationSender) mergeGroupFull(ctx context.Context, groupID st
}
func (g *GroupNotificationSender) GroupCreatedNotification(ctx context.Context, group *relation.GroupModel, members []*relation.GroupMemberModel, userMap map[string]*sdkws.UserInfo) (err error) {
defer log.ZDebug(ctx, "return")
defer log.ZDebug(ctx, "GroupCreatedNotification.return")
defer func() {
if err != nil {
log.ZError(ctx, utils.GetFuncName(1)+" failed", err)

@ -7,7 +7,7 @@ type OptionsOpt func(Options)
func NewOptions(opts ...OptionsOpt) Options {
options := make(map[string]bool, 11)
options[constant.IsNotification] = false
options[constant.IsNotNotification] = false
options[constant.IsSendMsg] = false
options[constant.IsHistory] = false
options[constant.IsPersistent] = false
@ -25,6 +25,12 @@ func NewOptions(opts ...OptionsOpt) Options {
return options
}
func NewMsgOptions() Options {
options := make(map[string]bool, 11)
options[constant.IsOfflinePush] = false
return make(map[string]bool)
}
func WithOptions(options Options, opts ...OptionsOpt) Options {
for _, opt := range opts {
opt(options)
@ -32,9 +38,9 @@ func WithOptions(options Options, opts ...OptionsOpt) Options {
return options
}
func WithNotification(b bool) OptionsOpt {
func WithNotNotification(b bool) OptionsOpt {
return func(options Options) {
options[constant.IsNotification] = b
options[constant.IsNotNotification] = b
}
}
@ -56,15 +62,15 @@ func WithPersistent() OptionsOpt {
}
}
func WithOfflinePush() OptionsOpt {
func WithOfflinePush(b bool) OptionsOpt {
return func(options Options) {
options[constant.IsOfflinePush] = true
options[constant.IsOfflinePush] = b
}
}
func WithUnreadCount() OptionsOpt {
func WithUnreadCount(b bool) OptionsOpt {
return func(options Options) {
options[constant.IsUnreadCount] = true
options[constant.IsUnreadCount] = b
}
}
@ -113,7 +119,7 @@ func (o Options) Is(notification string) bool {
}
func (o Options) IsNotNotification() bool {
return o.Is(constant.IsNotification)
return o.Is(constant.IsNotNotification)
}
func (o Options) IsSendMsg() bool {

@ -170,6 +170,43 @@ func GetHashCode(s string) uint32 {
return crc32.ChecksumIEEE([]byte(s))
}
func MsgIsNotification(msg *sdkws.MsgData) bool {
options := Options(msg.Options)
return !options.IsNotNotification()
}
func GetNotificationConversationID(msg *sdkws.MsgData) string {
switch msg.SessionType {
case constant.SingleChatType:
l := []string{msg.SendID, msg.RecvID}
sort.Strings(l)
return "n_" + strings.Join(l, "_")
case constant.GroupChatType:
return "n_" + msg.GroupID
case constant.SuperGroupChatType:
return "n_" + msg.GroupID
case constant.NotificationChatType:
return "n_" + msg.SendID + "_" + msg.RecvID
}
return ""
}
func GetChatConversationIDByMsg(msg *sdkws.MsgData) string {
switch msg.SessionType {
case constant.SingleChatType:
l := []string{msg.SendID, msg.RecvID}
sort.Strings(l)
return "si_" + strings.Join(l, "_")
case constant.GroupChatType:
return "g_" + msg.GroupID
case constant.SuperGroupChatType:
return "sg_" + msg.GroupID
case constant.NotificationChatType:
return "sn_" + msg.SendID + "_" + msg.RecvID
}
return ""
}
func GetConversationIDByMsg(msg *sdkws.MsgData) string {
options := Options(msg.Options)
switch msg.SessionType {
@ -256,7 +293,7 @@ func GetSeqsBeginEnd(seqs []int64) (int64, int64) {
if len(seqs) == 0 {
return 0, 0
}
return seqs[len(seqs)-1], seqs[0]
return seqs[0], seqs[len(seqs)-1]
}
type MsgBySeq []*sdkws.MsgData

Loading…
Cancel
Save