diff --git a/protocol/communities_messenger_test.go b/protocol/communities_messenger_test.go index 8a9939c68..c1e85b598 100644 --- a/protocol/communities_messenger_test.go +++ b/protocol/communities_messenger_test.go @@ -1746,12 +1746,32 @@ func (s *MessengerCommunitiesSuite) TestLeaveAndRejoinCommunity() { s.Require().NoError(err) s.Require().Equal(2, joinedCommunities[0].MembersCount()) + chats, err := s.alice.persistence.Chats() + s.Require().NoError(err) + var numberInactiveChats = 0 + for i := 0; i < len(chats); i++ { + if !chats[i].Active { + numberInactiveChats++ + } + } + s.Require().Equal(3, numberInactiveChats) + // alice can rejoin s.joinCommunity(community, s.alice) joinedCommunities, err = s.admin.communitiesManager.Joined() s.Require().NoError(err) s.Require().Equal(3, joinedCommunities[0].MembersCount()) + + chats, err = s.alice.persistence.Chats() + s.Require().NoError(err) + numberInactiveChats = 0 + for i := 0; i < len(chats); i++ { + if !chats[i].Active { + numberInactiveChats++ + } + } + s.Require().Equal(1, numberInactiveChats) } func (s *MessengerCommunitiesSuite) TestShareCommunity() { diff --git a/protocol/message_persistence.go b/protocol/message_persistence.go index 0e2441c03..2e28861dc 100644 --- a/protocol/message_persistence.go +++ b/protocol/message_persistence.go @@ -2149,7 +2149,7 @@ func (db sqlitePersistence) ClearHistoryFromSyncMessage(chat *Chat, currentClock } // Deactivate chat sets a chat as inactive and clear its history -func (db sqlitePersistence) DeactivateChat(chat *Chat, currentClockValue uint64) (err error) { +func (db sqlitePersistence) DeactivateChat(chat *Chat, currentClockValue uint64, doClearHistory bool) (err error) { var tx *sql.Tx tx, err = db.db.BeginTx(context.Background(), &sql.TxOptions{}) @@ -2165,18 +2165,21 @@ func (db sqlitePersistence) DeactivateChat(chat *Chat, currentClockValue uint64) // don't shadow original error _ = tx.Rollback() }() - err = db.deactivateChat(chat, currentClockValue, tx) + err = db.deactivateChat(chat, currentClockValue, tx, doClearHistory) return } -func (db sqlitePersistence) deactivateChat(chat *Chat, currentClockValue uint64, tx *sql.Tx) error { +func (db sqlitePersistence) deactivateChat(chat *Chat, currentClockValue uint64, tx *sql.Tx, doClearHistory bool) error { chat.Active = false err := db.saveChat(tx, *chat) if err != nil { return err } + if !doClearHistory { + return nil + } return db.clearHistory(chat, currentClockValue, tx, true) } diff --git a/protocol/messenger_chats.go b/protocol/messenger_chats.go index 8bbe7bd02..140a16994 100644 --- a/protocol/messenger_chats.go +++ b/protocol/messenger_chats.go @@ -347,10 +347,10 @@ func (m *Messenger) DeactivateChat(request *requests.DeactivateChat) (*Messenger return nil, err } - return m.deactivateChat(request.ID, 0, true) + return m.deactivateChat(request.ID, 0, true, true) } -func (m *Messenger) deactivateChat(chatID string, deactivationClock uint64, shouldBeSynced bool) (*MessengerResponse, error) { +func (m *Messenger) deactivateChat(chatID string, deactivationClock uint64, shouldBeSynced bool, doClearHistory bool) (*MessengerResponse, error) { var response MessengerResponse chat, ok := m.allChats.Load(chatID) if !ok { @@ -380,7 +380,7 @@ func (m *Messenger) deactivateChat(chatID string, deactivationClock uint64, shou deactivationClock, _ = chat.NextClockAndTimestamp(m.getTimesource()) } - err = m.persistence.DeactivateChat(chat, deactivationClock) + err = m.persistence.DeactivateChat(chat, deactivationClock, doClearHistory) if err != nil { return nil, err diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index d64a95278..d0be8ff6f 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -861,12 +861,12 @@ func (m *Messenger) leaveCommunity(communityID types.HexBytes) (*MessengerRespon // Make chat inactive for chatID := range community.Chats() { communityChatID := communityID.String() + chatID - err := m.deleteChat(communityChatID) + response.AddRemovedChat(communityChatID) + + _, err = m.deactivateChat(communityChatID, 0, false, false) if err != nil { return nil, err } - response.AddRemovedChat(communityChatID) - _, err = m.transport.RemoveFilterByChatID(communityChatID) if err != nil { return nil, err diff --git a/protocol/messenger_contacts.go b/protocol/messenger_contacts.go index 28f5718b0..4b372fca9 100644 --- a/protocol/messenger_contacts.go +++ b/protocol/messenger_contacts.go @@ -471,7 +471,7 @@ func (m *Messenger) removeContact(ctx context.Context, response *MessengerRespon _, ok = m.allChats.Load(profileChatID) if ok { - chatResponse, err := m.deactivateChat(profileChatID, 0, false) + chatResponse, err := m.deactivateChat(profileChatID, 0, false, true) if err != nil { return err } diff --git a/protocol/messenger_handler.go b/protocol/messenger_handler.go index 3909c4634..3de6c27b4 100644 --- a/protocol/messenger_handler.go +++ b/protocol/messenger_handler.go @@ -619,7 +619,7 @@ func (m *Messenger) HandleSyncChatRemoved(state *ReceivedMessageState, message p } } - response, err := m.deactivateChat(message.Id, message.Clock, false) + response, err := m.deactivateChat(message.Id, message.Clock, false, true) if err != nil { return err } diff --git a/protocol/messenger_installations_test.go b/protocol/messenger_installations_test.go index 4e73781fc..2eedcdc41 100644 --- a/protocol/messenger_installations_test.go +++ b/protocol/messenger_installations_test.go @@ -172,7 +172,7 @@ func (s *MessengerInstallationSuite) TestSyncInstallation() { chat2.DeletedAtClockValue = 1 err = s.m.SaveChat(chat2) s.Require().NoError(err) - _, err = s.m.deactivateChat(removedChatID, 0, true) + _, err = s.m.deactivateChat(removedChatID, 0, true, true) s.Require().NoError(err) // pair diff --git a/protocol/messenger_sync_chat_test.go b/protocol/messenger_sync_chat_test.go index b98215324..c37bfaf05 100644 --- a/protocol/messenger_sync_chat_test.go +++ b/protocol/messenger_sync_chat_test.go @@ -121,7 +121,7 @@ func (s *MessengerSyncChatSuite) TestRemovePubChat() { s.Pair() - _, err = s.alice1.deactivateChat(publicChatName, 0, true) + _, err = s.alice1.deactivateChat(publicChatName, 0, true, true) s.Require().NoError(err) var allChats []*Chat diff --git a/protocol/persistence_test.go b/protocol/persistence_test.go index fc3c92d22..e794fd263 100644 --- a/protocol/persistence_test.go +++ b/protocol/persistence_test.go @@ -1150,7 +1150,7 @@ func TestDeactivatePublicChat(t *testing.T) { publicChat.LastMessage = &lastMessage publicChat.UnviewedMessagesCount = 1 - err = p.DeactivateChat(publicChat, currentClockValue) + err = p.DeactivateChat(publicChat, currentClockValue, true) // It does not set deleted at for a public chat require.NoError(t, err) @@ -1219,7 +1219,7 @@ func TestDeactivateOneToOneChat(t *testing.T) { chat.LastMessage = &lastMessage chat.UnviewedMessagesCount = 1 - err = p.DeactivateChat(chat, currentClockValue) + err = p.DeactivateChat(chat, currentClockValue, true) // It does set deleted at for a public chat require.NoError(t, err)