2023-01-28 13:19:36 +08:00
package relation
2022-04-24 15:55:19 +08:00
import (
2023-02-16 17:20:08 +08:00
"context"
2023-03-23 19:02:20 +08:00
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2023-01-11 16:23:16 +08:00
"gorm.io/gorm"
2022-04-24 15:55:19 +08:00
)
2023-02-16 17:20:08 +08:00
type ConversationGorm struct {
2023-03-15 18:35:06 +08:00
* MetaDB
2022-04-24 15:55:19 +08:00
}
2023-03-15 18:35:06 +08:00
func NewConversationGorm ( db * gorm . DB ) relation . ConversationModelInterface {
return & ConversationGorm { NewMetaDB ( db , & relation . ConversationModel { } ) }
2022-04-24 15:55:19 +08:00
}
2023-02-20 16:42:52 +08:00
2023-03-15 18:35:06 +08:00
func ( c * ConversationGorm ) NewTx ( tx any ) relation . ConversationModelInterface {
return & ConversationGorm { NewMetaDB ( tx . ( * gorm . DB ) , & relation . ConversationModel { } ) }
2023-02-20 16:42:52 +08:00
}
2022-04-24 15:55:19 +08:00
2023-02-22 14:31:30 +08:00
func ( c * ConversationGorm ) Create ( ctx context . Context , conversations [ ] * relation . ConversationModel ) ( err error ) {
2023-03-23 19:02:20 +08:00
return utils . Wrap ( c . db ( ctx ) . Create ( & conversations ) . Error , "" )
2022-04-24 15:55:19 +08:00
}
2023-02-22 14:31:30 +08:00
func ( c * ConversationGorm ) Delete ( ctx context . Context , groupIDs [ ] string ) ( err error ) {
2023-03-23 19:02:20 +08:00
return utils . Wrap ( c . db ( ctx ) . Where ( "group_id in (?)" , groupIDs ) . Delete ( & relation . ConversationModel { } ) . Error , "" )
2022-04-24 15:55:19 +08:00
}
2023-04-21 14:51:57 +08:00
func ( c * ConversationGorm ) UpdateByMap ( ctx context . Context , userIDList [ ] string , conversationID string , args map [ string ] interface { } ) ( rows int64 , err error ) {
result := c . db ( ctx ) . Where ( "owner_user_id IN (?) and conversation_id=?" , userIDList , conversationID ) . Updates ( args )
return result . RowsAffected , utils . Wrap ( result . Error , "" )
2022-04-24 15:55:19 +08:00
}
2023-04-20 16:23:41 +08:00
func ( c * ConversationGorm ) Update ( ctx context . Context , conversation * relation . ConversationModel ) ( err error ) {
2023-04-20 16:31:36 +08:00
return utils . Wrap ( c . db ( ctx ) . Where ( "owner_user_id = ? and conversation_id = ?" , conversation . OwnerUserID , conversation . ConversationID ) . Updates ( conversation ) . Error , "" )
2022-04-24 15:55:19 +08:00
}
2023-02-22 14:31:30 +08:00
func ( c * ConversationGorm ) Find ( ctx context . Context , ownerUserID string , conversationIDs [ ] string ) ( conversations [ ] * relation . ConversationModel , err error ) {
2023-03-23 19:02:20 +08:00
err = utils . Wrap ( c . db ( ctx ) . Where ( "owner_user_id=? and conversation_id IN (?)" , ownerUserID , conversationIDs ) . Find ( & conversations ) . Error , "" )
2022-04-24 15:55:19 +08:00
return conversations , err
}
2022-08-19 12:09:05 +08:00
2023-02-22 14:31:30 +08:00
func ( c * ConversationGorm ) Take ( ctx context . Context , userID , conversationID string ) ( conversation * relation . ConversationModel , err error ) {
2023-02-16 17:20:08 +08:00
cc := & relation . ConversationModel { }
2023-03-23 19:02:20 +08:00
return cc , utils . Wrap ( c . db ( ctx ) . Where ( "conversation_id = ? And owner_user_id = ?" , conversationID , userID ) . Take ( cc ) . Error , "" )
2022-04-24 15:55:19 +08:00
}
2023-03-14 18:47:40 +08:00
2023-04-21 11:26:48 +08:00
func ( c * ConversationGorm ) FindUserID ( ctx context . Context , userIDs [ ] string , conversationIDs [ ] string ) ( existUserID [ ] string , err error ) {
return existUserID , utils . Wrap ( c . db ( ctx ) . Where ( " owner_user_id IN (?) and conversation_id in (?)" , userIDs , conversationIDs ) . Pluck ( "owner_user_id" , & existUserID ) . Error , "" )
2022-04-24 15:55:19 +08:00
}
2023-03-14 18:47:40 +08:00
2023-02-22 14:31:30 +08:00
func ( c * ConversationGorm ) FindConversationID ( ctx context . Context , userID string , conversationIDList [ ] string ) ( existConversationID [ ] string , err error ) {
2023-03-23 19:02:20 +08:00
return existConversationID , utils . Wrap ( c . db ( ctx ) . Where ( " conversation_id IN (?) and owner_user_id=?" , conversationIDList , userID ) . Pluck ( "conversation_id" , & existConversationID ) . Error , "" )
2023-02-16 17:20:08 +08:00
}
2023-03-14 18:47:40 +08:00
2023-02-22 14:31:30 +08:00
func ( c * ConversationGorm ) FindUserIDAllConversationID ( ctx context . Context , userID string ) ( conversationIDList [ ] string , err error ) {
2023-03-23 19:02:20 +08:00
return conversationIDList , utils . Wrap ( c . db ( ctx ) . Where ( "owner_user_id=?" , userID ) . Pluck ( "conversation_id" , & conversationIDList ) . Error , "" )
}
func ( c * ConversationGorm ) FindUserIDAllConversations ( ctx context . Context , userID string ) ( conversations [ ] * relation . ConversationModel , err error ) {
return conversations , utils . Wrap ( c . db ( ctx ) . Where ( "owner_user_id=?" , userID ) . Find ( & conversations ) . Error , "" )
2022-08-19 12:09:05 +08:00
}
2023-02-23 11:04:51 +08:00
func ( c * ConversationGorm ) FindRecvMsgNotNotifyUserIDs ( ctx context . Context , groupID string ) ( userIDs [ ] string , err error ) {
2023-03-23 19:02:20 +08:00
return userIDs , utils . Wrap ( c . db ( ctx ) . Where ( "group_id = ? and recv_msg_opt = ?" , groupID , constant . ReceiveNotNotifyMessage ) . Pluck ( "user_id" , & userIDs ) . Error , "" )
}
func ( c * ConversationGorm ) FindSuperGroupRecvMsgNotNotifyUserIDs ( ctx context . Context , groupID string ) ( userIDs [ ] string , err error ) {
return userIDs , utils . Wrap ( c . db ( ctx ) . Where ( "group_id = ? and recv_msg_opt = ? and conversation_type = ?" , groupID , constant . ReceiveNotNotifyMessage , constant . SuperGroupChatType ) . Pluck ( "user_id" , & userIDs ) . Error , "" )
}
func ( c * ConversationGorm ) GetUserRecvMsgOpt ( ctx context . Context , ownerUserID , conversationID string ) ( opt int , err error ) {
var conversation relation . ConversationModel
return int ( conversation . RecvMsgOpt ) , utils . Wrap ( c . db ( ctx ) . Where ( "conversation_id = ? And owner_user_id = ?" , conversationID , ownerUserID ) . Select ( "recv_msg_opt" ) . Find ( & conversation ) . Error , "" )
2023-02-23 11:04:51 +08:00
}