@ -47,48 +47,49 @@ type ConversationDataBase struct {
func ( c * ConversationDataBase ) SetUsersConversationFiledTx ( ctx context . Context , userIDs [ ] string , conversation * relationTb . ConversationModel , filedMap map [ string ] interface { } ) error {
func ( c * ConversationDataBase ) SetUsersConversationFiledTx ( ctx context . Context , userIDs [ ] string , conversation * relationTb . ConversationModel , filedMap map [ string ] interface { } ) error {
return c . tx . Transaction ( func ( tx any ) error {
return c . tx . Transaction ( func ( tx any ) error {
conversationTx := c . conversationDB . NewTx ( tx )
conversationTx := c . conversationDB . NewTx ( tx )
haveUserIDs , err := conversationTx . FindUserID ( ctx , userIDs , conversation . ConversationID )
haveUserIDs , err := conversationTx . FindUserID ( ctx , userIDs , [ ] string { conversation . ConversationID } )
if err != nil {
if err != nil {
return err
return err
}
}
cache := c . cache . NewCache ( )
if len ( haveUserIDs ) > 0 {
if len ( haveUserIDs ) > 0 {
err = conversationTx . UpdateByMap ( ctx , haveUserIDs , conversation . ConversationID , filedMap )
_, err = conversationTx . UpdateByMap ( ctx , haveUserIDs , conversation . ConversationID , filedMap )
if err != nil {
if err != nil {
return err
return err
}
}
cache = cache . DelUsersConversation ( conversation . ConversationID , haveUserIDs ... )
}
}
NotUserIDs := utils . DifferenceString ( haveUserIDs , userIDs )
NotUserIDs := utils . DifferenceString ( haveUserIDs , userIDs )
log . ZDebug ( ctx , "SetUsersConversationFiledTx" , "NotUserIDs" , NotUserIDs , "haveUserIDs" , haveUserIDs , "userIDs" , userIDs )
log . ZDebug ( ctx , "SetUsersConversationFiledTx" , "NotUserIDs" , NotUserIDs , "haveUserIDs" , haveUserIDs , "userIDs" , userIDs )
var c List [ ] * relationTb . ConversationModel
var c onversations [ ] * relationTb . ConversationModel
for _ , v := range NotUserIDs {
for _ , v := range NotUserIDs {
temp := new ( relationTb . ConversationModel )
temp := new ( relationTb . ConversationModel )
if err := utils . CopyStructFields ( temp , conversation ) ; err != nil {
if err := utils . CopyStructFields ( temp , conversation ) ; err != nil {
return err
return err
}
}
temp . OwnerUserID = v
temp . OwnerUserID = v
c List = append ( cList , temp )
c onversations = append ( conversations , temp )
}
}
cache := c . cache . NewCache ( )
if len ( c List ) > 0 {
if len ( c onversations ) > 0 {
err = conversationTx . Create ( ctx , c List )
err = conversationTx . Create ( ctx , c onversations )
if err != nil {
if err != nil {
return err
return err
}
}
cache = cache . DelConversationIDs ( NotUserIDs )
cache = cache . DelConversationIDs ( NotUserIDs )
log . ZDebug ( ctx , "SetUsersConversationFiledTx" , "cache" , cache . GetPreDelKeys ( ) , "addr" , & cache )
}
}
// clear cache
// clear cache
log . ZDebug ( ctx , "SetUsersConversationFiledTx" , "cache" , cache . GetPreDelKeys ( ) , "addr" , & cache )
log . ZDebug ( ctx , "SetUsersConversationFiledTx" , "cache" , cache . GetPreDelKeys ( ) , "addr" , & cache )
return cache . DelUsersConversation( haveUserIDs , conversation . ConversationID ) . ExecDel( ctx )
return cache . ExecDel( ctx )
} )
} )
}
}
func ( c * ConversationDataBase ) UpdateUsersConversationFiled ( ctx context . Context , userIDs [ ] string , conversationID string , args map [ string ] interface { } ) error {
func ( c * ConversationDataBase ) UpdateUsersConversationFiled ( ctx context . Context , userIDs [ ] string , conversationID string , args map [ string ] interface { } ) error {
err := c . conversationDB . UpdateByMap ( ctx , userIDs , conversationID , args )
_, err := c . conversationDB . UpdateByMap ( ctx , userIDs , conversationID , args )
if err != nil {
if err != nil {
return err
return err
}
}
return c . cache . DelUsersConversation ( userIDs, conversationID ) . ExecDel ( ctx )
return c . cache . DelUsersConversation ( conversationID, userIDs ... ) . ExecDel ( ctx )
}
}
func ( c * ConversationDataBase ) CreateConversation ( ctx context . Context , conversations [ ] * relationTb . ConversationModel ) error {
func ( c * ConversationDataBase ) CreateConversation ( ctx context . Context , conversations [ ] * relationTb . ConversationModel ) error {
@ -102,45 +103,32 @@ func (c *ConversationDataBase) CreateConversation(ctx context.Context, conversat
func ( c * ConversationDataBase ) SyncPeerUserPrivateConversationTx ( ctx context . Context , conversation * relationTb . ConversationModel ) error {
func ( c * ConversationDataBase ) SyncPeerUserPrivateConversationTx ( ctx context . Context , conversation * relationTb . ConversationModel ) error {
return c . tx . Transaction ( func ( tx any ) error {
return c . tx . Transaction ( func ( tx any ) error {
userIDList := [ ] string { conversation . OwnerUserID , conversation . UserID }
conversationTx := c . conversationDB . NewTx ( tx )
conversationTx := c . conversationDB . NewTx ( tx )
haveUserIDs , err := conversationTx . FindUserID ( ctx , userIDList , conversation . ConversationID )
cache := c . cache . NewCache ( )
if err != nil {
for _ , v := range [ ] [ 3 ] string { { conversation . OwnerUserID , conversation . ConversationID , conversation . UserID } , { conversation . UserID , utils . GetConversationIDBySessionType ( conversation . OwnerUserID , constant . SingleChatType ) , conversation . OwnerUserID } } {
return err
haveUserIDs , err := conversationTx . FindUserID ( ctx , [ ] string { v [ 0 ] } , [ ] string { v [ 1 ] } )
}
filedMap := map [ string ] interface { } { "is_private_chat" : conversation . IsPrivateChat }
if len ( haveUserIDs ) > 0 {
err = conversationTx . UpdateByMap ( ctx , haveUserIDs , conversation . ConversationID , filedMap )
if err != nil {
if err != nil {
return err
return err
}
}
}
if len ( haveUserIDs ) > 0 {
NotUserIDs := utils . DifferenceString ( haveUserIDs , userIDList )
_ , err := conversationTx . UpdateByMap ( ctx , [ ] string { v [ 0 ] } , v [ 1 ] , map [ string ] interface { } { "is_private_chat" : conversation . IsPrivateChat } )
var cList [ ] * relationTb . ConversationModel
if err != nil {
for _ , v := range NotUserIDs {
return err
temp := new ( relationTb . ConversationModel )
}
if v == conversation . UserID {
cache = cache . DelUsersConversation ( v [ 1 ] , v [ 0 ] )
temp . OwnerUserID = conversation . UserID
temp . ConversationID = utils . GetConversationIDBySessionType ( conversation . OwnerUserID , constant . SingleChatType )
temp . ConversationType = constant . SingleChatType
temp . UserID = conversation . OwnerUserID
temp . IsPrivateChat = conversation . IsPrivateChat
} else {
} else {
if err := utils . CopyStructFields ( temp , conversation ) ; err != nil {
newConversation := * conversation
newConversation . OwnerUserID = v [ 0 ]
newConversation . UserID = v [ 2 ]
newConversation . ConversationID = v [ 1 ]
newConversation . IsPrivateChat = conversation . IsPrivateChat
if err := conversationTx . Create ( ctx , [ ] * relationTb . ConversationModel { & newConversation } ) ; err != nil {
return err
return err
}
}
temp . OwnerUserID = v
cache = cache . DelConversationIDs ( [ ] string { v [ 0 ] } )
}
cList = append ( cList , temp )
}
if len ( NotUserIDs ) > 0 {
err = c . conversationDB . Create ( ctx , cList )
if err != nil {
return err
}
}
}
}
// clear cache
return c . cache . ExecDel ( ctx )
return c . cache . DelConversationIDs ( NotUserIDs ) . DelUsersConversation ( haveUserIDs , conversation . ConversationID ) . ExecDel ( ctx )
} )
} )
}
}
@ -168,9 +156,11 @@ func (c *ConversationDataBase) SetUserConversations(ctx context.Context, ownerUs
return err
return err
}
}
if len ( existConversations ) > 0 {
if len ( existConversations ) > 0 {
err = conversationTx . Update ( ctx , conversations )
for _ , conversation := range conversations {
if err != nil {
err = conversationTx . Update ( ctx , conversation )
return err
if err != nil {
return err
}
}
}
}
}
var existConversationIDs [ ] string
var existConversationIDs [ ] string