-
Notifications
You must be signed in to change notification settings - Fork 44
/
common_functions.go
183 lines (156 loc) · 6.26 KB
/
common_functions.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
package keeper
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/desmos-labs/desmos/x/staging/posts/types"
)
// IteratePosts iterates through the posts set and performs the provided function
func (k Keeper) IteratePosts(ctx sdk.Context, fn func(index int64, post types.Post) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.PostStorePrefix)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
var post types.Post
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &post)
stop := fn(i, post)
if stop {
break
}
i++
}
}
// ValidatePost checks if the given post is valid according to the current posts' module params
func (k Keeper) ValidatePost(ctx sdk.Context, post types.Post) error {
params := k.GetParams(ctx)
maxMsgLen := params.MaxPostMessageLength.Int64()
maxOpFieldNum := params.MaxAdditionalAttributesFieldsNumber.Int64()
maxOpFieldValLen := params.MaxAdditionalAttributesFieldValueLength.Int64()
if int64(len(post.Message)) > maxMsgLen {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
fmt.Sprintf("post with id %s has more than %d characters", post.PostID, maxMsgLen))
}
if int64(len(post.AdditionalAttributes)) > maxOpFieldNum {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
fmt.Sprintf("post with id %s contains additional attributes with more than %d key-value pairs",
post.PostID, maxOpFieldNum))
}
for _, additionalAttribute := range post.AdditionalAttributes {
if int64(len(strings.TrimSpace(additionalAttribute.Value))) > maxOpFieldValLen {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
fmt.Sprintf("post with id %s has additional attributes with key %s which value exceeds %d characters.",
post.PostID, additionalAttribute.Key, maxOpFieldValLen))
}
}
return post.Validate()
}
// IsCreatorBlockedBySomeTags checks if some of the post's tags have blocked the post's creator
func (k Keeper) IsCreatorBlockedBySomeTags(ctx sdk.Context, attachments types.Attachments, creator, subspace string) error {
for _, attachment := range attachments {
for _, tag := range attachment.Tags {
// check if the request's receiver has blocked the sender before
if k.IsUserBlocked(ctx, tag, creator, subspace) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
fmt.Sprintf("The user with address %s has blocked you", tag))
}
}
}
return nil
}
// ExtractReactionValueAndShortcode parse the given registeredReactions returning its correct value and shortcode
func (k Keeper) ExtractReactionValueAndShortcode(ctx sdk.Context, reaction string, subspace string) (string, string, error) {
var reactionShortcode, reactionValue string
// Parse registeredReactions adding the variation selector-16 to let the emoji being readable
parsedReaction := strings.ReplaceAll(reaction, "️", "")
if emojiReact, found := types.GetEmojiByShortCodeOrValue(reaction); found {
reactionShortcode = emojiReact.Shortcodes[0]
reactionValue = emojiReact.Value
} else {
// The registeredReactions is a shortcode that should be registered
regReaction, registered := k.GetRegisteredReaction(ctx, reaction, subspace)
if !registered {
return "", "", sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
fmt.Sprintf("short code %s must be registered before using it", parsedReaction))
}
reactionShortcode = regReaction.ShortCode
reactionValue = regReaction.Value
}
return reactionShortcode, reactionValue, nil
}
// IterateUserAnswers iterates through the user answers and perform the provided function
func (k Keeper) IterateUserAnswers(ctx sdk.Context, fn func(index int64, answer types.UserAnswer) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.UserAnswersStorePrefix)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
answer := types.MustUnmarshalUserAnswer(k.cdc, iterator.Value())
stop := fn(i, answer)
if stop {
break
}
i++
}
}
// IterateUserAnswersByPost iterates through the user answers with the given post id and performs the provided function
func (k Keeper) IterateUserAnswersByPost(ctx sdk.Context, postID string, fn func(index int64, answer types.UserAnswer) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.UserAnswersByPostPrefix(postID))
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
answer := types.MustUnmarshalUserAnswer(k.cdc, iterator.Value())
stop := fn(i, answer)
if stop {
break
}
i++
}
}
// IterateRegisteredReactions iterates through the registered reactions and performs the provided function
func (k Keeper) IterateRegisteredReactions(ctx sdk.Context, fn func(index int64, reaction types.RegisteredReaction) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.RegisteredReactionsStorePrefix)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
reaction := types.MustUnmarshalRegisteredReaction(k.cdc, iterator.Value())
stop := fn(i, reaction)
if stop {
break
}
i++
}
}
// IteratePostReactions iterates through the post reactions and performs the provided function
func (k Keeper) IteratePostReactions(ctx sdk.Context, fn func(index int64, reaction types.PostReaction) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.PostReactionsStorePrefix)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
reaction := types.MustUnmarshalPostReaction(k.cdc, iterator.Value())
stop := fn(i, reaction)
if stop {
break
}
i++
}
}
// IteratePostReactionsByPost iterates through the post reactions added to the post with the given id and performs the provided function
func (k Keeper) IteratePostReactionsByPost(ctx sdk.Context, postID string, fn func(index int64, reaction types.PostReaction) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.PostReactionsPrefix(postID))
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
reaction := types.MustUnmarshalPostReaction(k.cdc, iterator.Value())
stop := fn(i, reaction)
if stop {
break
}
i++
}
}