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
|
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 {
|
func (o *Community) TokenPermissionsByType(permissionType protobuf.CommunityTokenPermission_Type) []*protobuf.CommunityTokenPermission {
|
||||||
return TokenPermissionsByType(o.TokenPermissions(), permissionType)
|
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
|
o.config.CommunityDescription.Encrypted = len(o.TokenPermissionsByType(protobuf.CommunityTokenPermission_BECOME_MEMBER)) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Community) AddTokenPermission(permission *protobuf.CommunityTokenPermission) (*CommunityChanges, error) {
|
func (o *Community) UpsertTokenPermission(tokenPermission *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) {
|
|
||||||
o.mutex.Lock()
|
o.mutex.Lock()
|
||||||
defer o.mutex.Unlock()
|
defer o.mutex.Unlock()
|
||||||
|
|
||||||
|
@ -1408,7 +1391,7 @@ func (o *Community) UpdateTokenPermission(permissionID string, tokenPermission *
|
||||||
return nil, ErrNotAuthorized
|
return nil, ErrNotAuthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
changes, err := o.updateTokenPermission(tokenPermission)
|
changes, err := o.upsertTokenPermission(tokenPermission)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1996,42 +1979,20 @@ func (o *Community) deleteChat(chatID string) *CommunityChanges {
|
||||||
return changes
|
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 {
|
if o.config.CommunityDescription.TokenPermissions == nil {
|
||||||
o.config.CommunityDescription.TokenPermissions = make(map[string]*protobuf.CommunityTokenPermission)
|
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
|
o.config.CommunityDescription.TokenPermissions[permission.Id] = permission
|
||||||
|
|
||||||
changes := o.emptyCommunityChanges()
|
changes := o.emptyCommunityChanges()
|
||||||
|
if existed {
|
||||||
if changes.TokenPermissionsAdded == nil {
|
changes.TokenPermissionsModified[permission.Id] = permission
|
||||||
changes.TokenPermissionsAdded = make(map[string]*protobuf.CommunityTokenPermission)
|
} 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
|
return changes, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -369,12 +369,12 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Permiss
|
||||||
modified := origin.CreateDeepCopy()
|
modified := origin.CreateDeepCopy()
|
||||||
|
|
||||||
for _, permission := range tc.originPermissions {
|
for _, permission := range tc.originPermissions {
|
||||||
_, err := origin.AddTokenPermission(permission)
|
_, err := origin.UpsertTokenPermission(permission)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, permission := range tc.modifiedPermissions {
|
for _, permission := range tc.modifiedPermissions {
|
||||||
_, err := modified.AddTokenPermission(permission)
|
_, err := modified.UpsertTokenPermission(permission)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -496,7 +496,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Members
|
||||||
s.Run(tc.name, func() {
|
s.Run(tc.name, func() {
|
||||||
origin := createTestCommunity(s.identity)
|
origin := createTestCommunity(s.identity)
|
||||||
for _, permission := range tc.permissions {
|
for _, permission := range tc.permissions {
|
||||||
_, err := origin.AddTokenPermission(permission)
|
_, err := origin.UpsertTokenPermission(permission)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
modified := origin.CreateDeepCopy()
|
modified := origin.CreateDeepCopy()
|
||||||
|
@ -599,7 +599,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Permiss
|
||||||
modified := origin.CreateDeepCopy()
|
modified := origin.CreateDeepCopy()
|
||||||
|
|
||||||
for _, permission := range tc.originPermissions {
|
for _, permission := range tc.originPermissions {
|
||||||
_, err := origin.AddTokenPermission(permission)
|
_, err := origin.UpsertTokenPermission(permission)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
for _, member := range tc.originMembers {
|
for _, member := range tc.originMembers {
|
||||||
|
@ -608,7 +608,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Permiss
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, permission := range tc.modifiedPermissions {
|
for _, permission := range tc.modifiedPermissions {
|
||||||
_, err := modified.AddTokenPermission(permission)
|
_, err := modified.UpsertTokenPermission(permission)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
for _, member := range tc.modifiedMembers {
|
for _, member := range tc.modifiedMembers {
|
||||||
|
@ -748,7 +748,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestChannelLevelKeyActions() {
|
||||||
modified := origin.CreateDeepCopy()
|
modified := origin.CreateDeepCopy()
|
||||||
|
|
||||||
for _, permission := range tc.originPermissions {
|
for _, permission := range tc.originPermissions {
|
||||||
_, err := origin.AddTokenPermission(permission)
|
_, err := origin.UpsertTokenPermission(permission)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
for _, member := range tc.originMembers {
|
for _, member := range tc.originMembers {
|
||||||
|
@ -759,7 +759,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestChannelLevelKeyActions() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, permission := range tc.modifiedPermissions {
|
for _, permission := range tc.modifiedPermissions {
|
||||||
_, err := modified.AddTokenPermission(permission)
|
_, err := modified.UpsertTokenPermission(permission)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
for _, member := range tc.modifiedMembers {
|
for _, member := range tc.modifiedMembers {
|
||||||
|
@ -810,7 +810,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestNilOrigin() {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, permission := range newCommunityPermissions {
|
for _, permission := range newCommunityPermissions {
|
||||||
_, err := newCommunity.AddTokenPermission(permission)
|
_, err := newCommunity.UpsertTokenPermission(permission)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -246,17 +246,9 @@ func (o *Community) updateCommunityDescriptionByCommunityEvent(communityEvent Co
|
||||||
o.config.CommunityDescription.Tags = communityEvent.CommunityConfig.Tags
|
o.config.CommunityDescription.Tags = communityEvent.CommunityConfig.Tags
|
||||||
|
|
||||||
case protobuf.CommunityEvent_COMMUNITY_MEMBER_TOKEN_PERMISSION_CHANGE:
|
case protobuf.CommunityEvent_COMMUNITY_MEMBER_TOKEN_PERMISSION_CHANGE:
|
||||||
_, exists := o.config.CommunityDescription.TokenPermissions[communityEvent.TokenPermission.Id]
|
_, err := o.upsertTokenPermission(communityEvent.TokenPermission)
|
||||||
if exists {
|
if err != nil {
|
||||||
_, err := o.updateTokenPermission(communityEvent.TokenPermission)
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_, err := o.addTokenPermission(communityEvent.TokenPermission)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case protobuf.CommunityEvent_COMMUNITY_MEMBER_TOKEN_PERMISSION_DELETE:
|
case protobuf.CommunityEvent_COMMUNITY_MEMBER_TOKEN_PERMISSION_DELETE:
|
||||||
|
|
|
@ -771,11 +771,11 @@ func (s *CommunitySuite) TestChannelTokenPermissionsByType() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, viewOnlyPermission := range viewOnlyPermissions {
|
for _, viewOnlyPermission := range viewOnlyPermissions {
|
||||||
_, err := org.AddTokenPermission(viewOnlyPermission)
|
_, err := org.UpsertTokenPermission(viewOnlyPermission)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
for _, viewAndPostPermission := range viewAndPostPermissions {
|
for _, viewAndPostPermission := range viewAndPostPermissions {
|
||||||
_, err := org.AddTokenPermission(viewAndPostPermission)
|
_, err := org.UpsertTokenPermission(viewAndPostPermission)
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -629,7 +629,7 @@ func (m *Manager) EditCommunityTokenPermission(request *requests.EditCommunityTo
|
||||||
|
|
||||||
tokenPermission := request.ToCommunityTokenPermission()
|
tokenPermission := request.ToCommunityTokenPermission()
|
||||||
|
|
||||||
changes, err := community.UpdateTokenPermission(tokenPermission.Id, &tokenPermission)
|
changes, err := community.UpsertTokenPermission(&tokenPermission)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -4722,7 +4722,7 @@ func (m *Manager) createCommunityTokenPermission(request *requests.CreateCommuni
|
||||||
|
|
||||||
tokenPermission := request.ToCommunityTokenPermission()
|
tokenPermission := request.ToCommunityTokenPermission()
|
||||||
tokenPermission.Id = uuid.New().String()
|
tokenPermission.Id = uuid.New().String()
|
||||||
changes, err := community.AddTokenPermission(&tokenPermission)
|
changes, err := community.UpsertTokenPermission(&tokenPermission)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue