-
Notifications
You must be signed in to change notification settings - Fork 44
/
keys.go
119 lines (96 loc) · 4.35 KB
/
keys.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
package types
// DONTCOVER
import (
"regexp"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
const (
ModuleName = "posts"
RouterKey = ModuleName
StoreKey = ModuleName
ActionCreatePost = "create_post"
ActionEditPost = "edit_post"
ActionReportPost = "report_post"
ActionAnswerPoll = "answer_poll"
ActionAddPostReaction = "add_post_reaction"
ActionRemovePostReaction = "remove_post_reaction"
ActionRegisterReaction = "register_reaction"
// Queries
QuerierRoute = ModuleName
QueryPost = "post"
QueryPosts = "posts"
QueryPostComments = "post-comments"
QueryReports = "reports"
QueryUserAnswers = "user-answers"
QueryRegisteredReactions = "registered-reactions"
QueryPostReactions = "post-reactions"
QueryParams = "params"
)
var (
postIDRegEx = regexp.MustCompile(`^[a-fA-F0-9]{64}$`)
shortCodeRegEx = regexp.MustCompile(`:[a-z0-9+-]([a-z0-9\d_-])*:`)
ModuleAddress = authtypes.NewModuleAddress(ModuleName)
PostStorePrefix = []byte("post")
CommentsStorePrefix = []byte("comments")
SubspacePostPrefix = []byte("subspace")
PostReactionsStorePrefix = []byte("p_reactions")
RegisteredReactionsStorePrefix = []byte("r_reactions")
UserAnswersStorePrefix = []byte("user_answers")
ReportsStorePrefix = []byte("reports")
)
// IsValidPostID tells whether the given value represents a valid post id or not
func IsValidPostID(value string) bool {
return postIDRegEx.MatchString(value)
}
// IsValidReactionCode tells whether the given value is a valid emoji shortcode or not
func IsValidReactionCode(value string) bool {
return shortCodeRegEx.MatchString(value)
}
// PostStoreKey turns an id to a key used to store a post into the posts store
func PostStoreKey(id string) []byte {
return append(PostStorePrefix, []byte(id)...)
}
// PostCommentsPrefix returns the prefix used to store all the comments for the parent post having the given id
func PostCommentsPrefix(postID string) []byte {
return append(CommentsStorePrefix, []byte(postID)...)
}
// CommentsStoreKey returns the store key used to store the comment containing the given data
func CommentsStoreKey(parentID, commentID string) []byte {
return append(PostCommentsPrefix(parentID), []byte(commentID)...)
}
// SubspacePostPrefix returns the prefix used to store all the posts present inside the subspace having the given id
func SubspacePostsPrefix(subspace string) []byte {
return append(SubspacePostPrefix, []byte(subspace)...)
}
// SubspacePostKey returns the key used to associate the post with the given id to the subspace with the provided id
func SubspacePostKey(subspace string, id string) []byte {
return append(SubspacePostsPrefix(subspace), []byte(id)...)
}
// PostReactionsPrefix returns the prefix used to store all the reactions for the post having the given id
func PostReactionsPrefix(id string) []byte {
return append(PostReactionsStorePrefix, []byte(id)...)
}
// PostReactionsStoreKey returns the key used to store the reaction containing the given data
func PostReactionsStoreKey(id, user, shortcode string) []byte {
return append(PostReactionsPrefix(id), []byte(user+shortcode)...)
}
// RegisteredReactionsPrefix returns the prefix used to store all the reactions for the subspace having the given id
func RegisteredReactionsPrefix(subspace string) []byte {
return append(RegisteredReactionsStorePrefix, []byte(subspace)...)
}
// RegisteredReactionsStoreKey returns the key used to store the registered reaction having the given short code for the given subspace
func RegisteredReactionsStoreKey(subspace, shortCode string) []byte {
return append(RegisteredReactionsPrefix(subspace), []byte(shortCode)...)
}
// UserAnswersByPostPrefix returns the prefix used to store all the user answers for the post having the given id
func UserAnswersByPostPrefix(id string) []byte {
return append(UserAnswersStorePrefix, []byte(id)...)
}
// UserAnswersStoreKey returns the store key used to store the user answer containing the given data
func UserAnswersStoreKey(id, user string) []byte {
return append(UserAnswersByPostPrefix(id), []byte(user)...)
}
// ReportStoreKey turns an id into a key used to store a report inside the reports store
func ReportStoreKey(id string) []byte {
return append(ReportsStorePrefix, []byte(id)...)
}