2021-01-11 10:32:51 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-02-02 17:59:48 +00:00
|
|
|
"github.com/status-im/status-go/images"
|
2021-01-11 10:32:51 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-05-24 19:20:13 +00:00
|
|
|
ErrCreateCommunityInvalidName = errors.New("create-community: invalid name")
|
|
|
|
ErrCreateCommunityInvalidColor = errors.New("create-community: invalid color")
|
|
|
|
ErrCreateCommunityInvalidDescription = errors.New("create-community: invalid description")
|
|
|
|
ErrCreateCommunityInvalidIntroMessage = errors.New("create-community: invalid intro message")
|
|
|
|
ErrCreateCommunityInvalidOutroMessage = errors.New("create-community: invalid outro message")
|
|
|
|
ErrCreateCommunityInvalidMembership = errors.New("create-community: invalid membership")
|
2022-06-24 13:40:12 +00:00
|
|
|
ErrCreateCommunityInvalidTags = errors.New("create-community: invalid tags")
|
2022-05-24 19:20:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxNameLength = 30
|
|
|
|
maxDescriptionLength = 140
|
|
|
|
maxIntroMessageLength = 1400
|
|
|
|
maxOutroMessageLength = 80
|
2021-01-11 10:32:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CreateCommunity struct {
|
2022-03-08 15:25:00 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
2022-05-24 19:20:13 +00:00
|
|
|
IntroMessage string `json:"introMessage,omitempty"`
|
|
|
|
OutroMessage string `json:"outroMessage,omitempty"`
|
2022-03-08 15:25:00 +00:00
|
|
|
Color string `json:"color"`
|
|
|
|
Emoji string `json:"emoji"`
|
|
|
|
Membership protobuf.CommunityPermissions_Access `json:"membership"`
|
|
|
|
EnsOnly bool `json:"ensOnly"`
|
|
|
|
Image string `json:"image"`
|
|
|
|
ImageAx int `json:"imageAx"`
|
|
|
|
ImageAy int `json:"imageAy"`
|
|
|
|
ImageBx int `json:"imageBx"`
|
|
|
|
ImageBy int `json:"imageBy"`
|
2023-02-02 17:59:48 +00:00
|
|
|
Banner images.CroppedImage `json:"banner"`
|
2022-03-08 15:25:00 +00:00
|
|
|
HistoryArchiveSupportEnabled bool `json:"historyArchiveSupportEnabled,omitempty"`
|
2022-05-10 14:21:38 +00:00
|
|
|
PinMessageAllMembersEnabled bool `json:"pinMessageAllMembersEnabled,omitempty"`
|
2022-06-24 13:40:12 +00:00
|
|
|
Tags []string `json:"tags,omitempty"`
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 17:59:48 +00:00
|
|
|
func adaptIdentityImageToProtobuf(img images.IdentityImage) *protobuf.IdentityImage {
|
2021-01-11 10:32:51 +00:00
|
|
|
return &protobuf.IdentityImage{
|
2024-01-24 20:09:28 +00:00
|
|
|
Payload: img.Payload,
|
|
|
|
SourceType: protobuf.IdentityImage_RAW_PAYLOAD,
|
|
|
|
ImageFormat: images.GetProtobufImageFormat(img.Payload),
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CreateCommunity) Validate() error {
|
2022-05-24 19:20:13 +00:00
|
|
|
if c.Name == "" || len(c.Name) > maxNameLength {
|
2021-01-11 10:32:51 +00:00
|
|
|
return ErrCreateCommunityInvalidName
|
|
|
|
}
|
|
|
|
|
2022-05-24 19:20:13 +00:00
|
|
|
if c.Description == "" || len(c.Description) > maxDescriptionLength {
|
2021-01-11 10:32:51 +00:00
|
|
|
return ErrCreateCommunityInvalidDescription
|
|
|
|
}
|
|
|
|
|
2022-05-24 19:20:13 +00:00
|
|
|
if len(c.IntroMessage) > maxIntroMessageLength {
|
|
|
|
return ErrCreateCommunityInvalidIntroMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.OutroMessage) > maxOutroMessageLength {
|
|
|
|
return ErrCreateCommunityInvalidOutroMessage
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
if c.Membership == protobuf.CommunityPermissions_UNKNOWN_ACCESS {
|
|
|
|
return ErrCreateCommunityInvalidMembership
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Color == "" {
|
|
|
|
return ErrCreateCommunityInvalidColor
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:40:12 +00:00
|
|
|
if !ValidateTags(c.Tags) {
|
|
|
|
return ErrCreateCommunityInvalidTags
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CreateCommunity) ToCommunityDescription() (*protobuf.CommunityDescription, error) {
|
|
|
|
ci := &protobuf.ChatIdentity{
|
|
|
|
DisplayName: c.Name,
|
|
|
|
Color: c.Color,
|
2021-10-04 13:02:25 +00:00
|
|
|
Emoji: c.Emoji,
|
2021-01-11 10:32:51 +00:00
|
|
|
Description: c.Description,
|
|
|
|
}
|
|
|
|
|
2022-04-15 18:20:12 +00:00
|
|
|
if c.Image != "" || c.Banner.ImagePath != "" {
|
2021-01-11 10:32:51 +00:00
|
|
|
ciis := make(map[string]*protobuf.IdentityImage)
|
2022-04-15 18:20:12 +00:00
|
|
|
if c.Image != "" {
|
|
|
|
log.Info("has-image", "image", c.Image)
|
2023-02-02 17:59:48 +00:00
|
|
|
imgs, err := images.GenerateIdentityImages(c.Image, c.ImageAx, c.ImageAy, c.ImageBx, c.ImageBy)
|
2022-04-15 18:20:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-05 05:40:43 +00:00
|
|
|
for i := range imgs {
|
|
|
|
ciis[imgs[i].Name] = adaptIdentityImageToProtobuf(imgs[i])
|
2022-04-15 18:20:12 +00:00
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
2022-04-15 18:20:12 +00:00
|
|
|
if c.Banner.ImagePath != "" {
|
|
|
|
log.Info("has-banner", "image", c.Banner.ImagePath)
|
2023-02-02 17:59:48 +00:00
|
|
|
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)
|
2022-04-15 18:20:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-05 05:40:43 +00:00
|
|
|
ciis[img.Name] = adaptIdentityImageToProtobuf(*img)
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
ci.Images = ciis
|
|
|
|
log.Info("set images", "images", ci)
|
|
|
|
}
|
|
|
|
|
|
|
|
description := &protobuf.CommunityDescription{
|
|
|
|
Identity: ci,
|
|
|
|
Permissions: &protobuf.CommunityPermissions{
|
|
|
|
Access: c.Membership,
|
|
|
|
EnsOnly: c.EnsOnly,
|
|
|
|
},
|
2022-05-10 14:21:38 +00:00
|
|
|
AdminSettings: &protobuf.CommunityAdminSettings{
|
|
|
|
PinMessageAllMembersEnabled: c.PinMessageAllMembersEnabled,
|
|
|
|
},
|
2022-05-24 19:20:13 +00:00
|
|
|
IntroMessage: c.IntroMessage,
|
|
|
|
OutroMessage: c.OutroMessage,
|
2022-06-24 13:40:12 +00:00
|
|
|
Tags: c.Tags,
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
return description, nil
|
|
|
|
}
|