refactor: base community key actions evaluation on Encrypted() property

This commit is contained in:
Patryk Osmaczko 2023-10-26 22:04:18 +02:00 committed by osmaczko
parent 953ed4c8e4
commit 667e427b63
1 changed files with 17 additions and 28 deletions

View File

@ -30,6 +30,7 @@ func EvaluateCommunityEncryptionKeyActions(origin, modified *Community) *Encrypt
// `modified` is a new community, create empty `origin` community // `modified` is a new community, create empty `origin` community
origin = &Community{ origin = &Community{
config: &Config{ config: &Config{
ID: modified.config.ID,
CommunityDescription: &protobuf.CommunityDescription{ CommunityDescription: &protobuf.CommunityDescription{
Members: map[string]*protobuf.CommunityMember{}, Members: map[string]*protobuf.CommunityMember{},
Permissions: &protobuf.CommunityPermissions{}, Permissions: &protobuf.CommunityPermissions{},
@ -54,16 +55,13 @@ func EvaluateCommunityEncryptionKeyActions(origin, modified *Community) *Encrypt
} }
func evaluateCommunityLevelEncryptionKeyAction(origin, modified *Community, changes *CommunityChanges) *EncryptionKeyAction { func evaluateCommunityLevelEncryptionKeyAction(origin, modified *Community, changes *CommunityChanges) *EncryptionKeyAction {
originBecomeMemberPermissions := origin.TokenPermissionsByType(protobuf.CommunityTokenPermission_BECOME_MEMBER)
modifiedBecomeMemberPermissions := modified.TokenPermissionsByType(protobuf.CommunityTokenPermission_BECOME_MEMBER)
return evaluateEncryptionKeyAction( return evaluateEncryptionKeyAction(
originBecomeMemberPermissions, origin.Encrypted(),
modifiedBecomeMemberPermissions, modified.Encrypted(),
changes.ControlNodeChanged != nil,
modified.config.CommunityDescription.Members, modified.config.CommunityDescription.Members,
changes.MembersAdded, changes.MembersAdded,
changes.MembersRemoved, changes.MembersRemoved,
changes.ControlNodeChanged != nil,
) )
} }
@ -71,16 +69,6 @@ func evaluateChannelLevelEncryptionKeyActions(origin, modified *Community, chang
result := make(map[string]EncryptionKeyAction) result := make(map[string]EncryptionKeyAction)
for channelID := range modified.config.CommunityDescription.Chats { for channelID := range modified.config.CommunityDescription.Chats {
chatID := modified.IDString() + channelID
originChannelViewOnlyPermissions := origin.ChannelTokenPermissionsByType(chatID, protobuf.CommunityTokenPermission_CAN_VIEW_CHANNEL)
originChannelViewAndPostPermissions := origin.ChannelTokenPermissionsByType(chatID, protobuf.CommunityTokenPermission_CAN_VIEW_AND_POST_CHANNEL)
originChannelPermissions := append(originChannelViewOnlyPermissions, originChannelViewAndPostPermissions...)
modifiedChannelViewOnlyPermissions := modified.ChannelTokenPermissionsByType(chatID, protobuf.CommunityTokenPermission_CAN_VIEW_CHANNEL)
modifiedChannelViewAndPostPermissions := modified.ChannelTokenPermissionsByType(chatID, protobuf.CommunityTokenPermission_CAN_VIEW_AND_POST_CHANNEL)
modifiedChannelPermissions := append(modifiedChannelViewOnlyPermissions, modifiedChannelViewAndPostPermissions...)
membersAdded := make(map[string]*protobuf.CommunityMember) membersAdded := make(map[string]*protobuf.CommunityMember)
membersRemoved := make(map[string]*protobuf.CommunityMember) membersRemoved := make(map[string]*protobuf.CommunityMember)
@ -91,19 +79,20 @@ func evaluateChannelLevelEncryptionKeyActions(origin, modified *Community, chang
} }
result[channelID] = *evaluateEncryptionKeyAction( result[channelID] = *evaluateEncryptionKeyAction(
originChannelPermissions, origin.ChannelEncrypted(channelID),
modifiedChannelPermissions, modified.ChannelEncrypted(channelID),
modified.config.CommunityDescription.Chats[channelID].Members,
membersAdded, membersRemoved,
changes.ControlNodeChanged != nil, changes.ControlNodeChanged != nil,
modified.config.CommunityDescription.Chats[channelID].Members,
membersAdded,
membersRemoved,
) )
} }
return &result return &result
} }
func evaluateEncryptionKeyAction(originPermissions, modifiedPermissions []*CommunityTokenPermission, func evaluateEncryptionKeyAction(originEncrypted, modifiedEncrypted, controlNodeChanged bool,
allMembers, membersAdded, membersRemoved map[string]*protobuf.CommunityMember, controlNodeChanged bool) *EncryptionKeyAction { allMembers, membersAdded, membersRemoved map[string]*protobuf.CommunityMember) *EncryptionKeyAction {
result := &EncryptionKeyAction{ result := &EncryptionKeyAction{
ActionType: EncryptionKeyNone, ActionType: EncryptionKeyNone,
Members: map[string]*protobuf.CommunityMember{}, Members: map[string]*protobuf.CommunityMember{},
@ -118,28 +107,28 @@ func evaluateEncryptionKeyAction(originPermissions, modifiedPermissions []*Commu
} }
// control node changed on closed community/channel // control node changed on closed community/channel
if controlNodeChanged && len(modifiedPermissions) > 0 { if controlNodeChanged && modifiedEncrypted {
result.ActionType = EncryptionKeyRekey result.ActionType = EncryptionKeyRekey
result.Members = copyMap(allMembers) result.Members = copyMap(allMembers)
return result return result
} }
// permission was just added // encryption was just added
if len(modifiedPermissions) > 0 && len(originPermissions) == 0 { if modifiedEncrypted && !originEncrypted {
result.ActionType = EncryptionKeyAdd result.ActionType = EncryptionKeyAdd
result.Members = copyMap(allMembers) result.Members = copyMap(allMembers)
return result return result
} }
// permission was just removed // encryption was just removed
if len(modifiedPermissions) == 0 && len(originPermissions) > 0 { if !modifiedEncrypted && originEncrypted {
result.ActionType = EncryptionKeyRemove result.ActionType = EncryptionKeyRemove
result.Members = copyMap(allMembers) result.Members = copyMap(allMembers)
return result return result
} }
// open community/channel does not require any actions // open community/channel does not require any actions
if len(modifiedPermissions) == 0 { if !modifiedEncrypted {
return result return result
} }