status-go/protocol/requests/create_community_request.go
Pascal Precht 714c03c635 feat: introduce CommunityAdminSettings in CommunityDescription
This allows to store community admin settings that are meant to be propagated
to community members (as opposed to the already existing
`CommunitySettings` which are considered local to every account).

The first setting introduced as part of this commit is one that enables
community admins to configure whether or not members of the community
are allowed to pin messages in community channels.

Prior to this commit, this was not restricted at all on the protocol
level and only enforced by clients via UI (e.g. members don't see an
option to pin messages, although they could).

This config setting now ensures that:

1. If turned off, members cannot send a pin message
2. If turned off, pin messages from members are not handled/processed

This is needed by https://github.com/status-im/status-desktop/issues/5662
2022-05-18 09:58:11 +02:00

97 lines
3.3 KiB
Go

package requests
import (
"errors"
"github.com/ethereum/go-ethereum/log"
userimages "github.com/status-im/status-go/images"
"github.com/status-im/status-go/protocol/images"
"github.com/status-im/status-go/protocol/protobuf"
)
var (
ErrCreateCommunityInvalidName = errors.New("create-community: invalid name")
ErrCreateCommunityInvalidColor = errors.New("create-community: invalid color")
ErrCreateCommunityInvalidDescription = errors.New("create-community: invalid description")
ErrCreateCommunityInvalidMembership = errors.New("create-community: invalid membership")
)
type CreateCommunity struct {
Name string `json:"name"`
Description string `json:"description"`
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"`
HistoryArchiveSupportEnabled bool `json:"historyArchiveSupportEnabled,omitempty"`
PinMessageAllMembersEnabled bool `json:"pinMessageAllMembersEnabled,omitempty"`
}
func adaptIdentityImageToProtobuf(img *userimages.IdentityImage) *protobuf.IdentityImage {
return &protobuf.IdentityImage{
Payload: img.Payload,
SourceType: protobuf.IdentityImage_RAW_PAYLOAD,
ImageType: images.ImageType(img.Payload),
}
}
func (c *CreateCommunity) Validate() error {
if c.Name == "" {
return ErrCreateCommunityInvalidName
}
if c.Description == "" {
return ErrCreateCommunityInvalidDescription
}
if c.Membership == protobuf.CommunityPermissions_UNKNOWN_ACCESS {
return ErrCreateCommunityInvalidMembership
}
if c.Color == "" {
return ErrCreateCommunityInvalidColor
}
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 != "" {
log.Info("has-image", "image", c.Image)
ciis := make(map[string]*protobuf.IdentityImage)
imgs, err := userimages.GenerateIdentityImages(c.Image, c.ImageAx, c.ImageAy, c.ImageBx, c.ImageBy)
if err != nil {
return nil, err
}
for _, img := range imgs {
ciis[img.Name] = adaptIdentityImageToProtobuf(img)
}
ci.Images = ciis
log.Info("set images", "images", ci)
}
description := &protobuf.CommunityDescription{
Identity: ci,
Permissions: &protobuf.CommunityPermissions{
Access: c.Membership,
EnsOnly: c.EnsOnly,
},
AdminSettings: &protobuf.CommunityAdminSettings{
PinMessageAllMembersEnabled: c.PinMessageAllMembersEnabled,
},
}
return description, nil
}