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

This commit is contained in:
Sale Djenic 2022-04-08 22:14:37 +02:00 committed by saledjenic
parent 9fef24917a
commit 10d0133974
4 changed files with 65 additions and 20 deletions

View File

@ -104,6 +104,10 @@ func (c *Contact) Block() {
c.Added = false c.Added = false
} }
func (c *Contact) BlockDesktop() {
c.Blocked = true
}
func (c *Contact) Unblock() { func (c *Contact) Unblock() {
c.Blocked = false c.Blocked = false
} }

View File

@ -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 // 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 var chats []*Chat
tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{}) tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil { if err != nil {
@ -1540,15 +1540,17 @@ func (db sqlitePersistence) BlockContact(contact *Contact) ([]*Chat, error) {
_ = tx.Rollback() _ = tx.Rollback()
}() }()
// Delete messages if !isDesktopFunc {
_, err = tx.Exec( // Delete messages
`DELETE _, err = tx.Exec(
FROM user_messages `DELETE
WHERE source = ?`, FROM user_messages
contact.ID, WHERE source = ?`,
) contact.ID,
if err != nil { )
return nil, err if err != nil {
return nil, err
}
} }
// Update contact // Update contact
@ -1557,10 +1559,12 @@ func (db sqlitePersistence) BlockContact(contact *Contact) ([]*Chat, error) {
return nil, err return nil, err
} }
// Delete one-to-one chat if !isDesktopFunc {
_, err = tx.Exec("DELETE FROM chats WHERE id = ?", contact.ID) // Delete one-to-one chat
if err != nil { _, err = tx.Exec("DELETE FROM chats WHERE id = ?", contact.ID)
return nil, err if err != nil {
return nil, err
}
} }
// Recalculate denormalized fields // Recalculate denormalized fields

View File

@ -331,7 +331,7 @@ func (m *Messenger) SetContactLocalNickname(request *requests.SetContactLocalNic
return response, nil 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) contact, ok := m.allContacts.Load(contactID)
if !ok { if !ok {
var err error 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() contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
chats, err := m.persistence.BlockContact(contact) chats, err := m.persistence.BlockContact(contact, isDesktopFunc)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -353,8 +357,11 @@ func (m *Messenger) blockContact(contactID string) ([]*Chat, error) {
for _, chat := range chats { for _, chat := range chats {
m.allChats.Store(chat.ID, chat) 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) err = m.syncContact(context.Background(), contact)
if err != nil { if err != nil {
@ -373,7 +380,30 @@ func (m *Messenger) blockContact(contactID string) ([]*Chat, error) {
func (m *Messenger) BlockContact(contactID string) (*MessengerResponse, error) { func (m *Messenger) BlockContact(contactID string) (*MessengerResponse, error) {
response := &MessengerResponse{} 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -299,6 +299,13 @@ func (api *PublicAPI) BlockContact(parent context.Context, contactID string) (*p
return api.service.messenger.BlockContact(contactID) 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 { func (api *PublicAPI) UnblockContact(parent context.Context, contactID string) error {
return api.service.messenger.UnblockContact(contactID) return api.service.messenger.UnblockContact(contactID)
} }