diff --git a/pkg/common/db/mysql_model/im_mysql_model/conversation_model.go b/pkg/common/db/mysql_model/im_mysql_model/conversation_model.go index 5588ba912..98a97653e 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/conversation_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/conversation_model.go @@ -83,12 +83,20 @@ func GetConversations(OwnerUserID string, conversationIDs []string) ([]db.Conver err := db.DB.MysqlDB.DefaultGormDB().Model(&db.Conversation{}).Where("conversation_id IN (?) and owner_user_id=?", conversationIDs, OwnerUserID).Find(&conversations).Error return conversations, err } + func GetConversationsByConversationIDMultipleOwner(OwnerUserIDList []string, conversationID string) ([]db.Conversation, error) { var conversations []db.Conversation err := db.DB.MysqlDB.DefaultGormDB().Model(&db.Conversation{}).Where("owner_user_id IN (?) and conversation_id=?", OwnerUserIDList, conversationID).Find(&conversations).Error return conversations, err } + func UpdateColumnsConversations(ownerUserIDList []string, conversationID string, args map[string]interface{}) error { return db.DB.MysqlDB.DefaultGormDB().Model(&db.Conversation{}).Where("owner_user_id IN (?) and conversation_id=?", ownerUserIDList, conversationID).Updates(args).Error } + +func GetConversationIDListByUserID(userID string) ([]string, error) { + var IDList []string + err := db.DB.MysqlDB.DefaultGormDB().Model(&db.Conversation{}).Where("user_id=?", userID).Pluck("conversation_id", &IDList).Error + return IDList, err +} diff --git a/pkg/common/db/rocks_cache/rocks_cache.go b/pkg/common/db/rocks_cache/rocks_cache.go index 80f18db6a..af6e75e21 100644 --- a/pkg/common/db/rocks_cache/rocks_cache.go +++ b/pkg/common/db/rocks_cache/rocks_cache.go @@ -32,6 +32,8 @@ const ( joinedSuperGroupListCache = "JOINED_SUPER_GROUP_LIST_CACHE:" groupMemberListHashCache = "GROUP_MEMBER_LIST_HASH_CACHE:" groupMemberNumCache = "GROUP_MEMBER_NUM_CACHE:" + conversationCache = "CONVERSATION_CACHE:" + conversationIDListCache = "CONVERSATION_ID_LIST_CACHE:" ) func init() { @@ -431,3 +433,63 @@ func GetGroupMemberNumFromCache(groupID string) (int64, error) { func DelGroupMemberNumFromCache(groupID string) error { return db.DB.Rc.TagAsDeleted(groupMemberNumCache + groupID) } + +func GetUserConversationIDListFromCache(userID string) ([]string, error) { + getConversationIDList := func() (string, error) { + conversationIDList, err := imdb.GetConversationIDListByUserID(userID) + if err != nil { + return "", utils.Wrap(err, "getConversationIDList failed") + } + bytes, err := json.Marshal(conversationIDList) + return string(bytes), utils.Wrap(err, "") + } + conversationIDListStr, err := db.DB.Rc.Fetch(conversationIDListCache+userID, time.Second*30*60, getConversationIDList) + var conversationIDList []string + err = json.Unmarshal([]byte(conversationIDListStr), &conversationIDList) + if err != nil { + return nil, err + } + return conversationIDList, nil +} + +func DelUserConversationIDListFromCache(userID string) error { + return db.DB.Rc.TagAsDeleted(conversationIDListCache + userID) +} + +func GetConversationFromCache(ownerUserID, conversationID string) (*db.Conversation, error) { + getConversation := func() (string, error) { + conversation, err := imdb.GetConversation(ownerUserID, conversationID) + if err != nil { + return "", utils.Wrap(err, "") + } + bytes, err := json.Marshal(conversation) + return string(bytes), utils.Wrap(err, "") + } + conversationStr, err := db.DB.Rc.Fetch(conversationCache+conversationID, time.Second*30*60, getConversation) + conversation := db.Conversation{} + err = json.Unmarshal([]byte(conversationStr), &conversation) + if err != nil { + return nil, err + } + return &conversation, nil +} + +func GetUserAllConversationList(ownerUserID string) ([]db.Conversation, error) { + IDList, err := GetUserConversationIDListFromCache(ownerUserID) + if err != nil { + return nil, err + } + var conversationList []db.Conversation + for _, conversationID := range IDList { + conversation, err := GetConversationFromCache(ownerUserID, conversationID) + if err != nil { + return nil, utils.Wrap(err, "GetConversationFromCache failed") + } + conversationList = append(conversationList, *conversation) + } + return conversationList, nil +} + +func DelConversationFromCache(conversationID string) error { + return db.DB.Rc.TagAsDeleted(conversationCache + conversationID) +}