fix(community): don't delete chat and messages when leaving community

This creates a smoother experience for users when they leave a community since they can see the exact same messages they had before without having to rely on the mailserver.
This commit is contained in:
Jonathan Rainville 2022-12-07 14:34:48 -05:00
parent 49b3c573f6
commit d87ed0e357
9 changed files with 38 additions and 15 deletions

View File

@ -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() {

View File

@ -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)
}

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -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)