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/relation/conversation_model.go

104 lines
4.5 KiB

2 years ago
package relation
3 years ago
import (
2 years ago
"Open_IM/pkg/common/db/table"
2 years ago
"gorm.io/gorm"
3 years ago
)
2 years ago
type ConversationGorm struct {
DB *gorm.DB
2 years ago
}
2 years ago
func SetConversation(conversation table.ConversationModel) (bool, error) {
var isUpdate bool
3 years ago
newConversation := conversation
2 years ago
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
return isUpdate, ConversationDB.Model(&Conversation{}).Create(&conversation).Error
3 years ago
// if exist, then update record
} else {
//force update
isUpdate = true
2 years ago
return isUpdate, ConversationDB.Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
2 years ago
Updates(map[string]interface{}{"recv_msg_opt": conversation.RecvMsgOpt, "is_pinned": conversation.IsPinned, "is_private_chat": conversation.IsPrivateChat,
3 years ago
"group_at_type": conversation.GroupAtType, "is_not_in_group": conversation.IsNotInGroup}).Error
}
}
func SetOneConversation(conversation Conversation) error {
2 years ago
return ConversationDB.Model(&Conversation{}).Create(&conversation).Error
3 years ago
}
func PeerUserSetConversation(conversation Conversation) error {
3 years ago
newConversation := conversation
2 years ago
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
return ConversationDB.Model(&Conversation{}).Create(&conversation).Error
3 years ago
// if exist, then update record
}
//force update
2 years ago
return ConversationDB.Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
2 years ago
Updates(map[string]interface{}{"is_private_chat": conversation.IsPrivateChat}).Error
3 years ago
}
func SetRecvMsgOpt(conversation Conversation) (bool, error) {
var isUpdate bool
3 years ago
newConversation := conversation
2 years ago
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
return isUpdate, ConversationDB.Model(&Conversation{}).Create(&conversation).Error
3 years ago
// if exist, then update record
} else {
//force update
isUpdate = true
2 years ago
return isUpdate, ConversationDB.Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
2 years ago
Updates(map[string]interface{}{"recv_msg_opt": conversation.RecvMsgOpt}).Error
3 years ago
}
}
func GetUserAllConversations(ownerUserID string) ([]Conversation, error) {
var conversations []Conversation
2 years ago
err := ConversationDB.Where("owner_user_id=?", ownerUserID).Find(&conversations).Error
3 years ago
return conversations, err
}
func GetMultipleUserConversationByConversationID(ownerUserIDList []string, conversationID string) ([]Conversation, error) {
var conversations []Conversation
2 years ago
err := ConversationDB.Where("owner_user_id IN ? and conversation_id=?", ownerUserIDList, conversationID).Find(&conversations).Error
3 years ago
return conversations, err
}
func GetExistConversationUserIDList(ownerUserIDList []string, conversationID string) ([]string, error) {
3 years ago
var resultArr []string
2 years ago
err := ConversationDB.Table("conversations").Where(" owner_user_id IN (?) and conversation_id=?", ownerUserIDList, conversationID).Pluck("owner_user_id", &resultArr).Error
3 years ago
if err != nil {
return nil, err
}
return resultArr, nil
}
2 years ago
func GetConversation(OwnerUserID, conversationID string) (table.ConversationModel, error) {
var conversation Conversation
2 years ago
err := ConversationDB.Table("conversations").Where("owner_user_id=? and conversation_id=?", OwnerUserID, conversationID).Take(&conversation).Error
3 years ago
return conversation, err
}
func GetConversations(OwnerUserID string, conversationIDs []string) ([]Conversation, error) {
var conversations []Conversation
2 years ago
err := ConversationDB.Model(&Conversation{}).Where("conversation_id IN (?) and owner_user_id=?", conversationIDs, OwnerUserID).Find(&conversations).Error
3 years ago
return conversations, err
}
func GetConversationsByConversationIDMultipleOwner(OwnerUserIDList []string, conversationID string) ([]Conversation, error) {
var conversations []Conversation
2 years ago
err := ConversationDB.Model(&Conversation{}).Where("owner_user_id IN (?) and conversation_id=?", OwnerUserIDList, conversationID).Find(&conversations).Error
3 years ago
return conversations, err
}
3 years ago
func UpdateColumnsConversations(ownerUserIDList []string, conversationID string, args map[string]interface{}) error {
2 years ago
return ConversationDB.Model(&Conversation{}).Where("owner_user_id IN (?) and conversation_id=?", ownerUserIDList, conversationID).Updates(args).Error
3 years ago
}
2 years ago
func GetConversationIDsByUserID(userID string) ([]string, error) {
var IDList []string
2 years ago
err := ConversationDB.Model(&Conversation{}).Where("owner_user_id=?", userID).Pluck("conversation_id", &IDList).Error
return IDList, err
}