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
}
func (c *Contact) BlockDesktop() {
c.Blocked = true
}
func (c *Contact) Unblock() {
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
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

View File

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

View File

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