You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/controller/conversation.go

243 lines
9.9 KiB

package controller
import (
"context"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/tx"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
)
2 years ago
type ConversationDatabase interface {
//UpdateUserConversationFiled 更新用户该会话的属性信息
2 years ago
UpdateUsersConversationFiled(ctx context.Context, userIDs []string, conversationID string, args map[string]interface{}) error
//CreateConversation 创建一批新的会话
2 years ago
CreateConversation(ctx context.Context, conversations []*relationTb.ConversationModel) error
//SyncPeerUserPrivateConversation 同步对端私聊会话内部保证事务操作
2 years ago
SyncPeerUserPrivateConversationTx(ctx context.Context, conversation []*relationTb.ConversationModel) error
//FindConversations 根据会话ID获取某个用户的多个会话
FindConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*relationTb.ConversationModel, error)
//FindRecvMsgNotNotifyUserIDs 获取超级大群开启免打扰的用户ID
FindRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error)
//GetUserAllConversation 获取一个用户在服务器上所有的会话
2 years ago
GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationTb.ConversationModel, error)
//SetUserConversations 设置用户多个会话属性,如果会话不存在则创建,否则更新,内部保证原子性
2 years ago
SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationTb.ConversationModel) error
//SetUsersConversationFiledTx 设置多个用户会话关于某个字段的更新操作,如果会话不存在则创建,否则更新,内部保证事务操作
2 years ago
SetUsersConversationFiledTx(ctx context.Context, userIDs []string, conversation *relationTb.ConversationModel, filedMap map[string]interface{}) error
2 years ago
CreateGroupChatConversation(ctx context.Context, groupID string, userIDs []string) error
2 years ago
GetConversationIDs(ctx context.Context, userID string) ([]string, error)
}
2 years ago
func NewConversationDatabase(conversation relationTb.ConversationModelInterface, cache cache.ConversationCache, tx tx.Tx) ConversationDatabase {
2 years ago
return &ConversationDataBase{
conversationDB: conversation,
cache: cache,
tx: tx,
}
}
type ConversationDataBase struct {
2 years ago
conversationDB relationTb.ConversationModelInterface
cache cache.ConversationCache
2 years ago
tx tx.Tx
}
2 years ago
func (c *ConversationDataBase) SetUsersConversationFiledTx(ctx context.Context, userIDs []string, conversation *relationTb.ConversationModel, filedMap map[string]interface{}) (err error) {
cache := c.cache.NewCache()
if err := c.tx.Transaction(func(tx any) error {
2 years ago
conversationTx := c.conversationDB.NewTx(tx)
2 years ago
haveUserIDs, err := conversationTx.FindUserID(ctx, userIDs, []string{conversation.ConversationID})
if err != nil {
return err
}
2 years ago
if len(haveUserIDs) > 0 {
2 years ago
_, err = conversationTx.UpdateByMap(ctx, haveUserIDs, conversation.ConversationID, filedMap)
if err != nil {
return err
}
2 years ago
cache = cache.DelUsersConversation(conversation.ConversationID, haveUserIDs...)
}
2 years ago
NotUserIDs := utils.DifferenceString(haveUserIDs, userIDs)
2 years ago
log.ZDebug(ctx, "SetUsersConversationFiledTx", "NotUserIDs", NotUserIDs, "haveUserIDs", haveUserIDs, "userIDs", userIDs)
2 years ago
var conversations []*relationTb.ConversationModel
2 years ago
for _, v := range NotUserIDs {
temp := new(relationTb.ConversationModel)
if err := utils.CopyStructFields(temp, conversation); err != nil {
return err
}
temp.OwnerUserID = v
2 years ago
conversations = append(conversations, temp)
2 years ago
}
2 years ago
if len(conversations) > 0 {
err = conversationTx.Create(ctx, conversations)
2 years ago
if err != nil {
return err
}
2 years ago
cache = cache.DelConversationIDs(NotUserIDs...)
}
2 years ago
return nil
}); err != nil {
return err
}
return cache.ExecDel(ctx)
}
2 years ago
func (c *ConversationDataBase) UpdateUsersConversationFiled(ctx context.Context, userIDs []string, conversationID string, args map[string]interface{}) error {
2 years ago
_, err := c.conversationDB.UpdateByMap(ctx, userIDs, conversationID, args)
2 years ago
if err != nil {
return err
}
2 years ago
return c.cache.DelUsersConversation(conversationID, userIDs...).ExecDel(ctx)
}
2 years ago
func (c *ConversationDataBase) CreateConversation(ctx context.Context, conversations []*relationTb.ConversationModel) error {
2 years ago
if err := c.conversationDB.Create(ctx, conversations); err != nil {
return err
}
2 years ago
var userIDs []string
for _, conversation := range conversations {
userIDs = append(userIDs, conversation.OwnerUserID)
}
return c.cache.DelConversationIDs(userIDs...).ExecDel(ctx)
}
2 years ago
func (c *ConversationDataBase) SyncPeerUserPrivateConversationTx(ctx context.Context, conversations []*relationTb.ConversationModel) error {
2 years ago
cache := c.cache.NewCache()
if err := c.tx.Transaction(func(tx any) error {
2 years ago
conversationTx := c.conversationDB.NewTx(tx)
2 years ago
for _, conversation := range conversations {
for _, v := range [][2]string{{conversation.OwnerUserID, conversation.UserID}, {conversation.UserID, conversation.OwnerUserID}} {
haveUserIDs, err := conversationTx.FindUserID(ctx, []string{v[0]}, []string{conversation.ConversationID})
2 years ago
if err != nil {
return err
}
2 years ago
if len(haveUserIDs) > 0 {
_, err := conversationTx.UpdateByMap(ctx, []string{v[0]}, conversation.ConversationID, map[string]interface{}{"is_private_chat": conversation.IsPrivateChat})
if err != nil {
return err
}
cache = cache.DelUsersConversation(conversation.ConversationID, v[0])
} else {
newConversation := *conversation
newConversation.OwnerUserID = v[0]
newConversation.UserID = v[1]
newConversation.ConversationID = conversation.ConversationID
newConversation.IsPrivateChat = conversation.IsPrivateChat
if err := conversationTx.Create(ctx, []*relationTb.ConversationModel{&newConversation}); err != nil {
return err
}
2 years ago
cache = cache.DelConversationIDs([]string{v[0]}...)
}
}
}
2 years ago
return nil
}); err != nil {
return err
}
return c.cache.ExecDel(ctx)
}
2 years ago
func (c *ConversationDataBase) FindConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*relationTb.ConversationModel, error) {
2 years ago
return c.cache.GetConversations(ctx, ownerUserID, conversationIDs)
}
2 years ago
func (c *ConversationDataBase) GetConversation(ctx context.Context, ownerUserID string, conversationID string) (*relationTb.ConversationModel, error) {
2 years ago
return c.cache.GetConversation(ctx, ownerUserID, conversationID)
}
2 years ago
func (c *ConversationDataBase) GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationTb.ConversationModel, error) {
2 years ago
return c.cache.GetUserAllConversations(ctx, ownerUserID)
}
2 years ago
func (c *ConversationDataBase) SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationTb.ConversationModel) error {
2 years ago
cache := c.cache.NewCache()
if err := c.tx.Transaction(func(tx any) error {
2 years ago
var conversationIDs []string
for _, conversation := range conversations {
2 years ago
conversationIDs = append(conversationIDs, conversation.ConversationID)
}
2 years ago
conversationTx := c.conversationDB.NewTx(tx)
2 years ago
existConversations, err := conversationTx.Find(ctx, ownerUserID, conversationIDs)
if err != nil {
return err
}
2 years ago
if len(existConversations) > 0 {
2 years ago
for _, conversation := range conversations {
err = conversationTx.Update(ctx, conversation)
if err != nil {
return err
}
}
}
2 years ago
var existConversationIDs []string
for _, conversation := range existConversations {
existConversationIDs = append(existConversationIDs, conversation.ConversationID)
}
2 years ago
var notExistConversations []*relationTb.ConversationModel
for _, conversation := range conversations {
2 years ago
if !utils.IsContain(conversation.ConversationID, existConversationIDs) {
notExistConversations = append(notExistConversations, conversation)
}
}
2 years ago
if len(notExistConversations) > 0 {
err = c.conversationDB.Create(ctx, notExistConversations)
if err != nil {
return err
}
2 years ago
cache = cache.DelConversationIDs([]string{ownerUserID}...)
}
2 years ago
cache = cache.DelConvsersations(ownerUserID, existConversationIDs...)
return nil
}); err != nil {
return err
}
return cache.ExecDel(ctx)
}
func (c *ConversationDataBase) FindRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error) {
2 years ago
return c.cache.GetSuperGroupRecvMsgNotNotifyUserIDs(ctx, groupID)
}
2 years ago
func (c *ConversationDataBase) CreateGroupChatConversation(ctx context.Context, groupID string, userIDs []string) error {
2 years ago
cache := c.cache.NewCache()
2 years ago
conversationID := utils.GetConversationIDBySessionType(constant.SuperGroupChatType, groupID)
2 years ago
if err := c.tx.Transaction(func(tx any) error {
existConversationUserIDs, err := c.conversationDB.FindUserID(ctx, userIDs, []string{conversationID})
2 years ago
if err != nil {
return err
}
notExistUserIDs := utils.DifferenceString(userIDs, existConversationUserIDs)
var conversations []*relationTb.ConversationModel
for _, v := range notExistUserIDs {
conversation := relationTb.ConversationModel{ConversationType: constant.SuperGroupChatType, GroupID: groupID, OwnerUserID: v, ConversationID: conversationID}
conversations = append(conversations, &conversation)
}
2 years ago
cache = cache.DelConversationIDs(notExistUserIDs...)
2 years ago
err = c.conversationDB.Create(ctx, conversations)
if err != nil {
return err
}
2 years ago
_, err = c.conversationDB.UpdateByMap(ctx, existConversationUserIDs, conversationID, map[string]interface{}{"max_seq": 0})
if err != nil {
return err
}
for _, v := range existConversationUserIDs {
cache = cache.DelConvsersations(v, conversationID)
}
return nil
}); err != nil {
2 years ago
return err
2 years ago
}
return cache.ExecDel(ctx)
2 years ago
}
2 years ago
func (c *ConversationDataBase) GetConversationIDs(ctx context.Context, userID string) ([]string, error) {
return c.cache.GetUserConversationIDs(ctx, userID)
}