Files
open-im-server/pkg/common/db/relation/conversation_model.go
T

124 lines
5.9 KiB
Go
Raw Normal View History

2023-01-28 13:19:36 +08:00
package relation
2022-04-24 15:55:19 +08:00
import (
2023-01-11 16:23:16 +08:00
"gorm.io/gorm"
2022-04-24 15:55:19 +08:00
)
2023-01-11 16:23:16 +08:00
var ConversationDB *gorm.DB
2023-02-02 19:47:21 +08:00
//type Conversation struct {
// OwnerUserID string `gorm:"column:owner_user_id;primary_key;type:char(128)" json:"OwnerUserID"`
// ConversationID string `gorm:"column:conversation_id;primary_key;type:char(128)" json:"conversationID"`
// ConversationType int32 `gorm:"column:conversation_type" json:"conversationType"`
// UserID string `gorm:"column:user_id;type:char(64)" json:"userID"`
// GroupID string `gorm:"column:group_id;type:char(128)" json:"groupID"`
// RecvMsgOpt int32 `gorm:"column:recv_msg_opt" json:"recvMsgOpt"`
// UnreadCount int32 `gorm:"column:unread_count" json:"unreadCount"`
// DraftTextTime int64 `gorm:"column:draft_text_time" json:"draftTextTime"`
// IsPinned bool `gorm:"column:is_pinned" json:"isPinned"`
// IsPrivateChat bool `gorm:"column:is_private_chat" json:"isPrivateChat"`
// BurnDuration int32 `gorm:"column:burn_duration;default:30" json:"burnDuration"`
// GroupAtType int32 `gorm:"column:group_at_type" json:"groupAtType"`
// IsNotInGroup bool `gorm:"column:is_not_in_group" json:"isNotInGroup"`
// UpdateUnreadCountTime int64 `gorm:"column:update_unread_count_time" json:"updateUnreadCountTime"`
// AttachedInfo string `gorm:"column:attached_info;type:varchar(1024)" json:"attachedInfo"`
// Ex string `gorm:"column:ex;type:varchar(1024)" json:"ex"`
//}
2023-01-11 16:23:16 +08:00
func (Conversation) TableName() string {
return "conversations"
}
2023-01-04 17:22:55 +08:00
func SetConversation(conversation Conversation) (bool, error) {
2022-08-21 17:33:40 +08:00
var isUpdate bool
2022-04-24 15:55:19 +08:00
newConversation := conversation
2023-01-11 16:23:16 +08:00
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
return isUpdate, ConversationDB.Model(&Conversation{}).Create(&conversation).Error
2022-04-24 15:55:19 +08:00
// if exist, then update record
} else {
//force update
2022-08-21 17:33:40 +08:00
isUpdate = true
2023-01-11 16:23:16 +08:00
return isUpdate, ConversationDB.Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
2022-07-14 12:08:28 +08:00
Updates(map[string]interface{}{"recv_msg_opt": conversation.RecvMsgOpt, "is_pinned": conversation.IsPinned, "is_private_chat": conversation.IsPrivateChat,
2022-04-24 15:55:19 +08:00
"group_at_type": conversation.GroupAtType, "is_not_in_group": conversation.IsNotInGroup}).Error
}
}
2023-01-04 17:22:55 +08:00
func SetOneConversation(conversation Conversation) error {
2023-01-11 16:23:16 +08:00
return ConversationDB.Model(&Conversation{}).Create(&conversation).Error
2022-04-24 15:55:19 +08:00
}
2023-01-04 17:22:55 +08:00
func PeerUserSetConversation(conversation Conversation) error {
2022-04-24 15:55:19 +08:00
newConversation := conversation
2023-01-11 16:23:16 +08:00
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
return ConversationDB.Model(&Conversation{}).Create(&conversation).Error
2022-04-24 15:55:19 +08:00
// if exist, then update record
}
//force update
2023-01-11 16:23:16 +08:00
return ConversationDB.Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
2022-07-14 12:08:28 +08:00
Updates(map[string]interface{}{"is_private_chat": conversation.IsPrivateChat}).Error
2022-04-24 15:55:19 +08:00
}
2023-01-04 17:22:55 +08:00
func SetRecvMsgOpt(conversation Conversation) (bool, error) {
2022-08-23 12:06:44 +08:00
var isUpdate bool
2022-04-24 15:55:19 +08:00
newConversation := conversation
2023-01-11 16:23:16 +08:00
if ConversationDB.Model(&Conversation{}).Find(&newConversation).RowsAffected == 0 {
return isUpdate, ConversationDB.Model(&Conversation{}).Create(&conversation).Error
2022-04-24 15:55:19 +08:00
// if exist, then update record
} else {
//force update
2022-08-23 12:06:44 +08:00
isUpdate = true
2023-01-11 16:23:16 +08:00
return isUpdate, ConversationDB.Model(conversation).Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
2022-07-14 12:08:28 +08:00
Updates(map[string]interface{}{"recv_msg_opt": conversation.RecvMsgOpt}).Error
2022-04-24 15:55:19 +08:00
}
}
2023-01-04 17:22:55 +08:00
func GetUserAllConversations(ownerUserID string) ([]Conversation, error) {
var conversations []Conversation
2023-01-11 16:23:16 +08:00
err := ConversationDB.Where("owner_user_id=?", ownerUserID).Find(&conversations).Error
2022-04-24 15:55:19 +08:00
return conversations, err
}
2023-01-04 17:22:55 +08:00
func GetMultipleUserConversationByConversationID(ownerUserIDList []string, conversationID string) ([]Conversation, error) {
var conversations []Conversation
2023-01-11 16:23:16 +08:00
err := ConversationDB.Where("owner_user_id IN ? and conversation_id=?", ownerUserIDList, conversationID).Find(&conversations).Error
2022-04-24 15:55:19 +08:00
return conversations, err
}
2022-04-25 19:56:46 +08:00
func GetExistConversationUserIDList(ownerUserIDList []string, conversationID string) ([]string, error) {
2022-04-24 15:55:19 +08:00
var resultArr []string
2023-01-11 16:23:16 +08:00
err := ConversationDB.Table("conversations").Where(" owner_user_id IN (?) and conversation_id=?", ownerUserIDList, conversationID).Pluck("owner_user_id", &resultArr).Error
2022-04-24 15:55:19 +08:00
if err != nil {
return nil, err
}
return resultArr, nil
}
2023-01-04 17:22:55 +08:00
func GetConversation(OwnerUserID, conversationID string) (Conversation, error) {
var conversation Conversation
2023-01-11 16:23:16 +08:00
err := ConversationDB.Table("conversations").Where("owner_user_id=? and conversation_id=?", OwnerUserID, conversationID).Take(&conversation).Error
2022-04-24 15:55:19 +08:00
return conversation, err
}
2023-01-04 17:22:55 +08:00
func GetConversations(OwnerUserID string, conversationIDs []string) ([]Conversation, error) {
var conversations []Conversation
2023-01-11 16:23:16 +08:00
err := ConversationDB.Model(&Conversation{}).Where("conversation_id IN (?) and owner_user_id=?", conversationIDs, OwnerUserID).Find(&conversations).Error
2022-04-24 15:55:19 +08:00
return conversations, err
}
2022-08-19 12:09:05 +08:00
2023-01-04 17:22:55 +08:00
func GetConversationsByConversationIDMultipleOwner(OwnerUserIDList []string, conversationID string) ([]Conversation, error) {
var conversations []Conversation
2023-01-11 16:23:16 +08:00
err := ConversationDB.Model(&Conversation{}).Where("owner_user_id IN (?) and conversation_id=?", OwnerUserIDList, conversationID).Find(&conversations).Error
2022-04-24 15:55:19 +08:00
return conversations, err
}
2022-08-19 12:09:05 +08:00
2022-04-24 15:55:19 +08:00
func UpdateColumnsConversations(ownerUserIDList []string, conversationID string, args map[string]interface{}) error {
2023-01-11 16:23:16 +08:00
return ConversationDB.Model(&Conversation{}).Where("owner_user_id IN (?) and conversation_id=?", ownerUserIDList, conversationID).Updates(args).Error
2022-04-24 15:55:19 +08:00
}
2022-08-19 12:09:05 +08:00
func GetConversationIDListByUserID(userID string) ([]string, error) {
var IDList []string
2023-01-11 16:23:16 +08:00
err := ConversationDB.Model(&Conversation{}).Where("owner_user_id=?", userID).Pluck("conversation_id", &IDList).Error
2022-08-19 12:09:05 +08:00
return IDList, err
}