-
Notifications
You must be signed in to change notification settings - Fork 44
/
posts.go
200 lines (163 loc) · 5.58 KB
/
posts.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
package types
import (
"fmt"
"strings"
"time"
subspacestypes "github.com/desmos-labs/desmos/x/staging/subspaces/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/desmos/x/commons"
)
// NewPost allows to build a new Post instance with the provided data
func NewPost(
postID string, parentID string, message string, commentsState CommentsState, subspace string,
additionalAttributes []Attribute, attachments []Attachment, pollData *PollData,
lastEdited time.Time, created time.Time, creator string,
) Post {
return Post{
PostID: postID,
ParentID: parentID,
Message: message,
Created: created,
LastEdited: lastEdited,
CommentsState: commentsState,
Subspace: subspace,
AdditionalAttributes: additionalAttributes,
Attachments: attachments,
PollData: pollData,
Creator: creator,
}
}
// Validate implements validator
func (post Post) Validate() error {
if !IsValidPostID(post.PostID) {
return fmt.Errorf("invalid post id: %s", post.PostID)
}
if post.PostID == post.ParentID {
return fmt.Errorf("post id and parent id cannot be the same")
}
if len(strings.TrimSpace(post.ParentID)) != 0 && !IsValidPostID(post.ParentID) {
return fmt.Errorf("invalid parent id: %s", post.ParentID)
}
if post.Creator == "" {
return fmt.Errorf("invalid post owner: %s", post.Creator)
}
if len(strings.TrimSpace(post.Message)) == 0 && len(post.Attachments) == 0 && post.PollData == nil {
return fmt.Errorf("post message, attachments or poll required, they cannot be all empty")
}
if !IsValidCommentsState(post.CommentsState) {
return fmt.Errorf("invalid comments state: %s", post.CommentsState)
}
if !subspacestypes.IsValidSubspace(post.Subspace) {
return fmt.Errorf("post subspace must be a valid sha-256 hash")
}
if post.Created.IsZero() {
return fmt.Errorf("invalid post creation time: %s", post.Created)
}
if !post.LastEdited.IsZero() && post.LastEdited.Before(post.Created) {
return fmt.Errorf("invalid post last edit time: %s", post.LastEdited)
}
for _, attachment := range post.Attachments {
err := attachment.Validate()
if err != nil {
return err
}
}
if post.PollData != nil {
err := post.PollData.Validate()
if err != nil {
return err
}
}
return nil
}
// ___________________________________________________________________________________________________________________
// CommentsStateFromString convert a string in the corresponding CommentsState
func CommentsStateFromString(comState string) (CommentsState, error) {
commentState, ok := CommentsState_value[comState]
if !ok {
return CommentsStateUnspecified, fmt.Errorf("'%s' is not a valid comments state", comState)
}
return CommentsState(commentState), nil
}
// NormalizeCommentsState - normalize user specified comments state
func NormalizeCommentsState(comState string) string {
switch strings.ToLower(comState) {
case "allowed":
return CommentsStateAllowed.String()
case "blocked":
return CommentsStateBlocked.String()
default:
return comState
}
}
// IsValidCommentsState checks if the commentsState given correspond to one of the valid ones
func IsValidCommentsState(commentsState CommentsState) bool {
return commentsState == CommentsStateAllowed || commentsState == CommentsStateBlocked
}
// ___________________________________________________________________________________________________________________
// NewAttachment builds a new Attachment instance with the provided data
func NewAttachment(uri, mimeType string, tags []string) Attachment {
return Attachment{
URI: uri,
MimeType: mimeType,
Tags: tags,
}
}
// Validate implements validator
func (attachments Attachment) Validate() error {
if !commons.IsURIValid(attachments.URI) {
return fmt.Errorf("invalid uri provided")
}
if len(strings.TrimSpace(attachments.MimeType)) == 0 {
return fmt.Errorf("mime type must be specified and cannot be empty")
}
for _, address := range attachments.Tags {
if address == "" {
return fmt.Errorf("invalid empty tag address: %s", address)
}
}
for _, tag := range attachments.Tags {
_, err := sdk.AccAddressFromBech32(tag)
if err != nil {
return err
}
}
return nil
}
// ___________________________________________________________________________________________________________________
// Attachments represents a slice of Attachment object
type Attachments []Attachment
// NewAttachments builds a new Attachments from the given attachments
func NewAttachments(attachments ...Attachment) Attachments {
return attachments
}
// Equals returns true iff the atts slice contains the same data in the same order of the other slice
func (attachments Attachments) Equal(other Attachments) bool {
if len(attachments) != len(other) {
return false
}
for index, attachment := range attachments {
if !attachment.Equal(other[index]) {
return false
}
}
return true
}
// AppendIfMissing appends the given otherAttachment to the atts slice if it does not exist inside it yet.
// It returns a new slice of Attachments containing such otherAttachment.
func (attachments Attachments) AppendIfMissing(otherAttachment Attachment) Attachments {
for _, att := range attachments {
if att.Equal(otherAttachment) {
return attachments
}
}
return append(attachments, otherAttachment)
}
// ___________________________________________________________________________________________________________________
// NewAttribute returns a new Attribute object
func NewAttribute(key, value string) Attribute {
return Attribute{
Key: key,
Value: value,
}
}