feat: delete profile showcase community entry on community leave or kicked (#4686)

This commit is contained in:
Mikhail Rogachev 2024-02-09 12:37:54 +03:00 committed by GitHub
parent 2350461818
commit 9b7926b23b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 94 additions and 6 deletions

View File

@ -1817,6 +1817,11 @@ func (m *Messenger) leaveCommunity(communityID types.HexBytes) (*MessengerRespon
}
}
err = m.DeleteProfileShowcaseCommunity(community)
if err != nil {
return nil, err
}
_, err = m.transport.RemoveFilterByChatID(communityID.String())
if err != nil {
return nil, err
@ -1834,6 +1839,11 @@ func (m *Messenger) kickedOutOfCommunity(communityID types.HexBytes) (*Messenger
return nil, err
}
err = m.DeleteProfileShowcaseCommunity(community)
if err != nil {
return nil, err
}
response.AddCommunity(community)
return response, nil
}

View File

@ -10,6 +10,7 @@ import (
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/protobuf"
)
@ -477,10 +478,25 @@ func (m *Messenger) UpdateProfileShowcaseWalletAccount(account *accounts.Account
}
func (m *Messenger) DeleteProfileShowcaseWalletAccount(account *accounts.Account) error {
err := m.persistence.DeleteProfileShowcaseAccountPreference(account.Address.Hex())
deleted, err := m.persistence.DeleteProfileShowcaseAccountPreference(account.Address.Hex())
if err != nil {
return err
}
return m.DispatchProfileShowcase()
if deleted {
return m.DispatchProfileShowcase()
}
return nil
}
func (m *Messenger) DeleteProfileShowcaseCommunity(community *communities.Community) error {
deleted, err := m.persistence.DeleteProfileShowcaseCommunityPreference(community.IDString())
if err != nil {
return err
}
if deleted {
return m.DispatchProfileShowcase()
}
return nil
}

View File

@ -17,6 +17,7 @@ const (
const upsertProfileShowcaseCommunityPreferenceQuery = "INSERT OR REPLACE INTO profile_showcase_communities_preferences(community_id, visibility, sort_order) VALUES (?, ?, ?)" // #nosec G101
const selectProfileShowcaseCommunityPreferenceQuery = "SELECT community_id, visibility, sort_order FROM profile_showcase_communities_preferences" // #nosec G101
const deleteProfileShowcaseCommunityPreferenceQuery = "DELETE FROM profile_showcase_communities_preferences WHERE community_id = ?" // #nosec G101
const upsertProfileShowcaseAccountPreferenceQuery = "INSERT OR REPLACE INTO profile_showcase_accounts_preferences(address, name, color_id, emoji, visibility, sort_order) VALUES (?, ?, ?, ?, ?, ?)" // #nosec G101
const selectProfileShowcaseAccountPreferenceQuery = "SELECT address, name, color_id, emoji, visibility, sort_order FROM profile_showcase_accounts_preferences" // #nosec G101
@ -255,10 +256,24 @@ func (db sqlitePersistence) GetProfileShowcaseAccountPreference(accountAddress s
return nil, err
}
func (db sqlitePersistence) DeleteProfileShowcaseAccountPreference(accountAddress string) error {
_, err := db.db.Exec(deleteProfileShowcaseAccountPreferenceQuery, accountAddress)
func (db sqlitePersistence) DeleteProfileShowcaseAccountPreference(accountAddress string) (bool, error) {
result, err := db.db.Exec(deleteProfileShowcaseAccountPreferenceQuery, accountAddress)
if err != nil {
return false, err
}
return err
rows, err := result.RowsAffected()
return rows > 0, err
}
func (db sqlitePersistence) DeleteProfileShowcaseCommunityPreference(communityID string) (bool, error) {
result, err := db.db.Exec(deleteProfileShowcaseCommunityPreferenceQuery, communityID)
if err != nil {
return false, err
}
rows, err := result.RowsAffected()
return rows > 0, err
}
func (db sqlitePersistence) saveProfileShowcaseCollectiblePreference(tx *sql.Tx, collectible *ProfileShowcaseCollectiblePreference) error {

View File

@ -421,8 +421,14 @@ func (s *TestProfileShowcasePersistence) TestUpdateProfileShowcaseAccountOnWalle
err = persistence.SaveProfileShowcaseAccountPreference(account)
s.Require().NoError(err)
err = persistence.DeleteProfileShowcaseAccountPreference(deleteAccountAddress)
deleted, err := persistence.DeleteProfileShowcaseAccountPreference(deleteAccountAddress)
s.Require().NoError(err)
s.Require().True(deleted)
// One more time to check correct error handling
deleted, err = persistence.DeleteProfileShowcaseAccountPreference(deleteAccountAddress)
s.Require().NoError(err)
s.Require().False(deleted)
preferencesBack, err := persistence.GetProfileShowcasePreferences()
s.Require().NoError(err)
@ -430,3 +436,44 @@ func (s *TestProfileShowcasePersistence) TestUpdateProfileShowcaseAccountOnWalle
s.Require().Len(preferencesBack.Accounts, 1)
s.Require().Equal(*preferencesBack.Accounts[0], *account)
}
func (s *TestProfileShowcasePersistence) TestUpdateProfileShowcaseCommunityOnChange() {
db, err := openTestDB()
s.Require().NoError(err)
persistence := newSQLitePersistence(db)
deleteCommunityID := "0x3243344513424"
preferences := &ProfileShowcasePreferences{
Communities: []*ProfileShowcaseCommunityPreference{
&ProfileShowcaseCommunityPreference{
CommunityID: "0x32433445133424",
ShowcaseVisibility: ProfileShowcaseVisibilityEveryone,
Order: 0,
},
&ProfileShowcaseCommunityPreference{
CommunityID: deleteCommunityID,
ShowcaseVisibility: ProfileShowcaseVisibilityContacts,
Order: 1,
},
},
}
err = persistence.SaveProfileShowcasePreferences(preferences)
s.Require().NoError(err)
deleted, err := persistence.DeleteProfileShowcaseCommunityPreference(deleteCommunityID)
s.Require().NoError(err)
s.Require().True(deleted)
// One more time to check correct error handling
deleted, err = persistence.DeleteProfileShowcaseCommunityPreference(deleteCommunityID)
s.Require().NoError(err)
s.Require().False(deleted)
preferencesBack, err := persistence.GetProfileShowcasePreferences()
s.Require().NoError(err)
s.Require().Len(preferencesBack.Communities, 1)
s.Require().Equal(*preferencesBack.Communities[0], *preferences.Communities[0])
}