refactor: merge AddTokenPermission and UpdateTokenPermission
This commit is contained in:
parent
86d969727f
commit
63131aebbc
|
@ -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,43 +1979,21 @@ 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
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -246,18 +246,10 @@ 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)
|
||||
_, err := o.upsertTokenPermission(communityEvent.TokenPermission)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
_, err := o.addTokenPermission(communityEvent.TokenPermission)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
case protobuf.CommunityEvent_COMMUNITY_MEMBER_TOKEN_PERMISSION_DELETE:
|
||||
_, err := o.deleteTokenPermission(communityEvent.TokenPermission.Id)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue