/
alias_functions.go
328 lines (275 loc) · 11.1 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package keeper
import (
"bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/magewar/mage/x/subspaces/types"
)
// IterateSubspaces iterates through the subspaces set and performs the given function
func (k Keeper) IterateSubspaces(ctx sdk.Context, fn func(subspace types.Subspace) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SubspacePrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var subspace types.Subspace
k.cdc.MustUnmarshal(iterator.Value(), &subspace)
stop := fn(subspace)
if stop {
break
}
}
}
// GetAllSubspaces returns a list of all the subspaces that have been store inside the given context
func (k Keeper) GetAllSubspaces(ctx sdk.Context) []types.Subspace {
var subspaces []types.Subspace
k.IterateSubspaces(ctx, func(subspace types.Subspace) (stop bool) {
subspaces = append(subspaces, subspace)
return false
})
return subspaces
}
// --------------------------------------------------------------------------------------------------------------------
// IterateSections iterates over all the sections stored and performs the provided function
func (k Keeper) IterateSections(ctx sdk.Context, fn func(section types.Section) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SectionsPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var section types.Section
k.cdc.MustUnmarshal(iterator.Value(), §ion)
stop := fn(section)
if stop {
break
}
}
}
// GetAllSections returns all the stored sections
func (k Keeper) GetAllSections(ctx sdk.Context) []types.Section {
var sections []types.Section
k.IterateSections(ctx, func(section types.Section) (stop bool) {
sections = append(sections, section)
return false
})
return sections
}
// IterateSubspaceSections iterates over all the sections for the given subspace and performs the provided function
func (k Keeper) IterateSubspaceSections(ctx sdk.Context, subspaceID uint64, fn func(section types.Section) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SubspaceSectionsPrefix(subspaceID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var section types.Section
k.cdc.MustUnmarshal(iterator.Value(), §ion)
stop := fn(section)
if stop {
break
}
}
}
// GetSubspaceSections returns all the sections for the given subspace
func (k Keeper) GetSubspaceSections(ctx sdk.Context, subspaceID uint64) []types.Section {
var sections []types.Section
k.IterateSubspaceSections(ctx, subspaceID, func(section types.Section) (stop bool) {
sections = append(sections, section)
return false
})
return sections
}
// IterateSectionPath iterates the path that leads from the section having the given id up towards the root section
// and performs the provided function on all the sections that are encountered over the path (including
// the initial section having the specified id).
func (k Keeper) IterateSectionPath(ctx sdk.Context, subspaceID uint64, sectionID uint32, fn func(section types.Section) (stop bool)) {
section, found := k.GetSection(ctx, subspaceID, sectionID)
if !found {
return
}
stop := fn(section)
if section.ID == types.RootSectionID || stop {
// End the iteration only if the user has told us to stop, or if we reached the root
return
}
// Continue to follow the path from the parent section up to the root
k.IterateSectionPath(ctx, section.SubspaceID, section.ParentID, fn)
}
// IterateSectionChildren iterates over all the children of the given section and performs the provided function
func (k Keeper) IterateSectionChildren(ctx sdk.Context, subspaceID uint64, sectionID uint32, fn func(section types.Section) (stop bool)) {
k.IterateSubspaceSections(ctx, subspaceID, func(section types.Section) (stop bool) {
stop = false
if section.ID != sectionID && section.ParentID == sectionID {
stop = fn(section)
}
return stop
})
}
// --------------------------------------------------------------------------------------------------------------------
// IterateUserGroups iterates over all the users groups stored
func (k Keeper) IterateUserGroups(ctx sdk.Context, fn func(group types.UserGroup) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GroupsPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var group types.UserGroup
k.cdc.MustUnmarshal(iterator.Value(), &group)
stop := fn(group)
if stop {
break
}
}
}
// GetAllUserGroups returns the information (name and members) for all the groups of all the subspaces
func (k Keeper) GetAllUserGroups(ctx sdk.Context) []types.UserGroup {
var groups []types.UserGroup
k.IterateUserGroups(ctx, func(group types.UserGroup) (stop bool) {
groups = append(groups, group)
return false
})
return groups
}
// IterateSubspaceUserGroups allows iterating over all the groups that are part of the subspace having the given id
func (k Keeper) IterateSubspaceUserGroups(ctx sdk.Context, subspaceID uint64, fn func(group types.UserGroup) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SubspaceGroupsPrefix(subspaceID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var group types.UserGroup
k.cdc.MustUnmarshal(iterator.Value(), &group)
stop := fn(group)
if stop {
break
}
}
}
// GetSubspaceUserGroups returns the list of all groups present inside a given subspace
func (k Keeper) GetSubspaceUserGroups(ctx sdk.Context, subspaceID uint64) []types.UserGroup {
var groups []types.UserGroup
k.IterateSubspaceUserGroups(ctx, subspaceID, func(group types.UserGroup) (stop bool) {
groups = append(groups, group)
return false
})
return groups
}
// IterateSectionUserGroups iterates over all the user groups for the given section and performs the provided function
func (k Keeper) IterateSectionUserGroups(ctx sdk.Context, subspaceID uint64, sectionID uint32, fn func(group types.UserGroup) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.SectionGroupsPrefix(subspaceID, sectionID))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var group types.UserGroup
k.cdc.MustUnmarshal(iterator.Value(), &group)
// Perform the function only on
stop := fn(group)
if stop {
break
}
}
}
// GetSectionUserGroups returns all the user groups present inside the given section
func (k Keeper) GetSectionUserGroups(ctx sdk.Context, subspaceID uint64, sectionID uint32) []types.UserGroup {
var groups []types.UserGroup
k.IterateSectionUserGroups(ctx, subspaceID, sectionID, func(group types.UserGroup) (stop bool) {
groups = append(groups, group)
return false
})
return groups
}
// --------------------------------------------------------------------------------------------------------------------
// IterateUserGroupsMembers iterates over all the group member entries and performs the provided function
func (k Keeper) IterateUserGroupsMembers(ctx sdk.Context, fn func(entry types.UserGroupMemberEntry) (stop bool)) {
store := ctx.KVStore(k.storeKey)
prefix := types.GroupsMembersPrefix
iterator := sdk.KVStorePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
subspaceID, groupID, user := types.SplitGroupMemberStoreKey(iterator.Key())
stop := fn(types.NewUserGroupMemberEntry(subspaceID, groupID, user))
if stop {
break
}
}
}
// IterateUserGroupMembers iterates over all the members of the group with the given id present inside the given subspace
func (k Keeper) IterateUserGroupMembers(ctx sdk.Context, subspaceID uint64, groupID uint32, fn func(member string) (stop bool)) {
store := ctx.KVStore(k.storeKey)
prefix := types.GroupMembersPrefix(subspaceID, groupID)
iterator := sdk.KVStorePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
member := types.GetAddressFromBytes(bytes.TrimPrefix(iterator.Key(), prefix))
stop := fn(member)
if stop {
break
}
}
}
// GetUserGroupMembers returns all the members of a group inside a specific subspace
func (k Keeper) GetUserGroupMembers(ctx sdk.Context, subspaceID uint64, groupID uint32) []string {
var members []string
k.IterateUserGroupMembers(ctx, subspaceID, groupID, func(member string) (stop bool) {
members = append(members, member)
return false
})
return members
}
// --------------------------------------------------------------------------------------------------------------------
// IterateUserPermissions iterates over all the stored user permissions
func (k Keeper) IterateUserPermissions(ctx sdk.Context, fn func(entry types.UserPermission) (stop bool)) {
store := ctx.KVStore(k.storeKey)
prefix := types.UserPermissionsStorePrefix
iterator := sdk.KVStorePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var permission types.UserPermission
k.cdc.MustUnmarshal(iterator.Value(), &permission)
stop := fn(permission)
if stop {
break
}
}
}
// IterateSubspaceUserPermissions iterates over all the user permissions set for the subspace with the given id
func (k Keeper) IterateSubspaceUserPermissions(ctx sdk.Context, subspaceID uint64, fn func(entry types.UserPermission) (stop bool)) {
store := ctx.KVStore(k.storeKey)
prefix := types.SubspacePermissionsPrefix(subspaceID)
iterator := sdk.KVStorePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var permission types.UserPermission
k.cdc.MustUnmarshal(iterator.Value(), &permission)
stop := fn(permission)
if stop {
break
}
}
}
// GetSubspaceUserPermissions returns all the user permissions set for the given subspace
func (k Keeper) GetSubspaceUserPermissions(ctx sdk.Context, subspaceID uint64) []types.UserPermission {
var entries []types.UserPermission
k.IterateSubspaceUserPermissions(ctx, subspaceID, func(entry types.UserPermission) (stop bool) {
entries = append(entries, entry)
return false
})
return entries
}
// IterateSectionUserPermissions iterates over all the permissions set for the given section and performs the provided function
func (k Keeper) IterateSectionUserPermissions(ctx sdk.Context, subspaceID uint64, sectionID uint32, fn func(entry types.UserPermission) (stop bool)) {
store := ctx.KVStore(k.storeKey)
prefix := types.SectionPermissionsPrefix(subspaceID, sectionID)
iterator := sdk.KVStorePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var permission types.UserPermission
k.cdc.MustUnmarshal(iterator.Value(), &permission)
stop := fn(permission)
if stop {
break
}
}
}
// GetSectionUserPermissions returns all the user permissions set inside the specific section
func (k Keeper) GetSectionUserPermissions(ctx sdk.Context, subspaceID uint64, sectionID uint32) []types.UserPermission {
var entries []types.UserPermission
k.IterateSectionUserPermissions(ctx, subspaceID, sectionID, func(entry types.UserPermission) (stop bool) {
entries = append(entries, entry)
return false
})
return entries
}