Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set only blocked flag to blocked contact for desktop app #2635

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions protocol/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
32 changes: 18 additions & 14 deletions protocol/message_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
42 changes: 36 additions & 6 deletions protocol/messenger_contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions services/ext/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down