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.
325 lines
15 KiB
325 lines
15 KiB
1 year ago
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
1 year ago
|
package controller
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||
|
relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||
1 year ago
|
"time"
|
||
1 year ago
|
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
|
||
9 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
||
7 months ago
|
"github.com/openimsdk/protocol/constant"
|
||
|
"github.com/openimsdk/tools/db/pagination"
|
||
|
"github.com/openimsdk/tools/db/tx"
|
||
|
"github.com/openimsdk/tools/log"
|
||
|
"github.com/openimsdk/tools/utils/datautil"
|
||
|
"github.com/openimsdk/tools/utils/stringutil"
|
||
1 year ago
|
)
|
||
|
|
||
|
type ConversationDatabase interface {
|
||
9 months ago
|
// UpdateUsersConversationField updates the properties of a conversation for specified users.
|
||
|
UpdateUsersConversationField(ctx context.Context, userIDs []string, conversationID string, args map[string]any) error
|
||
|
// CreateConversation creates a batch of new conversations.
|
||
6 months ago
|
CreateConversation(ctx context.Context, conversations []*relationtb.Conversation) error
|
||
9 months ago
|
// SyncPeerUserPrivateConversationTx ensures transactional operation while syncing private conversations between peers.
|
||
6 months ago
|
SyncPeerUserPrivateConversationTx(ctx context.Context, conversation []*relationtb.Conversation) error
|
||
9 months ago
|
// FindConversations retrieves multiple conversations of a user by conversation IDs.
|
||
6 months ago
|
FindConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*relationtb.Conversation, error)
|
||
9 months ago
|
// GetUserAllConversation fetches all conversations of a user on the server.
|
||
6 months ago
|
GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationtb.Conversation, error)
|
||
9 months ago
|
// SetUserConversations sets multiple conversation properties for a user, creates new conversations if they do not exist, or updates them otherwise. This operation is atomic.
|
||
6 months ago
|
SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationtb.Conversation) error
|
||
9 months ago
|
// SetUsersConversationFieldTx updates a specific field for multiple users' conversations, creating new conversations if they do not exist, or updates them otherwise. This operation is
|
||
|
// transactional.
|
||
6 months ago
|
SetUsersConversationFieldTx(ctx context.Context, userIDs []string, conversation *relationtb.Conversation, fieldMap map[string]any) error
|
||
9 months ago
|
// CreateGroupChatConversation creates a group chat conversation for the specified group ID and user IDs.
|
||
1 year ago
|
CreateGroupChatConversation(ctx context.Context, groupID string, userIDs []string) error
|
||
9 months ago
|
// GetConversationIDs retrieves conversation IDs for a given user.
|
||
1 year ago
|
GetConversationIDs(ctx context.Context, userID string) ([]string, error)
|
||
9 months ago
|
// GetUserConversationIDsHash gets the hash of conversation IDs for a given user.
|
||
1 year ago
|
GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error)
|
||
9 months ago
|
// GetAllConversationIDs fetches all conversation IDs.
|
||
1 year ago
|
GetAllConversationIDs(ctx context.Context) ([]string, error)
|
||
9 months ago
|
// GetAllConversationIDsNumber returns the number of all conversation IDs.
|
||
1 year ago
|
GetAllConversationIDsNumber(ctx context.Context) (int64, error)
|
||
9 months ago
|
// PageConversationIDs paginates through conversation IDs based on the specified pagination settings.
|
||
12 months ago
|
PageConversationIDs(ctx context.Context, pagination pagination.Pagination) (conversationIDs []string, err error)
|
||
9 months ago
|
// GetConversationsByConversationID retrieves conversations by their IDs.
|
||
6 months ago
|
GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*relationtb.Conversation, error)
|
||
9 months ago
|
// GetConversationIDsNeedDestruct fetches conversations that need to be destructed based on specific criteria.
|
||
6 months ago
|
GetConversationIDsNeedDestruct(ctx context.Context) ([]*relationtb.Conversation, error)
|
||
9 months ago
|
// GetConversationNotReceiveMessageUserIDs gets user IDs for users in a conversation who have not received messages.
|
||
1 year ago
|
GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error)
|
||
7 months ago
|
// GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error)
|
||
|
// FindRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func NewConversationDatabase(conversation database.Conversation, cache cache.ConversationCache, tx tx.Tx) ConversationDatabase {
|
||
1 year ago
|
return &conversationDatabase{
|
||
|
conversationDB: conversation,
|
||
|
cache: cache,
|
||
|
tx: tx,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type conversationDatabase struct {
|
||
6 months ago
|
conversationDB database.Conversation
|
||
1 year ago
|
cache cache.ConversationCache
|
||
7 months ago
|
tx tx.Tx
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (c *conversationDatabase) SetUsersConversationFieldTx(ctx context.Context, userIDs []string, conversation *relationtb.Conversation, fieldMap map[string]any) (err error) {
|
||
12 months ago
|
return c.tx.Transaction(ctx, func(ctx context.Context) error {
|
||
6 months ago
|
cache := c.cache.CloneConversationCache()
|
||
12 months ago
|
if conversation.GroupID != "" {
|
||
|
cache = cache.DelSuperGroupRecvMsgNotNotifyUserIDs(conversation.GroupID).DelSuperGroupRecvMsgNotNotifyUserIDsHash(conversation.GroupID)
|
||
|
}
|
||
|
haveUserIDs, err := c.conversationDB.FindUserID(ctx, userIDs, []string{conversation.ConversationID})
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if len(haveUserIDs) > 0 {
|
||
9 months ago
|
_, err = c.conversationDB.UpdateByMap(ctx, haveUserIDs, conversation.ConversationID, fieldMap)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
cache = cache.DelUsersConversation(conversation.ConversationID, haveUserIDs...)
|
||
9 months ago
|
if _, ok := fieldMap["has_read_seq"]; ok {
|
||
1 year ago
|
for _, userID := range haveUserIDs {
|
||
|
cache = cache.DelUserAllHasReadSeqs(userID, conversation.ConversationID)
|
||
|
}
|
||
|
}
|
||
9 months ago
|
if _, ok := fieldMap["recv_msg_opt"]; ok {
|
||
1 year ago
|
cache = cache.DelConversationNotReceiveMessageUserIDs(conversation.ConversationID)
|
||
|
}
|
||
1 year ago
|
}
|
||
7 months ago
|
NotUserIDs := stringutil.DifferenceString(haveUserIDs, userIDs)
|
||
9 months ago
|
log.ZDebug(ctx, "SetUsersConversationFieldTx", "NotUserIDs", NotUserIDs, "haveUserIDs", haveUserIDs, "userIDs", userIDs)
|
||
6 months ago
|
var conversations []*relationtb.Conversation
|
||
1 year ago
|
now := time.Now()
|
||
1 year ago
|
for _, v := range NotUserIDs {
|
||
6 months ago
|
temp := new(relationtb.Conversation)
|
||
7 months ago
|
if err = datautil.CopyStructFields(temp, conversation); err != nil {
|
||
1 year ago
|
return err
|
||
1 year ago
|
}
|
||
|
temp.OwnerUserID = v
|
||
1 year ago
|
temp.CreateTime = now
|
||
1 year ago
|
conversations = append(conversations, temp)
|
||
|
}
|
||
|
if len(conversations) > 0 {
|
||
12 months ago
|
err = c.conversationDB.Create(ctx, conversations)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
1 year ago
|
cache = cache.DelConversationIDs(NotUserIDs...).DelUserConversationIDsHash(NotUserIDs...).DelConversations(conversation.ConversationID, NotUserIDs...)
|
||
1 year ago
|
}
|
||
6 months ago
|
return cache.ChainExecDel(ctx)
|
||
12 months ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
9 months ago
|
func (c *conversationDatabase) UpdateUsersConversationField(ctx context.Context, userIDs []string, conversationID string, args map[string]any) error {
|
||
1 year ago
|
_, err := c.conversationDB.UpdateByMap(ctx, userIDs, conversationID, args)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
6 months ago
|
cache := c.cache.CloneConversationCache()
|
||
1 year ago
|
cache = cache.DelUsersConversation(conversationID, userIDs...)
|
||
|
if _, ok := args["recv_msg_opt"]; ok {
|
||
|
cache = cache.DelConversationNotReceiveMessageUserIDs(conversationID)
|
||
|
}
|
||
6 months ago
|
return cache.ChainExecDel(ctx)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (c *conversationDatabase) CreateConversation(ctx context.Context, conversations []*relationtb.Conversation) error {
|
||
1 year ago
|
if err := c.conversationDB.Create(ctx, conversations); err != nil {
|
||
|
return err
|
||
|
}
|
||
1 year ago
|
var userIDs []string
|
||
6 months ago
|
cache := c.cache.CloneConversationCache()
|
||
1 year ago
|
for _, conversation := range conversations {
|
||
1 year ago
|
cache = cache.DelConversations(conversation.OwnerUserID, conversation.ConversationID)
|
||
1 year ago
|
cache = cache.DelConversationNotReceiveMessageUserIDs(conversation.ConversationID)
|
||
1 year ago
|
userIDs = append(userIDs, conversation.OwnerUserID)
|
||
|
}
|
||
6 months ago
|
return cache.DelConversationIDs(userIDs...).DelUserConversationIDsHash(userIDs...).ChainExecDel(ctx)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (c *conversationDatabase) SyncPeerUserPrivateConversationTx(ctx context.Context, conversations []*relationtb.Conversation) error {
|
||
12 months ago
|
return c.tx.Transaction(ctx, func(ctx context.Context) error {
|
||
6 months ago
|
cache := c.cache.CloneConversationCache()
|
||
1 year ago
|
for _, conversation := range conversations {
|
||
|
for _, v := range [][2]string{{conversation.OwnerUserID, conversation.UserID}, {conversation.UserID, conversation.OwnerUserID}} {
|
||
1 year ago
|
ownerUserID := v[0]
|
||
|
userID := v[1]
|
||
12 months ago
|
haveUserIDs, err := c.conversationDB.FindUserID(ctx, []string{ownerUserID}, []string{conversation.ConversationID})
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if len(haveUserIDs) > 0 {
|
||
12 months ago
|
_, err := c.conversationDB.UpdateByMap(ctx, []string{ownerUserID}, conversation.ConversationID, map[string]any{"is_private_chat": conversation.IsPrivateChat})
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
1 year ago
|
cache = cache.DelUsersConversation(conversation.ConversationID, ownerUserID)
|
||
1 year ago
|
} else {
|
||
|
newConversation := *conversation
|
||
1 year ago
|
newConversation.OwnerUserID = ownerUserID
|
||
|
newConversation.UserID = userID
|
||
1 year ago
|
newConversation.ConversationID = conversation.ConversationID
|
||
|
newConversation.IsPrivateChat = conversation.IsPrivateChat
|
||
6 months ago
|
if err := c.conversationDB.Create(ctx, []*relationtb.Conversation{&newConversation}); err != nil {
|
||
1 year ago
|
return err
|
||
|
}
|
||
1 year ago
|
cache = cache.DelConversationIDs(ownerUserID).DelUserConversationIDsHash(ownerUserID)
|
||
1 year ago
|
}
|
||
|
}
|
||
|
}
|
||
6 months ago
|
return cache.ChainExecDel(ctx)
|
||
12 months ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (c *conversationDatabase) FindConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*relationtb.Conversation, error) {
|
||
1 year ago
|
return c.cache.GetConversations(ctx, ownerUserID, conversationIDs)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (c *conversationDatabase) GetConversation(ctx context.Context, ownerUserID string, conversationID string) (*relationtb.Conversation, error) {
|
||
1 year ago
|
return c.cache.GetConversation(ctx, ownerUserID, conversationID)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (c *conversationDatabase) GetUserAllConversation(ctx context.Context, ownerUserID string) ([]*relationtb.Conversation, error) {
|
||
1 year ago
|
return c.cache.GetUserAllConversations(ctx, ownerUserID)
|
||
|
}
|
||
|
|
||
6 months ago
|
func (c *conversationDatabase) SetUserConversations(ctx context.Context, ownerUserID string, conversations []*relationtb.Conversation) error {
|
||
12 months ago
|
return c.tx.Transaction(ctx, func(ctx context.Context) error {
|
||
6 months ago
|
cache := c.cache.CloneConversationCache()
|
||
|
groupIDs := datautil.Distinct(datautil.Filter(conversations, func(e *relationtb.Conversation) (string, bool) {
|
||
12 months ago
|
return e.GroupID, e.GroupID != ""
|
||
|
}))
|
||
|
for _, groupID := range groupIDs {
|
||
|
cache = cache.DelSuperGroupRecvMsgNotNotifyUserIDs(groupID).DelSuperGroupRecvMsgNotNotifyUserIDsHash(groupID)
|
||
|
}
|
||
1 year ago
|
var conversationIDs []string
|
||
|
for _, conversation := range conversations {
|
||
|
conversationIDs = append(conversationIDs, conversation.ConversationID)
|
||
1 year ago
|
cache = cache.DelConversations(conversation.OwnerUserID, conversation.ConversationID)
|
||
1 year ago
|
}
|
||
12 months ago
|
existConversations, err := c.conversationDB.Find(ctx, ownerUserID, conversationIDs)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if len(existConversations) > 0 {
|
||
|
for _, conversation := range conversations {
|
||
12 months ago
|
err = c.conversationDB.Update(ctx, conversation)
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
var existConversationIDs []string
|
||
|
for _, conversation := range existConversations {
|
||
|
existConversationIDs = append(existConversationIDs, conversation.ConversationID)
|
||
|
}
|
||
|
|
||
6 months ago
|
var notExistConversations []*relationtb.Conversation
|
||
1 year ago
|
for _, conversation := range conversations {
|
||
7 months ago
|
if !datautil.Contain(conversation.ConversationID, existConversationIDs...) {
|
||
1 year ago
|
notExistConversations = append(notExistConversations, conversation)
|
||
|
}
|
||
|
}
|
||
|
if len(notExistConversations) > 0 {
|
||
|
err = c.conversationDB.Create(ctx, notExistConversations)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
12 months ago
|
cache = cache.DelConversationIDs(ownerUserID).
|
||
|
DelUserConversationIDsHash(ownerUserID).
|
||
6 months ago
|
DelConversationNotReceiveMessageUserIDs(datautil.Slice(notExistConversations, func(e *relationtb.Conversation) string { return e.ConversationID })...)
|
||
1 year ago
|
}
|
||
6 months ago
|
return cache.ChainExecDel(ctx)
|
||
12 months ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
7 months ago
|
// func (c *conversationDatabase) FindRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) ([]string, error) {
|
||
12 months ago
|
// return c.cache.GetSuperGroupRecvMsgNotNotifyUserIDs(ctx, groupID)
|
||
|
//}
|
||
1 year ago
|
|
||
1 year ago
|
func (c *conversationDatabase) CreateGroupChatConversation(ctx context.Context, groupID string, userIDs []string) error {
|
||
12 months ago
|
return c.tx.Transaction(ctx, func(ctx context.Context) error {
|
||
6 months ago
|
cache := c.cache.CloneConversationCache()
|
||
7 months ago
|
conversationID := msgprocessor.GetConversationIDBySessionType(constant.ReadGroupChatType, groupID)
|
||
1 year ago
|
existConversationUserIDs, err := c.conversationDB.FindUserID(ctx, userIDs, []string{conversationID})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
7 months ago
|
notExistUserIDs := stringutil.DifferenceString(userIDs, existConversationUserIDs)
|
||
6 months ago
|
var conversations []*relationtb.Conversation
|
||
1 year ago
|
for _, v := range notExistUserIDs {
|
||
6 months ago
|
conversation := relationtb.Conversation{ConversationType: constant.ReadGroupChatType, GroupID: groupID, OwnerUserID: v, ConversationID: conversationID}
|
||
1 year ago
|
conversations = append(conversations, &conversation)
|
||
1 year ago
|
cache = cache.DelConversations(v, conversationID).DelConversationNotReceiveMessageUserIDs(conversationID)
|
||
1 year ago
|
}
|
||
|
cache = cache.DelConversationIDs(notExistUserIDs...).DelUserConversationIDsHash(notExistUserIDs...)
|
||
|
if len(conversations) > 0 {
|
||
|
err = c.conversationDB.Create(ctx, conversations)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
12 months ago
|
_, err = c.conversationDB.UpdateByMap(ctx, existConversationUserIDs, conversationID, map[string]any{"max_seq": 0})
|
||
1 year ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
for _, v := range existConversationUserIDs {
|
||
1 year ago
|
cache = cache.DelConversations(v, conversationID)
|
||
1 year ago
|
}
|
||
6 months ago
|
return cache.ChainExecDel(ctx)
|
||
12 months ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
|
func (c *conversationDatabase) GetConversationIDs(ctx context.Context, userID string) ([]string, error) {
|
||
|
return c.cache.GetUserConversationIDs(ctx, userID)
|
||
|
}
|
||
|
|
||
1 year ago
|
func (c *conversationDatabase) GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error) {
|
||
1 year ago
|
return c.cache.GetUserConversationIDsHash(ctx, ownerUserID)
|
||
|
}
|
||
|
|
||
|
func (c *conversationDatabase) GetAllConversationIDs(ctx context.Context) ([]string, error) {
|
||
|
return c.conversationDB.GetAllConversationIDs(ctx)
|
||
|
}
|
||
|
|
||
1 year ago
|
func (c *conversationDatabase) GetAllConversationIDsNumber(ctx context.Context) (int64, error) {
|
||
|
return c.conversationDB.GetAllConversationIDsNumber(ctx)
|
||
|
}
|
||
|
|
||
12 months ago
|
func (c *conversationDatabase) PageConversationIDs(ctx context.Context, pagination pagination.Pagination) ([]string, error) {
|
||
|
return c.conversationDB.PageConversationIDs(ctx, pagination)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (c *conversationDatabase) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*relationtb.Conversation, error) {
|
||
1 year ago
|
return c.conversationDB.GetConversationsByConversationID(ctx, conversationIDs)
|
||
|
}
|
||
1 year ago
|
|
||
6 months ago
|
func (c *conversationDatabase) GetConversationIDsNeedDestruct(ctx context.Context) ([]*relationtb.Conversation, error) {
|
||
1 year ago
|
return c.conversationDB.GetConversationIDsNeedDestruct(ctx)
|
||
|
}
|
||
1 year ago
|
|
||
|
func (c *conversationDatabase) GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error) {
|
||
|
return c.cache.GetConversationNotReceiveMessageUserIDs(ctx, conversationID)
|
||
|
}
|