github.com/status-im/status-go@v1.1.0/protocol/communities/validator.go (about)

     1  package communities
     2  
     3  import (
     4  	"github.com/status-im/status-go/protocol/protobuf"
     5  	"github.com/status-im/status-go/protocol/requests"
     6  )
     7  
     8  func validateCommunityChat(desc *protobuf.CommunityDescription, chat *protobuf.CommunityChat) error {
     9  	if chat == nil {
    10  		return ErrInvalidCommunityDescription
    11  	}
    12  	if chat.Permissions == nil {
    13  		return ErrInvalidCommunityDescriptionNoChatPermissions
    14  	}
    15  	if chat.Permissions.Access == protobuf.CommunityPermissions_UNKNOWN_ACCESS {
    16  		return ErrInvalidCommunityDescriptionUnknownChatAccess
    17  	}
    18  
    19  	if len(chat.CategoryId) != 0 {
    20  		if _, exists := desc.Categories[chat.CategoryId]; !exists {
    21  			return ErrInvalidCommunityDescriptionUnknownChatCategory
    22  		}
    23  	}
    24  
    25  	if chat.Identity == nil {
    26  		return ErrInvalidCommunityDescriptionChatIdentity
    27  	}
    28  
    29  	for pk := range chat.Members {
    30  		if desc.Members == nil {
    31  			return ErrInvalidCommunityDescriptionMemberInChatButNotInOrg
    32  		}
    33  		// Check member is in the org as well
    34  		if _, ok := desc.Members[pk]; !ok {
    35  			return ErrInvalidCommunityDescriptionMemberInChatButNotInOrg
    36  		}
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  func validateCommunityCategory(category *protobuf.CommunityCategory) error {
    43  	if len(category.CategoryId) == 0 {
    44  		return ErrInvalidCommunityDescriptionCategoryNoID
    45  	}
    46  
    47  	if len(category.Name) == 0 {
    48  		return ErrInvalidCommunityDescriptionCategoryNoName
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  func ValidateCommunityDescription(desc *protobuf.CommunityDescription) error {
    55  	if desc == nil {
    56  		return ErrInvalidCommunityDescription
    57  	}
    58  	if desc.Permissions == nil {
    59  		return ErrInvalidCommunityDescriptionNoOrgPermissions
    60  	}
    61  	if desc.Permissions.Access == protobuf.CommunityPermissions_UNKNOWN_ACCESS {
    62  		return ErrInvalidCommunityDescriptionUnknownOrgAccess
    63  	}
    64  
    65  	valid := requests.ValidateTags(desc.Tags)
    66  	if !valid {
    67  		return ErrInvalidCommunityTags
    68  	}
    69  
    70  	for _, category := range desc.Categories {
    71  		if err := validateCommunityCategory(category); err != nil {
    72  			return err
    73  		}
    74  	}
    75  
    76  	for _, chat := range desc.Chats {
    77  		if err := validateCommunityChat(desc, chat); err != nil {
    78  			return err
    79  		}
    80  	}
    81  
    82  	return nil
    83  }