github.com/status-im/status-go@v1.1.0/protocol/requests/create_community_request.go (about)

     1  package requests
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/ethereum/go-ethereum/log"
     7  	"github.com/status-im/status-go/images"
     8  	"github.com/status-im/status-go/protocol/protobuf"
     9  )
    10  
    11  var (
    12  	ErrCreateCommunityInvalidName         = errors.New("create-community: invalid name")
    13  	ErrCreateCommunityInvalidColor        = errors.New("create-community: invalid color")
    14  	ErrCreateCommunityInvalidDescription  = errors.New("create-community: invalid description")
    15  	ErrCreateCommunityInvalidIntroMessage = errors.New("create-community: invalid intro message")
    16  	ErrCreateCommunityInvalidOutroMessage = errors.New("create-community: invalid outro message")
    17  	ErrCreateCommunityInvalidMembership   = errors.New("create-community: invalid membership")
    18  	ErrCreateCommunityInvalidTags         = errors.New("create-community: invalid tags")
    19  )
    20  
    21  const (
    22  	maxNameLength         = 30
    23  	maxDescriptionLength  = 140
    24  	maxIntroMessageLength = 1400
    25  	maxOutroMessageLength = 80
    26  )
    27  
    28  type CreateCommunity struct {
    29  	Name                         string                               `json:"name"`
    30  	Description                  string                               `json:"description"`
    31  	IntroMessage                 string                               `json:"introMessage,omitempty"`
    32  	OutroMessage                 string                               `json:"outroMessage,omitempty"`
    33  	Color                        string                               `json:"color"`
    34  	Emoji                        string                               `json:"emoji"`
    35  	Membership                   protobuf.CommunityPermissions_Access `json:"membership"`
    36  	EnsOnly                      bool                                 `json:"ensOnly"`
    37  	Image                        string                               `json:"image"`
    38  	ImageAx                      int                                  `json:"imageAx"`
    39  	ImageAy                      int                                  `json:"imageAy"`
    40  	ImageBx                      int                                  `json:"imageBx"`
    41  	ImageBy                      int                                  `json:"imageBy"`
    42  	Banner                       images.CroppedImage                  `json:"banner"`
    43  	HistoryArchiveSupportEnabled bool                                 `json:"historyArchiveSupportEnabled,omitempty"`
    44  	PinMessageAllMembersEnabled  bool                                 `json:"pinMessageAllMembersEnabled,omitempty"`
    45  	Tags                         []string                             `json:"tags,omitempty"`
    46  }
    47  
    48  func adaptIdentityImageToProtobuf(img images.IdentityImage) *protobuf.IdentityImage {
    49  	return &protobuf.IdentityImage{
    50  		Payload:     img.Payload,
    51  		SourceType:  protobuf.IdentityImage_RAW_PAYLOAD,
    52  		ImageFormat: images.GetProtobufImageFormat(img.Payload),
    53  	}
    54  }
    55  
    56  func (c *CreateCommunity) Validate() error {
    57  	if c.Name == "" || len(c.Name) > maxNameLength {
    58  		return ErrCreateCommunityInvalidName
    59  	}
    60  
    61  	if c.Description == "" || len(c.Description) > maxDescriptionLength {
    62  		return ErrCreateCommunityInvalidDescription
    63  	}
    64  
    65  	if len(c.IntroMessage) > maxIntroMessageLength {
    66  		return ErrCreateCommunityInvalidIntroMessage
    67  	}
    68  
    69  	if len(c.OutroMessage) > maxOutroMessageLength {
    70  		return ErrCreateCommunityInvalidOutroMessage
    71  	}
    72  
    73  	if c.Membership == protobuf.CommunityPermissions_UNKNOWN_ACCESS {
    74  		return ErrCreateCommunityInvalidMembership
    75  	}
    76  
    77  	if c.Color == "" {
    78  		return ErrCreateCommunityInvalidColor
    79  	}
    80  
    81  	if !ValidateTags(c.Tags) {
    82  		return ErrCreateCommunityInvalidTags
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  func (c *CreateCommunity) ToCommunityDescription() (*protobuf.CommunityDescription, error) {
    89  	ci := &protobuf.ChatIdentity{
    90  		DisplayName: c.Name,
    91  		Color:       c.Color,
    92  		Emoji:       c.Emoji,
    93  		Description: c.Description,
    94  	}
    95  
    96  	if c.Image != "" || c.Banner.ImagePath != "" {
    97  		ciis := make(map[string]*protobuf.IdentityImage)
    98  		if c.Image != "" {
    99  			log.Info("has-image", "image", c.Image)
   100  			imgs, err := images.GenerateIdentityImages(c.Image, c.ImageAx, c.ImageAy, c.ImageBx, c.ImageBy)
   101  			if err != nil {
   102  				return nil, err
   103  			}
   104  			for i := range imgs {
   105  				ciis[imgs[i].Name] = adaptIdentityImageToProtobuf(imgs[i])
   106  			}
   107  		}
   108  		if c.Banner.ImagePath != "" {
   109  			log.Info("has-banner", "image", c.Banner.ImagePath)
   110  			img, err := images.GenerateBannerImage(c.Banner.ImagePath, c.Banner.X, c.Banner.Y, c.Banner.X+c.Banner.Width, c.Banner.Y+c.Banner.Height)
   111  			if err != nil {
   112  				return nil, err
   113  			}
   114  			ciis[img.Name] = adaptIdentityImageToProtobuf(*img)
   115  		}
   116  		ci.Images = ciis
   117  		log.Info("set images", "images", ci)
   118  	}
   119  
   120  	description := &protobuf.CommunityDescription{
   121  		Identity: ci,
   122  		Permissions: &protobuf.CommunityPermissions{
   123  			Access:  c.Membership,
   124  			EnsOnly: c.EnsOnly,
   125  		},
   126  		AdminSettings: &protobuf.CommunityAdminSettings{
   127  			PinMessageAllMembersEnabled: c.PinMessageAllMembersEnabled,
   128  		},
   129  		IntroMessage: c.IntroMessage,
   130  		OutroMessage: c.OutroMessage,
   131  		Tags:         c.Tags,
   132  	}
   133  	return description, nil
   134  }