2020-11-18 09:16:51 +00:00
|
|
|
package communities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2022-06-24 13:40:12 +00:00
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
2020-11-18 09:16:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func validateCommunityChat(desc *protobuf.CommunityDescription, chat *protobuf.CommunityChat) error {
|
|
|
|
if chat == nil {
|
|
|
|
return ErrInvalidCommunityDescription
|
|
|
|
}
|
|
|
|
if chat.Permissions == nil {
|
|
|
|
return ErrInvalidCommunityDescriptionNoChatPermissions
|
|
|
|
}
|
|
|
|
if chat.Permissions.Access == protobuf.CommunityPermissions_UNKNOWN_ACCESS {
|
|
|
|
return ErrInvalidCommunityDescriptionUnknownChatAccess
|
|
|
|
}
|
|
|
|
|
2021-05-23 13:34:17 +00:00
|
|
|
if len(chat.CategoryId) != 0 {
|
|
|
|
if _, exists := desc.Categories[chat.CategoryId]; !exists {
|
|
|
|
return ErrInvalidCommunityDescriptionUnknownChatCategory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 08:54:53 +00:00
|
|
|
if chat.Identity == nil {
|
|
|
|
return ErrInvalidCommunityDescriptionChatIdentity
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
for pk := range chat.Members {
|
|
|
|
if desc.Members == nil {
|
|
|
|
return ErrInvalidCommunityDescriptionMemberInChatButNotInOrg
|
|
|
|
}
|
|
|
|
// Check member is in the org as well
|
|
|
|
if _, ok := desc.Members[pk]; !ok {
|
|
|
|
return ErrInvalidCommunityDescriptionMemberInChatButNotInOrg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-23 13:34:17 +00:00
|
|
|
func validateCommunityCategory(category *protobuf.CommunityCategory) error {
|
|
|
|
if len(category.CategoryId) == 0 {
|
|
|
|
return ErrInvalidCommunityDescriptionCategoryNoID
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(category.Name) == 0 {
|
|
|
|
return ErrInvalidCommunityDescriptionCategoryNoName
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
func ValidateCommunityDescription(desc *protobuf.CommunityDescription) error {
|
|
|
|
if desc == nil {
|
|
|
|
return ErrInvalidCommunityDescription
|
|
|
|
}
|
|
|
|
if desc.Permissions == nil {
|
|
|
|
return ErrInvalidCommunityDescriptionNoOrgPermissions
|
|
|
|
}
|
|
|
|
if desc.Permissions.Access == protobuf.CommunityPermissions_UNKNOWN_ACCESS {
|
|
|
|
return ErrInvalidCommunityDescriptionUnknownOrgAccess
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:40:12 +00:00
|
|
|
valid := requests.ValidateTags(desc.Tags)
|
|
|
|
if !valid {
|
|
|
|
return ErrInvalidCommunityTags
|
|
|
|
}
|
|
|
|
|
2021-05-23 13:34:17 +00:00
|
|
|
for _, category := range desc.Categories {
|
|
|
|
if err := validateCommunityCategory(category); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
for _, chat := range desc.Chats {
|
|
|
|
if err := validateCommunityChat(desc, chat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|