/
alias_functions.go
210 lines (174 loc) · 7.6 KB
/
alias_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
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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
poststypes "github.com/desmos-labs/desmos/v6/x/posts/types"
subspacestypes "github.com/desmos-labs/desmos/v6/x/subspaces/types"
"github.com/desmos-labs/desmos/v6/x/reactions/types"
)
// HasProfile returns true iff the given user has a profile, or an error if something is wrong.
func (k Keeper) HasProfile(ctx sdk.Context, user string) bool {
return k.ak.HasProfile(ctx, user)
}
// HasSubspace tells whether the subspace with the given id exists or not
func (k Keeper) HasSubspace(ctx sdk.Context, subspaceID uint64) bool {
return k.sk.HasSubspace(ctx, subspaceID)
}
// HasPermission tells whether the given user has the provided permission inside the subspace with the specified id
func (k Keeper) HasPermission(ctx sdk.Context, subspaceID uint64, sectionID uint32, user string, permission subspacestypes.Permission) bool {
return k.sk.HasPermission(ctx, subspaceID, sectionID, user, permission)
}
// HasUserBlocked tells whether the given blocker has blocked the user inside the provided subspace
func (k Keeper) HasUserBlocked(ctx sdk.Context, blocker, user string, subspaceID uint64) bool {
return k.rk.HasUserBlocked(ctx, blocker, user, subspaceID)
}
// HasPost tells whether the given post exists or not
func (k Keeper) HasPost(ctx sdk.Context, subspaceID uint64, postID uint64) bool {
return k.pk.HasPost(ctx, subspaceID, postID)
}
// GetPost returns the post associated with the given id
func (k Keeper) GetPost(ctx sdk.Context, subspaceID uint64, postID uint64) (poststypes.Post, bool) {
return k.pk.GetPost(ctx, subspaceID, postID)
}
// --------------------------------------------------------------------------------------------------------------------
// IterateRegisteredReactions iterates over all the registered reactions and performs the provided function
func (k Keeper) IterateRegisteredReactions(ctx sdk.Context, fn func(reaction types.RegisteredReaction) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.RegisteredReactionPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var registeredReaction types.RegisteredReaction
k.cdc.MustUnmarshal(iterator.Value(), ®isteredReaction)
stop := fn(registeredReaction)
if stop {
break
}
}
}
// GetRegisteredReactions returns all the stored registered reactions
func (k Keeper) GetRegisteredReactions(ctx sdk.Context) []types.RegisteredReaction {
var reactions []types.RegisteredReaction
k.IterateRegisteredReactions(ctx, func(reaction types.RegisteredReaction) (stop bool) {
reactions = append(reactions, reaction)
return false
})
return reactions
}
// IterateSubspaceRegisteredReactions iterates over all the given subspace registered reactions and performs the provided function
func (k Keeper) IterateSubspaceRegisteredReactions(ctx sdk.Context, subspaceID uint64, fn func(reaction types.RegisteredReaction) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SubspaceRegisteredReactionsPrefix(subspaceID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var registeredReaction types.RegisteredReaction
k.cdc.MustUnmarshal(iterator.Value(), ®isteredReaction)
stop := fn(registeredReaction)
if stop {
break
}
}
}
// GetSubspaceRegisteredReactions returns all the subspaces registered reactions
func (k Keeper) GetSubspaceRegisteredReactions(ctx sdk.Context, subspaceID uint64) []types.RegisteredReaction {
var registeredReactions []types.RegisteredReaction
k.IterateSubspaceRegisteredReactions(ctx, subspaceID, func(reaction types.RegisteredReaction) (stop bool) {
registeredReactions = append(registeredReactions, reaction)
return false
})
return registeredReactions
}
// --------------------------------------------------------------------------------------------------------------------
// IterateReactions iterates over all the reactions and performs the provided function
func (k Keeper) IterateReactions(ctx sdk.Context, fn func(reaction types.Reaction) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.ReactionPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var reaction types.Reaction
k.cdc.MustUnmarshal(iterator.Value(), &reaction)
stop := fn(reaction)
if stop {
break
}
}
}
// GetReactions returns all the stored reactions
func (k Keeper) GetReactions(ctx sdk.Context) []types.Reaction {
var reactions []types.Reaction
k.IterateReactions(ctx, func(reaction types.Reaction) (stop bool) {
reactions = append(reactions, reaction)
return false
})
return reactions
}
// IterateSubspaceReactions iterates over all the given subspace reactions and performs the provided function
func (k Keeper) IterateSubspaceReactions(ctx sdk.Context, subspaceID uint64, fn func(reaction types.Reaction) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SubspaceReactionsPrefix(subspaceID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var reaction types.Reaction
k.cdc.MustUnmarshal(iterator.Value(), &reaction)
stop := fn(reaction)
if stop {
break
}
}
}
// GetSubspaceReactions returns all the reactions associated to this subspace
func (k Keeper) GetSubspaceReactions(ctx sdk.Context, subspaceID uint64) []types.Reaction {
var reactions []types.Reaction
k.IterateSubspaceReactions(ctx, subspaceID, func(reaction types.Reaction) (stop bool) {
reactions = append(reactions, reaction)
return false
})
return reactions
}
// IteratePostReactions iterates over all the given post reactions and performs the provided function
func (k Keeper) IteratePostReactions(ctx sdk.Context, subspaceID uint64, postID uint64, fn func(reaction types.Reaction) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.PostReactionsPrefix(subspaceID, postID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var reaction types.Reaction
k.cdc.MustUnmarshal(iterator.Value(), &reaction)
stop := fn(reaction)
if stop {
break
}
}
}
// HasReacted tells whether the given user has already reacted with the same reaction value to the provided post
func (k Keeper) HasReacted(ctx sdk.Context, subspaceID uint64, postID uint64, user string, value types.ReactionValue) bool {
found := false
k.IteratePostReactions(ctx, subspaceID, postID, func(reaction types.Reaction) (stop bool) {
if reaction.Author == user && reaction.Value.GetCachedValue().(types.ReactionValue).Equal(value) {
found = true
}
return found
})
return found
}
// --------------------------------------------------------------------------------------------------------------------
// IterateReactionsParams iterates over all the stored subspace reactions params and performs the provided function
func (k Keeper) IterateReactionsParams(ctx sdk.Context, fn func(params types.SubspaceReactionsParams) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.ReactionsParamsPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var params types.SubspaceReactionsParams
k.cdc.MustUnmarshal(iterator.Value(), ¶ms)
stop := fn(params)
if stop {
break
}
}
}
// GetReactionsParams returns all the stored reactions parameters
func (k Keeper) GetReactionsParams(ctx sdk.Context) []types.SubspaceReactionsParams {
var parameters []types.SubspaceReactionsParams
k.IterateReactionsParams(ctx, func(params types.SubspaceReactionsParams) (stop bool) {
parameters = append(parameters, params)
return false
})
return parameters
}