2023-01-28 13:19:36 +08:00
package relation
2022-04-24 15:55:19 +08:00
import (
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/db/table/relation"
"OpenIM/pkg/common/tracelog"
"OpenIM/pkg/utils"
2023-02-16 17:20:08 +08:00
"context"
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 Conversation interface {
2023-02-22 14:31:30 +08:00
Create ( ctx context . Context , conversations [ ] * relation . ConversationModel ) ( err error )
Delete ( ctx context . Context , groupIDs [ ] string ) ( err error )
UpdateByMap ( ctx context . Context , userIDList [ ] string , conversationID string , args map [ string ] interface { } ) ( err error )
Update ( ctx context . Context , conversations [ ] * relation . ConversationModel ) ( err error )
Find ( ctx context . Context , ownerUserID string , conversationIDs [ ] string ) ( conversations [ ] * relation . ConversationModel , err error )
FindUserID ( ctx context . Context , userIDList [ ] string , conversationID string ) ( [ ] string , error )
FindUserIDAllConversationID ( ctx context . Context , userID string ) ( [ ] string , error )
Take ( ctx context . Context , userID , conversationID string ) ( conversation * relation . ConversationModel , err error )
FindConversationID ( ctx context . Context , userID string , conversationIDList [ ] string ) ( existConversationID [ ] string , err error )
2023-02-23 11:04:51 +08:00
FindRecvMsgNotNotifyUserIDs ( ctx context . Context , groupID string ) ( [ ] string , error )
2023-02-20 16:42:52 +08:00
NewTx ( tx any ) Conversation
2023-01-11 16:23:16 +08:00
}
2023-02-16 17:20:08 +08:00
type ConversationGorm struct {
DB * gorm . DB
2022-04-24 15:55:19 +08:00
}
2023-02-16 17:20:08 +08:00
func NewConversationGorm ( DB * gorm . DB ) Conversation {
return & ConversationGorm { DB : DB }
2022-04-24 15:55:19 +08:00
}
2023-02-20 16:42:52 +08:00
func ( c * ConversationGorm ) NewTx ( tx any ) Conversation {
return & ConversationGorm { DB : tx . ( * gorm . DB ) }
}
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-02-16 17:20:08 +08:00
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "conversations" , conversations )
} ( )
2023-02-22 14:31:30 +08:00
return utils . Wrap ( c . DB . 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-02-16 17:20:08 +08:00
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "groupIDs" , groupIDs )
} ( )
2023-02-22 14:31:30 +08:00
return utils . Wrap ( c . DB . Where ( "group_id in (?)" , groupIDs ) . Delete ( & relation . ConversationModel { } ) . Error , "" )
2022-04-24 15:55:19 +08:00
}
2023-02-22 14:31:30 +08:00
func ( c * ConversationGorm ) UpdateByMap ( ctx context . Context , userIDList [ ] string , conversationID string , args map [ string ] interface { } ) ( err error ) {
2023-02-16 17:20:08 +08:00
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userIDList" , userIDList , "conversationID" , conversationID )
} ( )
2023-02-22 14:31:30 +08:00
return utils . Wrap ( c . DB . Model ( & relation . ConversationModel { } ) . Where ( "owner_user_id IN (?) and conversation_id=?" , userIDList , conversationID ) . Updates ( args ) . Error , "" )
2022-04-24 15:55:19 +08:00
}
2023-02-22 14:31:30 +08:00
func ( c * ConversationGorm ) Update ( ctx context . Context , conversations [ ] * relation . ConversationModel ) ( err error ) {
2023-02-16 17:20:08 +08:00
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "conversations" , conversations )
} ( )
2023-02-22 14:31:30 +08:00
return utils . Wrap ( c . DB . Updates ( & conversations ) . 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-02-16 17:20:08 +08:00
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "ownerUserID" , ownerUserID , "groups" , conversations )
} ( )
2023-02-22 14:31:30 +08:00
err = utils . Wrap ( c . DB . 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 { }
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userID" , userID , "conversation" , * conversation )
} ( )
2023-02-22 14:31:30 +08:00
return cc , utils . Wrap ( c . DB . Where ( "conversation_id = ? And owner_user_id = ?" , conversationID , userID ) . Take ( cc ) . Error , "" )
2022-04-24 15:55:19 +08:00
}
2023-02-22 14:31:30 +08:00
func ( c * ConversationGorm ) FindUserID ( ctx context . Context , userIDList [ ] string , conversationID string ) ( existUserID [ ] string , err error ) {
2023-02-16 17:20:08 +08:00
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userID" , userIDList , "existUserID" , existUserID )
} ( )
2023-02-22 14:31:30 +08:00
return existUserID , utils . Wrap ( c . DB . Where ( " owner_user_id IN (?) and conversation_id=?" , userIDList , conversationID ) . Pluck ( "owner_user_id" , & existUserID ) . Error , "" )
2022-04-24 15:55:19 +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-02-16 17:20:08 +08:00
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userID" , userID , "existConversationIDList" , existConversationID )
} ( )
2023-02-22 14:31:30 +08:00
return existConversationID , utils . Wrap ( c . DB . Where ( " conversation_id IN (?) and owner_user_id=?" , conversationIDList , userID ) . Pluck ( "conversation_id" , & existConversationID ) . Error , "" )
2023-02-16 17:20:08 +08:00
}
2023-02-22 14:31:30 +08:00
func ( c * ConversationGorm ) FindUserIDAllConversationID ( ctx context . Context , userID string ) ( conversationIDList [ ] string , err error ) {
2023-02-16 17:20:08 +08:00
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "userID" , userID , "conversationIDList" , conversationIDList )
} ( )
2023-02-22 14:31:30 +08:00
return conversationIDList , utils . Wrap ( c . DB . Model ( & relation . ConversationModel { } ) . Where ( "owner_user_id=?" , userID ) . Pluck ( "conversation_id" , & conversationIDList ) . 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 ) {
defer func ( ) {
tracelog . SetCtxDebug ( ctx , utils . GetFuncName ( 1 ) , err , "groupID" , groupID , "userIDs" , userIDs )
} ( )
2023-02-23 17:28:57 +08:00
return userIDs , utils . Wrap ( c . DB . Model ( & relation . ConversationModel { } ) . Where ( "group_id = ? and recv_msg_opt = ?" , groupID , constant . ReceiveNotNotifyMessage ) . Pluck ( "user_id" , & userIDs ) . Error , "" )
2023-02-23 11:04:51 +08:00
}