/
messages_comment.go
229 lines (201 loc) · 6.59 KB
/
messages_comment.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var _ sdk.Msg = &MsgCreateComment{}
func NewMsgCreateComment(creator string, repositoryid uint64, parentIid uint64, parent CommentParent, body string, attachments []*Attachment, diffHunk string, path string, position uint64) *MsgCreateComment {
return &MsgCreateComment{
Creator: creator,
RepositoryId: repositoryid,
ParentIid: parentIid,
Parent: parent,
Body: body,
Attachments: attachments,
DiffHunk: diffHunk,
Path: path,
Position: position,
}
}
func (msg *MsgCreateComment) Route() string {
return RouterKey
}
func (msg *MsgCreateComment) Type() string {
return "CreateComment"
}
func (msg *MsgCreateComment) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
func (msg *MsgCreateComment) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg *MsgCreateComment) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
switch msg.Parent {
case CommentParentIssue:
if len(msg.Path) > 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Cannot provide Path with comment parent issue")
}
if len(msg.DiffHunk) > 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Cannot provide DiffHunk with comment parent issue")
}
case CommentParentPullRequest:
if len(msg.Path) > 255 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Path exceeds limit: 255")
}
if len(msg.DiffHunk) > 255 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "DiffHunk exceeds limit: 255")
}
default:
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid parent (%s)", msg.Parent)
}
if err := ValidateCommentBody(msg.Body); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
}
if len(msg.Attachments) > 0 {
if len(msg.Attachments) > 20 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "attachments exceeds limit: 20")
}
unique := make(map[string]bool, len(msg.Attachments))
for _, attachment := range msg.Attachments {
if attachment.Size_ == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "attachment size can't be 0")
}
if attachment.Name == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "attachment name can't be empty")
}
if attachment.Sha == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "attachment sha can't be empty")
}
_, err := sdk.AccAddressFromBech32(attachment.Uploader)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid uploader (%v)", attachment.Uploader)
}
if !unique[attachment.Name] {
unique[attachment.Name] = true
} else {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "duplicate name (%s)", attachment.Name)
}
}
}
return nil
}
var _ sdk.Msg = &MsgUpdateComment{}
func NewMsgUpdateComment(creator string, repositoryid uint64, parentIid uint64, parent CommentParent, commentIid uint64, body string, attachments []*Attachment) *MsgUpdateComment {
return &MsgUpdateComment{
Creator: creator,
RepositoryId: repositoryid,
ParentIid: parentIid,
Parent: parent,
CommentIid: commentIid,
Body: body,
Attachments: attachments,
}
}
func (msg *MsgUpdateComment) Route() string {
return RouterKey
}
func (msg *MsgUpdateComment) Type() string {
return "UpdateComment"
}
func (msg *MsgUpdateComment) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
func (msg *MsgUpdateComment) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg *MsgUpdateComment) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
switch msg.Parent {
case CommentParentIssue:
case CommentParentPullRequest:
default:
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid parent (%s)", msg.Parent)
}
if err := ValidateCommentBody(msg.Body); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
}
if len(msg.Attachments) > 0 {
if len(msg.Attachments) > 20 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "attachments exceeds limit: 20")
}
unique := make(map[string]bool, len(msg.Attachments))
for _, attachment := range msg.Attachments {
if attachment.Size_ == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "attachment size can't be 0")
}
if attachment.Name == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "attachment name can't be empty")
}
if attachment.Sha == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "attachment sha can't be empty")
}
_, err := sdk.AccAddressFromBech32(attachment.Uploader)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid uploader (%v)", attachment.Uploader)
}
if !unique[attachment.Name] {
unique[attachment.Name] = true
} else {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "duplicate name (%s)", attachment.Name)
}
}
}
return nil
}
var _ sdk.Msg = &MsgDeleteComment{}
func NewMsgDeleteComment(creator string, repositoryid uint64, parentIid uint64, parent CommentParent, commentIid uint64) *MsgDeleteComment {
return &MsgDeleteComment{
Creator: creator,
RepositoryId: repositoryid,
ParentIid: parentIid,
Parent: parent,
CommentIid: commentIid,
}
}
func (msg *MsgDeleteComment) Route() string {
return RouterKey
}
func (msg *MsgDeleteComment) Type() string {
return "DeleteComment"
}
func (msg *MsgDeleteComment) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
func (msg *MsgDeleteComment) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg *MsgDeleteComment) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
switch msg.Parent {
case CommentParentIssue:
case CommentParentPullRequest:
default:
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid parent (%s)", msg.Parent)
}
return nil
}