From 83c384989967131afcb47d7f4df673725adf6a71 Mon Sep 17 00:00:00 2001 From: Parvesh Monu Date: Mon, 6 Dec 2021 18:14:40 +0530 Subject: [PATCH] Decline pending group invites from user, when user is blocked (#2455) --- VERSION | 2 +- protocol/activity_center_persistence.go | 5 +++++ protocol/messenger_contacts.go | 24 +++++++++++++++++++++++- protocol/messenger_handler.go | 7 +++++-- protocol/messenger_test.go | 11 ++++++----- services/ext/api.go | 2 +- services/shhext/api_nimbus.go | 2 +- 7 files changed, 42 insertions(+), 11 deletions(-) diff --git a/VERSION b/VERSION index 3c23dbe2a..36545ad33 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.91.15 +0.92.0 diff --git a/protocol/activity_center_persistence.go b/protocol/activity_center_persistence.go index c31ee0803..b7ffabbdd 100644 --- a/protocol/activity_center_persistence.go +++ b/protocol/activity_center_persistence.go @@ -366,6 +366,11 @@ func (db sqlitePersistence) DismissAllActivityCenterNotifications() error { return err } +func (db sqlitePersistence) DismissAllActivityCenterNotificationsFromUser(userPublicKey string) error { + _, err := db.db.Exec(`UPDATE activity_center_notifications SET dismissed = 1 WHERE NOT dismissed AND NOT accepted AND author = ?`, userPublicKey) + return err +} + func (db sqlitePersistence) DismissActivityCenterNotifications(ids []types.HexBytes) error { idsArgs := make([]interface{}, 0, len(ids)) diff --git a/protocol/messenger_contacts.go b/protocol/messenger_contacts.go index 84f4ead68..0115f8a94 100644 --- a/protocol/messenger_contacts.go +++ b/protocol/messenger_contacts.go @@ -319,7 +319,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) ([]*Chat, error) { contact, ok := m.allContacts.Load(contactID) if !ok { var err error @@ -358,6 +358,28 @@ func (m *Messenger) BlockContact(contactID string) ([]*Chat, error) { return chats, nil } +func (m *Messenger) BlockContact(contactID string) (*MessengerResponse, error) { + response := &MessengerResponse{} + + chats, err := m.blockContact(contactID) + 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 +} + func (m *Messenger) UnblockContact(contactID string) error { contact, ok := m.allContacts.Load(contactID) if !ok || !contact.Blocked { diff --git a/protocol/messenger_handler.go b/protocol/messenger_handler.go index 82c30dbaf..90333bcc5 100644 --- a/protocol/messenger_handler.go +++ b/protocol/messenger_handler.go @@ -365,11 +365,14 @@ func (m *Messenger) HandleSyncInstallationContact(state *ReceivedMessageState, m if message.Blocked != contact.Blocked { if message.Blocked { state.AllContacts.Store(contact.ID, contact) - chats, err := m.BlockContact(contact.ID) + response, err := m.BlockContact(contact.ID) + if err != nil { + return err + } + err = state.Response.Merge(response) if err != nil { return err } - state.Response.AddChats(chats) } else { contact.Unblock() } diff --git a/protocol/messenger_test.go b/protocol/messenger_test.go index 3fc305e02..4a16c98aa 100644 --- a/protocol/messenger_test.go +++ b/protocol/messenger_test.go @@ -1433,14 +1433,15 @@ func (s *MessengerSuite) TestBlockContact() { s.Require().NoError(err) response, err := s.m.BlockContact(contact.ID) + chats := response.Chats() s.Require().NoError(err) var actualChat2, actualChat3 *Chat - for idx := range response { - if response[idx].ID == chat2.ID { - actualChat2 = response[idx] - } else if response[idx].ID == chat3.ID { - actualChat3 = response[idx] + for idx := range chats { + if chats[idx].ID == chat2.ID { + actualChat2 = chats[idx] + } else if chats[idx].ID == chat3.ID { + actualChat3 = chats[idx] } } // The new unviewed count is updated diff --git a/services/ext/api.go b/services/ext/api.go index 844e8dfdb..0cc3f9dce 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -293,7 +293,7 @@ func (api *PublicAPI) UnmuteChat(parent context.Context, chatID string) error { return api.service.messenger.UnmuteChat(chatID) } -func (api *PublicAPI) BlockContact(parent context.Context, contactID string) ([]*protocol.Chat, error) { +func (api *PublicAPI) BlockContact(parent context.Context, contactID string) (*protocol.MessengerResponse, error) { api.log.Info("blocking contact", "contact", contactID) return api.service.messenger.BlockContact(contactID) } diff --git a/services/shhext/api_nimbus.go b/services/shhext/api_nimbus.go index 24036ca2b..15a1a5011 100644 --- a/services/shhext/api_nimbus.go +++ b/services/shhext/api_nimbus.go @@ -428,7 +428,7 @@ func (api *NimbusPublicAPI) SaveContact(parent context.Context, contact *protoco return api.service.messenger.SaveContact(contact) } -func (api *NimbusPublicAPI) BlockContact(parent context.Context, contact *protocol.Contact) ([]*protocol.Chat, error) { +func (api *NimbusPublicAPI) BlockContact(parent context.Context, contact *protocol.Contact) (*protocol.MessengerResponse, error) { api.log.Info("blocking contact", "contact", contact.ID) return api.service.messenger.BlockContact(contact) }