-
Notifications
You must be signed in to change notification settings - Fork 20
/
msg.go
196 lines (158 loc) · 5.54 KB
/
msg.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package types
import (
"net/url"
"unicode/utf8"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/gofrs/uuid"
)
const (
maxTitleLength = 150
maxPostLength = 64 * 1000
minPostLength = 15
maxURLLength = 4 * 1024
)
// MsgCreatePost defines a CreatePost message
type MsgCreatePost struct {
UUID string `json:"uuid"`
Owner sdk.AccAddress `json:"owner"`
Title string `json:"title"`
Category Category `json:"category"`
PreviewImage string `json:"previewImage"`
Text string `json:"text"`
}
// MsgDeletePost defines a DeletePost message
type MsgDeletePost struct {
PostUUID string `json:"postUUID"`
PostOwner sdk.AccAddress `json:"postOwner"`
Owner sdk.AccAddress `json:"owner"`
}
// MsgSetLike defines a SetLike message
type MsgSetLike struct {
PostOwner sdk.AccAddress `json:"postOwner"`
PostUUID string `json:"postUuid"`
Owner sdk.AccAddress `json:"owner"`
Weight LikeWeight `json:"weight"`
}
// NewMsgCreatePost is a constructor function for MsgCreatePost
func NewMsgCreatePost(title string, category Category, previewImage string, text string, owner sdk.AccAddress) MsgCreatePost {
return MsgCreatePost{
UUID: uuid.Must(uuid.NewV1()).String(),
Owner: owner,
Title: title,
Category: category,
PreviewImage: previewImage,
Text: text,
}
}
// Route should return the name of the module
func (msg MsgCreatePost) Route() string { return RouterKey }
// Type should return the action
func (msg MsgCreatePost) Type() string { return "create_post" }
// ValidateBasic runs stateless checks on the message
func (msg MsgCreatePost) ValidateBasic() error {
if msg.Owner.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Owner.String())
}
if len(msg.Title) > maxTitleLength || len(msg.Title) == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "title should be shorter then %d and not empty", maxTitleLength)
}
if msg.Category == UndefinedCategory || msg.Category > SportsCategory {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid category")
}
if !IsPreviewImageValid(msg.PreviewImage) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "previewImage is invalid")
}
if utf8.RuneCountInString(msg.Text) < minPostLength || len(msg.Text) > maxPostLength {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "post's length should be between %d symbols and %d bytes", minPostLength, maxPostLength)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgCreatePost) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgCreatePost) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}
// NewMsgDeletePost is a constructor function for MsgDeletePost
func NewMsgDeletePost(owner sdk.AccAddress, postUUID uuid.UUID, postOwner sdk.AccAddress) MsgDeletePost {
return MsgDeletePost{
Owner: owner,
PostUUID: postUUID.String(),
PostOwner: postOwner,
}
}
// Route should return the name of the module
func (msg MsgDeletePost) Route() string { return RouterKey }
// Type should return the action
func (msg MsgDeletePost) Type() string { return "delete_post" }
// ValidateBasic runs stateless checks on the message
func (msg MsgDeletePost) ValidateBasic() error {
if msg.Owner.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Owner.String())
}
if msg.PostOwner.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.PostOwner.String())
}
if _, err := uuid.FromString(msg.PostUUID); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid uuid")
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgDeletePost) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgDeletePost) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}
func IsPreviewImageValid(str string) bool {
if len(str) > maxURLLength {
return false
}
if str == "" {
return true
}
url, err := url.Parse(str)
if err != nil {
return false
}
return url.Scheme == "http" || url.Scheme == "https"
}
// NewMsgSetLike is a constructor function for MsgSetLike
func NewMsgSetLike(postOwner sdk.AccAddress, postUUID uuid.UUID, owner sdk.AccAddress, weight LikeWeight) MsgSetLike {
return MsgSetLike{
PostOwner: postOwner,
PostUUID: postUUID.String(),
Owner: owner,
Weight: weight,
}
}
// Route should return the name of the module
func (msg MsgSetLike) Route() string { return RouterKey }
// Type should return the action
func (msg MsgSetLike) Type() string { return "set_like" }
// ValidateBasic runs stateless checks on the message
func (msg MsgSetLike) ValidateBasic() error {
if msg.Owner.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "owner is empty")
}
if _, err := uuid.FromString(msg.PostUUID); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid post uuid")
}
if msg.Weight > LikeWeightUp || msg.Weight < LikeWeightDown {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid weight")
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgSetLike) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgSetLike) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}