-
Notifications
You must be signed in to change notification settings - Fork 241
/
create_community_request.go
136 lines (119 loc) · 4.92 KB
/
create_community_request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package requests
import (
"errors"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/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")
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"`
Encrypted bool `json:"encrypted,omitempty"`
Tags []string `json:"tags,omitempty"`
}
func adaptIdentityImageToProtobuf(img images.IdentityImage) *protobuf.IdentityImage {
return &protobuf.IdentityImage{
Payload: img.Payload,
SourceType: protobuf.IdentityImage_RAW_PAYLOAD,
ImageType: images.GetProtobufImageType(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 != "" {
log.Info("has-image", "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 != "" {
log.Info("has-banner", "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
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,
},
IntroMessage: c.IntroMessage,
OutroMessage: c.OutroMessage,
Encrypted: c.Encrypted,
Tags: c.Tags,
}
return description, nil
}