diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index f83e0e551..cbeb9240b 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -88,12 +88,11 @@ func syncPeerUserConversation(conversation *pbUser.Conversation, operationID str AttachedInfo: "", Ex: "", } - err := imdb.CreateConversationIfNotExist(peerUserConversation) + err := imdb.PeerUserSetConversation(peerUserConversation) if err != nil { log.NewError(operationID, utils.GetSelfFuncName(), "SetConversation error", err.Error()) return err } - chat.SetConversationNotification(operationID, conversation.UserID) } return nil @@ -140,9 +139,9 @@ func (s *userServer) BatchSetConversations(ctx context.Context, req *pbUser.Batc continue } resp.Success = append(resp.Success, v.ConversationID) - //if err := syncPeerUserConversation(v, req.OperationID); err != nil { - // log.NewError(req.OperationID, utils.GetSelfFuncName(), "syncPeerUserConversation", err.Error()) - //} + if err := syncPeerUserConversation(v, req.OperationID); err != nil { + log.NewError(req.OperationID, utils.GetSelfFuncName(), "syncPeerUserConversation", err.Error()) + } } chat.SetConversationNotification(req.OperationID, req.OwnerUserID) log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc return", resp.String()) @@ -219,10 +218,12 @@ func (s *userServer) SetConversation(ctx context.Context, req *pbUser.SetConvers resp.CommonResp = &pbUser.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg} return resp, nil } - // sync peer user conversation if conversation is singleChatType - //if err := syncPeerUserConversation(req.Conversation, req.OperationID); err != nil { - // log.NewError(req.OperationID, utils.GetSelfFuncName(), "syncPeerUserConversation", err.Error()) - //} + //sync peer user conversation if conversation is singleChatType + if err := syncPeerUserConversation(req.Conversation, req.OperationID); err != nil { + log.NewError(req.OperationID, utils.GetSelfFuncName(), "syncPeerUserConversation", err.Error()) + resp.CommonResp = &pbUser.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg} + return resp, nil + } chat.SetConversationNotification(req.OperationID, req.Conversation.OwnerUserID) log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "rpc return", resp.String()) resp.CommonResp = &pbUser.CommonResp{} @@ -241,6 +242,17 @@ func (s *userServer) SetRecvMsgOpt(ctx context.Context, req *pbUser.SetRecvMsgOp resp.CommonResp = &pbUser.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg} return resp, nil } + stringList := strings.Split(req.ConversationID, "_") + if len(stringList) > 1 { + switch stringList[0] { + case "single_": + conversation.UserID = stringList[1] + conversation.ConversationType = constant.SingleChatType + case "group": + conversation.GroupID = stringList[1] + conversation.ConversationType = constant.GroupChatType + } + } err := imdb.SetRecvMsgOpt(conversation) if err != nil { log.NewError(req.OperationID, utils.GetSelfFuncName(), "SetConversation error", err.Error()) diff --git a/pkg/common/db/mysql_model/im_mysql_model/user_model.go b/pkg/common/db/mysql_model/im_mysql_model/user_model.go index ab367a5ff..5719371c5 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/user_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/user_model.go @@ -325,6 +325,25 @@ func SetConversation(conversation db.Conversation) error { } } +func PeerUserSetConversation(conversation db.Conversation) error { + dbConn, err := db.DB.MysqlDB.DefaultGormDB() + if err != nil { + return err + } + dbConn.LogMode(true) + newConversation := conversation + if dbConn.Model(&db.Conversation{}).Find(&newConversation).RowsAffected == 0 { + log.NewDebug("", utils.GetSelfFuncName(), "conversation", conversation, "not exist in db, create") + return dbConn.Model(&db.Conversation{}).Create(conversation).Error + // if exist, then update record + } + log.NewDebug("", utils.GetSelfFuncName(), "conversation", conversation, "exist in db, update") + //force update + return dbConn.Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID). + Update(map[string]interface{}{"is_private_chat": conversation.IsPrivateChat}).Error + +} + func SetRecvMsgOpt(conversation db.Conversation) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { @@ -377,12 +396,3 @@ func GetConversations(OwnerUserID string, conversationIDs []string) ([]db.Conver err = dbConn.Model(&db.Conversation{}).Where("conversation_id IN (?) and owner_user_id=?", conversationIDs, OwnerUserID).Find(&conversations).Error return conversations, err } - -func CreateConversationIfNotExist(conversation db.Conversation) error { - dbConn, err := db.DB.MysqlDB.DefaultGormDB() - if err != nil { - return err - } - err = dbConn.Model(&db.Conversation{}).Create(conversation).Error - return nil -}