Files
status-go/internal/protocol/requests/create_community_request.go
Igor Sirotin f3e4363808 refactor: move protocol to internal/protocol
Part of the Go project layout migration, item 27.

Pure move plus import-path rewrite across 502 files. No API or behaviour
change. `internal/` keeps the messaging application logic unimportable
from outside the module, which is what the issue asks for -- status-go is
consumed through the C-bindings in mobile/, not as a Go library.

Things that had to follow the move, beyond the Go imports:

- tools/generate-handlers/template.txt. messenger_handlers.go is
  generated, and the template hard-codes the imports it emits, so the
  generated file kept importing protocol/common and failed typecheck.
- .gitignore. The ignore rule for that generated file was pinned to the
  old path; without moving it, a 1486-line generated file starts being
  tracked.
- Makefile: the logosstorage and torrent test targets (both the archive
  packages and ./protocol itself), the archive README, migration-protocol.
- scripts/run_unit_tests.sh, which names the protocol package explicitly
  to shard its tests.
- scripts/cleanup_generated_files.sh and .golangci.yml.

scripts/migration_check.sh also needed a fix that is not specific to this
move: it validated every file the branch touched under a migration dir
against the timestamp naming rule, and a directory rename makes every
migration in it look newly added. It now excludes renames, so moving a
migration is not mistaken for adding one.

refs #7067
2026-08-21 10:11:05 +02:00

136 lines
4.9 KiB
Go

package requests
import (
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/status-im/status-go/internal/images"
"github.com/status-im/status-go/internal/logutils"
"github.com/status-im/status-go/internal/protocol/protobuf"
)
var (
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")
ErrCreateCommunityInvalidTags = errors.New("create-community: invalid tags")
)
const (
maxNameLength = 30
maxDescriptionLength = 140
maxIntroMessageLength = 1400
maxOutroMessageLength = 80
)
type CreateCommunity struct {
Name string `json:"name"`
Description string `json:"description"`
IntroMessage string `json:"introMessage,omitempty"`
OutroMessage string `json:"outroMessage,omitempty"`
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"`
Banner images.CroppedImage `json:"banner"`
HistoryArchiveSupportEnabled bool `json:"historyArchiveSupportEnabled,omitempty"`
PinMessageAllMembersEnabled bool `json:"pinMessageAllMembersEnabled,omitempty"`
Tags []string `json:"tags,omitempty"`
}
func adaptIdentityImageToProtobuf(img images.IdentityImage) *protobuf.IdentityImage {
return &protobuf.IdentityImage{
Payload: img.Payload,
SourceType: protobuf.IdentityImage_RAW_PAYLOAD,
ImageFormat: images.GetProtobufImageFormat(img.Payload),
}
}
func (c *CreateCommunity) Validate() error {
if c.Name == "" || len(c.Name) > maxNameLength {
return ErrCreateCommunityInvalidName
}
if c.Description == "" || len(c.Description) > maxDescriptionLength {
return ErrCreateCommunityInvalidDescription
}
if len(c.IntroMessage) > maxIntroMessageLength {
return ErrCreateCommunityInvalidIntroMessage
}
if len(c.OutroMessage) > maxOutroMessageLength {
return ErrCreateCommunityInvalidOutroMessage
}
if c.Membership == protobuf.CommunityPermissions_UNKNOWN_ACCESS {
return ErrCreateCommunityInvalidMembership
}
if c.Color == "" {
return ErrCreateCommunityInvalidColor
}
if !ValidateTags(c.Tags) {
return ErrCreateCommunityInvalidTags
}
return nil
}
func (c *CreateCommunity) ToCommunityDescription() (*protobuf.CommunityDescription, error) {
ci := &protobuf.ChatIdentity{
DisplayName: c.Name,
Color: c.Color,
Emoji: c.Emoji,
Description: c.Description,
}
if c.Image != "" || c.Banner.ImagePath != "" {
ciis := make(map[string]*protobuf.IdentityImage)
if c.Image != "" {
logutils.ZapLogger().Debug("has-image", zap.String("image", c.Image))
imgs, err := images.GenerateIdentityImages(c.Image, c.ImageAx, c.ImageAy, c.ImageBx, c.ImageBy)
if err != nil {
return nil, err
}
for i := range imgs {
ciis[imgs[i].Name] = adaptIdentityImageToProtobuf(imgs[i])
}
}
if c.Banner.ImagePath != "" {
logutils.ZapLogger().Debug("has-banner", zap.String("image", c.Banner.ImagePath))
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)
if err != nil {
return nil, err
}
ciis[img.Name] = adaptIdentityImageToProtobuf(*img)
}
ci.Images = ciis
logutils.ZapLogger().Debug("set images", zap.Any("images", ci))
}
description := &protobuf.CommunityDescription{
Identity: ci,
Permissions: &protobuf.CommunityPermissions{
Access: c.Membership,
EnsOnly: c.EnsOnly,
},
AdminSettings: &protobuf.CommunityAdminSettings{
PinMessageAllMembersEnabled: c.PinMessageAllMembersEnabled,
},
IntroMessage: c.IntroMessage,
OutroMessage: c.OutroMessage,
Tags: c.Tags,
}
return description, nil
}