From 10d0133974f2d0e85c8c281632021eb0179fe620 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Fri, 8 Apr 2022 22:14:37 +0200 Subject: [PATCH] fix: set only blocked flag to blocked contact for desktop app --- protocol/contact.go | 4 ++++ protocol/message_persistence.go | 32 ++++++++++++++----------- protocol/messenger_contacts.go | 42 ++++++++++++++++++++++++++++----- services/ext/api.go | 7 ++++++ 4 files changed, 65 insertions(+), 20 deletions(-) diff --git a/protocol/contact.go b/protocol/contact.go index 94a4a2770..878cd6d73 100644 --- a/protocol/contact.go +++ b/protocol/contact.go @@ -104,6 +104,10 @@ func (c *Contact) Block() { c.Added = false } +func (c *Contact) BlockDesktop() { + c.Blocked = true +} + func (c *Contact) Unblock() { c.Blocked = false } diff --git a/protocol/message_persistence.go b/protocol/message_persistence.go index 60d6ca8e9..0c3014997 100644 --- a/protocol/message_persistence.go +++ b/protocol/message_persistence.go @@ -1525,7 +1525,7 @@ func (db sqlitePersistence) UpdateMessageOutgoingStatus(id string, newOutgoingSt } // BlockContact updates a contact, deletes all the messages and 1-to-1 chat, updates the unread messages count and returns a map with the new count -func (db sqlitePersistence) BlockContact(contact *Contact) ([]*Chat, error) { +func (db sqlitePersistence) BlockContact(contact *Contact, isDesktopFunc bool) ([]*Chat, error) { var chats []*Chat tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{}) if err != nil { @@ -1540,15 +1540,17 @@ func (db sqlitePersistence) BlockContact(contact *Contact) ([]*Chat, error) { _ = tx.Rollback() }() - // Delete messages - _, err = tx.Exec( - `DELETE - FROM user_messages - WHERE source = ?`, - contact.ID, - ) - if err != nil { - return nil, err + if !isDesktopFunc { + // Delete messages + _, err = tx.Exec( + `DELETE + FROM user_messages + WHERE source = ?`, + contact.ID, + ) + if err != nil { + return nil, err + } } // Update contact @@ -1557,10 +1559,12 @@ func (db sqlitePersistence) BlockContact(contact *Contact) ([]*Chat, error) { return nil, err } - // Delete one-to-one chat - _, err = tx.Exec("DELETE FROM chats WHERE id = ?", contact.ID) - if err != nil { - return nil, err + if !isDesktopFunc { + // Delete one-to-one chat + _, err = tx.Exec("DELETE FROM chats WHERE id = ?", contact.ID) + if err != nil { + return nil, err + } } // Recalculate denormalized fields diff --git a/protocol/messenger_contacts.go b/protocol/messenger_contacts.go index ee4acec92..d9e9d7135 100644 --- a/protocol/messenger_contacts.go +++ b/protocol/messenger_contacts.go @@ -331,7 +331,7 @@ func (m *Messenger) SetContactLocalNickname(request *requests.SetContactLocalNic return response, nil } -func (m *Messenger) blockContact(contactID string) ([]*Chat, error) { +func (m *Messenger) blockContact(contactID string, isDesktopFunc bool) ([]*Chat, error) { contact, ok := m.allContacts.Load(contactID) if !ok { var err error @@ -341,10 +341,14 @@ func (m *Messenger) blockContact(contactID string) ([]*Chat, error) { } } - contact.Block() + if isDesktopFunc { + contact.BlockDesktop() + } else { + contact.Block() + } contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime() - chats, err := m.persistence.BlockContact(contact) + chats, err := m.persistence.BlockContact(contact, isDesktopFunc) if err != nil { return nil, err } @@ -353,8 +357,11 @@ func (m *Messenger) blockContact(contactID string) ([]*Chat, error) { for _, chat := range chats { m.allChats.Store(chat.ID, chat) } - m.allChats.Delete(contact.ID) - m.allChats.Delete(buildProfileChatID(contact.ID)) + + if !isDesktopFunc { + m.allChats.Delete(contact.ID) + m.allChats.Delete(buildProfileChatID(contact.ID)) + } err = m.syncContact(context.Background(), contact) if err != nil { @@ -373,7 +380,30 @@ func (m *Messenger) blockContact(contactID string) ([]*Chat, error) { func (m *Messenger) BlockContact(contactID string) (*MessengerResponse, error) { response := &MessengerResponse{} - chats, err := m.blockContact(contactID) + chats, err := m.blockContact(contactID, false) + if err != nil { + return nil, err + } + response.AddChats(chats) + + response, err = m.DeclineAllPendingGroupInvitesFromUser(response, contactID) + if err != nil { + return nil, err + } + + err = m.persistence.DismissAllActivityCenterNotificationsFromUser(contactID) + if err != nil { + return nil, err + } + + return response, nil +} + +// The same function as the one above. +func (m *Messenger) BlockContactDesktop(contactID string) (*MessengerResponse, error) { + response := &MessengerResponse{} + + chats, err := m.blockContact(contactID, true) if err != nil { return nil, err } diff --git a/services/ext/api.go b/services/ext/api.go index 6be440c62..6ce53a167 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -299,6 +299,13 @@ func (api *PublicAPI) BlockContact(parent context.Context, contactID string) (*p return api.service.messenger.BlockContact(contactID) } +// This function is the same as the one above, but used only on the desktop side, since at the end it doesn't set +// `Added` flag to `false`, but only `Blocked` to `true` +func (api *PublicAPI) BlockContactDesktop(parent context.Context, contactID string) (*protocol.MessengerResponse, error) { + api.log.Info("blocking contact", "contact", contactID) + return api.service.messenger.BlockContactDesktop(contactID) +} + func (api *PublicAPI) UnblockContact(parent context.Context, contactID string) error { return api.service.messenger.UnblockContact(contactID) }