From 63131aebbc42eb1cd25bcf2ca2ed8bc92ed587fa Mon Sep 17 00:00:00 2001 From: Patryk Osmaczko Date: Thu, 17 Aug 2023 17:19:18 +0200 Subject: [PATCH] refactor: merge AddTokenPermission and UpdateTokenPermission --- protocol/communities/community.go | 75 +++++-------------- .../community_encryption_key_action_test.go | 16 ++-- protocol/communities/community_event.go | 14 +--- protocol/communities/community_test.go | 4 +- protocol/communities/manager.go | 4 +- 5 files changed, 33 insertions(+), 80 deletions(-) diff --git a/protocol/communities/community.go b/protocol/communities/community.go index e2aa79b9a..aa6bbfafd 100644 --- a/protocol/communities/community.go +++ b/protocol/communities/community.go @@ -1347,6 +1347,15 @@ func TokenPermissionsByType(permissions map[string]*protobuf.CommunityTokenPermi return result } +func (o *Community) tokenPermissionByID(ID string) *protobuf.CommunityTokenPermission { + permissions := o.config.CommunityDescription.TokenPermissions + if permissions == nil { + return nil + } + + return permissions[ID] +} + func (o *Community) TokenPermissionsByType(permissionType protobuf.CommunityTokenPermission_Type) []*protobuf.CommunityTokenPermission { return TokenPermissionsByType(o.TokenPermissions(), permissionType) } @@ -1374,33 +1383,7 @@ func (o *Community) updateEncrypted() { o.config.CommunityDescription.Encrypted = len(o.TokenPermissionsByType(protobuf.CommunityTokenPermission_BECOME_MEMBER)) > 0 } -func (o *Community) AddTokenPermission(permission *protobuf.CommunityTokenPermission) (*CommunityChanges, error) { - o.mutex.Lock() - defer o.mutex.Unlock() - - if !(o.IsControlNode() || o.hasPermissionToSendTokenPermissionCommunityEvent(protobuf.CommunityEvent_COMMUNITY_MEMBER_TOKEN_PERMISSION_CHANGE, permission.Type)) { - return nil, ErrNotAuthorized - } - - changes, err := o.addTokenPermission(permission) - if err != nil { - return nil, err - } - - if o.IsControlNode() { - o.updateEncrypted() - o.increaseClock() - } else { - err := o.addNewCommunityEvent(o.ToCommunityTokenPermissionChangeCommunityEvent(permission)) - if err != nil { - return nil, err - } - } - - return changes, nil -} - -func (o *Community) UpdateTokenPermission(permissionID string, tokenPermission *protobuf.CommunityTokenPermission) (*CommunityChanges, error) { +func (o *Community) UpsertTokenPermission(tokenPermission *protobuf.CommunityTokenPermission) (*CommunityChanges, error) { o.mutex.Lock() defer o.mutex.Unlock() @@ -1408,7 +1391,7 @@ func (o *Community) UpdateTokenPermission(permissionID string, tokenPermission * return nil, ErrNotAuthorized } - changes, err := o.updateTokenPermission(tokenPermission) + changes, err := o.upsertTokenPermission(tokenPermission) if err != nil { return nil, err } @@ -1996,42 +1979,20 @@ func (o *Community) deleteChat(chatID string) *CommunityChanges { return changes } -func (o *Community) addTokenPermission(permission *protobuf.CommunityTokenPermission) (*CommunityChanges, error) { +func (o *Community) upsertTokenPermission(permission *protobuf.CommunityTokenPermission) (*CommunityChanges, error) { + existed := o.tokenPermissionByID(permission.Id) != nil + if o.config.CommunityDescription.TokenPermissions == nil { o.config.CommunityDescription.TokenPermissions = make(map[string]*protobuf.CommunityTokenPermission) } - - if _, exists := o.config.CommunityDescription.TokenPermissions[permission.Id]; exists { - return nil, ErrTokenPermissionAlreadyExists - } - o.config.CommunityDescription.TokenPermissions[permission.Id] = permission changes := o.emptyCommunityChanges() - - if changes.TokenPermissionsAdded == nil { - changes.TokenPermissionsAdded = make(map[string]*protobuf.CommunityTokenPermission) + if existed { + changes.TokenPermissionsModified[permission.Id] = permission + } else { + changes.TokenPermissionsAdded[permission.Id] = permission } - changes.TokenPermissionsAdded[permission.Id] = permission - - return changes, nil -} - -func (o *Community) updateTokenPermission(permission *protobuf.CommunityTokenPermission) (*CommunityChanges, error) { - if o.config.CommunityDescription.TokenPermissions == nil { - o.config.CommunityDescription.TokenPermissions = make(map[string]*protobuf.CommunityTokenPermission) - } - if _, ok := o.config.CommunityDescription.TokenPermissions[permission.Id]; !ok { - return nil, ErrTokenPermissionNotFound - } - - changes := o.emptyCommunityChanges() - o.config.CommunityDescription.TokenPermissions[permission.Id] = permission - - if changes.TokenPermissionsModified == nil { - changes.TokenPermissionsModified = make(map[string]*protobuf.CommunityTokenPermission) - } - changes.TokenPermissionsModified[permission.Id] = o.config.CommunityDescription.TokenPermissions[permission.Id] return changes, nil } diff --git a/protocol/communities/community_encryption_key_action_test.go b/protocol/communities/community_encryption_key_action_test.go index 9d2053286..1c86616bd 100644 --- a/protocol/communities/community_encryption_key_action_test.go +++ b/protocol/communities/community_encryption_key_action_test.go @@ -369,12 +369,12 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Permiss modified := origin.CreateDeepCopy() for _, permission := range tc.originPermissions { - _, err := origin.AddTokenPermission(permission) + _, err := origin.UpsertTokenPermission(permission) s.Require().NoError(err) } for _, permission := range tc.modifiedPermissions { - _, err := modified.AddTokenPermission(permission) + _, err := modified.UpsertTokenPermission(permission) s.Require().NoError(err) } @@ -496,7 +496,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Members s.Run(tc.name, func() { origin := createTestCommunity(s.identity) for _, permission := range tc.permissions { - _, err := origin.AddTokenPermission(permission) + _, err := origin.UpsertTokenPermission(permission) s.Require().NoError(err) } modified := origin.CreateDeepCopy() @@ -599,7 +599,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Permiss modified := origin.CreateDeepCopy() for _, permission := range tc.originPermissions { - _, err := origin.AddTokenPermission(permission) + _, err := origin.UpsertTokenPermission(permission) s.Require().NoError(err) } for _, member := range tc.originMembers { @@ -608,7 +608,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Permiss } for _, permission := range tc.modifiedPermissions { - _, err := modified.AddTokenPermission(permission) + _, err := modified.UpsertTokenPermission(permission) s.Require().NoError(err) } for _, member := range tc.modifiedMembers { @@ -748,7 +748,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestChannelLevelKeyActions() { modified := origin.CreateDeepCopy() for _, permission := range tc.originPermissions { - _, err := origin.AddTokenPermission(permission) + _, err := origin.UpsertTokenPermission(permission) s.Require().NoError(err) } for _, member := range tc.originMembers { @@ -759,7 +759,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestChannelLevelKeyActions() { } for _, permission := range tc.modifiedPermissions { - _, err := modified.AddTokenPermission(permission) + _, err := modified.UpsertTokenPermission(permission) s.Require().NoError(err) } for _, member := range tc.modifiedMembers { @@ -810,7 +810,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestNilOrigin() { }, } for _, permission := range newCommunityPermissions { - _, err := newCommunity.AddTokenPermission(permission) + _, err := newCommunity.UpsertTokenPermission(permission) s.Require().NoError(err) } diff --git a/protocol/communities/community_event.go b/protocol/communities/community_event.go index 21e8ec5f0..6eccffb53 100644 --- a/protocol/communities/community_event.go +++ b/protocol/communities/community_event.go @@ -246,17 +246,9 @@ func (o *Community) updateCommunityDescriptionByCommunityEvent(communityEvent Co o.config.CommunityDescription.Tags = communityEvent.CommunityConfig.Tags case protobuf.CommunityEvent_COMMUNITY_MEMBER_TOKEN_PERMISSION_CHANGE: - _, exists := o.config.CommunityDescription.TokenPermissions[communityEvent.TokenPermission.Id] - if exists { - _, err := o.updateTokenPermission(communityEvent.TokenPermission) - if err != nil { - return err - } - } else { - _, err := o.addTokenPermission(communityEvent.TokenPermission) - if err != nil { - return err - } + _, err := o.upsertTokenPermission(communityEvent.TokenPermission) + if err != nil { + return err } case protobuf.CommunityEvent_COMMUNITY_MEMBER_TOKEN_PERMISSION_DELETE: diff --git a/protocol/communities/community_test.go b/protocol/communities/community_test.go index b8d8c0c70..9ce11327e 100644 --- a/protocol/communities/community_test.go +++ b/protocol/communities/community_test.go @@ -771,11 +771,11 @@ func (s *CommunitySuite) TestChannelTokenPermissionsByType() { } for _, viewOnlyPermission := range viewOnlyPermissions { - _, err := org.AddTokenPermission(viewOnlyPermission) + _, err := org.UpsertTokenPermission(viewOnlyPermission) s.Require().NoError(err) } for _, viewAndPostPermission := range viewAndPostPermissions { - _, err := org.AddTokenPermission(viewAndPostPermission) + _, err := org.UpsertTokenPermission(viewAndPostPermission) s.Require().NoError(err) } diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index a4323330c..e0207fa37 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -629,7 +629,7 @@ func (m *Manager) EditCommunityTokenPermission(request *requests.EditCommunityTo tokenPermission := request.ToCommunityTokenPermission() - changes, err := community.UpdateTokenPermission(tokenPermission.Id, &tokenPermission) + changes, err := community.UpsertTokenPermission(&tokenPermission) if err != nil { return nil, nil, err } @@ -4722,7 +4722,7 @@ func (m *Manager) createCommunityTokenPermission(request *requests.CreateCommuni tokenPermission := request.ToCommunityTokenPermission() tokenPermission.Id = uuid.New().String() - changes, err := community.AddTokenPermission(&tokenPermission) + changes, err := community.UpsertTokenPermission(&tokenPermission) if err != nil { return nil, nil, err }